<template>
	<view class="container">
		<view class="card">
			<view class="card">
				<u-form
					ref="formRef"
					label-position="left"
					:model="form"
					:rules="rules"
					label-width="auto"
				>
					<block v-if="entranceType === 'hidden_troubleshooting_check_item'">
						<u-cell title="检查内容">
							<template #label>
								<view class="mt-5">{{ checkContent }}</view>
							</template>
						</u-cell>
					</block>
					<!-- 上传多张图片 -->
					<app-upload
						:model-value="form.imageFile"
						label="隐患照片"
						prop="imageFile"
					/>
					<!-- 上传一个视频资料 -->
					<app-upload
						:model-value="form.videoFile"
						label="隐患视频"
						prop="videoFile"
						:required="false"
						:max-count="1"
						:multiple="false"
						accept="video"
					/>
					<u-form-item
						label="隐患描述"
						label-position="top"
						prop="HIDDENDESCR"
						required
					>
						<u-textarea
							v-model="form.HIDDENDESCR"
							placeholder="请输入隐患描述"
							border="none"
							auto-height
						/>
					</u-form-item>
					<u-line />
					<!-- 隐患部位 -->
					<app-hidden-position
						:id="form.HIDDENPART"
						:name="form.HIDDENPARTNAME"
						prop="HIDDENPARTNAME"
					/>
					<!-- 隐患级别 -->
					<app-hidden-level
						:id="form.HIDDENLEVEL"
						:name="form.HIDDENLEVELNAME"
						:is-show-neglect="false"
						prop="HIDDENLEVELNAME"
					/>
					<!-- 隐患类型 -->
					<app-hidden-type
						:id="form.HIDDENTYPE"
						:name="form.HIDDENTYPE1NANEM"
						prop="HIDDENTYPE1NANEM"
					/>
					<u-form-item label="是否立即整改" prop="RECTIFICATIONTYPE" required>
						<u-radio-group
							v-model="form.RECTIFICATIONTYPE"
							direction="horizontal"
						>
							<block
								v-if="
									form.HIDDENLEVEL !== 'jdyh001' &&
									form.HIDDENLEVEL !== 'hiddenLevel2002'
								"
							>
								<u-radio name="1" label="是" />
							</block>
							<u-radio name="2" label="否" />
						</u-radio-group>
					</u-form-item>
					<u-line />
					<block
						v-if="
							form.RECTIFICATIONTYPE === '2' &&
							form.HIDDENLEVEL !== 'jdyh001' &&
							form.HIDDENLEVEL !== 'hiddenLevel2002'
						"
					>
						<!-- 整改责任部门 -->
						<app-department
							:id="form.RECTIFICATIONDEPT"
							:name="form.RECTIFICATIONDEPTNAME"
							:form="form"
							clear-key="RECTIFICATIONORNAME,RECTIFICATIONOR"
							prop="RECTIFICATIONDEPTNAME"
							label="整改责任部门"
							:required="false"
						/>
						<!-- 整改责任人 -->
						<app-personnel
							:id="form.RECTIFICATIONOR"
							:name="form.RECTIFICATIONORNAME"
							:department-id="form.RECTIFICATIONDEPT"
							prop="RECTIFICATIONORNAME"
							label="整改责任人"
							:required="false"
						/>
						<!-- 整改期限 -->
						<app-time
							:model-value="form.RECTIFICATIONDEADLINE"
							label="整改期限"
							prop="RECTIFICATIONDEADLINE"
							mode="date"
							:required="false"
							@fn-confirm-emit="fnConfirmEmit"
						/>
					</block>
					<block
						v-if="
							form.HIDDENLEVEL !== 'jdyh001' &&
							form.HIDDENLEVEL !== 'hiddenLevel2002'
						"
					>
						<app-personnel
							:id="form.CONFIRM_USER"
							:name="form.CONFIRM_USER_NAME"
							department-id=""
							is-hidden-confirm-personnel
							prop="CONFIRM_USER_NAME"
							label="隐患确认人"
						/>
					</block>
					<app-time
						:model-value="form.DISCOVERYTIME"
						label="隐患发现时间"
						prop="DISCOVERYTIME"
						:max-date="new Date().getTime()"
					/>
					<u-form-item label="是否相关方" prop="ISRELEVANT" required>
						<u-radio-group v-model="form.ISRELEVANT" direction="horizontal">
							<u-radio name="1" label="是" />
							<u-radio name="2" label="否" />
						</u-radio-group>
					</u-form-item>
					<u-line />
					<u-form-item
						label="隐患位置描述"
						label-position="top"
						prop="POSITIONDESC"
						required
					>
						<u-textarea
							v-model="form.POSITIONDESC"
							placeholder="请输入隐患位置描述"
							border="none"
							auto-height
						/>
					</u-form-item>
					<u-line />
					<block v-if="!!hiddenId && entranceType === 'hidden_report'">
						<view v-for="(item, index) in repulseCause" :key="index">
							<u-cell title="打回意见及时间">
								<template #label>
									<view class="mt-5">{{ item.REPULSE_CAUSE }}</view>
									<view class="mt-5">{{ item.OPERATTIME }}</view>
								</template>
							</u-cell>
						</view>
					</block>
					<app-location
						:longitude="form.LONGITUDE"
						:latitude="form.LATITUDE"
						longitude-prop="LONGITUDE"
						latitude-prop="LATITUDE"
						label="隐患上报位置"
					/>
					<app-department
						:id="form.DEPARTMENT_ID"
						:name="form.DEPARTMENT_NAME"
						:form="form"
						clear-key="USER_NAME,USER_ID"
						prop="DEPARTMENT_NAME"
						label="隐患发现人部门"
					/>
					<app-personnel
						:id="form.USER_ID"
						:name="form.USER_NAME"
						:department-id="form.DEPARTMENT_ID"
						prop="USER_NAME"
						label="隐患发现人"
					/>
					<block v-if="form.RECTIFICATIONTYPE === '1'">
						<u-form-item
							label="整改描述"
							label-position="top"
							prop="RECTIFYDESCR"
							required
						>
							<u-textarea
								v-model="form.RECTIFYDESCR"
								placeholder="请输入整改描述"
								border="none"
								auto-height
							/>
						</u-form-item>
						<u-line />
						<app-upload
							v-model="form.AfterRectificationFile"
							label="整改后图片"
							prop="AfterRectificationFile"
						/>
					</block>
					<view class="mt-10">
						<u-button
							type="primary"
							shape="circle"
							text="提交"
							:throttle-time="1000"
							@click="fnSubmit"
						/>
					</view>
				</u-form>
			</view>
		</view>
	</view>
</template>

<script>
import AppUpload from "@/components/upload/index.vue";
import AppHiddenPosition from "@/components/hiddenPosition/index.vue";
import AppHiddenLevel from "@/components/hidden_level/index.vue";
import AppHiddenType from "@/components/hiddenType/index.vue";
import AppDepartment from "@/components/department/index.vue";
import AppPersonnel from "@/components/personnel/index.vue";
import AppTime from "@/components/time/index.vue";
import AppLocation from "@/components/location/index.vue";
import { getHiddenInfo } from "@/api";
import dayjs from "dayjs";
import useFormValidate from "@/utils/useFormValidate.js";

export default {
	components: {
		AppUpload,
		AppHiddenPosition,
		AppHiddenLevel,
		AppHiddenType,
		AppDepartment,
		AppPersonnel,
		AppTime,
		AppLocation,
	},

	onLoad(query) {
		this.hiddenId = query.HIDDEN_ID;
		this.checkContent = query.CHECK_CONTENT;
		this.listCheckItemId = query.LISTCHECKITEM_ID;
		this.recordItemId = query.RECORDITEM_ID;
		this.listManagerId = query.LISTMANAGER_ID;
		this.entranceType = query.entranceType || "hidden_report";
		this.fnGetData(query.HIDDEN_ID);
	},

	data() {
		return {
			store: this.$store,
			// form 表单字段
			form: {
				imageFile: [],
				videoFile: [],
				HIDDENDESCR: "",
				HIDDENPART: "",
				HIDDENPARTNAME: "", // 隐患部位
				HIDDENLEVEL: "",
				HIDDENLEVELNAME: "",
				HIDDENTYPE: "",
				HIDDENTYPE1NANEM: "",
				RECTIFICATIONTYPE: "2",
				RECTIFICATIONDEPT: "",
				RECTIFICATIONDEPTNAME: "",
				RECTIFICATIONOR: "",
				RECTIFICATIONORNAME: "",
				RECTIFICATIONDEADLINE: "",
				CONFIRM_USER: "",
				CONFIRM_USER_NAME: "",
				DISCOVERYTIME: dayjs().format("YYYY-MM-DD HH:mm"),
				ISRELEVANT: "2",
				POSITIONDESC: "",
				LONGITUDE: "",
				LATITUDE: "",
				DEPARTMENT_ID: this.$store.state.userInfo.DEPARTMENT_ID,
				DEPARTMENT_NAME: this.$store.state.userInfo.DEPARTMENT_NAME,
				USER_ID: this.$store.state.userInfo.USER_ID,
				USER_NAME: this.$store.state.userInfo.NAME,
				RECTIFYDESCR: "",
				AfterRectificationFile: [],
			},
			rules: {
				imageFile: {
					type: "array",
					required: true,
					message: "请上传隐患照片",
					trigger: ["blur", "change"],
				},
				HIDDENDESCR: {
					type: "string",
					required: true,
					message: "请输入隐患描述",
					trigger: ["blur", "change"],
				},
				HIDDENLEVELNAME: {
					type: "string",
					required: true,
					message: "请选择隐患级别",
					trigger: ["blur", "change"],
				},
				HIDDENTYPE1NANEM: {
					type: "string",
					required: true,
					message: "请选择隐患类型",
					trigger: ["blur", "change"],
				},
				RECTIFICATIONTYPE: {
					type: "string",
					required: true,
					message: "请选择是否立即整改",
					trigger: ["blur", "change"],
				},
				CONFIRM_USER_NAME: {
					type: "string",
					required: true,
					message: "请选择隐患确认人",
					trigger: ["blur", "change"],
				},
				DISCOVERYTIME: {
					type: "string",
					required: true,
					message: "请选择隐患发现时间",
					trigger: ["blur", "change"],
				},
				ISRELEVANT: {
					type: "string",
					required: true,
					message: "请选择是否相关方",
					trigger: ["blur", "change"],
				},
				POSITIONDESC: {
					type: "string",
					required: true,
					message: "请输入隐患位置描述",
					trigger: ["blur", "change"],
				},
				HIDDENPARTNAME: {
					type: "string",
					required: true,
					message: "请选择隐患部位",
					trigger: ["blur", "change"],
				},
				LONGITUDE: {
					type: "string",
					required: true,
					message: "请选择经度",
					trigger: ["blur", "change"],
				},
				LATITUDE: {
					type: "string",
					required: true,
					message: "请选择纬度",
					trigger: ["blur", "change"],
				},
				DEPARTMENT_NAME: {
					type: "string",
					required: true,
					message: "请选择隐患发现人部门",
					trigger: ["blur", "change"],
				},
				USER_NAME: {
					type: "string",
					required: true,
					message: "请选择隐患发现人",
					trigger: ["blur", "change"],
				},
				RECTIFYDESCR: {
					type: "string",
					required: true,
					message: "请输入整改描述",
					trigger: ["blur", "change"],
				},
				AfterRectificationFile: {
					type: "array",
					required: true,
					message: "请上传整改后图片",
					trigger: ["blur", "change"],
				},
			},
			hiddenId: "",
			checkContent: "",
			listCheckItemId: "",
			recordItemId: "",
			listManagerId: "",
			entranceType: "",
			repulseCause: []
		};
	},

	methods: {
		async fnGetData(hidden_id) {
			if (!hidden_id) return null;
			const responseData = await getHiddenInfo({
				hidden_id,
			});
			for (let i = 0; i < responseData.hImgs.length; i++) {
				responseData.hImgs[i].url = filePath + responseData.hImgs[i].FILEPATH;
			}
			for (let i = 0; i < responseData.rImgs.length; i++) {
				responseData.rImgs[i].url = filePath + responseData.rImgs[i].FILEPATH;
			}
			for (let i = 0; i < responseData.hiddenVideo.length; i++) {
				responseData.hiddenVideo[i].url =
					filePath + responseData.hiddenVideo[i].FILEPATH;
			}
			this.form = responseData.pd;
			this.form.CONFIRM_USER_NAME = responseData.pd.conUserName;
			this.form.HIDDENTYPE1NANEM = responseData.pd.HIDDENTYPENAME;
			this.form.HIDDENPARTNAME = responseData.pd.hregionName;
			this.form.imageFile = responseData.hImgs;
			this.form.videoFile = responseData.hiddenVideo;
			this.form.AfterRectificationFile = responseData.rImgs;
			this.form.DEPARTMENT_ID = responseData.hiddenFindPeople[0].DEPARTMENT_ID;
			this.form.DEPARTMENT_NAME = responseData.hiddenFindPeople[0].deptName;
			this.form.USER_ID = responseData.hiddenFindPeople[0].USER_ID;
			this.form.USER_NAME = responseData.hiddenFindPeople[0].NAME;
			this.repulseCause = responseData.repulsecause;
		},
		fnConfirmEmit({ mode, value }) {
			if (mode === "datetime") {
				this.form = {
					...this.form,
					RECTIFICATIONDEADLINE: dayjs(value).format("YYYY-MM-DD HH:mm"),
				};
			} else if (mode === "date") {
				this.form = {
					...this.form,
					RECTIFICATIONDEADLINE: dayjs(value).format("YYYY-MM-DD"),
				};
			} else if (mode === "year-month") {
				this.form = {
					...this.form,
					RECTIFICATIONDEADLINE: dayjs(value).format("YYYY-MM"),
				};
			}
		},
		async fnSubmit() {
			const { userInfo } = this.store.state;
			// 校验表单必填项
			await useFormValidate(this.$refs.formRef);
			const other = [
				{
					DEPARTMENT_ID: this.form.DEPARTMENT_ID,
					DEPARTMENT_NAME: this.form.DEPARTMENT_NAME,
					USER_ID: this.form.USER_ID,
					USER_NAME: this.form.USER_NAME,
				},
			];
			let params = {
				...this.form,
				HIDDENFINDDEPT: userInfo.DEPARTMENT_ID,
				CREATOR: userInfo.USER_ID,
				USERNAME: userInfo.NAME,
				other: JSON.stringify(other),
			};
			let currentHiddenId = "";
			if (this.entranceType === "hidden_report") {
				const { pd } = !hiddenId.value
					? await setHiddenAdd(params)
					: await setHiddenUpdate(params);
				currentHiddenId = pd.HIDDEN_ID;
			}
			if (
				this.entranceType === "hidden_troubleshooting_check_item" ||
				this.entranceType === "hidden_troubleshooting_other_hidden"
			) {
				if (this.entranceType === "hidden_troubleshooting_check_item") {
					params = {
						...params,
						SOURCE: "2",
						STATE: "0",
						CLEARIMG: "1",
						CHECK_CONTENT: this.checkContent,
						RECORDITEM_ID: this.recordItemId,
						LISTMANAGER_ID: this.listManagerId,
						RISKITEM_ID: this.listCheckItemId,
					};
				}
				if (this.entranceType === "hidden_troubleshooting_other_hidden") {
					params = {
						...params,
						SOURCE: "3",
						STATE: "0",
						LISTMANAGER_ID: this.listManagerId,
					};
				}
				const { pd } = !hiddenId.value
					? await setHiddenTroubleshootingCheckItemHiddenAdd(params)
					: await setHiddenTroubleshootingCheckItemHiddenUpdate(params);
				currentHiddenId = pd.HIDDEN_ID;
			}
			for (let i = 0; i < this.form.imageFile.length; i++) {
				if (!this.form.imageFile[i].FILEPATH)
					await fnUploadFile(this.form.imageFile[i].url, currentHiddenId, 3);
			}
			for (let i = 0; i < this.form.videoFile.length; i++) {
				if (!this.form.videoFile[i].FILEPATH)
					await fnUploadFile(this.form.videoFile[i].url, currentHiddenId, 102);
			}
			for (let i = 0; i < this.form.AfterRectificationFile.length; i++) {
				if (!this.form.AfterRectificationFile[i].FILEPATH)
					await fnUploadFile(
						this.form.AfterRectificationFile[i].url,
						currentHiddenId,
						4
					);
			}
			if (this.entranceType === "hidden_troubleshooting_check_item") {
				uni.$emit("hiddenTroubleshootingCheckItemHiddenSaveSuccess", {
					hiddenId: currentHiddenId,
				});
			}
			uni.showToast({
				icon: "none",
				title: "提交成功",
			});
			setTimeout(() => {
				uni.navigateBack();
			}, 1000);
		},
	},
};
</script>

<style scoped lang="scss"></style>
