<!-- 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 required label="项目名称" prop="Nav_Project.NAME" borderBottom  @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 label="检查类别" prop="ISORNOTShow" borderBottom @click="handleChange()">
					<u--input disabled placeholder="请选择检查类别" disabledColor="#fff" v-model="model.CHECK_CATEGORYShow" 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="END_TIME" borderBottom @click="showCheckDate(1)">
					<u--input disabledColor="#fff" placeholder="请选择检查时间" disabled v-model="model.CHECK_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-item label="发现隐患条数" prop="ISORNOTShow" borderBottom>
					<u--input type="number" disabledColor="#fff" placeholder="请填写隐患条数" v-model="model.HIDDEN_DANGER_COUNT" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>
				<view class="upload-title">检查照片</view>
				<full-upload v-model="model.Nav_Photos" :isShowBtn='true' :listProp='listPropUpload' :listPropVal='listPropValUpload'></full-upload>
				<view class="upload-title">附件</view>
				<full-upload v-model="model.Nav_Files" :isShowBtn='true' :listProp='listPropUpload' :listPropVal='listPropValUpload'></full-upload>


			</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" mode="date"></u-datetime-picker>
		<u-picker :show="comPickerInfo.showSheet" :columns="comPickerInfo.columns" @confirm="onConfirmPicker" @close="comPickerInfo.showSheet=false" @cancel="comPickerInfo.showSheet=false" keyName="NAME"></u-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 {
		WBSafeCheckGet,
		WBSCFullUpdate,
		OrderEntitiesJobSafeMeasureEnum,
		CheckUser,
		ProjectInputDangerApply,
		WBDepartment,
		GetProject
	} 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: ''
					},
					Nav_Photos:[],
					Nav_Files:[],
					DEPARTMENT_ID: '',
				},
				rules: {
					'NAME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请填写【课程名称】'
					},
				},
				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: '',
					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.getEnums()
		},
		methods: {
			getEnums() {
				var dataParm = {
					'name': "WBCHECKCATAGORY"
				}
				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() {
				if (this.model.ID == undefined || this.model.ID == '') {
					this.isLoadOK = false
					this.isNew = true
					if (this.model.ID == undefined || this.model.ID == '')
						this.model.ID = guid()
					let userPro = uni.getStorageSync('appInfo').User.PROJECT_ID
					if (userPro && userPro !== null) {
						let json = initFilter();
						extendRule(json, 'ID', 1, userPro);
						GetProject(json).then(ret => {
							this.model.PROJECT_ID = ret.ID
							this.model.Nav_Project.NAME = ret.NAME
							this.model.Nav_Project.UNIT_NAME = ret.UNIT_NAME
							this.model.DEPARTMENT_ID = ret.DEPARTMENT_ID

							this.isLoadOK = false
						});
					} else {
						this.isLoadOK = false
					}
				} else {
					let json = initFilter(this.ORG_ID);
					extendRule(json, 'ID', 1, this.model.ID);
					extendInclude(json, 'Nav_Project')
					extendInclude(json, 'Nav_Files.Nav_ImgFile')
					extendInclude(json, 'Nav_Photos.Nav_ImgFile')
					WBSafeCheckGet(json).then(ret => {
						this.model = ret
						this.listPropValUpload.push(this.model.ID)

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

				this.listPropValUpload.push(this.model.ID)
			},
			handleChange() {
				this.comPickerInfo = {
					showSheet: true,
					columns: [this.listISORNOTText],
				}
			},
			onConfirmPicker(e) {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
				this.model.CHECK_CATEGORYShow = e.value[0]
				this.model.CHECK_CATEGORY = this.listISORNOT[e.indexs[0]].code
			},
			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;
				}
				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
				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.CHECK_TIME = uni.$u.timeFormat(e.value,
					'yyyy-mm-dd')
				this.dateTimePickerInfo.showCheckDate = false
			},

			submit() {
				if(!this.model.Nav_Project.NAME){
					uni.$showErrorInfo('请选择项目名称！')
					return
				}
				this.modelEdit = JSON.parse(JSON.stringify(this.model))
				this.modelEdit.HIDDEN_DANGER_COUNT = Number(this.modelEdit.HIDDEN_DANGER_COUNT )||''
				this.modelEdit.UNIT_NAME = this.modelEdit.Nav_Project.UNIT_NAME||''
				this.modelEdit.NAME = this.modelEdit.Nav_Project.NAME||''
				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'
				// console.log(this.modelEdit)
				WBSCFullUpdate(this.modelEdit).then(res => {
					uni.$showMsgFunc('操作成功!', () => {
						uni.navigateBack()
					}, 'success', 1000)
				})

			},
		}
	}
</script>

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

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