<!-- 应急物资  -->
<template>
	<view class="page-wrap">

		<!-- 物资操作 -->
		<view class="card">
			<uni-card margin="0" :is-shadow="true">
				<u--form labelPosition="left" labelWidth="auto" labelAlign="center" :model="model" ref="wForm"
					:rules="rules">
					<u-form-item label="编号" prop="CODE" borderBottom>
						<u--input disabled v-model="model.CODE" disabledColor="#ffffff" border="none" slot="right"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item required label="物资名称" prop="NAME" borderBottom>
						<u--input disabledColor="#fff" v-model="model.NAME" border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item required label="资源类型" prop="Nav_MaterialType.NAME" borderBottom
						@click="handleShowSheet({title: '选择资源类型', name: 'TypeNAME',itemData:model})">
						<u--input disabledColor="#fff" v-model="model.Nav_MaterialType.NAME" border="none"
							inputAlign="right"></u--input>
						<u-icon style="margin-left: 4px;" slot="right" name="arrow-down"></u-icon>
					</u-form-item>
					<u-form-item label="内部编号" prop="INTERNAL_NO" borderBottom>
						<u--input disabled disabledColor="#fff" v-model="model.INTERNAL_NO" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="规格/型号" prop="SPECIFICATION" borderBottom>
						<u--input disabledColor="#fff" v-model="model.SPECIFICATION" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item required label="数量" prop="COUNT" borderBottom>
						<u--input disabledColor="#fff" type='number' v-model="model.COUNT" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item required label="单位" prop="UNIT" borderBottom>
						<u--input disabledColor="#fff" v-model="model.UNIT" border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item required label="存放位置" prop="LOCATION" borderBottom>
						<u--input disabledColor="#fff" v-model="model.LOCATION" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="用途" prop="PURPOSE" borderBottom>
						<u--input disabledColor="#fff" v-model="model.PURPOSE" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item required label="管理部门" prop="Nav_Department.NAME" borderBottom
						@click="handleShowSheet({title: '选择组织', name: 'DepartmentNAME',itemData:model})">
						<u--input disabledColor="#fff" v-model="model.Nav_Department.NAME" border="none"
							inputAlign="right"></u--input>
						<u-icon style="margin-left: 4px;" slot="right" name="arrow-down"></u-icon>
					</u-form-item>
					<u-form-item required label="责任人" prop="Nav_User.NAME" borderBottom
						@click="handleShowSheet({title: '选择人员', name: 'UserNAME',itemData:model})">
						<u--input disabledColor="#fff" v-model="model.Nav_User.NAME" border="none"
							inputAlign="right"></u--input>
						<u-icon style="margin-left: 4px;" slot="right" name="arrow-down"></u-icon>
					</u-form-item>
					<u-form-item label="主要技术参数" prop="MAIN_TEC_PARAMS" borderBottom>
						<u--input disabledColor="#fff" v-model="model.MAIN_TEC_PARAMS" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
				</u--form>

			</uni-card>
		</view>

		<!-- 出入库操作 -->
		<view class="card" v-if="ISOPERATE">
			<uni-card margin="0" :is-shadow="true">
				<u--form labelPosition="left" labelWidth="auto" labelAlign="center" :model="modelOperate" ref="sForm"
					:rules="rulesOperate">
					<u-form-item label="编号" prop="CODE" borderBottom>
						<u--input disabled v-model="modelOperate.CODE" disabledColor="#ffffff" border="none"
							slot="right" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item required label="操作类型" prop="OPERATETYPE" borderBottom
						@click="handleShowSheet({title: '选择操作类型', name: 'CMOperationOutReturn',itemData:modelOperate})">
						<u--input disabledColor="#fff" v-model="modelOperate.OPERATETYPE_SHOW" border="none"
							inputAlign="right"></u--input>
						<u-icon style="margin-left: 4px;" slot="right" name="arrow-down"></u-icon>
					</u-form-item>
					<u-form-item required label="需要归还" v-if="modelOperate.OPERATETYPE==10" prop="ISEND" borderBottom>
						<u-radio-group v-model="modelOperate.ISEND" inputAlign="right" placement="row">
							<u-radio style="margin-left: 20px;" v-for="item in radioItems" :name="item.value"
								:key="item.value" :label="item.text">
							</u-radio>
						</u-radio-group>
					</u-form-item>
					<u-form-item required label="操作数量" prop="COUNT_OPERATE" borderBottom>
						<u--input disabledColor="#fff" type='number' v-model="modelOperate.COUNT_OPERATE" border="none"
							inputAlign="right"></u--input>
					</u-form-item>

					<u-form-item label="备注" prop="REMARK" borderBottom>
						<u--input disabledColor="#fff" v-model="modelOperate.REMARK" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
				</u--form>
			</uni-card>
		</view>

		<query-selector :show="selectorInfo.showPopup" :defaultChecked="selectorInfo.defaultChecked"
			:title="selectorInfo.title" :lists="selectorInfo.dataLists" @search="handleSearch" @select="handleSelected"
			@close="selectorInfo.showPopup=false" :total="selectorInfo.totalCount" />

		<view class="bottom-button">

			<!-- 物资操作 -->
			<button type="primary"
				style="background-color: #f7f7f7;color: black;width: 45%;float: left; margin-right: 5%;"
				v-if='isLoadOK&&!ISOPERATE' @click="FullCheckUpdate">物资修改</button>
			<button type="primary" style="width: 45%; float: left;margin-left: 5%;" v-if='isLoadOK&&!ISOPERATE'
				@click="ISOPERATE=true">出入库</button>

			<!-- 出入库操作 -->
			<button type="primary"
				style="background-color: #f7f7f7;color: black;width: 45%;float: left; margin-right: 5%;"
				v-if='ISOPERATE' @click="ISOPERATE=false">返回</button>
			<button type="primary" style="width: 45%; float: left;margin-left: 5%;" v-if='ISOPERATE'
				@click="FullUpdatePer">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		initFilter,
		extendRule,
		extendInclude,
		fileStructChange
	} from '../../../../utils/common'

	import {
		GetLibrary,
		OrderPagedType,
		FullUpdateMaterialInfo,
		FullUpdatePer
	} from '../../../../services/apply/subPages/SCCM/emergency'

	import {
		UserPagedLimit,
		getEnum
	} from '../../../../services/common'
	import {
		getDepartmentLists,
	} from '../../../../services/app'

	export default {
		data() {
			return {
				tableKey: '0',
				TaskID: '',
				enums: [{
					ID: '',
					NAME: ''
				}],
				ISOPERATE: false,
				modelEdit: {},
				model: {
					ID: '',
					NAME: '',
					TaskID: '',
					DEPARMTNET_ID: '',
					USER_ID: '',
					MATERIAL_TYPE_ID: '',
					Nav_User: {
						NAME: ''
					},
					Nav_MaterialType: {
						NAME: ''
					},
					Nav_Department: {
						NAME: ''
					},
				},
				rules: {
					'NAME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '物资名称不能为空'
					},
					'MATERIAL_TYPE_ID': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '资源类型不能为空'
					},
					'COUNT': {
						type: 'number',
						required: true,
						trigger: ['blur', 'change'],
						message: '数量不能为空'
					},
					'UNIT': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '单位不能为空'
					},
					'LOCATION': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '存放位置不能为空'
					},
					'DEPARMTNET_ID': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '管理部门不能为空'
					},
					'USER_ID': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '责任人不能为空'
					}
				},

				// 出入库操作
				modelOperate: {
					CODE: 'YJ' + (new Date()).Format('yyyyMMddHHmmss'),
					OPERATETYPE: '',
					ORG_ID: '',
					OPERATETYPE_SHOW: '',
					COUNT_OPERATE: '',
					ISEND: true,
					REMARK: '',
					MATERIAL_ID: ''
				},
				rulesOperate: {
					'CODE': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '编号不能为空'
					},
					'OPERATETYPE': {
						type: 'number',
						required: true,
						trigger: ['blur', 'change'],
						message: '请选择操作类型'
					},
					'COUNT_OPERATE': {
						type: 'number',
						required: true,
						trigger: ['blur', 'change'],
						message: '操作数量不能为空'
					}
				},
				radioItems: [{
						text: '需要',
						value: true
					},
					{
						text: '不需要',
						value: false
					}
				],

				ApproveID: '',
				isAudit: false,
				okApi: '',
				refuseApi: '',
				isLoadOK: false,
				isShowUpload: false,
				selectorInfo: {
					isSubShow: false,
					// isMultiple: false,
					showPopup: false,
					totalCount: 0,
					title: '人员选择',
					itemData: {},
					index: 0,
					dataLists: [],
					defaultText: '',
					defaultValue: '',
					name: '',
					defaultChecked: []
				},
			}
		},
		onLoad(option) {
			this.model.ID = option.ID
			// this.enums = JSON.parse(option.enums)
			this.model.TaskID = option.taskID ? option.taskID : ''
			this.TaskID = this.model.TaskID
			this.tableKey = option.tableKey ? option.tableKey : '0'
			this.ApproveID = option.ApproveID
			this.isAudit = option.isAudit
			this.okApi = option.okApi
			this.refuseApi = option.refuseApi
			this.auditModel = {
				...this.$store.state.auditModel,
				TaskID: this.TaskID
			}
			if (this.isAudit || this.tableKey == 0) {
				this.isShowUpload = false
			}
			this.loadData()
		},
		methods: {
			loadData() {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "CODE", 1)
				extendRule(json, 'ID', 1, this.model.ID);
				extendInclude(json, "Nav_MaterialType");
				extendInclude(json, "Nav_Department");
				extendInclude(json, "Nav_User");
				json.IgnoreDataRule = true
				GetLibrary(json).then(res => {
					if (res.IsSuccessful) {
						this.model = res.Data
						this.modelOperate.MATERIAL_ID = this.model.ID
						this.modelOperate.ORG_ID = this.model.ORG_ID
						this.isLoadOK = true
					}
				})
			},
			FullCheckUpdate() {
				const ele = this.$refs
				ele['wForm'].validate().then(res => {
					this.modelEdit = JSON.parse(JSON.stringify(this.model))
					this.modelEdit.Nav_User = null
					this.modelEdit.Nav_MaterialType = null
					this.modelEdit.Nav_Department = null
					if (this.TaskID && this.TaskID.length > 0)
						this.modelEdit.TaskID = this.TaskID
					FullUpdateMaterialInfo(this.modelEdit).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					})
				}).catch(err => {
					if (err.length > 0) {
						uni.$showErrorInfo(err[0].message)
					} else {
						uni.$showErrorInfo('校验失败！')
					}
				})
			},
			FullUpdatePer() {
				const ele = this.$refs
				ele['sForm'].validate().then(res => {
					const orgId = uni.getStorageSync('orgId')
					this.modelEdit = JSON.parse(JSON.stringify(this.modelOperate))
					if (this.TaskID && this.TaskID.length > 0)
						this.modelEdit.TaskID = this.TaskID
					FullUpdatePer(this.modelEdit).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					})
				}).catch(err => {
					if (err.length > 0) {
						uni.$showErrorInfo(err[0].message)
					} else {
						uni.$showErrorInfo('校验失败！')
					}
				})
			},
			onRadioChange(value) {
				this.modelOperate.ISEND = value
			},
			changeCount(record) {
				if (record.COUNT_END < 0)
					uni.$showErrorInfo('盘点数量不能小于0！')
				if (record.OPERATETYPE == 40) {
					//虽然是 【盘点】 如果数量修改  同时切换 操作类型为 【库存修改 50】
					record.COUNT_OPERATE = record.COUNT_END
					if (record.COUNT_OPERATE != record.COUNT_START) {
						record.OPERATETYPE = 50
					} else {
						record.OPERATETYPE = 40
					}
				} else {
					record.COUNT_OPERATE = record.COUNT_END
				}
			},
			//下拉数据加载
			async handleShowSheet(p) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", 0, p.pageIndex ?? 1)
				let dataSelect = []
				var defaultChecked = []
				if (p.name == 'UserNAME') {
					if (p.val) {
						extendRule(json, 'NAME', 9, p.val);
					}
					json.Limit = 20
					json.Sort = 'NAME'
					if (p.pageIndex) {
						json.Start = (p.pageIndex - 1) * json.Limit;
					}
					dataSelect = await UserPagedLimit(json).then(res => {
						this.selectorInfo.totalCount = res.TotalCount
						if (res.Data != 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].CODE
							}
						}
						return res.Data
					})
					if (this.model.Nav_User != null && this.model.Nav_User.NAME != '') {
						defaultChecked.push(this.model.Nav_User.NAME)
					}
				} else if (p.name == 'DepartmentNAME') {
					if (p.val) {
						extendRule(json, 'NAME', 9, p.val);
					}
					json.Limit = 20
					json.Sort = 'NAME'
					if (p.pageIndex) {
						json.Start = (p.pageIndex - 1) * json.Limit;
					}
					dataSelect = await getDepartmentLists(json).then(res => {
						this.selectorInfo.totalCount = res.TotalCount
						if (res.Data != 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].CODE
							}
						}
						return res.Data
					})
					if (this.model.Nav_Department != null && this.model.Nav_Department.NAME != '') {
						defaultChecked.push(this.model.Nav_Department.NAME)
					}
				} else if (p.name == 'TypeNAME') {
					if (p.val) {
						extendRule(json, 'NAME', 9, p.val);
					}
					json.Limit = 20
					json.Sort = 'NAME'
					if (p.pageIndex) {
						json.Start = (p.pageIndex - 1) * json.Limit;
					}
					dataSelect = await OrderPagedType(json).then(res => {
						this.selectorInfo.totalCount = res.TotalCount
						if (res.Data != 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].CODE
							}
						}
						return res.Data
					})
					if (this.model.Nav_MaterialType != null && this.model.Nav_MaterialType.NAME != '') {
						defaultChecked.push(this.model.Nav_MaterialType.NAME)
					}
				} else if (p.name == 'CMOperationOutReturn') {
					if (!this.enums || this.enums.length < 2) {
						dataSelect = await getEnum({
							name: 'CMOperationOutReturn'
						}).then(res => {
							this.selectorInfo.totalCount = res.length
							const enumsNew = res.map(i => {
								return {
									...i,
									name: i.NAME,
									// code: i.ID
								}
							})
							return enumsNew
						})
					}
				}

				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: false,
						totalCount: this.selectorInfo.totalCount,
						showPopup: true,
						title: p.title,
						name: p.name,
						dataLists: dataSelect,
						defaultChecked: defaultChecked
					}
				} else {
					// 暂无数据
				}
			},
			handleSelected(e) {
				this.selectorInfo.showPopup = false
				if (this.selectorInfo.name == "TypeNAME") {
					this.model.Nav_MaterialType.NAME = e.NAME
					this.model.MATERIAL_TYPE_ID = e.ID
				} else if (this.selectorInfo.name == 'DepartmentNAME') {
					this.model.DEPARMTNET_ID = e.ID
					this.model.Nav_Department.NAME = e.NAME
				} else if (this.selectorInfo.name == 'UserNAME') {
					this.model.USER_ID = e.ID
					this.model.Nav_User.NAME = e.NAME
				} else if (this.selectorInfo.name == 'CMOperationOutReturn') {
					this.modelOperate.OPERATETYPE = e.ID
					this.modelOperate.OPERATETYPE_SHOW = e.NAME
				}
			},
			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
			},
			//数据加载
			handleSearch(val, pageIndex) {
				var p = {
					name: this.selectorInfo.name,
					title: this.selectorInfo.title,
					val: val,
					pageIndex: pageIndex
				}
				this.handleShowSheet(p)
			},
		},
		computed: {

		}
	}
</script>
<style>
	@import url("../../../../style/css/editTemplate.css");
</style>