<template>
	<view>
		<view class="search-bar">
			<uni-easyinput suffixIcon="search" v-model="subIpt" placeholder="请输入关键字" @iconClick="searchKeyword"></uni-easyinput>
		</view>
		<view
			class="single-fetch"
			v-for="(pre, index) in preCrashList"
			:key="index"
		>
			<view class="fetch-upper">
				<checkbox-group 
					v-show="pre.taskStatus == 124" 
					style="margin-right: 15rpx;"
					@change="checkClick(pre)"
				>
					<checkbox :checked="pre.checked" />
				</checkbox-group>
				<view class="upper-lef">
					{{ pre.createBy == null ? '' : pre.createBy.slice(0, 1) }}
				</view>
				<view class="upper-rig">
					<h4 style="font-size: 16px;">{{ pre.createBy }}</h4>
					<h4 style="color: #989898;">{{ pre.unitName }}</h4>
				</view>
			</view>
			<view class="fetch-lower">
				<view>
					<span>预报废单号</span>
					<h4>{{ pre.repairNum }}</h4>
				</view>
				<view>
					<span>报废单号</span>
					<h4>{{ pre.scrapNum }}</h4>
				</view>
				<view>
					<span>报废来源</span>
					<h4>{{ pre.scrapSource == '1' ? '退料' : '维修' }}</h4>
				</view>
				<view>
					<span>设备类型</span>
					<h4>{{ pre.itemType }}</h4>
				</view>
			</view>
			<view class="fetch-btns">
				<view 
					style="background-color: #3788FF;" 
					@click="seeListDetail(pre.taskId)"
				>
					<uni-icons style="color: #fff;" type="eye"></uni-icons>
					查看
				</view>
				<view 
					style="background-color: #FCA30D;" 
					v-show="pre.taskStatus == '61'"
				>
					<uni-icons style="color: #fff;" type="auth"></uni-icons>
					编辑
				</view>
				<view
					style="background-color: #09dcfc;" 
					v-show="pre.taskStatus == '58'"
					@click="seeListExamDetail(pre.scrapAuditorSetList)"
				>
					<uni-icons style="color: #fff;" type="chat"></uni-icons>
					审批详情
				</view>
				<view
					style="background-color: #1BD30B;" 
					v-show="pre.taskStatus == '124'"
					@click="subSingle(pre)"
				>
					<uni-icons style="color: #fff;" type="paperplane"></uni-icons>
					提交
				</view>
			</view>
			<view class="sticky-area">
				<!-- <image src="/static/passed.png" v-show="pre.taskStatus == 59" mode=""></image>
				<image src="/static/noPass.png" v-show="pre.taskStatus == 61" mode=""></image>
				<image src="/static/noExam.png" v-show="pre.taskStatus == 58" mode=""></image> -->
				<h4
					v-show="pre.taskStatus == '124'"
					style="border: 1px solid #ff9717; color: #ff9717;"
				>
					待提交
				</h4>
				<h4
					v-show="pre.taskStatus == '58'"
				>
					审核中
				</h4>
				<h4
					v-show="pre.taskStatus == '59'"
					style="border: 1px solid #27D870; color: #27D870;"
				>
					已通过
				</h4>
				<h4
					v-show="pre.taskStatus == '61'"
					style="border: 1px solid #EE603D; color: #EE603D;"
				>
					已驳回
				</h4>
			</view>
		</view>
		<view class="btm-sticky">
			<view class="checked">
				<checkbox-group @tap="checkAll" style="margin-right: 15rpx;">
					<checkbox :checked="allChecked" />
				</checkbox-group>
				<text>全选</text>
			</view>
			<view style="display: flex;">
				<view 
					class="exam"
					@click="clickSubmit"
					style="margin-right: 15rpx;"
				>
					提交
				</view>
				<!-- <view
					class="exam"
					style="background-color: #fff; color: #000; border: 1px solid #EBEBEB;"
					@click="clickReject"
				>
					驳回
				</view> -->
			</view>
		</view>
		<uni-popup
			ref="popup1" 
			type="center"
			:mask-click="false"
		>
			<view class="popup1">
				<view class="pop-top">
					<h4>选择审批部门</h4>
					<uni-icons 
						style="color: #AAAAAA; font-weight: bold;" 
						type="closeempty" 
						@click="closePopup1"
					>
					</uni-icons>
				</view>
				<view class="select-area">
					<!-- <uni-easyinput v-model="rejectReason" placeholder="请填写驳回原因"></uni-easyinput> -->
					<custom-tree-select
						:listData="deptList"
						v-model="deptArr"
						:mutiple="true"
						:search="true"
						:choseParent="false"
						:pathMode="true"
					></custom-tree-select>
					<view class="btn" @click="confirmDeptList">确定</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup
			ref="popup2" 
			type="center"
			:mask-click="false"
		>
			<view class="popup2">
				<view class="pop-top">
					<h4>审批详情</h4>
					<uni-icons 
						style="color: #AAAAAA; font-weight: bold;" 
						type="closeempty" 
						@click="closePopup2"
					>
					</uni-icons>
				</view>
				<view class="select-area">
					<!-- <view class="area-top">
						<span>审批部门</span>
						<span>审批状态</span>
						<span>审批意见</span>
					</view> -->
					<uni-table ref="table" border stripe emptyText="暂无更多数据">
						<uni-tr>
							<uni-th align="center">审批部门</uni-th>
							<uni-th align="center" width="150">审批状态</uni-th>
							<uni-th align="center">审批意见</uni-th>
						</uni-tr>
						<uni-tr 
							v-for="(dept, index) in examDetailList" 
							:key="dept.id"
						>
							<uni-td align="center">
								{{ dept.deptName }}
							</uni-td>
							<uni-td align="center">
								<view 
									:class="[
										{ 'orange': dept.status == '0' },
										{ 'green': dept.status == '1' },
										{ 'red': dept.status == '2' }
									]"
								>
									{{ dept.mark }}
								</view>
							</uni-td>
							<uni-td align="center">
								{{ dept.reasonRejection == null ? '--' : dept.reasonRejection }}
							</uni-td>
						</uni-tr>
					</uni-table>
					<!-- <view 
						class="single-dept"
						v-for="(dept, index) in examDetailList"
						:key="dept.id"
					>
						<span>{{ dept.deptName }}</span>
						<span>{{ dept.mark }}</span>
						<span>{{ dept.reasonRejection == null ? '--' : dept.reasonRejection }}</span>
					</view> -->
				</view>
			</view>
		</uni-popup>
		<u-loading-page :loading="showLoading" color="#000" loading-text="提交中,请稍后..."></u-loading-page>
	</view>
</template>

<script>
	import QS from 'qs'
	export default {
		data() {
			return {
				showLoading: false,
				allChecked: false,
				preCrashList: [],
				subIpt: '',
				deptList: [],
				deptArr: [],
				taskIdList: [],
				examDetailList: []
			}
		},
		methods: {
			searchKeyword () {
				this.getSubList(this.subIpt)
			},
			getSubList (keyword) {
				let that = this
				that.$api.preCrashList.fetchPreSubList({
					keyword
				}).then(res => {
					console.log(res);
					if (res.data.code == 200) {
						res.data.data.forEach(item => {
							item.checked = false
						})
						that.preCrashList = res.data.data
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						})
					}
				}).catch(err => {
					console.log(err);
				})
			},
			getDeptTree () {
				let that = this
				that.$api.preCrashList.fetchDeptTree().then(res => {
					that.deptList = that.veri(res.data.data)
					console.log(that.deptList);
				}).catch(err => {
					console.log(err);
				})
			},
			veri (data) {
				let that = this
				if (!data || data.length <= 0) {
					return null;
				}
				return data.map(x => {
					const model = {
						name: x.label,
						id: x.id
					}
				    
				    const children = that.veri(x.children);
						
				    if (children) { 
					    model.children = children;
				    }
					
					return model;
				});
			},
			seeListDetail (taskId) {
				console.log(taskId);
				uni.navigateTo({
					url: `/pages/preCrashListDetail/preCrashListDetail?taskId=${taskId}`
				})
			},
			seeListExamDetail (examList) {
				console.log(examList);
				examList.forEach(item => {
					switch (item.status) {
						case '0':
							item.mark = '待审批'
							break;
						case '1':
							item.mark = '通过'
							break;
						case '2':
							item.mark = '已驳回'
							break;
					}
				})
				this.examDetailList = examList
				this.$refs.popup2.open()
				/* let transList = QS.stringify(examList)
				uni.navigateTo({
					url: `/pages/preCrashDeptList/preCrashDeptList?list=${transList}`
				}) */
			},
			checkClick(item) {
				item.checked = !item.checked
				if (!item.checked) {
					this.allChecked = false
				} else {
					const goods = this.preCrashList.every(item => {
						return item.checked === true
					})
					if (goods) {
						this.allChecked = true
					} else {
						this.allChecked = false
					}
				}
			},
			checkAll() {
				this.allChecked = !this.allChecked
				if (this.allChecked) {
					this.preCrashList.map(item => {
						if (item.checked == false) {
							item.checked = true
						}
					})
				} else {
					this.preCrashList.map(item => {
						if (item.checked == true) {
							item.checked = false
						}
					})
				}
			},
			clickSubmit () {
				let that = this
				that.taskIdList = []
				let subList = that.preCrashList.filter(item => {
					return item.checked == true && item.taskStatus == 124
				})
				if (subList.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '未选中提交项！'
					})
				} else {
					// that.taskIdList = subList
					subList.forEach(item => {
						that.taskIdList.push(item.taskId)
					})
					console.log(that.taskIdList);
					that.$refs.popup1.open()
				}
			},
			subSingle (list) {
				let that = this
				that.taskIdList = []
				that.taskIdList.push(list.taskId)
				console.log(that.taskIdList);
				that.$refs.popup1.open()
			},
			closePopup1 () {
				this.$refs.popup1.close()
			},
			closePopup2 () {
				this.$refs.popup2.close()
			},
			confirmDeptList () {
				let that = this
				that.showLoading = true
				console.log(that.deptArr, that.taskIdList);
				that.$api.preCrashList.submitCrashTask({
					taskIdList: that.taskIdList,
					deptIds: that.deptArr
				}).then(res => {
					if (res.data.code == 200) {
						that.showLoading = false
						uni.showToast({
							icon: 'none',
							title: res.data.msg,
							success: () => {
								uni.redirectTo({
									url: '/pages/preCrashList/preCrashList'
								})
							}
						})
					} else {
						that.showLoading = false
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						})
					}
				}).catch(err => {
					that.showLoading = false
					uni.showToast({
						icon: 'none',
						title: '网络异常，请稍后再试！'
					})
				})
			}
		},
		onShow() {
			this.getSubList('')
			this.getDeptTree()
		}
	}
</script>

<style lang="scss">
	body{
		background-color: #f8f8f8;
		box-sizing: border-box;
		padding-bottom: 10vh;
	}
	.search-bar{
		width: 95%;
		margin: 20rpx auto;
	}
	.single-fetch{
		width: 95%;
		margin: 20rpx auto;
		background-color: #fff;
		border-radius: 15rpx;
		box-sizing: border-box;
		padding: 15rpx;
		position: relative;
		.fetch-upper{
			width: 100%;
			box-sizing: border-box;
			padding: 15rpx;
			border-bottom: 1px solid #D7D7D7;
			display: flex;
			align-items: center;
			.upper-lef{
				width: 12%;
				height: 5vh;
				border-radius: 15rpx;
				overflow: hidden;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 24px;
				background-color: #3788FF;
				color: #fff;
			}
			.upper-rig{
				flex: 1;
				box-sizing: border-box;
				padding: 10rpx 20rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-around;
				h4{
					font-weight: normal;
					font-size: 14px;
				}
			}
		}
		.fetch-lower{
			width: 100%;
			box-sizing: border-box;
			padding: 15rpx;
			border-bottom: 1px solid #D7D7D7;
			view{
				width: 100%;
				display: flex;
				align-items: center;
				margin-bottom: 15rpx;
				font-size: 14px;
				span{
					color: #9D9D9D;
					padding-right: 20rpx;
				}
				h4{
					color: #000;
					font-size: 14px;
					font-weight: normal;
				}
			}
			view:last-child{
				margin-bottom: 0;
			}
		}
		.fetch-btns{
			width: 100%;
			box-sizing: border-box;
			padding: 15rpx;
			display: flex;
			justify-content: flex-end;
			view{
				box-sizing: border-box;
				padding: 8rpx 25rpx;
				border-radius: 15rpx;
				margin-right: 15rpx;
				color: #fff;
				font-size: 14px;
			}
		}
		.sticky-area{
			position: absolute;
			top: 20rpx;
			right: 20rpx;
			/* width: 11vh;
			height: 10vh;
			image{
				width: 100%;
				height: 100%;
			} */
			h4{
				box-sizing: border-box;
				padding: 8rpx 20rpx;
				border-radius: 15rpx;
				font-size: 14px;
				font-weight: normal;
				background-color: #fff;
				border: 1px solid #3788FF; 
				color: #3788FF;
			}
		}
	}
	.btm-sticky{
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		box-sizing: border-box;
		padding: 15rpx 30rpx;
		background-color: #fff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.checked{
			display: flex;
			align-items: center;
		}
		.exam{
			box-sizing: border-box;
			padding: 10rpx 50rpx;
			border-radius: 30rpx;
			background-color: #3788FF;
			font-size: 14px;
			color: #fff;
		}
	}
	.popup1{
		width: 80vw;
		height: 80vh;
		background-color: #fff;
		border-radius: 15rpx;
		overflow-y: auto;
		background: linear-gradient(#D9E7FE, #fff, #fff, #fff);
		.pop-top{
			width: 100%;
			height: 5vh;
			box-sizing: border-box;
			padding: 0 25rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
		.select-area{
			width: 85%;
			margin: 40rpx auto;
			.btn{
				width: 30%;
				box-sizing: border-box;
				padding: 10rpx 0;
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: #3788FF;
				color: #fff;
				font-size: 14px;
				border-radius: 15rpx;
				margin: 40rpx auto;
			}
		}
	}
	.popup2{
		width: 80vw;
		height: 80vh;
		background-color: #fff;
		border-radius: 15rpx;
		overflow-y: auto;
		background: linear-gradient(#D9E7FE, #fff, #fff, #fff);
		.pop-top{
			width: 100%;
			height: 5vh;
			box-sizing: border-box;
			padding: 0 25rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
		.select-area{
			width: 85%;
			margin: 40rpx auto;
			font-size: 14px;
			.red, .orange, .green{
				width: 50%;
				margin-left: 25%;
				color: #fff;
				border-radius: 10rpx;
			}
			.red{
				background-color: #FF4141;
			}
			.orange{
				background-color: #FCA20D;
			}
			.green{
				background-color: #1BD30B;
			}
		}
	}
</style>
