<!-- 物资操作单 查看 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">

					<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 label="操作人" prop="Nav_User.NAME" borderBottom>
						<u--input disabled disabledColor="#ffffff" v-model="model.Nav_User.NAME" border="none"
							slot="right" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="受托人" v-if="model.USER_ID_OPERATE!=null&&model.USER_ID_OPERATE!=''"
						prop="Nav_UserOperate" borderBottom>
						<u--input disabled disabledColor="#ffffff" v-model="model.Nav_UserOperate.NAME" border="none"
							slot="right" inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="操作时间" prop="CREATE_TIME" borderBottom>
						<u--input disabled disabledColor="#ffffff" v-model="model.CREATE_TIME" border="none"
							slot="right" inputAlign="right"></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 +'）'):'')):''}}
										</view>
									</view>
									<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.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.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.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.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="COUNT_START" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.COUNT_START" 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="操作类型" prop="OPERATETYPE_SHOW" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.OPERATETYPE_SHOW"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="操作数量" prop="COUNT_OPERATE" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.COUNT_OPERATE"
											border="none" inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="最新库存" prop="item.COUNT_END" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.COUNT_END" border="none"
											inputAlign="right"></u--input>
									</u-form-item>
									<u-form-item label="说明" prop="REASON" borderBottom>
										<u--input disabled disabledColor="#fff" v-model="item.REASON" border="none"
											inputAlign="right"></u--input>
									</u-form-item>
								</u-collapse-item>
							</uni-card>
						</u-collapse>
					</u--form>
				</view>
			</view>
		</u-sticky>



		<!-- 归还 -->
		<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 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 required label="是否结束" 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 label="说明" prop="REASON" borderBottom>
						<u--input disabledColor="#fff" v-model="modelOperate.REASON" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
				</u--form>
			</uni-card>
		</view>

		<view class="bottom-button">
			<!-- 如果只有一个借出明细 并且没有结束 -->
			<button type="primary"
				v-if='!ISOPERATE&& isLoadOK&&!model.ISSYSTEM&&model.OPERATETYPE!=null &&model.OPERATETYPE==10&&model.Nav_listDetail!=null&&model.Nav_listDetail.length==1'
				@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="FullUpdateReturnApp">提交</button>
		</view>

	</view>
</template>

<script>
	import {
		initFilter,
		extendRule,
		extendInclude,
		fileStructChange
	} from '../../../../utils/common'
	import {
		GetOperate,
		FullUpdateReturnApp
	} from '../../../../services/apply/subPages/SCCM/emergency'
	import {
		getEnum
	} from '../../../../services/common'

	export default {
		data() {
			return {
				tableKey: '0',
				TaskID: '',
				enums: [{
					ID: '',
					NAME: ''
				}],
				ISOPERATE: false,
				model: {
					ID: '',
					TaskID: '',
					Nav_User: {
						NAME: ''
					},
					USER_ID_OPERATE: '',
					Nav_UserOperate: {
						NAME: ''
					},
					Nav_listDetail: [{
						OPERATETYPE: '',
						OPERATETYPE_SHOW: '',
						Nav_Material: {
							NAME: '',
							Nav_MaterialType: {
								NAME: ''
							}
						}
					}]
				},


				rulesOperate: {
					'COUNT_OPERATE': {
						type: 'number',
						required: true,
						trigger: ['blur', 'change'],
						message: '操作数量不能为空'
					}
				},
				// 入库操作
				modelOperate: {
					CODE: 'YJ' + (new Date()).Format('yyyyMMddHHmmss'),
					OPERATETYPE: '20',
					ORG_ID: '',
					COUNT_OPERATE: '',
					ISEND: true,
					REASON: '',
				},
				radioItems: [{
						text: '结束',
						value: true
					},
					{
						text: '不结束',
						value: false
					}
				],
				ApproveID: '',
				isAudit: false,
				okApi: '',
				refuseApi: '',
				isLoadOK: false,
				isShowUpload: false
			}
		},
		onLoad(option) {
			var isPassEnums = false
			this.model.ID = option.ID
			if (option.enums) {
				isPassEnums = true
				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
			}
			if (isPassEnums) {
				this.loadData()
			} else {
				this.ini()
			}
		},
		methods: {

			ini() {
				getEnum({
					name: 'CMOperation'
				}).then(res => {
					var listEnums = res
					this.enums = listEnums
					this.loadData()
				})
			},
			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_UserOperate');
				extendInclude(json, 'Nav_listDetail.Nav_Material');
				extendInclude(json, 'Nav_listDetail.Nav_Material.Nav_MaterialType');
				json.IgnoreDataRule = true
				GetOperate(json).then(res => {
					if (res.IsSuccessful) {
						if (this.enums && this.enums.length > 0) {
							if (res.Data.Nav_listDetail && res.Data.Nav_listDetail.length > 0) {
								res.Data.Nav_listDetail.forEach(i => {
									i.OPERATETYPE_SHOW = this.enums.find(e => e.ID == i.OPERATETYPE)?.NAME
								})
							}
						} else {
							res.Data.Nav_listDetail.forEach(i => {
								i.OPERATETYPE_SHOW = i.OPERATETYPE
							})
						}
						this.model = res.Data
						this.isLoadOK = true
					}
				})
			},
			FullUpdateReturnApp() {
				const ele = this.$refs
				ele['sForm'].validate().then(res => {
					this.modelEdit = JSON.parse(JSON.stringify(this.modelOperate))
					this.modelEdit.PARENT_ID = this.model.Nav_listDetail[0].ID
					if (this.TaskID && this.TaskID.length > 0)
						this.modelEdit.TaskID = this.TaskID
					FullUpdateReturnApp(this.modelEdit).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					})

				}).catch(err => {
					if (err.length > 0) {
						uni.$showErrorInfo(err[0].message)
					} else {
						uni.$showErrorInfo('校验失败！')
					}
				})
			}
		},
		computed: {

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