<!-- WB013 外包工程危险作业申请编辑 -->
<template>
	<view class="todo-page">
		<view class="background"></view>
		<view class="card">
			<u--form labelPosition="left" labelWidth="auto" labelAlign="center" :model="model" :rules="rules" ref="wForm" errorType="border-bottom" 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="Nav_Project.NAME" borderBottom v-if="!isNew">
					<u--input disabled v-model="model.Nav_Project.NAME" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>
				<u-form-item label="项目名称" prop="Nav_Project.NAME" borderBottom v-if="isNew" @click="handleShowSheet({title: '项目名称', name: 'ProjectName'})">
					<u--input v-model="model.Nav_Project.NAME" 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_Project.UNIT_NAME" borderBottom>
					<u--input disabled v-model="model.Nav_Project.UNIT_NAME" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>

				<u-form-item required label="课程名称" prop="NAME" borderBottom>
					<u--input placeholder="请填写课程名称" v-model="model.NAME" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>




				<u-form-item label="培训部门:" prop="Nav_Department.NAME" borderBottom @click="handleShowSheet({title: '培训部门', name: 'Nav_Department'})">
					<u--input disabledColor="#fff" disabled placeholder="请选择培训部门" v-model="model.Nav_Department.NAME" 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="Nav_User.NAME" borderBottom @click="handleShowSheet({title: '培训讲师', name: 'UserName'})">
					<u--input disabledColor="#fff" disabled placeholder="请选择培训讲师" 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"></u-icon>
				</u-form-item>
				<u-form-item required label="最迟培训时间:" prop="END_TIME" borderBottom @click="showCheckDate(1)">
					<u--input disabledColor="#fff" placeholder="请选择最迟培训时间" disabled v-model="model.END_TIME" 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>




		<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-datetime-picker :show="dateTimePickerInfo.showCheckDate" v-model='dateTimePickerInfo.defaultDateTime' @confirm="handleCheckDate" @close="dateTimePickerInfo.showCheckDate = false"
			@cancel="dateTimePickerInfo.showCheckDate = false"></u-datetime-picker>
		<view class="bottom-button">

			<u-button type="primary" @click="submit" color="#3d4b70" style="margin-left: 5px;">提交</u-button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		guid,
		initFilter,
		initFilterGroup
	} from '../../../../utils/common'
	import {
		getRequest,
	} from '../../../../services/apply/FOServices/FOServices';
	import {
		WBTSGet,
		WBTSFull,
		OrderEntitiesJobSafeMeasureEnum,
		CheckUser,
		ProjectInputDangerApply,
		WBDepartment
	} from '../../../../services/apply/subPages/scWB';

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

	import config from '../../../../config/common'
	export default {
		data() {
			return {
				tableKey: '0',
				TaskID: '',
				ORG_ID: uni.getStorageSync('orgId'),
				isLoadOK: true,
				stepsPageList: 0,
				listISORNOT: [], //是否涉及
				listISORNOTText: [],
				listPropUpload: ['PARENT_ID'],
				listPropValUpload: [],
				indexShow: 0,
				isNew: false,
				model: {
					ID: '',
					ORG_ID: '',
					TaskID: null,
					AUDIT_OPINION: '',
					PROJECT_ID: '',
					Nav_Project: {
						NAME: '',
						UNIT_NAME: ''
					},
					NAME: '',
					Nav_Department: {
						NAME: ''
					},
					DEPARTMENT_ID: '',
					END_TIME: '',
					PARENT_ID: '',
					USER_ID: '',
					Nav_User: {
						NAME: ''
					},
				},
				rules: {
					'NAME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请填写【课程名称】'
					},
					'Nav_User.NAME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请选择【培训讲师】'
					},
					'END_TIME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请选择【最迟培训时间】'
					}
				},
				dateTimePickerInfo: {
					showCheckDate: false,
					dataIndex: undefined,
					defaultDateTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM:ss'),
					value: '',
					name: ''
				},
				selectorInfo: {
					name: 'user',
					isMultiple: false,
					showPopup: false,
					totalCount: 0,
					title: '考核结果选择',
					dataItem: {},
					index: 0,
					columns: [],
					dataLists: [],
					defaultText: '',
					defaultChecked: []
				},
			}
		},
		onLoad(option) {
			// this.TaskID = option.taskID ? option.taskID : '';
			// this.model.ID = option.ID ? option.ID : '';
			// this.JOBID = option.ID ? option.ID : '';
			// this.tableKey = option.tableKey ? option.tableKey : '0'
			// this.loadData();

			this.model.ID = option.ID
			this.TaskID = option.taskID
			this.tableKey = option.tableKey
			if (option.taskID != null && option.taskID != '')
				this.model.TaskID = option.taskID
			this.loadData()
		},
		methods: {
			loadData() {
				if (this.model.ID == undefined || this.model.ID == '') {
					this.isLoadOK = false
					this.isNew = true
					this.initializeMissingFields();
					if (this.model.ID == undefined || this.model.ID == '')
						this.model.ID = guid()
				} else {
					let json = initFilter(this.ORG_ID);
					extendRule(json, 'ID', 1, this.model.ID);
					extendInclude(json, 'Nav_Project')
					extendInclude(json, 'Nav_Department')
					extendInclude(json, 'Nav_User')
					WBTSGet(json).then(ret => {
						this.model = ret
						this.initializeMissingFields();
						this.listPropValUpload.push(this.model.ID)

						// uni.$updateData()
						this.isLoadOK = false
					});
				}

				this.listPropValUpload.push(this.model.ID)
			},
			initializeMissingFields() {
				const fields = ['Nav_Project', 'Nav_Department', 'Nav_User'];
				fields.forEach(field => {
					if (!this.model[field]) {
						if (field == 'Nav_Project') {
							this.model[field] = {
								NAME: '',
								UNIT_NAME: ''
							};
						} else {
							this.model[field] = {
								NAME: ''
							};
						}

					}
				});
			},
			async handleShowSheet(p) {
				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.Sort = "NAME"
				// json.Order = 1
				json.Limit = 20
				if (p.pageIndex) {
					json.Start = (p.pageIndex - 1) * json.Limit;
				}
				if (p.name == 'Nav_Department') {
					extendRule(json, 'ENABLE_STATUS', 1, '0');
					dataSelect = await WBDepartment(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].CODE
							}
						}
						return res.Data
					})
				} else if (p.name == 'UserName') {
					if (this.model.DEPARTMENT_ID != undefined && this.model.DEPARTMENT_ID != '')
						extendRule(json, 'DEPARTMENT_ID', 1, this.model.DEPARTMENT_ID);
					extendRule(json, 'ENABLE_STATUS', 1, '0');
					dataSelect = await CheckUser(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].CODE
							}
						}
						return res.Data
					})
				} else if (p.name == 'ProjectName') { //项目名称
					dataSelect = await ProjectInputDangerApply(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].UNIT_NAME
							}
						}
						return res.Data
					})
				}


				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
				}
			},
			//数据加载
			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 == 'UserName') { //交底人
					this.model.Nav_User = e
					this.model.USER_ID = e.ID
				} else if (this.selectorInfo.name == 'Nav_Department') {
					this.model.Nav_Department = e
					this.model.DEPARTMENT_ID = e.ID
				} else if (this.selectorInfo.name == 'ProjectName') { //项目名称
					this.model.PROJECT_ID = e.ID
					this.model.Nav_Project = e
				}
			},
			//显示时间控件
			showCheckDate(dataIndex) {
				this.dateTimePickerInfo.showCheckDate = true
				this.dateTimePickerInfo = {
					showCheckDate: true,
					dataIndex: dataIndex,
				}
			},
			//隐藏控件 显示时间
			handleCheckDate(e) {
				this.model.END_TIME = uni.$u.timeFormat(e.value,
					'yyyy-mm-dd hh:MM:ss')
				this.dateTimePickerInfo.showCheckDate = false
			},

			submit() {
				const ele = this.$refs
				ele['wForm'].validate().then(res => {
					this.modelEdit = JSON.parse(JSON.stringify(this.model))
					delete this.modelEdit.Nav_Project
					delete this.modelEdit.Nav_User
					delete this.modelEdit.Nav_Department
					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
					this.modelEdit.PUBLISH = 'SaveAndNotify'
					WBTSFull(this.modelEdit).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					})

				}).catch(err => {
					uni.$showErrorInfo(err[0].message + '！')
				})

			},
		}
	}
</script>

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

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