<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 disabledColor="#ffffff" v-model="model.Nav_Department.NAME" border="none" inputAlign="right" fontSize="14px" v-if="model.Nav_Department"></u--input>
				</u-form-item>
				<u-form-item label="申请人员" prop="USER_ID" borderBottom>
					<u--input disabled disabledColor="#ffffff" v-model="model.Nav_User.NAME" border="none" inputAlign="right" fontSize="14px" v-if="model.Nav_User"></u--input>
				</u-form-item>

				<u-form-item class="subRequired" label="设备设施名称" prop="Nav_Device.NAME" borderBottom @click="handleShowSheet({title: '设备名称', name: 'Nav_DeviceBase'})">
					<u--input disabled disabledColor="#ffffff" fontSize="14px" placeholder="请选择设备名称" v-model="model.Nav_DeviceBase.NAME" border="none" inputAlign="right" v-if="model.Nav_DeviceBase"></u--input>
					<u-icon style="margin-left: 4px;" name="arrow-down" size="12" slot="right"></u-icon>
				</u-form-item>
				<u-form-item label="设备设施编号" prop="POSITION" borderBottom>
					<u--input disabled disabledColor="#ffffff" 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 disabledColor="#ffffff" 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 disabledColor="#ffffff" v-model="model.POSITION" border="none" inputAlign="right" fontSize="14px">
					</u--input>
				</u-form-item>
				<u-form-item required label="报废原因" prop="REASON" borderBottom>
					<u--input placeholder="请填写报废原因" v-model="model.REASON" border="none" inputAlign="right" fontSize="14px">
					</u--input>
				</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 {
		DMDeviceScrapGet,
		DMDeviceScrapFull,
		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_User: {
						NAME: ""
					},
					Nav_DeviceBase: {
						NAME: ''
					},
					ID: '',
					ORG_ID: '',
					TaskID: null,
					// STATUS: 0,
				},
				rules: {
					'REASON': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请填写【报废原因】'
					},
				},
				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: {
			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_Department');
					extendInclude(json, 'Nav_User');
					extendInclude(json, 'Nav_DeviceBase');

					DMDeviceScrapGet(json).then(ret => {
						this.isLoadOK = false
						this.isNew = false
						this.model = ret
						this.model.ID = this.ID
						this.model.SPEC = this.model.Nav_DeviceBase.SPEC
						// this.model.DEVICEBASE_ID = this.model.Nav_DeviceBase.DEVICEBASE_ID
						this.model.MACHINE_CODE = this.model.Nav_DeviceBase.MACHINE_CODE
						this.model.POSITION = this.model.Nav_DeviceBase.POSITION
						this.listPropValUpload.push(this.ID)
						// 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
				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
				})

				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
				this.model.Nav_DeviceBase = e;
				this.model.DEVICEBASE_ID = e.ID;
				this.model.MACHINE_CODE = e.MACHINE_CODE;
				this.model.SPEC = e.SPEC;
				this.model.POSITION = e.POSITION;



			},
			submit() {
				const ele = this.$refs
				ele['wForm'].validate().then(res => {
					this.modelEdit = JSON.parse(JSON.stringify(this.model))
					delete this.modelEdit.Nav_Department
					delete this.modelEdit.Nav_DeviceBase
					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'
					DMDeviceScrapFull(this.modelEdit).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					})
					
				}).catch(err => {
					uni.$showErrorInfo(err[0].message + '！')
				
				})
				
			},
			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>