<template>
	<view class="assign-mission">
		<scroll-view class="scroll-y" scroll-y="true">
			<tm-radio-group :list="desicionList" label="改善工具" :defaultValue='desicion' @change="changeDesicion" />
			<view class="switch-box">
				<text class="label">需要审核改善方案</text>
				<switch :checked="needApproveFlag" style="transform:scale(0.6)" @change="switchChange" />
			</view>
			<tm-radio-group :list="empDeptList" :setting="{value: 'empId',name: 'empName'}"
				:defaultValue='desPersopn.empId' @change="changeDesPersopn" />
		</scroll-view>
		<view class="fixed-buttom-btn" @click="sure">
			<text class="btn-text">确定</text>
		</view>
	</view>
</template>

<script>
	// 指派改善任务
	export default {
		props: {
			// 是否禁用
			disabled: {
				type: Boolean,
				default: false
			},
			// 详情回显的数据
			values: {
				type: Object,
				default: () => {
					return {}
				}
			},
			// 按钮信息 (包过请求的key)
			btnInfo: {
				type: Object,
				default: () => {
					return {}
				}
			},
			// 任务详情
			missionDetails: {
				type: Object,
				default: () => {
					return {}
				}
			}
		},
		data() {
			return {
				// 选中改善方案
				desicion: 0,
				// 改善方案列表(0 改善方案 目前只有pdca)
				desicionList: [{
					value: 0,
					name: 'PDCA'
				}],
				// 是否需要审核改善方案
				needApproveFlag: false,
				// 人员列表
				empDeptList: [],
				// 更改指派人
				desPersopn: {}
			}
		},
		created() {
			uni.setNavigationBarTitle({
				title: '指派改善任务'
			});
			this.getEmpDeptTree();
		},
		methods: {
			// 更改改善方案
			changeDesicion(selectVal, selectData, i) {
				this.desicion = selectVal;
			},
			switchChange(e) {
				if (this.missionDetails.improveEmpId) {
					this.needApproveFlag = e.target.value;
				} else {
					uni.showModal({
						title: '提示',
						content: '请先选择改善人',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								this.needApproveFlag = false;
							}
						}
					});
				}
			},
			// 更改指派人
			changeDesPersopn(selectVal, selectData, i) {
				this.desPersopn = selectData;
			},
			// 确定
			sure() {
				let requestParams = {};
				if(JSON.stringify(this.desPersopn) == '{}'){
					   uni.showModal({
					   	title: '提示',
					   	content: '请先选择改善人',
					   	showCancel: false,
					   	success: function(res) {
					   		if (res.confirm) {
					   			this.needApproveFlag = false;
					   		}
					   	}
					   });
					   return;
				}
				this.btnInfo.params && this.btnInfo.params.map(item => {
					if (item.valueKey) {
						requestParams[item.paramsKey] = (
							item.isOutvalueKey ?
							this.missionDetails :
							this.btnInfo
						)[item.valueKey];
					} else if (item.value) {
						requestParams[item.paramsKey] = item.value;
					} else {
						switch (item.paramsKey) {
							case 'desicion':
								requestParams[item.paramsKey] = this.desicion;
								break;
							case 'receiveEmpId':
								requestParams[item.paramsKey] = this.desPersopn.empId;
								break;
							case 'receiveEmpName':
								requestParams[item.paramsKey] = this.desPersopn.empName;
								break;
							case 'needApproveFlag':
								requestParams[item.paramsKey] = this.needApproveFlag;
								break;
							default:
								requestParams[item.paramsKey] = '';
								break;
						}
					}
				});
				// console.log({'1':requestParams});
				this.$emit('comRequest', requestParams);
			},
			// 查询部门人员树
			getEmpDeptTree() {
				this.$store.dispatch({
					type: 'mission/commActions',
					payload: {
						key: "getEmpDeptTree",
						data: {
							deptId: this.missionDetails.deptId // 单位id
						}
					}
				}).then(data => {
					if (data) {
						let _empDeptList = [];
						data && data[0] && data[0].responseList.map(item => {
							if (item.deptManage == 1) { // 是否部门负责人 1是 0 否
								_empDeptList.push({
									...item,
									empName: '单位负责人' + item.empName
								});
							} else {
								_empDeptList.push(item);
							}
						})
						let _empDeptList2 = [];
						_empDeptList.map((item) => {
							if (item.isImprove) {
								_empDeptList2.push(item)
							}
						})
						this.empDeptList = _empDeptList2;
					}
				});
			}
		}
	}
</script>

<style lang="less">
	.assign-mission {
		height: 100%;

		.scroll-y {
			height: calc(100% - 87.5rpx);
			.switch-box {
				display: flex;
				justify-content:space-between;
				align-items: center;
				margin: 15rpx 0;
				height: 87.5rpx;
				background-color: #fff;
				padding: 0 25rpx;
				padding-right: 0;

				.label {
					font-size: 22.5rpx;
					color: #292C33;
				}
			}
		}
	}
</style>
