<template>
	<view class="todo-page">
		<step-title :stepPage="stepsPage" :stepText="stepsText"></step-title>
		<view class="allview">
			<view class="card">
				<u--form labelWidth="auto" :model="dataModel" ref="wForm" class="demo-ruleForm">
					<view style="position: relative;">
						<view style="position: absolute;left: -10px;color: #3d4b70;top: -3px;">*</view>
					</view>

					<view style="font-size: 15px; font-weight: bold;color: #3d4b70;margin-bottom: 10px;">基本信息填写</view>
					<u-form-item label="年度：" prop="YEAR">
						<u--input v-model="dataModel.YEAR" disabledColor="#ffffff" placeholder="请填写年度" border="none" inputAlign="right" fontSize="14px">
						</u--input>
					</u-form-item>

					<u-form-item label="辨识开始时间" @click="handleChangeRecitify('start')">
						<u--input v-model="dataModel.START_DATE" placeholder="请选择时间" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
						</u--input>
						<u-icon style="margin-left: 4px;" slot="right" name="arrow-down" size="12">
						</u-icon>
					</u-form-item>
					<u-form-item label="辨识结束时间" @click="handleChangeRecitify('end')">
						<u--input v-model="dataModel.END_DATE" placeholder="请选择时间" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
						</u--input>
						<u-icon style="margin-left: 4px;" slot="right" name="arrow-down" size="12">
						</u-icon>
					</u-form-item>
					<u-form-item label="评估结束时间" @click="handleChangeRecitify('evaluate')">
						<u--input v-model="dataModel.EVALUATION_END_DATE" placeholder="请选择时间" disabled disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
						</u--input>
						<u-icon style="margin-left: 4px;" slot="right" name="arrow-down" size="12">
						</u-icon>
					</u-form-item>
					<u-form-item label="辨识与评估内容:">
						<!-- <u--input v-model="dataModel.CONTENTS" placeholder="请填写内容" disabledColor="#ffffff" inputAlign="right" fontSize="14px" border="none">
						</u--input> -->
					</u-form-item>
					<u--textarea v-model="dataModel.CONTENTS" placeholder="请输入内容" autoHeight></u--textarea>
					<view class="upload-title">附件：</view>
					<full-upload v-model="dataModel.Nav_Files" :isShowBtn='true' :listProp='listPropUpload' :listPropVal='listPropValUpload'></full-upload>

				</u--form>
				<u--form labelWidth="auto" :model="dataModel" ref="wForm" class="demo-ruleForm" style="margin-top: 20px;border-top: #ceccca 1px solid;">
					<!-- <view style="position: relative;">
						<view style="position: absolute;left: -10px;color: #3d4b70;top: -3px;">*</view>
					</view>

					<view style="font-size: 15px; font-weight: bold;color: #3d4b70;margin-bottom: 10px;">辨识与评估确认</view> -->
					<u-form-item label="辨识与评估" @click="handleNewCheckContent">
						<u-button text="新增部门与辨识人员" size="small" type="primary" style="margin-left: 30%;"></u-button>
						<!-- <u-icon name="plus-circle" size="24" color="#3d9cff" slot="right"></u-icon> -->
					</u-form-item>
					<view v-for="(item,index) in dataModel.Nav_Details" :key="index" style="border-bottom: #ceccca 1px dashed;padding-bottom: 10px;">
						<u-form-item>
							<view style="display: flex;flex-direction: row;justify-content: space-between;align-items: center;flex: 1;">
								<view @click="handleChangeStep(index)" style="flex: 1;display: flex;flex-direction: row;align-items: center;">
									<view>
										{{'部门 : ' +item.Nav_Department.NAME}}
									</view>
									<u-icon style="margin-left: 5px;" name="arrow-down" size="12">
									</u-icon>
								</view>
								<view class="action" style="display: flex;flex-direction: row;align-items: center;justify-content: space-around;">
									<u-icon @click="handleDelRowBefore(index)" name="trash" color="#ff4d4f" size="21"></u-icon>
								</view>
							</view>
						</u-form-item>
						<u-form-item label="辨识人员" @click="handleShowUserSelector(item,index)">
							<u-icon name="man-add" size="24" color="#3d9cff" slot="right"></u-icon>
						</u-form-item>
						<view v-show="!!item.Nav_DetailUsers.length" class="tag-view">
							<u-textarea v-model="item.userlist" disabled></u-textarea>
						</view>
					</view>

				</u--form>
			</view>
			<view style="padding: 10px 16px;" class="bottom-button">
				<u-button type="primary" @click="submit" color="#3d4b70">提交</u-button>
			</view>

			<u-modal :show="showDelModalIndex >= 0" @confirm="confirmDel" @cancel="cacelDel" :showCancelButton="true" title="确认删除？"></u-modal>
			<u-datetime-picker :show="showPopupRecitifyDate" v-model="dateRecitifyTime" mode="date" @confirm="confirmRecitifyDate" @cancel="cancelRecitifyDate"></u-datetime-picker>
			<people-selector-dep :defaultChecked="peopleSelectOption.defaultChecked" :depId="peopleSelectOption.depId" :show="peopleSelectOption.showSelector" @select="handleSelectorPeople"
				@close="peopleSelectOption.showSelector = false">
			</people-selector-dep>
			<query-selector :show="showPopupStep" :total="curTotalStep" :lists="stepLists" :defaultValue="currentOperateStep.NAME" @close="handleClosePopupStep" @search="handleSearchStep" @select="handleSelectedStep" />
		</view>
	</view>
</template>

<script>
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		extendIgnoreDataRule,
		guid,
		initFilter,
		initFilterGroup,
		handleOriginalArray
	} from '../../../../utils/common'
	import {
		FMProductionUnit,
		CheckPeriods,
		CheckLevels
	} from '../../../../utils/enums.js'
	import {
		EvaluationPlanGet,
		EvaluationPlanFullUpdate,
		getUserLists,
		GetDepartmentInfo
	} from '../../../../services/apply/subPages/SK/SKServices.js'
	import {
		getRequest,
	} from '../../../../services/apply/FOServices/FOServices';
	import stepTitle from '@/components/custom/step-title.vue'
	import swipeActionItem from '../../../../uni_modules/uview-ui/libs/config/props/swipeActionItem';
	export default {
		components: {
			stepTitle,
		},
		data() {
			return {
				showDelModalIndex: undefined,
				showPopupDate: false,
				showPopupRecitifyDate: false,
				listPropUpload: ['NOTIFY_ID'],
				listPropValUpload: [],
				ORG_ID: uni.getStorageSync('orgId'),
				currentOperateStep: {},
				showPopupStep: false,
				stepLists: [],
				curTotalStep: 0,
				dataModel: {
					YEAR: '',
					START_DATE: '',
					END_DATE: '',
					EVALUATION_END_DATE: '',
					CONTENTS: '',
					Nav_Files: [],

					Nav_Details: [{
						Nav_Department: {
							NAME: ''
						},
						Nav_DetailUsers: [{
							Nav_User: {
								NAME: ''
							}
						}],
						userlist: '',
					}]
				},
				model: {
					ORG_ID: '',
					YEAR: '',
					START_DATE: '',
					END_DATE: '',
					EVALUATION_END_DATE: '',
					CONTENTS: '',
					Nav_Files: [],
					Nav_Details: [{
						Nav_Department: {
							NAME: ''
						},
						Nav_DetailUsers: [{
							Nav_User: {
								NAME: ''
							}
						}]
					}]

				},
				peopleSelectOption: {
					showSelector: false,
					value: null,
					index: 0,
					depId: '',
					defaultChecked: []
				},
				dateRecitifyTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
				stepsText: ['发起', '审批', '归档'],
				stepsPage: 0,
				stepsPageList: 0,
				TaskID: '',
				nowName: '',
				nowIndex: 0,

			}
		},
		onLoad(option) {
			this.TaskID = option.taskID ? option.taskID : '';
			this.ID = option.ID ? option.ID : '';
			this.loadData()
		},
		methods: {
			loadData() {
				if (this.ID == undefined || this.ID == '') {
					this.stepLoad = false
					this.model.Nav_Details = []
					if (this.ID == undefined || this.ID == '')
						this.ID = guid()
					if (this.TaskID == undefined || this.TaskID == '')
						this.TaskID = '00000000-0000-0000-0000-000000000000'

				} else {
					// const json = {}
					const orgId = uni.getStorageSync('orgId')
					const json = initFilter(orgId, "", "")
					extendRule(json, 'ID', 1, this.ID);
					extendInclude(json, 'Nav_Files.Nav_ImgFile');
					extendInclude(json, 'Nav_Details');
					extendInclude(json, 'Nav_Details.Nav_Department');
					extendInclude(json, 'Nav_Details.Nav_DetailUsers.Nav_User');
					extendIgnoreDataRule(json)
					// extendRule(json, 'TASK_ID', 1, this.TaskID);
					EvaluationPlanGet(json).then(res => {
						this.dataModel = res;
						this.stepsPage = res.STATUS == 0 ? 0 : res.STATUS == 20 ? 1 : 2
						this.dataModel.START_DATE = uni.$u.timeFormat(res.START_DATE, 'yyyy-mm-dd'),
							this.dataModel.END_DATE = uni.$u.timeFormat(res.END_DATE, 'yyyy-mm-dd'),
							this.dataModel.EVALUATION_END_DATE = uni.$u.timeFormat(res.EVALUATION_END_DATE, 'yyyy-mm-dd'),
							this.dataModel.Nav_Details.map((item, index) => {
								let arr = item.Nav_DetailUsers.map((item1, index1) => {
									if (!item1.Nav_User.IS_DELETED) {
										return '  ' + item1.Nav_User.NAME
									}

								})
								item['userlist'] = String(arr)
							})

					})
				}
			},
			handleNewCheckContent() {
				this.dataModel.Nav_Details.unshift({
					Nav_Department: {
						NAME: ''
					},
					Nav_DetailUsers: [{
						Nav_User: {
							NAME: ''
						}
					}]
				})

				this.dataModel.Nav_Details = [...this.dataModel.Nav_Details]
			},
			handleDelRowBefore(index) {
				this.showDelModalIndex = index
				this.nowIndex = index


			},
			confirmDel() {
				this.dataModel.Nav_Details.splice(this.nowIndex, 1)
				this.showDelModalIndex = undefined

			},
			cacelDel() {
				this.showDelModalIndex = undefined
			},
			cancelRecitifyDate() {
				this.showPopupRecitifyDate = false
			},
			handleChangeRecitify(name) {
				this.nowName = name
				this.showPopupRecitifyDate = true
			},
			confirmRecitifyDate(e) {
				if (this.nowName == 'start') {
					this.dataModel.START_DATE = uni.$u.timeFormat(e.value,
						'yyyy-mm-dd ')

				} else if (this.nowName == 'end') {
					this.dataModel.END_DATE = uni.$u.timeFormat(e.value,
						'yyyy-mm-dd ')
				} else {
					this.dataModel.EVALUATION_END_DATE = uni.$u.timeFormat(e.value,
						'yyyy-mm-dd ')
				}
				this.dateRecitifyTime = uni.$u.timeFormat(e.value,
					'yyyy-mm-dd ')

				this.showPopupRecitifyDate = false
			},
			handleChangeStep(index) {
				this.currentOperateStep = {}
				this.showPopupStep = true
				this.nowIndex = index
				this.handleSearchStep('init')
			},
			handleClosePopupStep() {
				this.showPopupStep = false
				// this.showPopupCertificate = false
			},
			handleSearchStep(val, pageIndex) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", 0, pageIndex ?? 1)
				json.Limit = 20
				if (pageIndex) {
					json.Start = (pageIndex - 1) * json.Limit;
				}
				if (val !== 'init') {
					const tempGroup = initFilterGroup(false);
					extendGroupRule(tempGroup, 'NAME', 9, val)
					extendFilterGroup(json, tempGroup);
				}
				// json.Limit = 100
				GetDepartmentInfo(json).then(res => {
					// if (res.IsSuccessful) {
					this.stepLists = (res.Data || res).map(i => {
						return {
							...i,
							id: i.ID,
							name: i.NAME,
						}
					})
					this.curTotalStep = res.TotalCount
					// }
				})
			},
			handleSelectedStep(val) {
				this.dataModel.Nav_Details[this.nowIndex].DEPARTMENT_ID = val.ID
				this.dataModel.Nav_Details[this.nowIndex].Nav_Department.NAME = val.NAME
				this.showPopupStep = false

			},
			handleSelectorPeople(e) {
				const {
					index: key
				} = this.peopleSelectOption
				this.dataModel.Nav_Details[key].Nav_DetailUsers = e.map(i => {
					return {
						USER_ID: i.USER_ID,
						IS_DELETED: i.IS_DELETED,
						NAME: i.NAME,
						Nav_User: {
							NAME: i.NAME,
							ID: i.USER_ID,
							IS_DELETED: i.IS_DELETED
						}
					}
				})
				let arr = ''
				this.dataModel.Nav_Details[key].Nav_DetailUsers.map(item => {
					if (!item.IS_DELETED) {
						arr += ' ' + item.Nav_User.NAME
					}
				})
				this.dataModel.Nav_Details[key].userlist = String(arr)
			},
			handleShowUserSelector(item, index) {
				this.peopleSelectOption = {
					showSelector: true,
					value: item,
					index,
					depId: item.DEPARTMENT_ID,
					defaultChecked: item.Nav_DetailUsers.map(i => {
						return {
							...i,
							NAME: i.Nav_User.NAME,
							USER_ID: i.Nav_User.ID,
						}

					})
				}
			},

			submit() {
				if (!this.dataModel.CONTENTS) {
					uni.showToast({
						title: '请填写辨识与评估内容',
						icon: 'none'
					})
					return
				}
				for (let item of this.dataModel.Nav_Details) {
					if (!item.Nav_Department.NAME) {
						uni.showToast({
							title: '部门不能为空',
							icon: 'none'
						})
						return
					}
					if (item.Nav_DetailUsers.length == 0) {
						uni.showToast({
							title: '辨识人员不能为空',
							icon: 'none'
						})
						return
					}
				}
				let arr = ['DEPARTMENT_ID', 'ENTITY_ORG_TPYE', 'Nav_DetailUsers', 'ID', 'IDENTIFY_EVALUATION_PLAN_ID', 'ORG_ID']
				this.model = {
					YEAR: this.dataModel.YEAR,
					START_DATE: this.dataModel.START_DATE,
					END_DATE: this.dataModel.END_DATE,
					EVALUATION_END_DATE: this.dataModel.EVALUATION_END_DATE,
					CONTENTS: this.dataModel.CONTENTS,
					ORG_ID: this.ORG_ID,
					Nav_Files: this.dataModel.Nav_Files,
					Nav_Details: handleOriginalArray(this.dataModel.Nav_Details, arr)
				}
				this.model.ID = this.ID
				this.model.TaskID = this.TaskID
				this.model.PUBLISH = "SaveAndNotify";
				EvaluationPlanFullUpdate(this.model).then(res => {
					if (res) {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					}
				})


			}

		}



	}
</script>

<style scoped>
	@import url("../../../../style/css/newTemplate.css");
</style>