<template>
	<view class="mission-action-page">
		<!-- 指派改善任务 -->
		<component :is="currentComponet" :disabled="disabled" :values="values" :btnInfo="btnInfo"
			:missionDetails="missionDetails" :pdcaSetting="pdcaSetting" @comRequest="comTaskCirculation" />
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	import assignMission from './components/assign-mission.vue';
	import disagree from './components/disagree.vue'
	import personnel from './components/personnel.vue'
	import writeBack from './components/write-back.vue'
	import pdca from './components/pdca.vue'

	export default {
		computed: {
			...mapState({
				missionDetails: state => state.mission.missionDetails
			})
		},
		data() {
			return {
				// 当前显示的组件
				currentComponet: '',
				disabled: false,
				// 查看详情回显的数据
				values: {},
				// 按钮信息
				btnInfo: {},
				compoentList: [{
						type: 1,
						name: '指派改善任务',
						component: 'assign-mission'
					},
					{
						type: 2,
						name: '原因',
						component: 'disagree'
					},
					{
						type: 3,
						name: '人员架构',
						component: 'personnel'
					},
					{
						type: 4,
						name: '改善回复',
						component: 'write-back'
					},
					{
						type: 5,
						name: 'PDCA',
						component: 'pdca'
					}
				],
				// pdca类型
				pdcaSetting: 'p',
				ifSetImproveEmp: null, //是否指定改善着
			}
		},
		onLoad({
			details,
			ifSetImproveEmp,
			improveType
		}) {
			
			this.getComponentInfo(details ? JSON.parse(details) : {});
			this.ifSetImproveEmp = ifSetImproveEmp;
			this.improveType = improveType;
		},
		methods: {
			// 获取组件信息
			getComponentInfo(details) {
				const {
					nextPermission,
					nextPermissionName,
					componentName,
					disabled,
					hasAnyData,
					isOutvalueKey,
					key,
					labelKey,
					dataKey,
					pdcaSetting,
					isEdit,
					params
				} = details;
				this.currentComponet = componentName;
				this.disabled = disabled;
				this.details = details;
				if (disabled) { // 查看xx详情
					let values = {};
					if (hasAnyData) { // 回显数据由多个key组成
						dataKey.map(item => {
							values[item.labelKey] = (item.isOutvalueKey ? this.missionDetails : details)[item.key];
						});
					} else {
						values[labelKey] = (isOutvalueKey ? this.missionDetails : details)[key]
					}
					this.values = values;
				} else { // 编辑流程
					this.btnInfo = details;
					// if(isEdit) { // 之前暂存过,需要先回显数据
					let values = {};
					params && params.map(item => {
						if (item.labelKey) {
							values[item.labelKey] = details[item.valueKey];
						}
					});
				
					this.values = values;
					// }
				}
				this.pdcaSetting = pdcaSetting;
			},
			// 公共改善任务接口
			comTaskCirculation(data) {
               
				if(data.taskPlan&&data.taskPlan.length == 0){
					 uni.showModal({title: '请填写改善计划！'}); 
					 return;
				}
				if((data.taskDoRequestList&&data.taskDoRequestList.length == 0)||(data.taskDoRequestList&&data.taskDoRequestList.length>0&&data.taskDoRequestList[0].taskDoProcess.length == 0)){
					 uni.showModal({title: '请填写执行过程！'});  
					 return;
				}
				if(data.taskCheckRequestList&&data.taskCheckRequestList.length == 0||(data.taskCheckRequestList&&data.taskCheckRequestList.length>0&&data.taskCheckRequestList[0].taskCheckProcess.length == 0)){
					 uni.showModal({title: '请填写改善确认！'});   
					 return;
				}
				if(data.taskAction&&data.taskAction.length == 0){
					 uni.showModal({title: '请填写对策处置！'});  
					 return;
				}
								
				
				this.$store.dispatch({
					type: 'mission/commActions',
					payload: {
						key: "comTaskCirculation",
						data:{
							...data,
							improveType:this.improveType
						}
					}
				}).then(data1 => {
                    
					if (data1) {
						let taskId = uni.getStorageSync('taskId');
						uni.redirectTo({
							url:`/pages/mission-details/mission-details?taskId=${taskId}`
						})

					}
					
				});
				
			}
		},
		components: {
			assignMission,
			disagree,
			personnel,
			writeBack,
			pdca
		}
	}
</script>

<style lang="less">
	.mission-action-page {
		height: 100%;
	}
</style>
