<template>
	<view class="todo-page">
		<step-title :stepPage="stepsPage" :stepText="stepsText"></step-title>
		<view class="allview">
			<view class="card">
				<u--form labelWidth="auto" :model="model" ref="wForm" :rules="rules" class="demo-ruleForm">

					<u-form-item label="日期" required prop="DATE" borderBottom @click="showCheckDate({name:'DATE'})">
						<u--input disabledColor="#fff" v-model="model.DATE" placeholder="请选择日期" border="none"
							inputAlign="right"></u--input>
						<u-icon style="margin-left: 4px;" slot="right" name="arrow-down"></u-icon>
					</u-form-item>

					<u-form-item class="subRequired" required label="沟通部门" prop="Nav_FormDepartment.NAME" borderBottom
						@click="handleShowSheet({title: '沟通部门', name: 'Nav_FormDepartmentNAME'})"> <u--input disabled
							disabledColor="#fff" v-model="model.Nav_FormDepartment.NAME" border="none"
							inputAlign="right"></u--input><u-icon style="margin-left: 4px;" slot="right"
							name="arrow-down"></u-icon></u-form-item>
					<u-form-item class="subRequired" required label="沟通人员" prop="Nav_FromUser.NAME" borderBottom
						@click="handleShowSheet({title: '沟通人员', name: 'Nav_FromUserNAME'})"> <u--input disabled
							disabledColor="#fff" v-model="model.Nav_FromUser.NAME" border="none"
							inputAlign="right"></u--input><u-icon style="margin-left: 4px;" slot="right"
							name="arrow-down"></u-icon></u-form-item>
					<u-form-item class="subRequired" required label="被沟通部门" prop="Nav_ToDepartment.NAME" borderBottom
						@click="handleShowSheet({title: '被沟通部门', name: 'Nav_ToDepartmentNAME'})"> <u--input disabled
							disabledColor="#fff" v-model="model.Nav_ToDepartment.NAME" border="none"
							inputAlign="right"></u--input><u-icon style="margin-left: 4px;" slot="right"
							name="arrow-down"></u-icon></u-form-item>
					<u-form-item class="subRequired" required label="被沟通人员" prop="Nav_ToUser.NAME" borderBottom
						@click="handleShowSheet({title: '被沟通人员', name: 'Nav_ToUserNAME'})"> <u--input disabled
							disabledColor="#fff" v-model="model.Nav_ToUser.NAME" border="none"
							inputAlign="right"></u--input><u-icon style="margin-left: 4px;" slot="right"
							name="arrow-down"></u-icon></u-form-item>
					<u-form-item label="沟通事项名称" required prop="NAME" borderBottom> <u--input disabledColor="#ffffff"
							v-model="model.NAME" border="none" slot="right" inputAlign="right"
							placeholder="请输入沟通事项名称"></u--input> </u-form-item>
					<u-form-item label="沟通目的" required prop="PURPOSE" borderBottom></u-form-item> <u--textarea
						autoHeight v-model="model.PURPOSE" placeholder="请输入沟通目的" border="surround"></u--textarea>
					<u-form-item label="沟通事项描述" required prop="DESC" borderBottom></u-form-item> <u--textarea autoHeight
						v-model="model.DESC" placeholder="请输入沟通事项描述" border="surround"></u--textarea>
					<view class="upload-title">沟通事项文件</view><full-upload v-model="model.Nav_ItemFiles" :isShowBtn="true"
						:listProp="listPropUpload" :listPropVal="listPropValUpload"></full-upload>
					<u-form-item class="subRequired" label="落实部门" prop="Nav_DoDepartment.NAME" borderBottom
						@click="handleShowSheet({title: '落实部门', name: 'Nav_DoDepartmentNAME'})"> <u--input disabled
							disabledColor="#fff" v-model="model.Nav_DoDepartment.NAME" border="none"
							inputAlign="right"></u--input><u-icon style="margin-left: 4px;" slot="right"
							name="arrow-down"></u-icon></u-form-item>
					<u-form-item class="subRequired" label="落实人员" prop="Nav_DoUser.NAME" borderBottom
						@click="handleShowSheet({title: '落实人员', name: 'Nav_DoUserNAME'})"> <u--input disabled
							disabledColor="#fff" v-model="model.Nav_DoUser.NAME" border="none"
							inputAlign="right"></u--input><u-icon style="margin-left: 4px;" slot="right"
							name="arrow-down"></u-icon></u-form-item>

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

		<query-selector :multiple="selectorInfo.isMultiple" :show="selectorInfo.showPopup"
			:defaultChecked="selectorInfo.defaultChecked" :title="selectorInfo.title" :lists="selectorInfo.dataLists"
			@search="handleSearch" @select="handleSelected" @close="selectorInfo.showPopup=false"
			:total="selectorInfo.totalCount" />
		<u-picker :show="comPickerInfo.showSheet" :columns="comPickerInfo.columns" @confirm="onConfirmPicker"
			@close="comPickerInfo.showSheet=false" @cancel="comPickerInfo.showSheet=false" keyName="NAME"></u-picker>


		<u-datetime-picker :show="dateTimePickerInfo.showCheckDate" mode="date"
			v-model='dateTimePickerInfo.defaultDateTime' @confirm="handleCheckDate"
			@close="dateTimePickerInfo.showCheckDate = false"
			@cancel="dateTimePickerInfo.showCheckDate = false"></u-datetime-picker>
	</view>
</template>

<script>
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		extendIgnoreDataRule,
		guid,
		initFilter,
		initFilterGroup
	} from '../../../../utils/common'

	import {
		// OrderPagedItemEnum,
		// FullUpdateOpinion,
		// GetOpinion,
		GetContact,
		FullUpdateContact
	} from '../../../../services/apply/subPages/scOG'

	import {
		getDepartmentLists
	} from '../../../../services/app'

	import {
		UserPagedLimit,
		OrderEntities,
		getEnum,
		GetEnumAnyOrder
	} from '../../../../services/common'


	import {
		FMProductionUnit,
		CheckPeriods,
		CheckLevels
	} from '../../../../utils/enums.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 {
				TaskID: null,
				tableKey: '',
				isLoadOK: false,
				ORG_ID: uni.getStorageSync('orgId'),
				stepsText: ['发起', '确认', '落实', '完成确认', '归档'], //头部导航
				stepsPage: 0, //步骤
				model: {
					ID: '',
					DATE: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
					Nav_FormDepartment: {
						NAME: ''
					},
					Nav_FromUser: {
						NAME: ''
					},
					Nav_ToDepartment: {
						NAME: ''
					},
					Nav_ToUser: {
						NAME: ''
					},
					PURPOSE: '',
					NAME: '',
					DESC: '',
					Nav_ItemFiles: [],
					Nav_DoDepartment: {
						NAME: ''
					},
					Nav_DoUser: {
						NAME: ''
					},

					FORM_DEPARTMENT_ID: '',
					FROM_USER_ID: '',
					TO_DEPARTMENT_ID: '',
					TO_USER_ID: '',
					DO_DEPARTMENT_ID: '',
					DO_USER_ID: ''
				},

				listPropUpload: ['CONTACT_ID'],
				listPropValUpload: [],

				selectorInfo: {
					isSubShow: false,
					isMultiple: false,
					showPopup: false,
					totalCount: 0,
					title: '检查人员',
					itemData: {},
					index: 0,
					dataLists: [],
					defaultText: '',
					defaultValue: '',
					name: '',
					defaultChecked: []
				},

				listEnumStatus: [],
				listEnumStatusText: [],

				listEnumSatisfaction: [],
				listEnumSatisfactionText: [],

				comPickerInfo: {
					showSheet: false,
					dataItem: {},
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},

				dateTimePickerInfo: {
					showCheckDate: false,
					dataIndex: undefined,
					defaultDateTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
					value: ''
				},

				rules: {
					'NAME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请填写建议事项名称'
					},
					'Nav_ItemEnum.NAME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请选择建议事项类别'
					}
				},


			}
		},
		onLoad(option) {
			this.model.ID = option.ID
			this.model.TaskID = option.taskID
			this.TaskID = option.taskID
			this.tableKey = option.tableKey

			this.ORG_ID = uni.getStorageSync('orgId')
			if (option.ID != null && option.ID != 'null') {
				this.listPropValUpload.push(this.model.ID)
				//受理枚举  OGAssigneeStatus// this.getEnums() // 
				this.loadData()
			} else {
				this.isLoadOK = true
				this.model.ID = guid()
				this.model.ORG_ID = this.ORG_ID
				this.listPropValUpload.push(this.model.ID)
				// this.model.Nav_ListSafeCheckDetail = null
			}
		},
		methods: {

			// getEnums() {
			// 	var dataParm = {
			// 		'name': "OGAssigneeStatus|OGSatisfactionStatus"
			// 	}

			// 	GetEnumAnyOrder(dataParm).then(res => {
			// 		this.listEnumStatus = res[0]
			// 		this.listEnumStatusText = []
			// 		this.listEnumStatus.forEach(e => {
			// 			e.name = e.NAME
			// 			e.code = e.ID
			// 			this.listEnumStatusText.push(e.NAME)
			// 		})

			// 		this.listEnumSatisfaction = res[1]
			// 		this.listEnumSatisfactionText = []
			// 		this.listEnumSatisfaction.forEach(e => {
			// 			e.name = e.NAME
			// 			e.code = e.ID
			// 			this.listEnumSatisfactionText.push(e.NAME)
			// 		})
			// 		this.loadData()
			// 	})
			// },

			loadData() {
				const json = initFilter(this.ORG_ID, "", "", 1)
				extendRule(json, 'ID', 1, this.model.ID);

				extendInclude(json, "Nav_FormDepartment")
				extendInclude(json, "Nav_ToDepartment")
				extendInclude(json, "Nav_DoDepartment")
				extendInclude(json, "Nav_User")
				extendInclude(json, "Nav_ItemFiles.Nav_ImgFile")
				extendInclude(json, "Nav_DoFiles.Nav_ImgFile")
				extendInclude(json, "Nav_FromUser")
				extendInclude(json, "Nav_ToUser")
				extendInclude(json, "Nav_DoUser")

				json.IgnoreDataRule = true
				GetContact(json).then(res => {
					if (res.Nav_DoDepartment == null)
						res.Nav_DoDepartment = {
							NAME: ''
						}
					if (res.Nav_DoUser == null)
						res.Nav_DoUser = {
							NAME: ''
						}
					res.ASSIGNEE_STATUS_SHOW = '' // = '未受理' //res
					this.listEnumStatus.forEach((e, index) => {
						if (e.code == res.ASSIGNEE_STATUS) {
							res.ASSIGNEE_STATUS_SHOW = e.name
						}
					})
					this.stepsPage = res.STATUS
					this.model = res
				})
			},


			//下拉数据加载
			async handleShowSheet(p) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", 0, p.pageIndex ?? 1)
				let dataSelect = []
				var defaultChecked = []
				// if (p.name == "ItemEnumNAME") {
				// 	if (p.val) {
				// 		extendRule(json, 'NAME', 9, p.val);
				// 	}
				// 	json.Sort = "NAME"
				// 	json.Order = 1
				// 	dataSelect = await OrderPagedItemEnum(json).then(res => {
				// 		if (res != undefined && res.length > 0) {
				// 			for (let i = 0; i < res.length; i++) {
				// 				res[i].name = res[i].NAME
				// 				// res[i].code = res[i].CODE
				// 			}
				// 		}
				// 		return res
				// 	})
				// } else

				if (p.name == "Nav_FromUserNAME" || p.name == "Nav_DoUserNAME" || p.name == "Nav_ToUserNAME") {
					if (p.val) {
						extendRule(json, 'NAME', 9, p.val);
					}
					if (p.name == "Nav_FromUserNAME" && this.model.FORM_DEPARTMENT_ID && this.model
						.FORM_DEPARTMENT_ID != '') {
						extendRule(json, 'DEPARTMENT_ID', 1, this.model.FORM_DEPARTMENT_ID);
					}
					if (p.name == "Nav_DoUserNAME" && this.model.DO_DEPARTMENT_ID && this.model
						.DO_DEPARTMENT_ID != '') {
						extendRule(json, 'DEPARTMENT_ID', 1, this.model.DO_DEPARTMENT_ID);
					}
					if (p.name == "Nav_ToUserNAME" && this.model.TO_DEPARTMENT_ID && this.model
						.TO_DEPARTMENT_ID != '') {
						extendRule(json, 'DEPARTMENT_ID', 1, this.model.TO_DEPARTMENT_ID);
					}

					json.Sort = "NAME"
					json.Order = 0
					json.PageIndex = p?.pageIndex ?? 1
					json.Limit = 20
					if (p?.pageIndex) {
						json.Start = (p?.pageIndex - 1) * json.Limit;
					}
					dataSelect = await OrderEntities(json).then(res => {
						this.selectorInfo.totalCount = res.TotalCount
						if (res != undefined && res.Data != undefined && res.Data.length > 0) {

							this.selectorInfo.dataLists = res.Data.map(i => {
								return {
									...i,
									name: i.NAME,
									code: i.CODE
								}
							})
						}
						return this.selectorInfo.dataLists
					})
				} else if (p.name == "Nav_FormDepartmentNAME" || p.name == "Nav_ToDepartmentNAME" || p.name ==
					"Nav_DoDepartmentNAME") {
					if (p.val) {
						extendRule(json, 'NAME', 9, p.val);
					}
					json.Sort = "NAME"
					json.Order = 0
					json.PageIndex = p?.pageIndex ?? 1
					json.Limit = 20
					if (p?.pageIndex) {
						json.Start = (p?.pageIndex - 1) * json.Limit;
					}
					dataSelect = await getDepartmentLists(json).then(res => {
						this.selectorInfo.totalCount = res.TotalCount
						if (res != undefined && res.Data != undefined && res.Data.length > 0) {

							this.selectorInfo.dataLists = res.Data.map(i => {
								return {
									...i,
									name: i.NAME,
									code: i.CODE
								}
							})
						}
						return this.selectorInfo.dataLists
					})
				}

				if (defaultChecked == null)
					defaultChecked = []
				if (dataSelect.length) {
					this.selectorInfo = {
						itemData: p.itemData != undefined ? p.itemData : this.selectorInfo
							.itemData, //itemData: p.itemData,  搜索的时候  p.itemData为空  赋值修改 为 她自己本身 this.selectorInfo.itemData
						// isMultiple: this.selectorInfo.isMultiple,
						totalCount: this.selectorInfo.totalCount,
						showPopup: true,
						title: p.title,
						name: p.name,
						dataLists: dataSelect,
						defaultChecked: defaultChecked
					}
				} else {
					// 暂无数据
				}
			},
			handleSelected(e) {
				this.selectorInfo.showPopup = false
				if (this.selectorInfo.name == "Nav_FromUserNAME") {
					this.model.Nav_FromUser = e
					this.model.FROM_USER_ID = e.ID
				} else if (this.selectorInfo.name == "Nav_ToUserNAME") {
					this.model.Nav_ToUser = e
					this.model.TO_USER_ID = e.ID
				} else if (this.selectorInfo.name == "Nav_DoUserNAME") {
					this.model.Nav_DoUser = e
					this.model.DO_USER_ID = e.ID
				} else if (this.selectorInfo.name == "Nav_FormDepartmentNAME") {
					this.model.Nav_FormDepartment = e
					this.model.FORM_DEPARTMENT_ID = e.ID
				} else if (this.selectorInfo.name == "Nav_ToDepartmentNAME") {
					this.model.Nav_ToDepartment = e
					this.model.TO_DEPARTMENT_ID = e.ID
				} else if (this.selectorInfo.name == "Nav_DoDepartmentNAME") {
					this.model.Nav_DoDepartment = e
					this.model.DO_DEPARTMENT_ID = e.ID
				}
			},
			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
			},

			// //显示组件
			// handleChange() {
			// 	this.selectorInfo.showPopup = true
			// 	this.handleSearch()
			// },

			//数据加载
			handleSearch(val, pageIndex) {
				var p = {
					name: this.selectorInfo.name,
					title: this.selectorInfo.title,
					val: val,
					pageIndex: pageIndex
				}
				this.handleShowSheet(p)
			},

			submit() {
				const ele = this.$refs
				this.modelEdit = JSON.parse(JSON.stringify(this.model))

				this.modelEdit.Nav_FormDepartment = null
				this.modelEdit.Nav_FromUser = null
				this.modelEdit.Nav_ToDepartment = null
				this.modelEdit.Nav_ToUser = null
				this.modelEdit.Nav_DoDepartment = null
				this.modelEdit.Nav_DoUser = null

				ele['wForm'].validate().then(res => {
					if (this.TaskID != null && this.TaskID != '' && this.TaskID.length > 0)
						this.modelEdit.TaskID = this.TaskID
					this.modelEdit.PUBLISH = "SaveAndNotify";

					FullUpdateContact(this.modelEdit).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					})
				}).catch(err => {
					if (err.length > 0) {
						uni.$showErrorInfo(err[0].message)
					} else {
						uni.$showErrorInfo('校验失败！')
					}
				})
			},

			handleChange(item) {
				var dataSour = []
				if (item.name == 'OGAssigneeStatus') {
					dataSour = this.listEnumStatusText
				} else if (item.name == 'OGSatisfactionStatus') {
					dataSour = this.listEnumSatisfactionText
				}
				this.comPickerInfo = {
					showSheet: true,
					columns: [dataSour],
					dataItem: item.data,
					title: item.title,
					name: item.name
				}
			},
			onConfirmPicker(e) {
				//赋枚举值
				if (this.comPickerInfo.name == 'OGAssigneeStatus') {
					this.comPickerInfo.dataItem.ASSIGNEE_STATUS_SHOW = e.value[0]
					this.comPickerInfo.dataItem.ASSIGNEE_STATUS = this.listEnumStatus[e.indexs[0]].code
				} else if (this.comPickerInfo.name == 'OGSatisfactionStatus') {
					this.comPickerInfo.dataItem.SATISFACTION_STATUS_SHOW = e.value[0]
					this.comPickerInfo.dataItem.SATISFACTION_STATUS = this.listEnumSatisfactionText[e.indexs[0]].code
				}
				this.comPickerInfo = {
					showSheet: false,
					dataItem: this.comPickerInfo.dataItem,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
			},

			//显示时间控件
			showCheckDate(dataIndex) {
				if (this.model.DATE == null) {
					this.model.DATE = uni.$u.timeFormat(new Date(), 'yyyy-mm-dd')
				}
				this.dateTimePickerInfo = {
					showCheckDate: true,
					dataIndex: dataIndex,
					defaultDateTime: this.model.DATE
				}
			},
			//隐藏控件 显示时间
			handleCheckDate(e) {
				let v = uni.$u.timeFormat(e.value, 'yyyy-mm-dd')
				const {
					name,
					dataIndex
				} = this.dateTimePickerInfo
				this.dateTimePickerInfo.showCheckDate = false
				this.model.DATE = uni.$u.timeFormat(e.value, 'yyyy-mm-dd')
			},
		}
	}
</script>

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

	.todo-page>>>.u-steps-item__line {
		width: 187.6px !important;
	}
</style>