<template>
  <view class="content">
    <view class="card">
      <app-hidden-view
          :hidden-id="hiddenId"
          @throw-basic-info="info = $event.pd"
      />
      <view class="card">
        <u-form
            ref="formRef"
            label-position="left"
            :model="form"
            :rules="rules"
            label-width="auto"
        >
          <u-form-item label="是否正常整改" prop="IS_NORMAL" required>
            <u-radio-group v-model="form.IS_NORMAL" direction="horizontal">
              <u-radio name="1" label="是" value="1"/>
              <u-radio name="2" label="否" value="2"/>
            </u-radio-group>
          </u-form-item>
          <u-line/>
          <block v-if="form.IS_NORMAL === '2'">
            <u-form-item
                label="无法整改原因"
                label-position="top"
                prop="disposalPlan"
                required
            >
              <u-textarea
                  v-model="form.disposalPlan"
                  placeholder="请输入无法整改原因"
                  border="none"
                  auto-height
              />
            </u-form-item>
            <u-line/>
          </block>
          <block v-if="form.IS_NORMAL === '1'">
            <u-divider text-position="left" text="隐患整改"></u-divider>
            <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-time
                v-model="form.RECTIFICATIONTIME"
                prop="RECTIFICATIONTIME"
                mode="date"
                label="整改日期"
            />
            <u-form-item label="投入资金（元）" prop="INVEST_FUNDS" required>
              <u-input
                  v-model="form.INVEST_FUNDS"
                  placeholder="请输入投入资金"
                  border="none"
                  type="number"
                  input-align="right"
              />
            </u-form-item>
            <u-line/>
            <app-upload
                v-model="form.afterRectificationFile"
                label="整改后照片"
                prop="afterRectificationFile"
            />
            <view>
              <u-form-item label="验收人员">
                <u-button
                    custom-style="width: 100rpx;margin-right:0"
                    type="primary"
                    size="mini"
                    text="添加"
                    @click="fnAddAcceptanceList"
                />
              </u-form-item>
              <u-line/>
              <view
                  v-for="(item, index) in acceptanceList"
                  :key="item.id"
                  style="
                  position: relative;
                  border: 1px dashed #ccc;
                  padding: 20rpx 20rpx 20rpx 40rpx;
                "
                  class="mt-10"
              >
                <app-department
                    :id.sync="item.DEPARTMENT_ID"
                    :name.sync="item.DEPARTMENT_NAME"
                    :form.sync="acceptanceList[index]"
                    clear-key="USER_NAME,USER_ID"
                    label="验收部门"
                />
                <app-personnel
                    :id.sync="item.USER_ID"
                    :name.sync="item.USER_NAME"
                    :department-id="item.DEPARTMENT_ID"
                    :is-allow-selection-current-person="false"
                    label="验收人"
                />
                <u-icon
                    v-if="index !== 0"
                    name="close-circle-fill"
                    color="#ff2929"
                    size="20"
                    custom-style="position: absolute;right: -20rpx;top: -20rpx"
                    @click="acceptanceList.splice(index, 1)"
                />
              </view>
            </view>
            <u-form-item label="是否有整改方案" prop="HAVESCHEME" required>
              <u-radio-group v-model="form.HAVESCHEME" direction="horizontal">
                <u-radio name="1" label="是"/>
                <u-radio name="0" label="否"/>
              </u-radio-group>
            </u-form-item>
            <u-line/>
            <block v-if="form.HAVESCHEME === '1'">
              <u-cell-group :border="false">
                <u-cell title="排查日期" :value="info.CREATTIME"/>
                <u-cell title="隐患清单" :value="info.LIST_NAME"/>
              </u-cell-group>
              <u-form-item
                  label="治理标准"
                  label-position="top"
                  prop="GOVERNSTANDARDS"
                  required
              >
                <u-textarea
                    v-model="form.GOVERNSTANDARDS"
                    placeholder="请输入治理标准"
                    border="none"
                    auto-height
                />
              </u-form-item>
              <u-line/>
              <u-form-item
                  label="治理方法"
                  label-position="top"
                  prop="GOVERNMETHOD"
                  required
              >
                <u-textarea
                    v-model="form.GOVERNMETHOD"
                    placeholder="请输入治理方法"
                    border="none"
                    auto-height
                />
              </u-form-item>
              <u-line/>
              <u-form-item
                  label="经费落实"
                  label-position="top"
                  prop="EXPENDITURE"
                  required
              >
                <u-textarea
                    v-model="form.EXPENDITURE"
                    placeholder="请输入经费落实"
                    border="none"
                    auto-height
                />
              </u-form-item>
              <u-line/>
              <u-form-item
                  label="负责人员"
                  label-position="top"
                  prop="PRINCIPAL"
                  required
              >
                <u-textarea
                    v-model="form.PRINCIPAL"
                    placeholder="请输入负责人员"
                    border="none"
                    auto-height
                />
              </u-form-item>
              <u-line/>
              <u-form-item
                  label="工时安排"
                  label-position="top"
                  prop="PROGRAMMING"
                  required
              >
                <u-textarea
                    v-model="form.PROGRAMMING"
                    placeholder="请输入工时安排"
                    border="none"
                    auto-height
                />
              </u-form-item>
              <u-line/>
              <u-form-item
                  label="时限要求"
                  label-position="top"
                  prop="TIMELIMITFOR"
                  required
              >
                <u-textarea
                    v-model="form.TIMELIMITFOR"
                    placeholder="请输入时限要求"
                    border="none"
                    auto-height
                />
              </u-form-item>
              <u-line/>
              <u-form-item
                  label="工作要求"
                  label-position="top"
                  prop="JOBREQUIREMENT"
                  required
              >
                <u-textarea
                    v-model="form.JOBREQUIREMENT"
                    placeholder="请输入工作要求"
                    border="none"
                    auto-height
                />
              </u-form-item>
              <u-line/>
              <u-form-item
                  label="其它事项"
                  label-position="top"
                  prop="OTHERBUSINESS"
                  required
              >
                <u-textarea
                    v-model="form.OTHERBUSINESS"
                    placeholder="请输入其它事项"
                    border="none"
                    auto-height
                />
              </u-form-item>
              <u-line/>
              <app-upload
                  v-model="form.programmeFile"
                  label="方案照片"
                  prop="programmeFile"
              />
            </block>
          </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 AppHiddenView from '@/components/hidden_view/index.vue'
import AppTime from "@/components/time/index.vue";
import AppUpload from "@/components/upload/index.vue";
import AppDepartment from "@/components/department/index.vue";
import AppPersonnel from "@/components/personnel/index.vue";
import {fileUpload, setHiddenNuRectificationSubmit, setHiddenRectificationSubmit} from "@/api/api";

export default {
  components: {
    AppHiddenView,
    AppTime,
    AppUpload,
    AppDepartment,
    AppPersonnel,
  },
  data() {
    return {
      hiddenId: '',
      info: {},
      form: {
        IS_NORMAL: "1",
        disposalPlan: "",
        RECTIFYDESCR: "",
        RECTIFICATIONTIME: "",
        INVEST_FUNDS: "",
        afterRectificationFile: [],
        HAVESCHEME: "0",
        GOVERNSTANDARDS: "",
        GOVERNMETHOD: "",
        EXPENDITURE: "",
        PRINCIPAL: "",
        PROGRAMMING: "",
        TIMELIMITFOR: "",
        JOBREQUIREMENT: "",
        OTHERBUSINESS: "",
        programmeFile: [],
      },
      rules: {
        IS_NORMAL: {
          type: "string",
          required: true,
          message: "请选择是否正常整改",
          trigger: ["blur", "change"],
        },
        disposalPlan: {
          type: "string",
          required: true,
          message: "请输入无法整改原因",
          trigger: ["blur", "change"],
        },
        RECTIFYDESCR: {
          type: "string",
          required: true,
          message: "请输入隐患整改详细描述",
          trigger: ["blur", "change"],
        },
        RECTIFICATIONTIME: {
          type: "string",
          required: true,
          message: "请选择整改日期",
          trigger: ["blur", "change"],
        },
        INVEST_FUNDS: {
          type: "number",
          required: true,
          message: "请输入投入资金",
          trigger: ["blur", "change"],
        },
        afterRectificationFile: {
          type: "array",
          required: true,
          message: "请上传整改后照片",
          trigger: ["blur", "change"],
        },
        HAVESCHEME: {
          type: "string",
          required: true,
          message: "请选择是否有整改方案",
          trigger: ["blur", "change"],
        },
        GOVERNSTANDARDS: {
          type: "string",
          required: true,
          message: "请输入治理标准",
          trigger: ["blur", "change"],
        },
        GOVERNMETHOD: {
          type: "string",
          required: true,
          message: "请输入治理方法",
          trigger: ["blur", "change"],
        },
        EXPENDITURE: {
          type: "string",
          required: true,
          message: "请输入经费落实",
          trigger: ["blur", "change"],
        },
        PRINCIPAL: {
          type: "string",
          required: true,
          message: "请输入负责人员",
          trigger: ["blur", "change"],
        },
        PROGRAMMING: {
          type: "string",
          required: true,
          message: "请输入工时安排",
          trigger: ["blur", "change"],
        },
        TIMELIMITFOR: {
          type: "string",
          required: true,
          message: "请输入时限要求",
          trigger: ["blur", "change"],
        },
        JOBREQUIREMENT: {
          type: "string",
          required: true,
          message: "请输入工作要求",
          trigger: ["blur", "change"],
        },
        OTHERBUSINESS: {
          type: "string",
          required: true,
          message: "请输入其它事项",
          trigger: ["blur", "change"],
        },
        programmeFile: {
          type: "array",
          required: true,
          message: "请上传方案照片",
          trigger: ["blur", "change"],
        },
      },
      acceptanceList: []
    }
  },
  onLoad(query) {
    this.hiddenId = query.HIDDEN_ID
    this.fnAddAcceptanceList()
  },
  methods: {
    fnAddAcceptanceList() {
      this.acceptanceList.push({
        DEPARTMENT_NAME: "",
        DEPARTMENT_ID: "",
        USER_NAME: "",
        USER_ID: "",
        id: uni.$u.guid(),
      });
    },
    async fnSubmit() {
      const isValidate = await this.$refs.formRef.validate()
      if (!isValidate) return;
      if (this.form.IS_NORMAL === 1 || this.form.IS_NORMAL === "1") {
        for (let i = 0; i < this.acceptanceList.length; i++) {
          if (
              !this.acceptanceList[i].DEPARTMENT_NAME ||
              !this.acceptanceList[i].USER_NAME
          ) {
            uni.showToast({
              icon: "none",
              title: "请选择部门人员",
            });
            return;
          }
        }
        await setHiddenRectificationSubmit({
          HIDDEN_ID: this.hiddenId,
          ...this.form,
          SCREENINGDATE: this.info.CREATTIME,
          LISTNAME: this.info.LIST_NAME,
          OTHER: JSON.stringify(this.acceptanceList),
        });
        for (let i = 0; i < this.form.afterRectificationFile.length; i++) {
          await this.fnUploadFile(
              this.form.afterRectificationFile[i].url,
              this.hiddenId,
              4
          );
        }
        for (let i = 0; i < this.form.programmeFile.length; i++) {
          await this.fnUploadFile(this.form.programmeFile[i].url, this.hiddenId, 8);
        }
        uni.showToast({
          icon: "none",
          title: "提交成功",
        });
        setTimeout(() => {
          uni.navigateBack();
        }, 1000);
      } else {
        if (!this.form.disposalPlan) {
          uni.showToast({
            icon: "none",
            title: "请输入无法整改原因",
          });
        }
        const data = await setHiddenNuRectificationSubmit({
          disposalPlan: this.form.disposalPlan,
          HIDDEN_ID: this.hiddenId,
          USER_ID: this.$store.getters.getUserInfo.USER_ID,
          USERNAME: this.$store.getters.getUserInfo.USERNAME,
          postMethod: 'application/json',
        });
        if (data.result === "success") {
          uni.showToast({
            icon: "none",
            title: data.msg,
          });
          setTimeout(() => {
            uni.navigateBack();
          }, 1000);
        } else {
          uni.showToast({
            icon: "none",
            title: "系统异常：" + data.msg,
          });
        }
      }
    },
    async fnUploadFile(filePath, FOREIGN_KEY, TYPE) {
      await fileUpload({
        filePath,
        name: "file",
        formData: {
          foreignKey: FOREIGN_KEY,
          corpInfoId: this.$store.getters.getUserInfo.CORPINFO_ID,
          type: TYPE,
        },
      });
    }
  },
}
</script>

<style scoped lang="scss">

</style>
