<template>
  <view>
    <view class="tips_text">
      严禁在本互联网非涉密平台处理、传输国家秘密和工作秘密，请确认扫描、传输的文件资料不涉及国家秘密和工作秘密
    </view>
    <hidden-view
      :special-rectification-inspect-hidden-id="
        SPECIAL_RECTIFICATION_INSPECT_HIDDEN_ID
      "
    />
    <view class="container">
      <u-divider
        text="隐患复查"
        text-position="left"
        text-color="rgb(42, 86, 247)"
        line-color="rgb(42, 86, 247)"
        dashed
      />
      <u--form
        ref="formRef"
        label-position="left"
        :model="form"
        :rules="rules"
        label-width="auto"
      >
        <u-form-item label="是否合格" prop="QUALIFIED" border-bottom required>
          <u-radio-group
            v-model="form.QUALIFIED"
            class="mt-10"
            style="justify-content: end"
          >
            <u-radio
              v-for="(item, index) in qualifiedList"
              :key="index"
              :name="item.ID"
              :label="item.NAME"
              style="margin-left: 10upx"
            />
          </u-radio-group>
        </u-form-item>
        <u-form-item
          v-if="form.QUALIFIED === 0"
          label="原因"
          prop="CORP_ADDRESS"
          border-bottom
          required
          label-position="top"
        >
          <u--input v-model="form.REVIEW_CONTENT" border="none" />
        </u-form-item>
        <u-form-item
          label="复查时间"
          prop="REVIEW_TIME"
          border-bottom
          required
          @click="fnReviewTimeShowChange"
        >
          <u--input
            :value="form.REVIEW_TIME || '请选择'"
            readonly
            border="none"
            input-align="right"
          />
          <u-icon name="arrow-right" color="#999999"></u-icon>
        </u-form-item>
        <u-form-item
          label="现场复查照片"
          prop="onSiteReviewPhotos"
          border-bottom
          label-position="top"
        >
          <u-upload
            class="mt-10"
            :file-list="form.onSiteReviewPhotos"
            multiple
            :max-count="10"
            @afterRead="fnOnSiteReviewPhotosAfterRead"
            @delete="fnOnSiteReviewPhotosDelete"
          />
        </u-form-item>
        <u-button type="primary" text="保存" class="mt-10" @click="fnSubmit" />
      </u--form>
      <u-datetime-picker
        v-model="reviewTimeCurrent"
        :show="reviewTimeShow"
        mode="datetime"
        @cancel="fnReviewTimeShowChange"
        @confirm="fnReviewTimeShowConfirm"
      />
    </view>
  </view>
</template>

<script>
import HiddenView from "./hidden_view.vue";
import {
  setSpecialRectificationInspectionEnterpriseHiddenReview,
  setSpecialRectificationInspectionImgReview,
} from "@/api";
export default {
  components: {
    HiddenView,
  },
  data() {
    return {
      SPECIAL_RECTIFICATION_INSPECT_HIDDEN_ID: "",
      form: {
        QUALIFIED: "",
        REVIEW_CONTENT: "",
        REVIEW_TIME: "",
        onSiteReviewPhotos: [],
      },
      rules: {
        QUALIFIED: {
          type: "number",
          required: true,
          message: "请选择是否合格",
          trigger: ["change"],
        },
        REVIEW_CONTENT: {
          type: "string",
          required: true,
          message: "请输入不合格原由",
          trigger: ["change"],
        },
        REVIEW_TIME: {
          type: "string",
          required: true,
          message: "请选择复查时间",
          trigger: ["change"],
        },
      },
      qualifiedList: [
        { ID: 1, NAME: "是" },
        { ID: 0, NAME: "否" },
      ],
      reviewTimeShow: false,
      reviewTimeCurrent: Number(new Date()),
    };
  },
  onLoad(query) {
    this.SPECIAL_RECTIFICATION_INSPECT_HIDDEN_ID =
      query.SPECIAL_RECTIFICATION_INSPECT_HIDDEN_ID;
  },
  computed: {
    userInfo() {
      return this.$store.getters.getUserInfo;
    },
  },
  methods: {
    fnReviewTimeShowChange() {
      this.reviewTimeShow = !this.reviewTimeShow;
    },
    fnReviewTimeShowConfirm(event) {
      this.form.REVIEW_TIME = uni.$u.timeFormat(
        event.value,
        "yyyy-mm-dd hh:MM",
      );
      this.fnReviewTimeShowChange();
    },
    fnOnSiteReviewPhotosAfterRead(event) {
      this.form.onSiteReviewPhotos.push(...event.file);
    },
    fnOnSiteReviewPhotosDelete(event) {
      this.form.onSiteReviewPhotos.splice(event.index, 1);
    },
    async fnSubmit() {
      this.$refs.formRef
        .validate()
        .then(async () => {
          uni.showLoading({
            title: "数据提交中",
          });
          await this.fnUploadOnSiteReviewPhotos(
            this.SPECIAL_RECTIFICATION_INSPECT_HIDDEN_ID,
          );
          await setSpecialRectificationInspectionEnterpriseHiddenReview({
            loading: false,
            SPECIAL_RECTIFICATION_INSPECT_HIDDEN_ID:
              this.SPECIAL_RECTIFICATION_INSPECT_HIDDEN_ID,
            ...this.form,
            OPERATOR: this.userInfo.NAME,
            USER_ID: this.userInfo.USER_ID,
            USER_NAME: this.userInfo.USERNAME,
            DEPARTMENT_ID: this.userInfo.DEPARTMENT_ID,
          });
          uni.hideLoading();
          uni.$u.toast("保存成功");
          uni.navigateBack();
        })
        .catch(() => {
          uni.$u.toast("请填写完整信息");
        });
    },
    async fnUploadOnSiteReviewPhotos(SPECIAL_RECTIFICATION_INSPECT_HIDDEN_ID) {
      for (let i = 0; i < this.form.onSiteReviewPhotos.length; i++) {
        const files = [];
        files.push({
          uri: this.form.onSiteReviewPhotos[i].url,
          name: "imgsList",
        });
        await setSpecialRectificationInspectionImgReview({
          loading: false,
          files,
          formData: {
            SPECIAL_RECTIFICATION_INSPECT_HIDDEN_ID,
            USER_NAME: this.userInfo.USERNAME,
            CORPINFO_ID: this.form.CORPINFO_ID,
          },
        });
      }
    },
  },
};
</script>

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