<!-- WB013 外包工程危险作业申请编辑 -->
<template>
	<view class="todo-page">
		<view class="background"></view>
		<view class="card" v-if="stepsPageList === 0">
			<!-- <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 label="驳回理由" prop="AUDIT_OPINION" borderBottom>
						<u--input disabled disabledColor="#ffffff" v-model="model.AUDIT_OPINION" border="none"
							 inputAlign="right"></u--input>
					</u-form-item> -->
				<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>
					<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.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="Nav_Type.NAME" borderBottom>
					<u--input disabled v-model="model.Nav_Type.NAME" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>

				<u-form-item label="危险作业名称" prop="DANGEROUS_JOB_NAME" borderBottom>
					<u--input disabled v-model="model.DANGEROUS_JOB_NAME" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>
				<u-form-item label="危险作业具体事项" prop="DANGEROUS_JOB_ITEM" borderBottom>
					<u--input disabled v-model="model.DANGEROUS_JOB_ITEM" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>
				<u-form-item label="作业风险辨识" prop="JOB_RISK_IDENTIFY" borderBottom>
					<u--input disabled v-model="model.JOB_RISK_IDENTIFY" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>

				<u-form-item label="作业开始时间" prop="START_TIME" borderBottom>
					<u--input disabled v-model="model.START_TIME" border="none" inputAlign="right" fontSize="14px"></u--input>

				</u-form-item>
				<u-form-item label="作业结束时间" prop="END_TIME" borderBottom>
					<u--input disabled v-model="model.END_TIME" border="none" inputAlign="right" fontSize="14px"></u--input>

				</u-form-item>

				<u-form-item label="作业地点" prop="JOB_ADDRESS" borderBottom>
					<u--input disabled v-model="model.JOB_ADDRESS" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>

				<u-form-item label="作业人员:" >
				</u-form-item>
				<u-textarea v-model="model.UsersName" disabled autoHeight ></u-textarea>

				<u-form-item label="现场监护人" prop="USER_ID" borderBottom>
					<u--input disabled v-model="model.Nav_User.NAME" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>

				<view class="upload-title">附件</view>
				<full-upload v-model="model.Nav_Files" :isShowBtn='false' :listProp='listPropUpload' :listPropVal='listPropValUpload'></full-upload>

			</u--form>

			<!-- </uni-card> -->
		</view>
		<view style="padding: 10px 16px;" v-if="stepsPageList === 0" class="bottom-button">
			<u-button type="primary" :disabled="isLoadOK" @click="stepAdd" color="#3d4b70">下一步</u-button>
		</view>
		<view class="card" v-if="stepsPageList === 1">
			<u--form labelPosition="left" labelWidth="auto" labelAlign="center" errorType="border-bottom" ref="sForm" class="sub-form">
				<uni-collapse :border="false" accordion>
					<view v-for="(item, index) in model.Nav_RecordDetails.filter(i => !i.IS_DELETED)" :key="index">
						<uni-collapse-item title-border="none" :border="false">
							<template v-slot:title>
								<view class="uni-collapse-item__title-box">
									<view class="uni-collapse-item__title-text">
										{{index+1+'.'+item.MEASURE}}
									</view>
									<view @click.stop style="margin-right: 10px;">
										<u-button type="primary" size="small" :disabled="item.ISOCONFIRM" @click="changeConfirm(item)" :text="item.ISOCONFIRM? '已确认' : '确认'"></u-button>
										<!-- <u-icon @click="handleDelRowBefore(item)" name="trash" color="#ff4d4f" size="21" style="margin-right: 10px;"></u-icon> -->
										<!-- <u-icon name="arrow-down" size="12"></u-icon> -->
									</view>
								</view>
							</template>
							<view class="content">
								<u-form-item label="序号" prop="ROW_NUM" borderBottom>
									<u--input disabledColor="#fff" v-model="item.ROW_NUM" border="none" inputAlign="right" fontSize="14px"></u--input>
								</u-form-item>
								<u-form-item label="安全措施" class="subRequired" required prop="MEASURE" borderBottom>
									<u--input disabledColor="#fff" v-model="item.MEASURE" border="none" inputAlign="right" fontSize="14px"></u--input>
								</u-form-item>
								<!-- <u-form-item label="是否涉及" class="subRequired" required prop="ISORNOTShow" borderBottom @click="handleChange( item)">
									<u--input disabled disabledColor="#fff" v-model="item.ISORNOTShow" 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="REMARK" borderBottom>
									<u--input disabledColor="#fff" v-model="item.REMARK" border="none" inputAlign="right" fontSize="14px"></u--input>
								</u-form-item>
								<!-- <u-form-item label="确认" prop="REMARK" borderBottom>
									<view style="display: flex;justify-content: end;flex: 1;align-items: center;">
										否<u-switch space="5" v-model="item.ISOCONFIRM" @change="changeConfirm($event,item)" inactiveColor="rgb(230, 230, 230)" style="margin: 0px 5px;"></u-switch>是
									</view>

								</u-form-item> -->
								<view class="upload-title">附件</view>
								<full-upload v-model="item.Nav_Files" :isShowBtn='true' :listProp='listPropUpload' :listPropVal='listPropValUpload'></full-upload>
								<view style="margin: 10px 0px;">
									<u-button type="error" icon="trash" @click="handleDelRowBefore(item)">删除</u-button>
								</view>

							</view>


						</uni-collapse-item>
					</view>
				</uni-collapse>
				<view style="margin-top: 10px;">
					<u-button text="新增" type="primary" @click="handleAdd"></u-button>
				</view>
			</u--form>

		</view>
		<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;" v-if="stepsPageList === 1" class="bottom-button">
			<u-button type="primary" @click="stepReduce" color="#3d4b70" :plain="true" style="margin-right: 5px;">上一步</u-button>
			<u-button type="primary" @click="submit" color="#3d4b70" style="margin-left: 5px;">提交</u-button>
		</view>
		<!-- <view style="padding: 10px 16px;" v-if='isLoadOK' class="bottom-button">
			<u-button type="primary" @click="submit" color="#3d4b70">提交</u-button>
		</view> -->
		<!-- 		<view class="bottom-button">
			<button type="primary" v-if='isLoadOK' @click="submit">提交</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 {
		WBRecordFull,
		JobTypeDangerApply,
		OrderEntitiesJobSafeMeasureEnum,
		ProjectInputDangerApply,
		WBUserOrderPaged,
		GetDangerousJob
	} 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,
				model: {
					ID: '',
					ORG_ID: '',
					TaskID: null,
					Nav_Files: [],
					AUDIT_OPINION: '',
					PROJECT_ID: '',
					Nav_Project: {
						NAME: ''
					},
					UNIT_NAME: '',
					TYPE_ID: '',
					Nav_Type: {
						NAME: ''
					},
					DANGEROUS_JOB_NAME: '',
					DANGEROUS_JOB_ITEM: '',
					JOB_RISK_IDENTIFY: '',
					START_TIME: '',
					END_TIME: '',
					JOB_ADDRESS: '',
					UsersName: '',
					Nav_Users: [{
						Nav_User: {
							NAME: ''
						}
					}],
					USER_ID: '',
					Nav_User: {
						NAME: ''
					},
					Nav_Files: [],
					Nav_RecordDetails: [{
						ID: '',
						ORG_ID: '',
						PARENT_ID: '',
						ROW_NUM: '',
						MEASURE: '安全措施',
						ISORNOT: '',
						ISORNOTShow: '',
						REMARK: '',
					}]
				},
				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.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()
			// this.loadData()
		},
		methods: {
			getEnums() {
				var dataParm = {
					'name': "ISORNOT"
				}
				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()
				})
			},
			changeConfirm(item) {
				const validDetails = this.model.Nav_RecordDetails.filter(i => !i.IS_DELETED);
				const index = validDetails.indexOf(item);
				const realIndex = this.model.Nav_RecordDetails.findIndex(item => item === validDetails[index]);
				this.model.Nav_RecordDetails[realIndex].ISOCONFIRM = true;
			},
			handleDelRowBefore(item) {
				item.IS_DELETED = true
				let indexNotDel = 1
				this.model.Nav_RecordDetails.forEach((e, i) => {
					if (!e.IS_DELETED) {
						e.ROW_NO = indexNotDel
						indexNotDel++
					}
				})
				this.$forceUpdate()
			},
			stepAdd() {
				this.stepsPageList = this.stepsPageList + 1

			},
			stepReduce() {
				this.stepsPageList = this.stepsPageList - 1
			},
			loadData() {
				if (this.model.ID == undefined || this.model.ID == '') {
					this.isLoadOK = false
					this.model.Nav_RecordDetails = []
					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_Files.Nav_ImgFile')
					extendInclude(json, 'Nav_Project')
					extendInclude(json, 'Nav_Type')
					extendInclude(json, 'Nav_Users')
					extendInclude(json, 'Nav_Users.Nav_User')
					extendInclude(json, 'Nav_User')
					extendInclude(json, 'Nav_RecordDetails')
					extendInclude(json, 'Nav_RecordDetails.Nav_Files.Nav_ImgFile')

					GetDangerousJob(json).then(ret => {
						this.isLoadOK = false
						this.model = ret
						this.initializeMissingFields();
						let UsersName = ''
						if (this.model.Nav_Users&&this.model.Nav_Users.length > 0) {
							this.model.Nav_Users.forEach((item, i) => {
								if (item.Nav_User.NAME != null) {
									if (i > 0) {
										UsersName += ' '
									}
									UsersName += item.Nav_User.NAME
								}
							})
						}
						this.model.UsersName = UsersName
						this.model.Nav_RecordDetails.map(item => {
							item.ISORNOTShow = item.ISORNOT == 1 ? '是' : '否'
							item.ORG_ID = this.ORG_ID
							item.PARENT_ID = this.model.ID
						})
						this.model.Nav_RecordDetails = this.model.Nav_RecordDetails.sort((a, b) => a.ROW_NUM - b.ROW_NUM);

						this.listPropValUpload.push(this.model.ID)

						// uni.$updateData()

					});


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

					}
				});
			},
			submit() {
				let arr = []
				this.model.Nav_RecordDetails.forEach((e, i) => {
					if (!e.IS_DELETED && !e.ISOCONFIRM) {
						arr.push(i + 1)

					}
				})
				if (arr.length > 0) {
					uni.$showErrorInfo('请确认所有安全措施信息！行:' + arr)
				} else {
					this.modelEdit = JSON.parse(JSON.stringify(this.model))
					delete this.modelEdit.Nav_Project
					delete this.modelEdit.Nav_Type
					delete this.modelEdit.Nav_User
					this.modelEdit.Nav_RecordDetails.map(item => {
						item.TaskID = this.TaskID
						item.PUBLISH = 'SaveAndNotify'
					})
					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'
					WBRecordFull(this.modelEdit).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					})
				}

			},
			handleAdd() {
				if (this.model.Nav_RecordDetails == null) {
					this.model.Nav_RecordDetails = []
				}

				var ROW_NO = 1;
				if (this.model.Nav_RecordDetails.length > 0) {
					this.model.Nav_RecordDetails.forEach(e => {
						if (e.IS_DELETED == undefined || e.IS_DELETED == false)
							ROW_NO++
					})
				}
				var modelAdd = {
					ID: guid(),
					ORG_ID: this.ORG_ID,
					PARENT_ID: this.model.ID,
					ROW_NUM: ROW_NO,
					MEASURE: '',
					ISORNOT: 1,
					ISORNOTShow: '是',
					REMARK: '',
					ISOCONFIRM: false,
					IS_DELETED: false,
				}
				this.model.Nav_RecordDetails.push(modelAdd)
				this.indexShow = ROW_NO
			},

			handleChange(item) {
				this.comPickerInfo = {
					showSheet: true,
					columns: [this.listISORNOTText],
					dataItem: item
				}
			},
			onConfirmPicker(e) {
				this.comPickerInfo = {
					showSheet: false,
					dataItem: this.comPickerInfo.dataItem,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
				this.comPickerInfo.dataItem.ISORNOTShow = e.value[0]
				this.comPickerInfo.dataItem.ISORNOT = this.listISORNOT[e.indexs[0]].code
			}
		}
	}
</script>

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

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