<template>
  <view class="container">
    <view class="tips_text">
      严禁在本互联网非涉密平台处理、传输国家秘密和工作秘密，请确认扫描、传输的文件资料不涉及国家秘密和工作秘密
    </view>
    <u--form
      ref="formRef"
      label-position="left"
      :model="form"
      :rules="rules"
      label-width="auto"
    >
      <u-form-item
        label="检查内容"
        prop="INSPECT_CONTENT"
        border-bottom
        required
        label-position="top"
      >
        <u--textarea
          v-model="form.INSPECT_CONTENT"
          disabled
          placeholder="请输入检查内容"
          border="none"
          auto-height
        />
      </u-form-item>
      <u-form-item
        label="隐患描述"
        prop="HIDDENDESCR"
        border-bottom
        required
        label-position="top"
      >
        <u--textarea
          v-model="form.HIDDENDESCR"
          placeholder="请输入隐患描述"
          border="none"
        />
      </u-form-item>
      <u-form-item label="隐患部位" prop="HIDDENPART" border-bottom required>
        <u--input
          v-model="form.HIDDENPART"
          border="none"
          input-align="right"
          placeholder="请输入隐患部位"
        />
      </u-form-item>
      <u-form-item
        label="隐患级别"
        prop="HIDDENLEVEL_NAME"
        border-bottom
        required
        @click="fnHiddenLevelPickerShow"
      >
        <u--input
          :value="form.HIDDENLEVEL_NAME || '请选择'"
          readonly
          border="none"
          input-align="right"
        />
        <u-icon name="arrow-right" color="#999999"></u-icon>
      </u-form-item>
      <u-form-item label="整改意见" prop="DISPOSE" border-bottom required>
        <u--input
          v-model="form.DISPOSE"
          border="none"
          input-align="right"
          placeholder="请输入隐患处置"
        />
      </u-form-item>
      <u-form-item
        label="发现时间"
        prop="DISCOVER_TIME"
        border-bottom
        required
        @click="fnDiscoverTimeShowChange"
      >
        <u--input
          :value="form.DISCOVER_TIME || '请选择'"
          readonly
          border="none"
          input-align="right"
        />
        <u-icon name="arrow-right" color="#999999"></u-icon>
      </u-form-item>
      <u-form-item
        label="整改截止时间"
        prop="RECTIFY_DEADLINE"
        border-bottom
        required
        @click="fnRectifyDeadlineShowChange"
      >
        <u--input
          :value="form.RECTIFY_DEADLINE || '请选择'"
          readonly
          border="none"
          input-align="right"
        />
        <u-icon name="arrow-right" color="#999999"></u-icon>
      </u-form-item>
      <u-form-item
        label="隐患图片"
        prop="hiddenImgs"
        border-bottom
        required
        label-position="top"
      >
        <u-upload
          style="margin-top: 20upx"
          :file-list="form.hiddenImgs"
          multiple
          :max-count="4"
          @afterRead="fnHiddenImgsAfterRead"
          @delete="fnHiddenImgsDelete"
        />
      </u-form-item>
      <u-button type="primary" text="保存" @click="fnSubmit" />
    </u--form>
    <u-picker
      :show="hiddenLevelPickerShow"
      :columns="hiddenLevelList"
      key-name="NAME"
      @cancel="fnHiddenLevelPickerShow"
      @confirm="fnHiddenLevelPickerConfirm"
    />
    <u-datetime-picker
      v-model="discoverTimeCurrent"
      :show="discoverTimeShow"
      mode="datetime"
      @cancel="fnDiscoverTimeShowChange"
      @confirm="fnInspectTimeConfirm"
    />
    <u-datetime-picker
      v-model="rectifyDeadlineCurrent"
      :show="rectifyDeadlineShow"
      mode="date"
      @cancel="fnRectifyDeadlineShowChange"
      @confirm="fnRectifyDeadlineConfirm"
    />
  </view>
</template>

<script>
import { getHiddenLevel } from "@/api";

export default {
  data() {
    return {
      type: "",
      index: 0,
      deleteImgsId: [],
      form: {
        CONTENT_ID: "",
        INSPECT_CONTENT: "",
        HIDDENDESCR: "",
        HIDDENPART: "",
        HIDDENLEVEL: "",
        HIDDENLEVEL_NAME: "",
        DISPOSE: "",
        DISCOVER_TIME: "",
        RECTIFY_DEADLINE: "",
        hiddenImgs: [],
      },
      rules: {
        INSPECT_CONTENT: {
          type: "string",
          required: true,
          message: "请输入检查内容",
          trigger: ["blur", "change"],
        },
        HIDDENDESCR: {
          type: "string",
          required: true,
          message: "请输入隐患描述",
          trigger: ["blur", "change"],
        },
        HIDDENPART: {
          type: "string",
          required: true,
          message: "请输入隐患部位",
          trigger: ["blur", "change"],
        },
        HIDDENLEVEL_NAME: {
          type: "string",
          required: true,
          message: "请选择隐患级别",
          trigger: ["change"],
        },
        DISPOSE: {
          type: "string",
          required: true,
          message: "请输入隐患处置",
          trigger: ["blur", "change"],
        },
        DISCOVER_TIME: {
          type: "string",
          required: true,
          message: "请选择发现时间",
          trigger: ["change"],
        },
        RECTIFY_DEADLINE: {
          type: "string",
          required: true,
          message: "请选择整改截止时间",
          trigger: ["change"],
        },
        hiddenImgs: {
          type: "array",
          required: true,
          message: "请上传现场检查照片",
          trigger: ["change"],
        },
      },
      hiddenLevelPickerShow: false,
      hiddenLevelList: [],
      discoverTimeShow: false,
      discoverTimeCurrent: Number(new Date()),
      rectifyDeadlineShow: false,
      rectifyDeadlineCurrent: Number(new Date()),
    };
  },
  onLoad(query) {
    if (
      query.SPECIAL_RECTIFICATION_INSPECT_CONTENT_ID &&
      query.SPECIAL_RECTIFICATION_INSPECT_CONTENT_ID !== "undefined" &&
      query.SPECIAL_RECTIFICATION_INSPECT_CONTENT_ID !== "null"
    ) {
      this.form.SPECIAL_RECTIFICATION_INSPECT_CONTENT_ID =
        query.SPECIAL_RECTIFICATION_INSPECT_CONTENT_ID;
    }
    this.type = query.type;
    this.index = query.index;
    this.form.CONTENT_ID = query.CONTENT_ID;
    this.form.INSPECT_CONTENT = query.INSPECT_CONTENT;
    this.fnGetHiddenLevel();
    this.getOpenerEventChannel().on("hiddenEdit", (event) => {
      this.form = event.form;
    });
  },
  methods: {
    async fnGetHiddenLevel() {
      const resData = await getHiddenLevel();
      this.hiddenLevelList.push(resData.list);
    },
    fnHiddenLevelPickerShow() {
      this.hiddenLevelPickerShow = !this.hiddenLevelPickerShow;
    },
    fnHiddenLevelPickerConfirm(event) {
      this.form.HIDDENLEVEL_NAME = event.value[0].NAME;
      this.form.HIDDENLEVEL = event.value[0].BIANMA;
      this.fnHiddenLevelPickerShow();
    },
    fnDiscoverTimeShowChange() {
      this.discoverTimeShow = !this.discoverTimeShow;
    },
    fnInspectTimeConfirm(event) {
      this.form.DISCOVER_TIME = uni.$u.timeFormat(
        event.value,
        "yyyy-mm-dd hh:MM",
      );
      this.fnDiscoverTimeShowChange();
    },
    fnRectifyDeadlineShowChange() {
      this.rectifyDeadlineShow = !this.rectifyDeadlineShow;
    },
    fnRectifyDeadlineConfirm(event) {
      this.form.RECTIFY_DEADLINE = uni.$u.timeFormat(event.value, "yyyy-mm-dd");
      this.fnRectifyDeadlineShowChange();
    },
    fnHiddenImgsAfterRead(event) {
      this.form.hiddenImgs.push(...event.file);
    },
    fnHiddenImgsDelete(event) {
      if (event.file.SPECIAL_RECTIFICATION_FILES_ID) {
        this.deleteImgsId.push(event.file.SPECIAL_RECTIFICATION_FILES_ID);
      }
      this.form.hiddenImgs.splice(event.index, 1);
    },
    fnSubmit() {
      this.$refs.formRef
        .validate()
        .then(async () => {
          this.getOpenerEventChannel().emit("hiddenAdd", {
            form: { ...this.form, id: uni.$u.guid() },
            SPECIAL_RECTIFICATION_FILES_ID: this.SPECIAL_RECTIFICATION_FILES_ID,
            type: this.type,
            index: this.index,
            deleteImgsId: this.deleteImgsId,
          });
          uni.$u.toast("保存成功");
          uni.navigateBack();
        })
        .catch(() => {
          uni.$u.toast("请填写完整信息");
        });
    },
  },
};
</script>

<style scoped lang="scss">
.container {
  padding: 20upx;
}
</style>
