<!-- 物资操作单 查看 CM043_SHOWPRINT -->
<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 v-if="model.USER_ID_OPERATE!=null&& model.USER_ID_OPERATE!=model.USER_ID" label="委托人"
						prop="Nav_User.NAME" borderBottom>
						<u--input disabled disabledColor="#fff" v-model="model.Nav_User.NAME" placeholder="请选择盘点时间"
							border="none" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="盘点时间" required prop="CREATE_TIME" borderBottom @click="showCheckDate()">
						<u--input disabledColor="#fff" v-model="model.CREATE_TIME" placeholder="请选择盘点时间" 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="LOCATION" borderBottom>
						<u--input disabledColor="#fff" v-model="model.LOCATION" placeholder="存放位置搜索" border="none"
							inputAlign="right" @blur="loadData()"></u--input>
					</u-form-item>
				</u--form>
			</uni-card>
		</view>
		<u-sticky offset-top="20">
			<view class="sub-form">
				<view class="sub-form-wrap">
					<u--form labelPosition="left" labelWidth="auto" labelAlign="center" errorType="border-bottom"
						ref="sForm">
						<u-collapse :border="false" accordion>
							<uni-card style="margin-bottom: 16px;padding: 10px 0px;" margin="0" padding="0" spacing="0"
								:is-shadow="false" v-for="(item, index) in model.Nav_listDetail">
								<u-collapse-item name="0">
									<view slot="title" class="collapse-title">
										<view class="text-num">{{index + 1+ '. '}}</view>
										<view class="text">
											{{ item.Nav_Material? (item.Nav_Material.NAME+ (item.Nav_Material.SPECIFICATION? ('（'+item.Nav_Material.SPECIFICATION +'）'):'') +(item.COUNT_START? item.COUNT_START:'') ):''}}
										</view>
									</view>

									<u-form-item label="物资名称" prop="Nav_Material.NAME" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.Nav_Material.NAME"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="物资编号" prop="Nav_Material.CODE" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.Nav_Material.CODE"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="存放位置" prop="Nav_Material.LOCATION" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.Nav_Material.LOCATION"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="资源类型" prop="Nav_Material.Nav_MaterialType.NAME" borderBottom>
										<u--input disabled disabledColor="#fff"
											v-model="item.Nav_Material.Nav_MaterialType.NAME" border="none"
											inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="规格/型号" prop="Nav_Material.SPECIFICATION" borderBottom>
										<u--input disabled disabledColor="#fff"
											v-model="item.Nav_Material.SPECIFICATION" border="none"
											inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="库存" prop="Nav_Material.COUNT" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.Nav_Material.COUNT"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="单位" prop="Nav_Material.UNIT" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.Nav_Material.UNIT"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="盘点数量" required prop="COUNT_END" borderBottom>
										<u--input disabledColor="#fff" type='number' @blur='changeCount(item)'
											style="width: 100px; !important" v-model="item.COUNT_END"
											inputAlign="right"></u--input>
									</u-form-item>
								</u-collapse-item>
							</uni-card>
						</u-collapse>
					</u--form>
				</view>
			</view>
		</u-sticky>
		<u-datetime-picker :show="dateTimePickerInfo.showCheckDate" v-model='dateTimePickerInfo.defaultDateTime'
			@confirm="handleCheckDate" @close="dateTimePickerInfo.showCheckDate = false"
			@cancel="dateTimePickerInfo.showCheckDate = false"></u-datetime-picker>
		<view class="bottom-button">
			<button type="primary" v-if='(isLoadOK && tableKey === "1")' @click="FullCheckUpdate">提交</button>
		</view>
	</view>
</template>

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

	import {
		GetInventory,
		FullUpdateInventory
	} from '../../../../services/apply/subPages/SCCM/emergency'

	export default {
		data() {
			return {
				tableKey: '0',
				TaskID: '',
				enums: [{
					ID: '',
					NAME: ''
				}],
				modelEdit: {},
				model: {
					ID: '',
					TaskID: '',
					Nav_User: {
						NAME: ''
					},
					LOCATION: '',
					USER_ID_OPERATE: '',
					Nav_UserOperate: {
						NAME: ''
					},
					Nav_listDetail: [{
						OPERATETYPE: '',
						OPERATETYPE_SHOW: '',
						COUNT_START: '',
						COUNT_END: '',
						Nav_Material: {
							NAME: '',
							Nav_MaterialType: {
								NAME: ''
							}
						}
					}]
				},
				rules: {
					'CREATE_TIME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请选择盘点时间'
					}
				},
				ApproveID: '',
				isAudit: false,
				okApi: '',
				refuseApi: '',
				isLoadOK: false,
				isShowUpload: false,
				dateTimePickerInfo: {
					showCheckDate: false,
					dataIndex: undefined,
					defaultDateTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM:ss'),
					value: ''
				},
			}
		},
		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_User");
				extendInclude(json, "Nav_listDetail.Nav_Material");
				extendInclude(json, "Nav_listDetail.Nav_Material.Nav_MaterialType");
				json.IgnoreDataRule = true
				if (this.model.LOCATION && this.model.LOCATION.length > 0)
					extendRule(json, "LOCATION", 9, this.model.LOCATION)
				GetInventory(json).then(res => {
					if (res.IsSuccessful) {
						this.model = res.Data
						this.isLoadOK = true
					}
				})
			},

			FullCheckUpdate() {
				this.model.Nav_User = null
				this.model.Nav_listDetail.forEach(e => {
					e.Nav_Material = null
				})
				const ele = this.$refs
				this.modelEdit = JSON.parse(JSON.stringify(this.model))
				ele['wForm'].validate().then(res => {
					const orgId = uni.getStorageSync('orgId')
					this.modelEdit.TaskID = this.TaskID
					FullUpdateInventory(this.modelEdit).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							if (this.tableKey == null || this.tableKey == 0) {
								uni.navigateTo({
									url: 'safeCheckIndex'
								})
							} else {
								uni.navigateBack()
							}
						}, 'success', 1000)
					})
				}).catch(err => {
					if (err.length > 0) {
						uni.$showErrorInfo(err[0].message)
					} else {
						uni.$showErrorInfo('校验失败！')
					}
				})
			},

			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
				}
			},

			//显示时间控件
			showCheckDate(dataIndex) {
				if (this.model.NAME == '') {
					uni.$showErrorInfo('请先选择隐患通知！')
					return false
				}
				if (this.model.CHECKTIME == null) {
					this.model.CHECKTIME = uni.$u.timeFormat(new Date(),
						'yyyy-mm-dd hh:MM:ss')
				}
				this.dateTimePickerInfo = {
					showCheckDate: true,
					dataIndex: dataIndex,
					defaultDateTime: this.model.CHECKTIME
				}
			},

			//隐藏控件 显示时间
			handleCheckDate(e) {
				let v = uni.$u.timeFormat(e.value, 'yyyy-mm-dd hh:MM:ss')
				const {
					name,
					dataIndex
				} = this.dateTimePickerInfo
				this.dateTimePickerInfo.showCheckDate = false
				this.model.CREATE_TIME = uni.$u.timeFormat(e.value,
					'yyyy-mm-dd hh:MM:ss')
			},
		},
		computed: {

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