<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 v-if="!isNew&&model.STATUS==0" label="检修时间" prop="OVERHAUL_TIME" borderBottom @click="showCheckDate(1)">
					<u--input disabled disabledColor="#fff" v-model="model.OVERHAUL_TIME" 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 v-else label="检修时间" prop="ENDTIME" borderBottom>
					<u--input disabled v-model="model.OVERHAUL_TIME" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>
				<u-form-item label="报修时间" prop="ENDTIME" borderBottom>
					<u--input disabled v-if="model.Nav_Repair" v-model="model.Nav_Repair.CREATE_TIME" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>
				<u-form-item label="报修人" prop="ENDTIME" borderBottom>
					<u--input disabled v-if="model.Nav_Repair&&model.Nav_Repair.Nav_User" v-model="model.Nav_Repair.Nav_User.NAME" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>
				<u-form-item v-if="!isNew&&model.IS_HELP" label="外部援助单位" prop="ENDTIME" borderBottom>
					<u--input :disabled="!(!isNew&&model.STATUS==0)" :placeholder="!isNew&&model.STATUS==0?'请填写外部援助单位':''" v-model="model.EXTERNAL_SUPPORT" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>
				<u-form-item v-if="!isNew&&model.IS_HELP" label="联系人" prop="ENDTIME" borderBottom>
					<u--input :disabled="!(!isNew&&model.STATUS==0)" :placeholder="!isNew&&model.STATUS==0?'请填写联系人':''" v-model="model.PERSON_NAME" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>
				<u-form-item v-if="!isNew&&model.IS_HELP" label="联系方式" prop="ENDTIME" borderBottom>
					<u--input :disabled="!(!isNew&&model.STATUS==0)" :placeholder="!isNew&&model.STATUS==0?'请填写联系方式':''" v-model="model.PHONE" border="none" inputAlign="right" fontSize="14px"></u--input>
				</u-form-item>
				<u-form-item class="subRequired" label="设备设施名称" prop="Nav_Device.NAME" borderBottom>
					<u--input disabled fontSize="14px" v-model="model.Nav_Device.NAME" border="none" inputAlign="right" v-if="model.Nav_Device"></u--input>

				</u-form-item>
				<u-form-item label="设备设施编号" prop="POSITION" borderBottom>
					<u--input disabled v-model="model.MACHINE_CODE" border="none" inputAlign="right" fontSize="14px">
					</u--input>
				</u-form-item>
				<u-form-item label="规格/型号" prop="POSITION" borderBottom>
					<u--input disabled v-model="model.SPEC" border="none" inputAlign="right" fontSize="14px">
					</u--input>
				</u-form-item>
				<u-form-item label="设备设施位置" prop="POSITION" borderBottom>
					<u--input disabled v-model="model.POSITION" border="none" inputAlign="right" fontSize="14px">
					</u--input>
				</u-form-item>
				<u-form-item label="故障描述" prop="POSITION" borderBottom>
					<u--input disabled v-model="model.MALFUCTION" border="none" inputAlign="right" fontSize="14px">
					</u--input>
				</u-form-item>
				<u-form-item v-if="!isNew&&model.STATUS==0" label="检修意见" prop="Product_Unit" @click="handleChange('DMRepairOpinionEnum')" borderBottom>
					<u--input v-model="model.OPINIONShow" 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-item v-else label="检修意见" prop="Product_Unit" borderBottom>
					<u--input v-model="model.OPINIONShow" disabled border="none" inputAlign="right" fontSize="14px">
					</u--input>
				</u-form-item>
				<u-form-item label="检修情况描述" prop="POSITION" borderBottom>
					<u--input :disabled="!(!isNew&&model.STATUS==0)" :placeholder="!isNew&&model.STATUS==0?'请填写检修情况描述':''" disabledColor="#ffffff" v-model="model.DESCRIPTION" border="none" inputAlign="right"
						fontSize="14px">
					</u--input>
				</u-form-item>
				<u-form-item label="故障照片" prop="POSITION">
				</u-form-item>
				<full-upload v-model="model.Nav_RepairFiles" :isShowBtn="false" :listProp="listPropUpload" :listPropVal="listPropValUpload"></full-upload>


				<u-form-item label="检修后附件" prop="POSITION">
				</u-form-item>
				<full-upload v-model="model.Nav_Files" :isShowBtn="!isNew&&model.STATUS==0" :listProp="listPropUpload" :listPropVal="listPropValUpload"></full-upload>
				<u-form-item v-if="!isNew&&model.STATUS>0" label="验收意见" @click="handleChange('CHECK_OPINION')" borderBottom>
					<u--input v-model="model.CHECK_OPINIONShow" 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>
		<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 {
		DMDeviceOverhaulGet,
		DMDeviceOverhaulFull,
		DMDeviceBase,
		DMDepartment,
		DMOrderPagedAuthority
	} 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: [],
				listResult: [],
				listResultText: [],
				listPropUpload: ['PARENT_ID'],
				listPropValUpload: [],
				indexShow: 0,
				isNew: true,
				model: {
					Nav_Department: {
						NAME: ''
					},
					Nav_Repair_User: {
						NAME: ""
					},
					Nav_Device: {
						NAME: ''
					},
					ID: '',
					IS_HELPShow: '否',
					IS_HELP: false,
					ORG_ID: '',
					TaskID: null,
					STATUS: 0,
				},
				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': "DMRepairOpinionEnum"
				}
				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.getConclusion()
				})
			},
			getConclusion() {
				var dataParm = {
					'name': "CheckOptionEnum"
				}
				getEnum(dataParm).then(res => {
					this.listResult = res
					this.listResultText = []
					this.listResult.forEach(e => {
						e.name = e.NAME
						e.code = e.ID
						this.listResultText.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.model.ID = guid()
					this.isNew = true
					// this.model.Nav_Department.NAME = uni.getStorageSync('appInfo').User.Nav_Department.NAME
					// this.model.Nav_User.NAME = uni.getStorageSync('appInfo').User.NAME
					// let mint = Number(uni.getStorageSync('appInfo').User.MineType)
					// this.model.Product_Unit = this.listISORNOT.find(item => item.code === mint).name
					// this.model.MineType = this.listISORNOT.find(item => item.code === mint).code
					this.isLoadOK = false
				} else {
					let json = initFilter(this.ORG_ID);
					extendRule(json, 'ID', 1, this.ID);
					extendInclude(json, 'Nav_User');
					extendInclude(json, 'Nav_Repair');
					extendInclude(json, 'Nav_Repair.Nav_User');
					extendInclude(json, 'Nav_Device');
					extendInclude(json, 'Nav_Files.Nav_ImgFile');

					DMDeviceOverhaulGet(json).then(ret => {
						this.isLoadOK = false
						this.isNew = false
						this.model = ret
						this.model.ID = this.ID
						this.listPropValUpload.push(this.ID)
						this.model.OPINIONShow = this.listISORNOT.find(item => item.code === this.model.OPINION).name
						this.model.CHECK_OPINIONShow = this.listResult.find(item => item.code === this.model.CHECK_OPINION).name
						// uni.$updateData()

					});
				}
				this.listPropValUpload.push(this.model.ID)
			},
			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_Device') {

					extendRule(json, 'DEVICE_STATUS', 1, '1');
					dataSelect = await DMDeviceBase(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
					})

				} else if (p.name == 'Nav_Department') {
					dataSelect = await DMDepartment(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
					})
				} else if (p.name == 'Nav_Repair_User') {
					extendRule(json, 'ENABLE_STATUS', 1, '0')
					dataSelect = await DMOrderPagedAuthority(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,
					}
				} 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_Device') {
					this.model.Nav_Device = e;
					this.model.DEVICE_BASE_ID = e.ID;
					this.model.MACHINE_CODE = e.MACHINE_CODE;
					this.model.SPEC = e.SPEC;
					this.model.POSITION = e.POSITION;
				} else if (this.selectorInfo.name == 'Nav_Department') {
					this.model.Nav_Department = e;
					this.model.DEPARTMENT_ID = e.ID;
				} else if (this.selectorInfo.name == 'Nav_Repair_User') {
					this.model.Nav_Repair_User = e;
					this.model.REPAIR_USER_ID = e.ID;
				}



			},
			submit() {
				this.modelEdit = JSON.parse(JSON.stringify(this.model))
				delete this.modelEdit.Nav_User
				delete this.modelEdit.Nav_Repair
				delete this.modelEdit.Nav_Device

				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
				if (this.modelEdit.Nav_Files && this.modelEdit.Nav_Files.length > 0) {
					this.modelEdit.Nav_Files.forEach(item => {
						if (item.OVERHAUL_ID == undefined) {
							item.OVERHAUL_ID = this.model.ID;
						}
					});
				}
				this.modelEdit.PUBLISH = 'SaveAndNotify'
				// console.log(this.modelEdit, 'this.modelEdit')
				DMDeviceOverhaulFull(this.modelEdit).then(res => {
					uni.$showMsgFunc('操作成功!', () => {
						uni.navigateBack()
					}, 'success', 1000)
				})

			},
			handleChange(name) {

				this.comPickerInfo = {
					showSheet: true,
					columns: [name == 'DMRepairOpinionEnum' ? this.listISORNOTText : this.listResultText],
					name: name
				}


			},
			onConfirmPicker(e) {
				if (this.comPickerInfo.name == 'DMRepairOpinionEnum') {
					this.model.OPINIONShow = e.value[0]
					this.model.OPINION = this.listISORNOT[e.indexs[0]].code
				} else if (this.comPickerInfo.name == 'CHECK_OPINION') {
					this.model.CHECK_OPINION = this.listResult[e.indexs[0]].code
					this.model.CHECK_OPINIONShow = e.value[0]
				}
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					formIndex: undefined
				}
			}
		}
	}
</script>

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

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