<template>
	<view class="content">
		<u--form ref="editForm" :model="form" :rules="rules" labelPosition="left" class="form">
			<u-row :customStyle="rowStyle">
				<u-col :span="4">
					<u-form-item label="库点名称" prop="orgName" label-width="100" class="item" required>
						<u-input v-model="form.orgName" readonly></u-input>
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="仓房名称" prop="basicStorehouseId" label-width="100" class="item" required>
						<simple-select v-model="form.basicStorehouseId" load-type="local" :data="houseOptions"
							:readonly="readonly" @on-change="houseChanged" />
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="货位名称" prop="warehouseId" label-width="100" class="item" required>
						<simple-select v-model="form.warehouseId" load-type="local" :data="wareOptions"
							:readonly="readonly" @on-change="wareChanged" />
					</u-form-item>
				</u-col>
			</u-row>

			<u-row :customStyle="rowStyle">
				<u-col :span="4">
					<u-form-item label="粮食种类" prop="grainVarietyName" label-width="100" class="item" required>
						<u-input v-model="form.grainVarietyName" readonly></u-input>
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="粮食等级" prop="grainGradeName" label-width="100" class="item" required>
						<u-input v-model="form.grainGradeName" readonly></u-input>
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="收获年度" prop="harvestYear" label-width="100" class="item" required>
						<year-select v-model.number="form.harvestYear" :readonly="readonly"></year-select>
					</u-form-item>
				</u-col>
			</u-row>

			<u-row :customStyle="rowStyle">
				<u-col :span="4">
					<u-form-item label="检查人" prop="inspector" label-width="100" class="item" required>
						<u-input v-model="form.inspector" :readonly="readonly"></u-input>
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="保管员" prop="keeper" label-width="100" class="item" required>
						<u-input v-model="form.keeper" :readonly="readonly"></u-input>
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="粮温℃" prop="grainTemperature" label-width="100" class="item">
						<u-input v-model="form.grainTemperature" :readonly="readonly"></u-input>
					</u-form-item>
				</u-col>
			</u-row>

			<u-row :customStyle="rowStyle">
				<u-col :span="4">
					<u-form-item label="仓温℃" prop="storeroomTemperature" label-width="100" class="item">
						<u-input v-model="form.storeroomTemperature" :readonly="readonly"></u-input>
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="气温℃" prop="ambientTemperature" label-width="100" class="item">
						<u-input v-model="form.ambientTemperature" :readonly="readonly"></u-input>
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="仓内湿%" prop="storeroomHumidity" label-width="100" class="item">
						<u-input v-model="form.storeroomHumidity" :readonly="readonly"></u-input>
					</u-form-item>
				</u-col>
			</u-row>

			<u-row :customStyle="rowStyle">
				<u-col :span="4">
					<u-form-item label="仓外湿%" prop="ambientHumidity" label-width="100" class="item">
						<u-input v-model="form.ambientHumidity" :readonly="readonly"></u-input>
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="检查日期" prop="inspectionDate" label-width="100" class="item">
						<uni-datetime-picker type="datetime" v-model="form.inspectionDate" :disabled="readonly" />
					</u-form-item>
				</u-col>
			</u-row>
			<u-divider text="日常检查"></u-divider>
			<u-row :customStyle="rowStyle">
				<u-col :span="4">
					<u-form-item label="鼠雀检查" prop="rodentCheck" label-width="100" class="item">
						<simple-select v-model="form.rodentCheck" data="OPT2" load-type="static" :readonly="readonly" />
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="是否结露" prop="isCondensation" label-width="100" class="item">
						<simple-select v-model="form.isCondensation" data="OPT2" load-type="static"
							:readonly="readonly" />
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="粮食霉变情况" prop="moldCondition" label-width="100" class="item">
						<simple-select v-model="form.moldCondition" data="OPT2" load-type="static"
							:readonly="readonly" />
					</u-form-item>
				</u-col>
			</u-row>

			<u-row :customStyle="rowStyle">
				<u-col :span="4">
					<u-form-item label="是否板结" prop="isLump" label-width="100" class="item">
						<simple-select v-model="form.isLump" data="OPT2" load-type="static" :readonly="readonly" />
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="发热现象" prop="heatingPhenomenon" label-width="100" class="item">
						<u-input v-model="form.heatingPhenomenon" :readonly="readonly"></u-input>
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="仓房有无渗漏" prop="isLeakage" label-width="100" class="item">
						<simple-select v-model="form.isLeakage" data="OPT2" load-type="static" :readonly="readonly" />
					</u-form-item>
				</u-col>
			</u-row>

			<u-row :customStyle="rowStyle">
				<u-col :span="4">
					<u-form-item label="仓房是否返潮" prop="isReturnDamp" label-width="100" class="item">
						<simple-select v-model="form.isReturnDamp" data="OPT2" load-type="static"
							:readonly="readonly" />
					</u-form-item>
				</u-col>
				<u-col :span="4">
					<u-form-item label="是否有害虫" prop="isPest" label-width="100" class="item">
						<simple-select v-model="form.isPest" data="OPT2" load-type="static" :readonly="readonly" />
					</u-form-item>
				</u-col>
			</u-row>
			<u-divider text="拍照上传"></u-divider>
			<simple-upload :file-list="uploadFiles" :readonly="readonly" @on-change="uploadChanged" />
		</u--form>
		<view class="buttons">
			<u-button v-if="! readonly" text="保存" @tap="save" type="primary" class="btn"></u-button>
			<u-button text="取消" @tap="back" class="btn"></u-button>
		</view>
	</view>
</template>

<script>
	import simpleEdit from '@/components/simple-edit/index.js'
	import simpleSelect from '@/components/simpleSelect.vue'
	import yearSelect from '@/components/yearSelect.vue'
	import {
		getDetail,
		update,
		getGrain
	} from '@/api/safeStorageInspect.js'

	import {
		defaultRequired,
		simpleRequired
	} from '@/utils/rules'
	import {
		deleteField,
		isNull,
		parseField2SomeType
	} from '@/utils/tools'
	import houseOptions from '@/components/plugins/houseOptions'
	import wareOptions from '@/components/plugins/wareOptions'
	import simpleUpload from '@/components/simpleUpload.vue'
	export default {
		mixins: [simpleEdit, houseOptions, wareOptions],
		components: {
			simpleSelect,
			simpleUpload, yearSelect
		},
		data() {
			return {
				listUrl: '/pages/safeStorageInspect/safeStorageInspect',
				uploadFiles: [],
				defaultForm: {
					orgName: null,
					basicStorehouseId: null,
					warehouseId: null,
					grainVarietyName: null,
					grainGradeName: null,
					harvestYear: null,
					inspector: null,
					keeper: null,
				},
				rules: {
					orgName: defaultRequired,
					basicStorehouseId: defaultRequired,
					warehouseId: defaultRequired,
					grainVarietyName: defaultRequired,
					grainGradeName: defaultRequired,
					harvestYear: defaultRequired,
					inspector: defaultRequired,
					keeper: defaultRequired,
				}
			}
		},
		methods: {
			setUp() {
				this.addAfterHook(() => {
					return this.getHouseOptions(this.form.orgId)
				})
				this.addAfterHook(() => {
					return this.getGrainInfo(this.form.basicStorehouseId, this.form.warehouseId, this.form.orgId)
				})
				this.addAfterHook(() => {
					console.log('edit data', this.editData, this.form);
					const files = this.form.images || ''
					if (files) {
						this.uploadFiles = files.split(',').map(d => {
							return {
								url: d,
							}
						})
					} else {
						this.uploadFiles = []
					}
					return this.getWareOptions(this.form.basicStorehouseId, this.form.orgId)
				})
			},

			getDataParam() {
				return this.editData.ssSafeGrainStorageInspectionId
			},

			getDataFun() {
				return getDetail
			},
			getGrainInfo(storehouseId, warehouseId, orgId) {
				return getGrain({
					storehouseId,
					warehouseId,
					orgId
				}).then(resp => {
					if (resp && resp.data) {
						this.form.grainGradeName = resp.data.grainGradeName
						this.form.grainVarietyName = resp.data.grainVarietyName
						this.form.grainVarietyId = resp.data.grainVarietyId
						this.form.grainGradeId = resp.data.grainGradeId
					}
				})
			},
			parseResp(resp) {
				const d = resp.data
				parseField2SomeType(d, ['rodentCheck', 'isCondensation', 'moldCondition', 'isLump', 'isLeakage',
					'isReturnDamp', 'isPest'
				], (v) => {
					if (v === true) return 1
					else if (v === false) return 0
					return v
				})

				/// for debug
				// d.images = 'http://172.16.0.36:15772/province-qinghai/2024/6/14/1801530630927626241.png'
				/// for debug

				return d
			},

			houseChanged(n, o) {
				this.form.storehouseName = o?.raw?.storehouseName
				// console.log(this.form.storehouseName);
				this.form.warehouseId = null
				this.getWareOptions(n, this.form.orgId)
			},
			wareChanged(n, o) {
				this.form.warehouseName = o?.raw?.warehouseName
				// console.log(this.form.warehouseName);
				if (n) {
					this.getGrainInfo(this.form.basicStorehouseId, this.form.warehouseId, this.form.orgId)
				}
			},

			uploadChanged(n) {
				const list = n || []
				this.form.images = n.map(d => d.url).join(',')
				console.log(this.form.images, n, list);
			},

			getLocalData() {
				const d = {
					...this.form
				}
				d.ssSafeGrainStorageInspectionId = d.ssSafeGrainStorageInspectionId || this.editData
					.ssSafeGrainStorageInspectionId
					
				parseField2SomeType(d, ['rodentCheck', 'isCondensation', 'moldCondition', 'isLump', 'isLeakage',
					'isReturnDamp', 'isPest'
				], (v) => {
					if (v === 1) return true
					else if (v === 0) return false
					return null
				})
				
				deleteField(d, [
					'id', 'createTime', 'createUser', 'updateTime', 'updateUser',
					'warehouseName', 'orgName', 'storehouseName', 'orgId', 'grainGradeName', 'grainVarietyName'
				])
				return d
			},

			getSaveFun() {
				return update
			}
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 10px;

		.buttons {
			display: flex;
			justify-content: flex-end;

			.btn {
				max-width: 100px;
				margin: 10px;
			}
		}

		.form {
			.item {
				margin-left: 12px;
				;
			}
		}
	}
</style>