<!-- WB013 外包工程危险作业申请编辑 -->
<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 label="驳回意见" prop="AUDIT_OPINION" borderBottom v-if="model.STATUS==40">
					<u--input disabled disabledColor="#ffffff" v-model="model.CONTEXT" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>

				<u-form-item label="申购部门" prop="Nav_Project.NAME" borderBottom>
					<u--input disabled v-model="model.Nav_Department.NAME" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>
				<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>


			</u--form>
			<u--form labelPosition="left" labelWidth="auto" labelAlign="center" errorType="border-bottom" ref="sForm" class="sub-form" style="margin-top: 20px;" v-if="model.Nav_Details">
				<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>

				<uni-collapse :border="false" accordion>
					<view v-for="(item, index) in model.Nav_Details.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.DEVICE_TYPEShow}}
									</view>
									<view @click.stop style="margin-right: 10px;">

										<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 class="subRequired" required label="类别" prop="DEVICE_TYPE" borderBottom @click="handleChange(item,'type')">
									<u--input disabled disabledColor="#fff" fontSize="14px" placeholder="请选择类别" v-model="item.DEVICE_TYPEShow" border="none" inputAlign="right"></u--input>
									<u-icon style="margin-left: 4px;" name="arrow-down" size="12" ></u-icon>
								</u-form-item>
								<u-form-item class="subRequired" required label="设备名称" prop="Nav_Device.NAME" borderBottom @click="handleShowSheetForArea(item,{title: '设备名称', name: 'Nav_Device'})">
									<u--input disabled fontSize="14px" placeholder="请选择设备名称" disabledColor="#fff" v-model="item.Nav_Device.NAME" border="none" inputAlign="right" v-if="item.Nav_Device"></u--input>
									<u-icon style="margin-left: 4px;" name="arrow-down" size="12" ></u-icon>
								</u-form-item>
								<u-form-item class="subRequired" required label="系统分类" prop="Nav_Category.NAME" borderBottom @click="handleShowSheetForArea(item,{title: '系统分类', name: 'Nav_Category'})"> <u--input disabled
										fontSize="14px" placeholder="请选择系统分类" disabledColor="#fff" v-model="item.Nav_Category.NAME" border="none" inputAlign="right" v-if="item.Nav_Category"></u--input><u-icon
										style="margin-left: 4px;" name="arrow-down" size="12" ></u-icon></u-form-item>
								<u-form-item class="subRequired" required label="类型" prop="Nav_Type.NAME" borderBottom @click="handleShowSheetForArea(item,{title: '类型', name: 'Nav_Type'})"> <u--input disabled
										fontSize="14px" placeholder="请选择类型" disabledColor="#fff" v-model="item.Nav_Type.NAME" border="none" inputAlign="right" v-if="item.Nav_Type"></u--input><u-icon
										style="margin-left: 4px;" name="arrow-down" size="12" ></u-icon></u-form-item>
								<u-form-item label="是否为应急物资" prop="IS_EMERGENCY" borderBottom @click="handleChange(item,'IS_EMERGENCY')"> <u--input disabled disabledColor="#ffffff" v-model="item.IS_EMERGENCYShow"
										border="none" inputAlign="right" fontSize="14px" placeholder="请输入是否为应急物资"></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="#ffffff" v-model="item.REMARK" border="none" inputAlign="right" fontSize="14px"
										placeholder="请输入需求描述"></u--input> </u-form-item>
								<u-form-item label="数量" prop="QTY" borderBottom> <u--input type="number" disabledColor="#ffffff" v-model="item.QTY" border="none" inputAlign="right" placeholder="请输入数量"
										fontSize="14px"></u--input>
								</u-form-item>
								<u-form-item label="单位" prop="UNIT" borderBottom> <u--input disabledColor="#ffffff" v-model="item.UNIT" border="none" inputAlign="right" placeholder="请输入单位" fontSize="14px"></u--input>
								</u-form-item>
								<view class="upload-title">设备设施资料上传</view><full-upload v-model="item.Nav_Files" :isShowBtn="true" :listProp="listPropUpload" :listPropVal="listPropValUpload"></full-upload>

							</view>


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

			<!-- </uni-card> -->
		</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-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 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 {
		DMDeviceBasePurchaseGet,
		DMDeviceBasePurchaseFull,
		DMDeviceGet,
		DMCategoryGet,
		DMTypeGet
	} from '../../../../services/apply/subPages/DM/dm';

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

	import config from '../../../../config/common'
	export default {
		data() {
			return {
				tableKey: '0',
				TaskID: '',
				ID: '',
				ORG_ID: uni.getStorageSync('orgId'),
				isLoadOK: true,
				stepsPageList: 0,
				listISORNOT: [], //是否涉及
				listISORNOTText: [],
				listPropUpload: ['PARENT_ID'],
				listPropValUpload: [],
				indexShow: 0,
				
				model: {
					Nav_Department: {
						NAME: ''
					},
					Nav_User: {
						NAME: ""
					},
					ID: '',
					ORG_ID: '',
					TaskID: null,
					STATUS: 0,
					Nav_Details: []
				},
				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.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': "DMDeviceTypeEnum"
				}
				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_Details.filter(i => !i.IS_DELETED);
				const index = validDetails.indexOf(item);
				const realIndex = this.model.Nav_Details.findIndex(item => item === validDetails[index]);
				this.model.Nav_Details[realIndex].ISOCONFIRM = true;
			},
			handleDelRowBefore(item) {
				item.IS_DELETED = true
				let indexNotDel = 1
				this.model.Nav_Details.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.ID == undefined || this.ID == '') {
					this.isLoadOK = false
					this.model.Nav_Details = []
					this.model.ID = guid()
					this.model.Nav_Department.NAME = uni.getStorageSync('appInfo').User.Nav_Department.NAME
					this.model.Nav_User.NAME = uni.getStorageSync('appInfo').User.NAME
				} else {
					let json = initFilter(this.ORG_ID);
					extendRule(json, 'ID', 1, this.ID);
					extendInclude(json, 'Nav_Department');
					extendInclude(json, 'Nav_User');
					extendInclude(json, 'Nav_Details');
					extendInclude(json, 'Nav_Details.Nav_Device');
					extendInclude(json, 'Nav_Details.Nav_Category');
					extendInclude(json, 'Nav_Details.Nav_Type');

					DMDeviceBasePurchaseGet(json).then(ret => {
						this.isLoadOK = false
						this.model = ret
						this.model.ID = this.ID
						this.initializeMissingFields();
						if (this.model.Nav_Details && this.model.Nav_Details.length > 0) {
							this.model.Nav_Details.map(item => {
								item.IS_EMERGENCYShow = item.IS_EMERGENCY ? '是' : '否'
								item.DEVICE_TYPEShow = this.listISORNOT.find(items => items.code === item.DEVICE_TYPE).name;
							})
						}

						// this.model.Nav_Details = this.model.Nav_Details.sort((a, b) => a.ROW_NUM - b.ROW_NUM);

						this.listPropValUpload.push(this.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: ''
							};
						}

					}
				});
			},

			handleShowSheetForArea(item, todo) {
				const validDetails = this.model.Nav_Details.filter(i => !i.IS_DELETED);
				const index = validDetails.indexOf(item);
				this.handleShowSheet({
					title: todo.title,
					name: todo.name,
					index: index
				});
			},
			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
				let jsonName = ''
				if (p.name == 'Nav_Device') {
					jsonName = DMDeviceGet
				} else if (p.name == 'Nav_Category') {
					jsonName = DMCategoryGet
				} else if (p.name == 'Nav_Type') {
					jsonName = DMTypeGet
				}
				dataSelect = await jsonName(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
						}
					}
					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,
						index: p.index
					}
				} else {
					// 暂无数据
				}
			},
			//数据加载
			handleSearch(val, pageIndex) {
				var p = {
					name: this.selectorInfo.name,
					title: this.selectorInfo.title,
					val: val,
					pageIndex: pageIndex,
					index: this.selectorInfo.index
				}
				this.handleShowSheet(p)
			},

			handleSelected(e) {
				this.selectorInfo.showPopup = false
				const validDetails = this.model.Nav_Details.filter(i => !i.IS_DELETED);
				const index = this.selectorInfo.index;
				const realIndex = this.model.Nav_Details.findIndex(item => item === validDetails[index]);
				if (this.selectorInfo.name == 'Nav_Device') {
					this.model.Nav_Details[realIndex].Nav_Device = e;
					this.model.Nav_Details[realIndex].DEVICE_ID = e.ID;
				} else if (this.selectorInfo.name == 'Nav_Category') {
					this.model.Nav_Details[realIndex].Nav_Category = e;
					this.model.Nav_Details[realIndex].CATEGORY_ID = e.ID;
				} else if (this.selectorInfo.name == 'Nav_Type') {
					this.model.Nav_Details[realIndex].Nav_Type = e;
					this.model.Nav_Details[realIndex].TYPE_ID = e.ID;
				}



			},
			submit() {
				this.modelEdit = JSON.parse(JSON.stringify(this.model))

				this.modelEdit.Nav_Details.map(item => {
					item.PUBLISH = 'SaveAndNotify'
					item.QTY = Number(item.QTY)
					delete item.Nav_Device
					delete item.Nav_Type
					delete item.Nav_Category
					if (item.DEVICE_TYPE == '') {
						delete item.DEVICE_TYPE
					}
					if (item.QTY == 0) {
						delete item.QTY
					}
				})
				delete this.modelEdit.Nav_Department
				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

				this.modelEdit.PUBLISH = 'SaveAndNotify'
				DMDeviceBasePurchaseFull(this.modelEdit).then(res => {
					uni.$showMsgFunc('操作成功!', () => {
						uni.navigateBack()
					}, 'success', 1000)
				})

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

				var ROW_NO = 1;
				if (this.model.Nav_Details.length > 0) {
					this.model.Nav_Details.forEach(e => {
						if (e.IS_DELETED == undefined || e.IS_DELETED == false)
							ROW_NO++
					})
				}
				var modelAdd = {
					ID: guid(),
					ORG_ID: this.ORG_ID,
					DEVICE_PURCHASE_ID: this.model.ID,
					ROW_NUM: ROW_NO,
					DEVICE_TYPE: 0,
					DEVICE_TYPEShow: this.listISORNOTText[0],
					Nav_Device: {
						NAME: ''
					},
					Nav_Category: {
						NAME: ''
					},
					Nav_Type: {
						NAME: ''
					},
					IS_EMERGENCYShow: '否',
					IS_EMERGENCY: false,
					REMARK: '',
					QTY: '',
					UNIT: '',
					Nav_Files: [],
					IS_DELETED: false,
				}
				this.model.Nav_Details.push(modelAdd)
				this.indexShow = ROW_NO
			},

			handleChange(item, name) {
				let columns = []
				if (name == 'type') {
					columns = this.listISORNOTText
				} else if (name == 'IS_EMERGENCY') {
					columns = ['是', '否']
				}
				this.comPickerInfo = {
					showSheet: true,
					columns: [columns],
					dataItem: item,
					name: name
				}
			},
			onConfirmPicker(e) {
				this.comPickerInfo = {
					showSheet: false,
					dataItem: this.comPickerInfo.dataItem,
					columns: [],
					title: '',
					name: this.comPickerInfo.name,
					formIndex: undefined
				}
				if (this.comPickerInfo.name == 'type') {
					this.comPickerInfo.dataItem.DEVICE_TYPEShow = e.value[0]
					this.comPickerInfo.dataItem.DEVICE_TYPE = this.listISORNOT[e.indexs[0]].code
				} else if (this.comPickerInfo.name == 'IS_EMERGENCY') {
					this.comPickerInfo.dataItem.IS_EMERGENCYShow = e.value[0]
					this.comPickerInfo.dataItem.IS_EMERGENCY = e.value[0] == '是' ? true : false
				}

			}
		}
	}
</script>

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

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