<template>
	<view class="todo-page">
		<view class="background"></view>
		<view class="card">
			<!-- <uni-card margin="0" :is-shadow="true"> -->
			<u--form labelPosition="left" labelWidth="auto" labelAlign="center" :model="model" :rules="rules" ref="wForm" errorType="border-bottom" class="demo-ruleForm">
				<u-form-item required label="休假人员" borderBottom @click="handleShowSheet({title: '休假人员', name: 'Nav_User'})">
					<u--input placeholder="请选择休假人员" disabled disabledColor="#ffffff" v-model="(model.Nav_User||{}).NAME" border="none" inputAlign="right" fontSize="14px"></u--input>
					<u-icon style="margin-left: 4px;" name="arrow-down" size="12" slot="right">
					</u-icon>
				</u-form-item>
				<u-form-item label="部门" prop="POSITION" borderBottom>
					<u--input disabled disabledColor="#ffffff" v-model="model.DEPARTMENT_NAME" border="none" inputAlign="right" fontSize="14px">
					</u--input>
				</u-form-item>
				<u-form-item  required label="开始时间" prop="CHECKDATETIME" borderBottom @click="showCheckDate(1)">
					<u--input disabled disabledColor="#fff" v-model="model.START_DATE" placeholder="请选择开始时间" border="none" inputAlign="right" fontSize="14px"></u--input>
					<u-icon style="margin-left: 4px;" name="arrow-down" size="12"></u-icon>
				</u-form-item>
				<u-form-item required label="结束时间" prop="ENDTIME" borderBottom @click="showCheckDate(2)">
					<u--input disabled disabledColor="#fff" v-model="model.END_DATE" placeholder="请选择结束时间" border="none" inputAlign="right" fontSize="14px"></u--input>
					<u-icon style="margin-left: 4px;" name="arrow-down" size="12"></u-icon>
				</u-form-item>
				<u-form-item label="代理人" prop="Nav_Repair_User.NAME" borderBottom @click="handleShowSheet({title: '代理人', name: 'Nav_Agent'})">
					<u--input placeholder="请选择代理人" disabled disabledColor="#ffffff" v-model="(model.Nav_Agent||{}).NAME" border="none" inputAlign="right" fontSize="14px"></u--input>
					<u-icon style="margin-left: 4px;" name="arrow-down" size="12" slot="right">
					</u-icon>
				</u-form-item>
				<u-form-item label="启用标志" prop="Product_Unit" @click="handleChange('ENABLE_STATUS')" borderBottom>
					<u--input v-model="model.ENABLE_STATUSShow" disabled disabledColor="#ffffff" placeholder="请选择" border="none" inputAlign="right" fontSize="14px">
					</u--input>
					<u-icon style="margin-left: 4px;" name="arrow-down" size="12">
					</u-icon>
				</u-form-item>



			</u--form>

		</view>
		<u-datetime-picker :show="dateTimePickerInfo.showCheckDate" v-model='dateTimePickerInfo.defaultDateTime' @confirm="handleCheckDate" @close="dateTimePickerInfo.showCheckDate = false"
			@cancel="dateTimePickerInfo.showCheckDate = false" :mode="dateTimePickerInfo.mode"></u-datetime-picker>
		<query-selector :multiple="selectorInfo.isMultiple" :show="selectorInfo.showPopup" :title="selectorInfo.title" :lists="selectorInfo.dataLists" :defaultChecked="selectorInfo.defaultChecked" @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>
		<view style="padding: 10px 16px;display: flex;flex-direction: row;" class="bottom-button">
			<u-button type="primary" @click="submit" color="#3d4b70" :disabled="isLoadOK">提交</u-button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		guid,
		initFilter,
		initFilterGroup,
	} from '../../../../utils/common'
	import {
		FMProductionUnit,
		CheckPeriods,
		CheckLevels,
		MineTypeIndex,
		MineTypeName
	} from '../../../../utils/enums.js'

	import {
		PersonLeaveFull,
		getFMUser
	} from '../../../../services/apply/FMServices/FMServices.js'
	import config from '../../../../config/common'

	import {
		getEnum
	} from '../../../../services/common';

	export default {
		data() {
			return {
				tableKey: '0',
				TaskID: '',
				ID: '',
				ORG_ID: uni.getStorageSync('orgId'),
				isLoadOK: true,
				stepsPageList: 0,
				listISORNOT: [], //是否涉及
				listISORNOTText: [],
				indexShow: 0,
				isNew: true,
				dateTimePickerInfo: {
					showCheckDate: false,
					dataIndex: undefined,
					defaultDateTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
					value: '',
					name: '',
					mode: ''
				},
				model: {

				},
				rules: {

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

				selectorInfo: {
					name: 'user',
					isMultiple: false,
					showPopup: false,
					totalCount: 0,
					title: '考核结果选择',
					dataItem: {},
					index: 0,
					columns: [],
					dataLists: [],
					defaultText: '',
					defaultChecked: []
				},
			}
		},
		onLoad(option) {
			this.getEnums()
		},
		methods: {
			getEnums() {
				var dataParm = {
					'name': "FMEnableStatusEnum"
				}
				getEnum(dataParm).then(res => {
					this.listISORNOT = res
					this.listISORNOTText = []
					this.listISORNOT.forEach(e => {
						e.name = e.NAME
						e.code = e.ID
						this.listISORNOTText.push(e.NAME)
					})
					this.loadData()
				})
			},
			loadData() {
				this.ID = guid()
				this.model.ID = this.ID
				this.isLoadOK = false
				this.model.ENABLE_STATUSShow = this.listISORNOT[0].name
				this.model.ENABLE_STATUS = this.listISORNOT[0].code
			},
			async handleShowSheet(p) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", 0, p.pageIndex ?? 1)
				// const json = initFilter(this.ORG_ID, "", "", 0, p.pageIndex ?? 1)
				let dataSelect = []
				var defaultChecked = []
				this.selectorInfo.isMultiple = false


				if (p.val) {
					extendRule(json, 'NAME', 9, p.val);
				}


				json.Limit = 20
				if (p.pageIndex) {
					json.Start = (p.pageIndex - 1) * json.Limit;
				}
				json.Sort = 'NAME'
				// json.Sort = "NAME"
				// json.Order = 1
				if (p.name == 'Nav_User') {
					extendInclude(json, 'Nav_Department');
				}
				dataSelect = await getFMUser(json).then(res => {
					this.selectorInfo.totalCount = res.TotalCount
					if (res != undefined && res.Data.length > 0) {
						for (let i = 0; i < res.Data.length; i++) {
							res.Data[i].name = res.Data[i].NAME
							res.Data[i].code = res.Data[i].MACHINE_CODE
						}
					}
					return res.Data
				})

				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 {
					// 暂无数据
				}
			},
			//数据加载
			handleSearch(val, pageIndex) {
				var p = {
					name: this.selectorInfo.name,
					title: this.selectorInfo.title,
					val: val,
					pageIndex: pageIndex,
				}
				this.handleShowSheet(p)
			},

			handleSelected(e) {
				this.selectorInfo.showPopup = false
				if (this.selectorInfo.name == 'Nav_User') {
					this.model.Nav_User = e;
					this.model.USER_ID = e.ID;
					this.model.DEPARTMENT_NAME = e.Nav_Department.NAME;
				} else if (this.selectorInfo.name == 'Nav_Agent') {
					this.model.Nav_Agent = e;
					this.model.AGENT_ID = e.ID;
				}



			},
			submit() {
				if(!this.model.USER_ID){
					uni.$showErrorInfo('请选择休假人员')
					return
				}
				if(!this.model.START_DATE){
					uni.$showErrorInfo('请选择开始时间')
					return
				}
				if(!this.model.END_DATE){
					uni.$showErrorInfo('请选择结束时间')
					return
				}
				this.modelEdit = JSON.parse(JSON.stringify(this.model))
				delete this.modelEdit.Nav_Department
				delete this.modelEdit.Nav_Agent
				delete this.modelEdit.Nav_User
				
				if (this.TaskID == null || this.TaskID == '') {
					this.modelEdit.TaskID = '00000000-0000-0000-0000-000000000000'
				} else {
					this.modelEdit.TaskID = this.TaskID
				}
				if (this.modelEdit.ORG_ID == null || this.modelEdit.ORG_ID == '')
					this.modelEdit.ORG_ID = this.ORG_ID
				PersonLeaveFull(this.modelEdit).then(res => {
					uni.$showMsgFunc('操作成功!', () => {
						uni.navigateBack()
					}, 'success', 1000)
				})


			},
			handleChange(name) {

				this.comPickerInfo = {
					showSheet: true,
					columns: [this.listISORNOTText],
					name: name
				}


			},
			onConfirmPicker(e) {
				this.model.ENABLE_STATUSShow = e.value[0]
				this.model.ENABLE_STATUS = this.listISORNOT.find(item => item.name === e.value[0]).code
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					formIndex: undefined
				}
			},
			showCheckDate(dataIndex) {
				// let date = new Date()
				// this.dateTimePickerInfo.showCheckDate = true
				this.dateTimePickerInfo = {
					showCheckDate: true,
					defaultDateTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
					dataIndex: dataIndex,
					mode: 'date'
				}
			},
			//隐藏控件 显示时间
			handleCheckDate(e) {

				if (this.dateTimePickerInfo.dataIndex == 1) {
					this.model.START_DATE = uni.$u.timeFormat(e.value,
						'yyyy-mm-dd')
				} else if (this.dateTimePickerInfo.dataIndex == 2) {
					this.model.END_DATE = uni.$u.timeFormat(e.value,
						'yyyy-mm-dd')
				}
				this.dateTimePickerInfo.showCheckDate = false

			},
		}
	}
</script>

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

	/* .todo-page {
		padding: 16px 16px 70px;
	} */
</style>