<template>
	<view class="todo-page">
		<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="AUDIT_OPINION" borderBottom v-if="model.SHIFT_STATUS==9">
					</u-form-item>
					<u--textarea v-html="model.AUDIT_OPINION" border="surround" v-if="model.SHIFT_STATUS==9"
						disabled></u--textarea>
					<u-form-item label="本班运行情况：" prop="CURRENT_CLASS_STATUS_SHOW"
						@click="handleShowSheet({title: '运行情况', name: 'CURRENT_CLASS_STATUS'})">
						<u--input :value="CURRENT_CLASS_STATUS_SHOW" disabled disabledColor="#fff"
							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="CURRENT_CLASS_QUESTION" borderBottom
						v-if="model.CURRENT_CLASS_STATUS==1" required>
					</u-form-item>
					<u--textarea v-model="model.CURRENT_CLASS_QUESTION" placeholder="请输入存在问题" border="surround"
						v-if="model.CURRENT_CLASS_STATUS==1" :required="model.CURRENT_CLASS_STATUS==1"></u--textarea>
					<u-form-item label="处理情况：" prop="CURRENT_CLASS_MEASURE" borderBottom
						v-if="model.CURRENT_CLASS_STATUS==1" required>
					</u-form-item>
					<u--textarea v-model="model.CURRENT_CLASS_MEASURE" placeholder="请输入处理情况" border="surround"
						v-if="model.CURRENT_CLASS_STATUS==1" :required="model.CURRENT_CLASS_STATUS==1"></u--textarea>

					<u-form-item label="设备运行情况：" prop="DEVICE_STATUS_SHOW"
						@click="handleShowSheet({title: '运行情况', name: 'DEVICE_STATUS'})">
						<u--input :value="DEVICE_STATUS_SHOW" disabled disabledColor="#fff" 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="DEVICE_QUESTION" borderBottom v-if="model.DEVICE_STATUS==1"
						required>
					</u-form-item>
					<u--textarea v-model="model.DEVICE_QUESTION" placeholder="请输入存在问题" border="surround"
						v-if="model.DEVICE_STATUS==1" :required="model.DEVICE_STATUS==1"></u--textarea>
					<u-form-item label="处理情况：" prop="DEVICE_MEASURE" borderBottom v-if="model.DEVICE_STATUS==1"
						required>
					</u-form-item>
					<u--textarea v-model="model.DEVICE_MEASURE" placeholder="请输入处理情况" border="surround"
						v-if="model.DEVICE_STATUS==1" :required="model.DEVICE_STATUS==1"></u--textarea>

					<u-form-item label="现场环境情况：" prop="ENVIRONMENT_STATUS_SHOW"
						@click="handleShowSheet({title: '运行情况', name: 'ENVIRONMENT_STATUS'})">
						<u--input :value="ENVIRONMENT_STATUS_SHOW" disabled disabledColor="#fff" 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="ENVIRONMENT_QUESTION" borderBottom
						v-if="model.ENVIRONMENT_STATUS==1" required>
					</u-form-item>
					<u--textarea v-model="model.ENVIRONMENT_QUESTION" placeholder="请输入存在问题" border="surround"
						v-if="model.ENVIRONMENT_STATUS==1"></u--textarea>
					<u-form-item label="处理情况：" prop="ENVIRONMENT_MEASURE" borderBottom
						v-if="model.ENVIRONMENT_STATUS==1" required>
					</u-form-item>
					<u--textarea v-model="model.ENVIRONMENT_MEASURE" placeholder="请输入处理情况" border="surround"
						v-if="model.ENVIRONMENT_STATUS==1"></u--textarea>

					<u-form-item label="劳保用品情况：" prop="SUPPLIES_STATUS_SHOW"
						@click="handleShowSheet({title: '运行情况', name: 'SUPPLIES_STATUS'})">
						<u--input :value="SUPPLIES_STATUS_SHOW" disabled disabledColor="#fff" 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="SUPPLIES_QUESTION" borderBottom v-if="model.SUPPLIES_STATUS==1"
						required>
					</u-form-item>
					<u--textarea v-model="model.SUPPLIES_QUESTION" placeholder="请输入存在问题" border="surround"
						v-if="model.SUPPLIES_STATUS==1"></u--textarea>
					<u-form-item label="处理情况：" prop="SUPPLIES_MEASURE" borderBottom v-if="model.SUPPLIES_STATUS==1"
						required>
					</u-form-item>
					<u--textarea v-model="model.SUPPLIES_MEASURE" placeholder="请输入处理情况" border="surround"
						v-if="model.SUPPLIES_STATUS==1"></u--textarea>

					<u-form-item label="是否佩戴：" prop="SUPPLIES_USED_SHOW"
						@click="handleShowSheet({title: '运行情况', name: 'SUPPLIES_USED'})">
						<u--input :value="SUPPLIES_USED_SHOW" disabled disabledColor="#fff" 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="DEVICE_STORAGE" borderBottom required>
					</u-form-item>
					<u--textarea v-model="model.DEVICE_STORAGE" placeholder="请输入设备工具使用存放情况
" border="surround"></u--textarea>
					<u-form-item label="当班人员：" prop="UserNames" borderBottom>
						<u--input v-html="model.UserNames" border="none" slot="right" inputAlign="right" disabled
							disabledColor="#ffffff"></u--input>
					</u-form-item>
					<view class="upload-title">附件：</view>
					<full-upload v-model="model.Nav_Files" :isShowBtn='true' :listProp='listPropUpload'
						:listPropVal='listPropValUpload'></full-upload>
				</u--form>
			</uni-card>
		</view>
		<u-picker :defaultIndex="[0]" :show="comPickerInfo.showSheet" :columns="comPickerInfo.columns"
			@confirm="onConfirmPicker" @close="closePicker" @cancel="closePicker" keyName="NAME"></u-picker>
		<view class="bottom-button">
			<button type="primary" :disabled="isSubmit" :loading="isSubmit" @click="submit" v-if='tableKey==1||isAdd==1'>提交</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		guid,
		initFilter,
		initFilterGroup
	} from '../../../../utils/common'
	import {
		getRequest,
	} from '../../../../services/apply/FOServices/FOServices';
	import config from '../../../../config/common'

	export default {
		data() {
			return {
				listPropUpload: ['CURRENT_CLASS_RECORD_ID'],
				listPropValUpload: [],
				UserNames: "",
				model: {
					CURRENT_CLASS_STATUS: 0,
					DEVICE_STATUS: 0,
					ENVIRONMENT_STATUS: 0,
					SUPPLIES_STATUS: 0,
					SUPPLIES_USED: 1,
					ORG_ID: "",
					ENABLE_STATUS: 0,
				},
				subModel: {
					Nav_User: {},
					USER_ID: ""
				},
				rules: {
					'CURRENT_CLASS_QUESTION': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'CURRENT_CLASS_MEASURE': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'DEVICE_QUESTION': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'DEVICE_MEASURE': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'ENVIRONMENT_QUESTION': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'ENVIRONMENT_MEASURE': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'SUPPLIES_QUESTION': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'SUPPLIES_MEASURE': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'DEVICE_STORAGE': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
				},
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},
				currentOperateUser: {},
				showPopup: false,
				tableKey: '0',
				TaskID: null,
				isLoadOK: false,
				showDelModalIndex: undefined,
				uploadFiles: [],
				isAdd: 0,
				ORG_ID: uni.getStorageSync('orgId'),
				CURRENT_CLASS_STATUS_SHOW:"",
				DEVICE_STATUS_SHOW:"",
				ENVIRONMENT_STATUS_SHOW:"",
				SUPPLIES_USED_SHOW:"",
				SUPPLIES_STATUS_SHOW:"",
				isSubmit:false
			}
		},
		onLoad(option) {
			this.model.ID = option.ID;
			this.TaskID = option.taskID ? option.taskID :"00000000-0000-0000-0000-000000000000" ;
			this.tableKey = option.tableKey ? option.tableKey : '0';
			if (option.isAdd != 1) {
				this.loadData();
			} else {
				this.isAdd = 1;
				this.model.ID = guid();

				this.model.CURRENT_CLASS_STATUS == 0 ? this.CURRENT_CLASS_STATUS_SHOW = "正常" : this
					.CURRENT_CLASS_STATUS_SHOW = "不正常";
				this.model.DEVICE_STATUS == 0 ? this.DEVICE_STATUS_SHOW = "正常" : this
					.DEVICE_STATUS_SHOW = "正常";
				this.model.ENVIRONMENT_STATUS == 0 ? this.ENVIRONMENT_STATUS_SHOW = "正常" : this
					.ENVIRONMENT_STATUS_SHOW = "正常";
				this.model.SUPPLIES_STATUS == 0 ? this.SUPPLIES_STATUS_SHOW = "正常" : this
					.SUPPLIES_STATUS_SHOW = "正常";
				this.model.SUPPLIES_USED == 0 ? this.SUPPLIES_USED_SHOW = "否" : this
					.SUPPLIES_USED_SHOW = "是";
			}
		},
		methods: {
			loadData() {
				const json = initFilter(this.ORG_ID, "", "")
				if (this.listPropValUpload.length == 0) {
					this.listPropValUpload.push(this.model.ID)
				}
				extendInclude(json, "Nav_Files.Nav_ImgFile.Nav_File")
				extendInclude(json, "Nav_Users.Nav_User")
				extendRule(json, 'ID', 1, this.model.ID)
				getRequest(json, "/FO/CurrentClassRecord/Get").then(res => {
					this.model = res;
					this.model.CURRENT_CLASS_STATUS == 0 ? this.CURRENT_CLASS_STATUS_SHOW = "正常" : this
						.CURRENT_CLASS_STATUS_SHOW = "不正常";
					this.model.DEVICE_STATUS == 0 ? this.DEVICE_STATUS_SHOW = "正常" : this
						.DEVICE_STATUS_SHOW = "正常";
					this.model.ENVIRONMENT_STATUS == 0 ? this.ENVIRONMENT_STATUS_SHOW = "正常" : this
						.ENVIRONMENT_STATUS_SHOW = "正常";
					this.model.SUPPLIES_STATUS == 0 ? this.SUPPLIES_STATUS_SHOW = "正常" : this
						.SUPPLIES_STATUS_SHOW = "正常";
					this.model.SUPPLIES_USED == 0 ? this.SUPPLIES_USED_SHOW = "否" : this
						.SUPPLIES_USED_SHOW = "是";
					let strUserName = ''
					if (res.Nav_Users && res.Nav_Users.length > 0) {
						for (let i = 0; i < res.Nav_Users.length; i++) {
							if (res.Nav_Users[i].USER_SHIFT_STATUS == 0) {
								strUserName += (strUserName.length > 0 ? "," : "") + "<text style='color:red'>" +
									res.Nav_Users[i].Nav_User.NAME + "</text>";
							} else {
								strUserName += (strUserName.length > 0 ? "," : "") + res.Nav_Users[
									i].Nav_User.NAME;
							}

						}
					}
					this.model.UserNames = strUserName;
				})
			},
			onConfirmPicker(e) {
				const {
					name,
					formIndex
				} = this.comPickerInfo
				this.model[name] = e.value[0].ID
				this[name + '_SHOW'] = e.value[0].NAME
				this.comPickerInfo.showSheet = false
			},

			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
			},
			submit() {
				this.isSubmit=true;
				const ele = this.$refs
				ele['wForm'].validate().then(res => {
					this.model.PUBLISH = "SaveAndNotify";
					if (this.model.ORG_ID == "") {
						this.model.ORG_ID = this.ORG_ID;
					}
					this.model.TaskID = this.TaskID;
					getRequest(this.model, "/FO/FOCurrentClassRecord/FullUpdate").then(res => {
						if (res) {
							this.isSubmit=false;
							uni.$showMsgFunc('操作成功!', () => {
								uni.navigateBack()
							}, 'success', 1000)
						}
					})
				}).catch(err => {
					uni.$showErrorInfo('请检查必填项，必填项不能为空')
				})
			},
			async handleShowSheet(p) {
				let column = []
				if (p.name === 'CURRENT_CLASS_STATUS' || p.name === 'DEVICE_STATUS' || p.name ===
					'ENVIRONMENT_STATUS' || p.name === 'SUPPLIES_STATUS') {
					column = [{
							NAME: '正常',
							ID: 0
						},
						{
							NAME: '不正常',
							ID: 1
						}
					]
				} else if (p.name === 'SUPPLIES_USED') {
					column = [{
							NAME: '否',
							ID: 0
						},
						{
							NAME: '是',
							ID: 1
						}
					]
				}
				if (column.length) {
					this.comPickerInfo = {
						showSheet: true,
						title: p.title,
						name: p.name,
						formIndex: p.formIndex,
						columns: [column]
					}
				} else {}
			},
		}
	}
</script>

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

	.todo-page {
		padding: 16px 16px 70px;
	}
</style>