<template>
	<view class="mainBox">
		<uni-nav-bar background-color="#157A2C" color="#fff" fixed="true" statusBar="true" left-icon="back" title="售后需求"
			@clickLeft="back">
		</uni-nav-bar>
		<view class="top-wrapper">
			<uni-section>
				<uni-easyinput prefixIcon="search" style="width: 460rpx" v-model="searchVal" placeholder="名称">
				</uni-easyinput>
			</uni-section>
			<button class="search_btn" @click="doSearch">搜索</button>
			<image class="menu_icon" src="~@/static/pda/menu.svg"></image>
		</view>
		<view class="wrapper">
			<u-list @scrolltolower="scrolltolower" class="listContent">
				<view v-for="(item, index) in tableList" :key="index" style="position: relative;">
					<myCard @del="del(item)" @handleAudit="handleAudit(item)" @handleRevoke="handleRevoke(item)"
						@edit="add('edit',item.id)" @details="add('view',item.id)" :item="item" :index="index+1"
						:columns="columns" :btnList="btnList">
					</myCard>
				</view>
			</u-list>
		</view>
		<view style='margin-top: 20vh;' v-if="tableList.length==0">
			<u-empty iconSize='150' textSize='32' text='暂无数据'>
			</u-empty>
		</view>
		<view class="add" @click="add('add','')">
			<u-icon name="plus" color="#fff"></u-icon>
		</view>
		<uni-popup ref="popup" type="center" :mask-closable="false">
			<view class="popup-content">
				<view class="popup-header">
					<text>审核</text>
					<view class="close-icon" @click="hidePopup">×</view>
				</view>
				<form @submit="handleSubmit">
					<view class="form-item">
						<label class="form-label">*审核结果</label>
						<!-- 		<uni-data-picker v-model="examForm.auditResult" slot="value" placeholder="请选择"
							:localdata="auditResultOptions" @change="handleAuditResultChange">
						</uni-data-picker> -->
						<!-- 		<uni-data-picker name="auditResult" :localdata="auditResultOptions"
							v-model="examForm.auditResult" @change="handleAuditResultChange">
						</uni-data-picker> -->
					</view>
					<view class="form-item">
						<label class="form-label">审核意见</label>
						<textarea name="auditOpinion" placeholder="请输入" v-model="examForm.auditOpinion"></textarea>
					</view>
					<view class="button-group">
						<button formType="submit" type="primary">确认</button>
						<button @click="hidePopup">取消</button>
					</view>
				</form>
			</view>
		</uni-popup>
		<u-toast ref="uToast"></u-toast>

	</view>
</template>

<script>
	import {
		deleteInformation
	} from '@/api/saleManage/businessOpportunity/index.js'
	import {
		getTableList,
		revokeSalesDemand
	} from '@/api/salesServiceManagement/demandList/index.js'
	import myCard from '@/pages/saleManage/components/myCard.vue'
	export default {
		components: {
			myCard,
		},

		data() {
			return {
				tableList: [],
				searchVal: '',
				page: 1,
				size: 10,
				isEnd: false,
				current: {},
				examForm: {
					auditOpinion: '',
					auditResult: '',
					id: ''
				},

				btnList: [{
						name: '详情',
						apiName: 'details',
						btnType: 'primary',
						type: '2',
						pageUrl: '',
					}, {
						name: '修改',
						apiName: 'edit',
						btnType: 'primary',
						type: '2',
						pageUrl: '',
						judge: [{
							authorities: '',
						}, {
							key: 'demandStatus',
							value: [0, 2, 3, 4, 5],

						}],
					},
					{
						name: '删除',
						apiName: 'del',
						btnType: 'error ',
						type: '2',
						pageUrl: '',
						judge: [{
							authorities: '',
						}, {
							key: 'demandStatus',
							value: [0, 2, 3, 4, 5],
						}],
					}, {
						name: '提交',
						apiName: 'handleAudit',
						btnType: 'primary',
						type: '2',
						pageUrl: '',
						judge: [{
							authorities: '',
						}, {
							key: 'demandStatus',
							value: [0],
						}],
					},
					{
						name: '撤回',
						apiName: 'handleRevoke',
						btnType: 'primary',
						type: '2',
						pageUrl: '',
						judge: [{
							authorities: '',
						}, {
							key: 'demandStatus',
							value: [0],
						}],
					}
				],
				columns: [
					[{
						label: '名称：',
						prop: 'name',
						type: 'title',
						className: 'perce100',
					}],
					[{
						label: '编码：',
						prop: 'code'
					}, {
						label: '客户编码：',
						prop: 'contactCode'
					}],
					[{
						label: '客户名称：',
						prop: 'contactName'
					}, {
						label: '故障等级：',
						prop: 'faultLevel'
					}],
					[{
						label: '创建人：',
						prop: 'createUserName'
					}, {
						label: '创建时间：',
						prop: 'createTime',
					}],
					[{
						label: '审核人：',
						prop: 'approvalUserName'
					}, {
						label: '状态：',
						prop: 'demandStatus',
						formatter: (row) => {
							let cellValue = row.demandStatus;
							return cellValue == 0 ?
								'待审核' :
								cellValue == 2 ?
								'已拒绝' :
								cellValue == 1 ?
								'已通过' :
								cellValue == 3 ?
								'已撤回' :
								'';
						}
					}],
					[{
						label: '操作：',
						prop: 'action',
						type: 'action',
						className: 'perce100',
					}],

				]
			}
		},
		computed: {

		},

		onShow() {
			this.isEnd = false
			this.page = 1
			this.getList()
		},

		methods: {
			doSearch() {
				this.isEnd = false
				this.page = 1
				this.getList()
			},
			//获取列表信息
			getList() {
				if (this.isEnd) {
					return
				}
				uni.showLoading({
					title: '加载中'
				})
				let data = {
					pageNum: this.page,
					size: this.size,
					type: 1,
				}
				getTableList(data).then(res => {
					if (this.page === 1) {
						this.tableList = res.list
					} else {
						this.tableList.push(...res.list)
					}
					this.page += 1
					this.isEnd = this.tableList.length >= res.count
					uni.hideLoading();
				}).catch((e) => {
					uni.hideLoading()
				})
			},
			hidePopup() {
				this.$refs.popup.close();
				this.examForm = {
					auditOpinion: '',
					auditResult: '',
					id: ''
				};
			},
			// 撤回
			async handleRevoke(row) {
				const res = await uni.showModal({
					title: '确认撤回',
					content: '确定要撤回该条数据吗?',
					confirmText: '撤回',
					confirmColor: '#FF4D4F',
				});
				if (res[1].confirm) {
					const data = await revokeSalesDemand({
						id: row.id
					});
					if (!data) return
					this.$refs.uToast.show({
						type: "success",
						message: "操作成功",
					})
					this.doSearch();
				} else if (res.cancel) {
					console.log('用户取消');
				}

			},
			add(type, id) {
				uni.navigateTo({
					url: `/pages/salesServiceManagement/demandList/add?type=${type}&id=${id}`
				})
			},
			scrolltolower() {
				if (this.isEnd) {
					return
				}
				this.getList();
			},
			async del(item) {
				console.log(item, 'item ---')
				const res = await uni.showModal({
					title: '确认删除',
					content: '确定要删除该条数据吗?',
					confirmText: '删除',
					confirmColor: '#FF4D4F',
				});
				if (res[1].confirm) {
					const data = await deleteInformation([row.id])
					if (!data) return
					this.$refs.uToast.show({
						type: "success",
						message: "操作成功",
					})
					this.doSearch();
				} else if (res.cancel) {
					console.log('用户取消');
				}
				// deleteInformation([item.id]).then(res => {
				// 	this.isEnd = false
				// 	this.page = 1
				// 	this.$refs.uToast.show({
				// 		type: "success",
				// 		message: "操作成功",
				// 	})
				// 	this.getList(this.where)
				// })
			},

			handleAudit(item) { 
				console.log(item,'iitem --')
				uni.navigateTo({ 
					url: `/pages/salesServiceManagement/demandList/components/submission?id=${item.id}`
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		width: 96rpx;
		height: 96rpx;
		border-radius: 48rpx;
		background: #3c9cff;
		position: fixed;
		bottom: 100rpx;
		right: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.top-wrapper {
		background-color: #fff;
		display: flex;
		width: 750rpx;
		height: 88rpx;
		padding: 16rpx 32rpx;
		align-items: center;
		gap: 16rpx;

		/deep/.uni-section {
			margin-top: 0px;
		}

		/deep/.uni-section-header {
			padding: 0px;

		}

		.search_btn {
			width: 120rpx;
			height: 70rpx;
			line-height: 70rpx;
			padding: 0 24rpx;
			background: $theme-color;
			font-size: 32rpx;
			color: #fff;
			margin: 0;
			margin-left: 26rpx;
		}

		.menu_icon {
			width: 44rpx;
			height: 44rpx;
			margin-left: 14rpx;
		}
	}
</style>