<!-- 盘点 -->
<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="isShowUser" 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 required label="委托盘点" prop="ISSEND" borderBottom>
						<u-radio-group v-model="model.ISSEND" 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 label="盘存人" v-if="model.ISSEND" prop="Nav_UserOperate"
						@click="handleShowSheet({title: '选择盘存人', name: 'ISSEND'})" borderBottom>
						<u--input disabled disabledColor="#fff" v-model="model.Nav_UserOperate.NAME"
							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>
		<!-- <query-selector :show="selectorInfo.showPopup" :title="selectorInfo.title" :lists="selectorInfo.dataLists"
			@search="handleSearch" @select="handleSelected" @close="selectorInfo.showPopup=false"
			:total="selectorInfo.totalCount" /> -->



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


		<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' @click="FullCheckUpdate">提交</button>
		</view>
	</view>
</template>

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

	import {
		OrderPagedUseful2,
	} from '../../../../services/common'

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

	export default {
		data() {
			return {
				tableKey: '0',
				TaskID: '',
				enums: [{
					ID: '',
					NAME: ''
				}],
				modelEdit: {},

				radioItems: [{
						text: '是',
						value: true
					},
					{
						text: '否',
						value: false
					}
				],
				isShowUser: false,
				model: {

					ID: '',
					LOCATION: '',
					TaskID: '',
					Nav_User: {
						NAME: ''
					},
					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: '请选择盘点时间'
					}
				},
				selectorInfo: {
					isMultiple: false,
					totalCount: 0,
					showPopup: false,
					title: '人员选择',
					index: 0,
					dataLists: [],
					defaultText: '',
					defaultValue: '',
					name: ''
				},


				// selectorInfo: {
				// 	// isSubShow: false,
				// 	// isMultiple: false,
				// 	showPopup: false,
				// 	totalCount: 0,
				// 	title: '检查人员',
				// 	itemData: {},
				// 	index: 0,
				// 	dataLists: [],
				// 	defaultText: '',
				// 	defaultValue: '',
				// 	name: '',
				// 	defaultChecked: []
				// },

				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)
				if (this.model.LOCATION)
					extendRule(json, "LOCATION", 9, this.model.LOCATION)
				extendInclude(json, "Nav_User");
				extendInclude(json, "Nav_UserOperate");
				extendInclude(json, "Nav_listDetail.Nav_Material");
				extendInclude(json, "Nav_listDetail.Nav_Material.Nav_MaterialType");

				json.IgnoreDataRule = true
				GetInventory(json).then(res => {
					if (res.IsSuccessful) {
						if (res.Data.USER_ID_OPERATE == null) {
							res.Data.Nav_UserOperate = {
								NAME: ''
							}
						}

						if (!res.Data.Nav_User)
							res.Data.Nav_User = {
								NAME: ''
							}
						this.model = res.Data
						this.isLoadOK = true
					}
				})
			},

			FullCheckUpdate() {

				if (this.model.ISSEND) {
					if (!this.model.Nav_UserOperate || !this.model.Nav_UserOperate.ID) {
						uni.$showErrorInfo('请选择盘存人')
					}
				} else {
					this.model.USER_ID_OPERATE = null
					this.model.Nav_UserOperate = null
				}
				const ele = this.$refs
				this.modelEdit = JSON.parse(JSON.stringify(this.model))
				this.modelEdit.Nav_User = null
				this.modelEdit.Nav_listDetail.forEach(e => {
					e.Nav_Material = null
				})

				ele['wForm'].validate().then(res => {
					const orgId = uni.getStorageSync('orgId')
					if (this.TaskID != '')
						this.modelEdit.TaskID = this.TaskID
					FullUpdateInventory(this.modelEdit).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							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.CREATE_TIME == null) {
					this.model.CREATE_TIME = uni.$u.timeFormat(new Date(),
						'yyyy-mm-dd hh:MM:ss')
				}
				this.dateTimePickerInfo = {
					showCheckDate: true,
					dataIndex: dataIndex,
					defaultDateTime: this.model.CREATE_TIME
				}
			},

			//隐藏控件 显示时间
			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')
			},

			//下拉数据加载
			async handleShowSheet(p) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", 0)
				json.Limit = 20
				if (p.val) {
					extendRule(json, 'NAME', 9, p.val);
				}
				let dataSelect = []
				if (p.name === 'ISSEND') {
					dataSelect = await OrderPagedUseful2(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].CODE
							}
						}
						return res.Data
					})
				}
				if (dataSelect.length) {
					this.selectorInfo = {
						isMultiple: false,
						showPopup: true,
						title: p.title,
						index: 0,
						name: p.name,
						dataIndex: p.dataIndex,
						dataLists: dataSelect,
						totalCount: this.selectorInfo.totalCount,
					}
				} else {
					// 暂无数据
				}
			},
			//选择信息
			handleSelected(e) {
				this.selectorInfo.showPopup = false
				if (this.selectorInfo.name === 'ISSEND') {
					this.model.Nav_UserOperate = e
					this.model.USER_ID_OPERATE = e.ID //会直接报错
				}
			},
			//数据加载
			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>