<!-- 日常观测-->
<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" :rules="rules"
					ref="wForm" errorType="border-bottom">
					<u-form-item label="尾矿库" prop="TAILING_NAME" borderBottom>
						<u--input disabledColor="#fff" v-model="model.TAILING_NAME" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="观测时间" prop="DATETIME" borderBottom>
						<u--input disabledColor="#fff" v-model="model.DATETIME" placeholder="请选择观测时间" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="观测人" borderBottom prop="ListUserName">
						<u--input disabledColor="#fff" v-model="model.ListUserName" border="none"
							inputAlign="right"></u--input>
					</u-form-item>

				</u--form>
			</uni-card>
		</view>
		<view class="card">
			<uni-card margin="0" :is-shadow="true">
				<u--form labelPosition="left" labelWidth="auto" labelAlign="center" errorType="border-bottom"
					v-if='model.Nav_ListDetail!=null &&model.Nav_ListDetail.length>0'>
					<u-form-item v-for="item in model.Nav_ListDetail"
						:label='item.Nav_Item.NAME+" ("+item.Nav_Item.UNITE+")"' :prop='item.ITEM_VALUE' borderBottom>
						<u--input disabledColor="#fff" placeholder="请输入观测值" border="none" v-model="item.ITEM_VALUE"
							inputAlign="right"></u--input>
					</u-form-item>
				</u--form>
			</uni-card>
		</view>

		<u-datetime-picker :show="dateTimePickerInfo.showCheckDate" v-model='dateTimePickerInfo.defaultDateTime'
			@confirm="handleCheckDate" @close="dateTimePickerInfo.showCheckDate = false"
			@cancel="dateTimePickerInfo.showCheckDate = false"></u-datetime-picker>

		<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" />

		<view class="bottom-button">
			<check-action v-if="isLoadOK && tableKey === '1' && isAudit === 'true'" :okApi="okApi"
				:refuseApi="refuseApi" :dataSource="$store.state.auditModel"></check-action>
			<button type="primary" v-if='(isLoadOK && tableKey === "1" && isAudit != "true")'
				@click="sendCheck">确认</button>
		</view>
	</view>
</template>

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

	import {
		GetTL037,
		sendCheckTL037
	} from '../../../../services/apply/subPages/TL/TLService'


	export default {
		data() {
			return {
				total: 0,
				tableKey: '0',
				TaskID: '',
				AUDIT_OPINION: '',
				ORG_ID: '',
				isDIY: false,
				model: {
					ID: '',
					TaskID: '',
					ORG_ID: '',
					TAILING_NAME: '',
					TAILING_ID: '',
					DATETIME: '',
					Nav_Tailing: {
						NAME: ''
					},
					ListUserName: '',
					Nav_ListUser: [{
						DAYLY_ID: '',
						USER_ID: '',
						ORG_ID: '',
						Nav_User: {
							ID: '',
							NAME: ''
						}
					}],
					//操作需要同时操作 Nav_ListSafeCheckDetailShow
					Nav_ListDetail: [{
						DAYLY_ID: '',
						ITEM_ID: '',
						Nav_Item: {
							ID: '',
							NAME: '',
							UNITE: ''
						},
						ITEM_VALUE: ''
					}] //子表
				},
				isAudit: true,
				isLoadOK: false,
				dateTimePickerInfo: {
					showCheckDate: false,
					dataIndex: undefined,
					defaultDateTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM:ss'),
					value: ''
				},
				selectorInfo: {
					isSubShow: false,
					isMultiple: false,
					showPopup: false,
					totalCount: 0,
					title: '检查人员',
					itemData: {},
					index: 0,
					dataLists: [],
					defaultText: '',
					defaultValue: '',
					name: '',
					defaultChecked: []
				},

				showDelModalIndex: undefined,
				rules: {
					'TAILING_NAME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请选择尾矿库'
					},
					'DATETIME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请选择观测时间'
					},
					'ListUserName': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请选择观测人'
					}
				},
			}
		},
		onLoad(option) {

			this.model.ID = option.ID
			this.model.TaskID = option.taskID
			this.TaskID = option.taskID
			this.tableKey = option.tableKey
			this.ORG_ID = uni.getStorageSync('orgId')

			this.isAudit = option.isAudit
			this.ApproveID = option.ApproveID
			this.okApi = option.okApi
			this.refuseApi = option.refuseApi
			this.auditModel = {
				...this.$store.state.auditModel,
				TaskID: this.TaskID
			}
			if (!this.tableKey || this.tableKey != 1)
				this.paddingBottom = 10

			if (option.ID != null && option.ID != 'null') {
				this.loadData()
			} else {
				this.isLoadOK = true
				this.model.ID = guid()
				this.model.ORG_ID = this.ORG_ID
				this.isDIY = true
				this.model.DATETIME = uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM:ss')

				var user = uni.getStorageSync('appInfo').User
				this.model.ListUserName = user.NAME
				this.model.Nav_ListUser = []
				this.model.Nav_ListUser.push({
					USER_ID: user.ID,
					DAYLY_ID: this.model.ID,
					Nav_User: user,
					ORG_ID: this.ORG_ID
				})
				this.init()
			}
		},
		methods: {
			async loadData() {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId)
				extendRule(json, 'ID', 1, this.model.ID);
				extendInclude(json, 'Nav_ListDetail.Nav_Item')
				extendInclude(json, 'Nav_ListUser.Nav_User')
				json.IgnoreDataRule = true
				GetTL037(json).then(res => {
					this.model = res
					if (res.Nav_ListDetail != null) {
						res.Nav_ListDetail.sort((a, b) => (a.Nav_Item.ROW_NO ?? 0) - (b.Nav_Item.ROW_NO ?? 0))
					}
					var ListUserName = ''
					if (this.model.Nav_ListUser != null && this.model.Nav_ListUser.length > 0) {
						this.model.Nav_ListUser.forEach(e => {
							ListUserName += e.Nav_User.NAME + ' '
						})
					}
					this.model.ListUserName = ListUserName
					this.isLoadOK = true
				})
			},
			async sendCheck() {
				let json = initFilter(this.ORG_ID, this.model.ID, null, null, null, this.TaskID);
				extendRule(json, 'ID', 1, this.model.ID);
				extendRule(json, "Parameter1", 1, this.TaskID)
				json.IgnoreDataRule = true
				sendCheckTL037(json).then(res => {
					if (res) {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					} else {
						uni.$showErrorInfo('操作失败！')
					}
				})
			}
		},
	}
</script>


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