<template>
  <div class="page-wrapper">
    <breadcrumb v-if="!multiPage" :breadcrumb-data="breadcrumbData" />
    <div class="page-container">
      <!-- 设备信息 -->
      <!-- 移转 -->
      <EqpTransferCard
        v-if="checkProcessType === 'TRANSFER_CHECK'"
        :info="info"
      />
      <!-- 借用 -->
      <BorrowCard v-if="checkProcessType === 'BORROW_CHECK'" :info="info" />
      <!-- 设备报废 -->
      <EqpScrapCard
        v-if="checkProcessType === 'EQP_SCRAP_CHECK'"
        :info="info"
      />
      <!-- 模具报废 -->
      <MoldScrapCard
        v-if="checkProcessType === 'MOLD_SCRAP_CHECK'"
        :info="info"
      />
      <!-- 备件报废 -->
      <PartScrapCard
        v-if="checkProcessType === 'PART_SCRAP_CHECK'"
        :info="info"
      />
      <!-- 备件领料 -->
      <PartApplyCard
        v-if="checkProcessType === 'PART_APPLY_CHECK'"
        :info="info"
      />
      <!-- 备件退料 -->
      <PartReturnCard
        v-if="checkProcessType === 'PART_RETURN_CHECK'"
        :info="info"
      />
      <!-- 叫修工单 开始 -->
      <a-collapse
        v-model="activeKey"
        expand-icon-position="right"
        class="collapse-header-divide"
        style="border-top: 8px solid #f0f2f5"
      >
        <a-collapse-panel key="a" :header="$t('AUDIT_PROCESS')">
          <!-- 审批流程 -->
          <ExamineProgress
            :process-list="info.epmCheckProcessLogBPOList || info.logList"
          ></ExamineProgress>
        </a-collapse-panel>
      </a-collapse>
      <!-- 叫修工单 结束 -->
      <a-collapse
        v-model="activeKeyC"
        expand-icon-position="right"
        class="collapse-header-divide"
      >
        <a-collapse-panel key="c" :header="$t('AUDIT_CONFIG')">
          <a-form-model
            ref="ruleForm"
            :model="form"
            :rules="rules"
            :label-col="{ span: 7 }"
            :wrapper-col="{ span: 17 }"
          >
            <a-row :gutter="24">
              <a-col :span="8">
                <a-form-model-item
                  :label="$t('EPMS_REJECT_REASON')"
                  prop="checkResult"
                  allow-clear
                >
                  <a-radio-group v-model="form.checkResult">
                    <a-radio
                      v-for="item in passList"
                      :key="item.value"
                      :value="item.value"
                      >{{ item.label }}</a-radio
                    >
                  </a-radio-group>
                </a-form-model-item>
                <a-form-model-item
                  v-if="form.checkResult === 'REJECT'"
                  :label="$t('EPMS_REJECT_REASON')"
                  prop="rejectReason"
                  allow-clear
                >
                  <a-input
                    v-model="form.rejectReason"
                    :placeholder="$t('PLACEENTER')"
                    onkeyup="value=value.slice(0, 50)"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </a-collapse-panel>
      </a-collapse>
    </div>
    <div class="page-bottom-op">
      <a-button type="primary" :loading="confirmLoading" @click="handleSave">
        <i class="iconfont icon-baocun"></i>
        {{ $t("SAVE") }}
      </a-button>
      <a-button @click="handleCancel">
        <i class="iconfont icon-quxiao"></i>
        {{ $t("CANCEL") }}
      </a-button>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import ApiConstant from "@/api/ApiConstant";
import ExamineProgress from "@/components/epms/ExamineProgress";
import EqpScrapCard from "./components/EqpScrapCard";
import EqpTransferCard from "./components/EqpTransferCard";
import BorrowCard from "./components/BorrowCard";
import MoldScrapCard from "./components/MoldScrapCard";
import PartScrapCard from "./components/PartScrapCard";
import PartApplyCard from "./components/PartApplyCard";
import PartReturnCard from "./components/PartReturnCard";
import breadcrumb from "@/components/breadcrumb";

export default {
  components: {
    EqpScrapCard,
    EqpTransferCard,
    BorrowCard,
    MoldScrapCard,
    PartScrapCard,
    PartApplyCard,
    PartReturnCard,
    breadcrumb,
    ExamineProgress,
  },
  data() {
    return {
      breadcrumbData: [
        {
          name: this.$t("AUDIT"),
          url: "/eam/to_do/examine",
        },
        { name: this.$t("ORDER_DETAIL"), url: "" },
      ],
      activeKey: "a",
      activeKeyB: "b",
      activeKeyC: "c",
      confirmLoading: false,
      info: {},
      form: {
        checkResult: "PASS",
      },
      unitList: [], // 单位列表
      passList: [
        { label: this.$t("EPMS_PASS"), value: "PASS" },
        { label: this.$t("REJECT"), value: "REJECT" },
      ],
      checkProcessType: "",
    };
  },
  computed: {
    ...mapState("setting", ["multiPage"]),
    rules() {
      return {
        rejectReason: [
          {
            required: true,
            message: this.$t("PLACEENTER"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  mounted() {
    this.checkProcessType = this.$route.query.checkProcessType || "";
    this.id = this.$route.query.id || "";
    this.init();
  },
  methods: {
    init() {
      let url = "";
      switch (this.checkProcessType) {
        case "TRANSFER_CHECK":
          url = `/api/epm/eam/eqp-transfer/todo/get-by-id/${this.id}`;
          break;
        case "MOLD_SCRAP_CHECK":
          url = `/api/epm/mam/mold-scrap/get-by-id/${this.id}`;
          break;
        case "BORROW_CHECK":
          url = `/api/epm/eam/eqp-borrow/todo/get-by-id/${this.id}`;
          break;
        case "EQP_SCRAP_CHECK":
          url = `/api/epm/eam/eqp-scrap/todo/get-by-id/${this.id}`;
          break;
        case "PART_SCRAP_CHECK":
          url = `/api/epm/part/part-scrap/get-by-id/${this.id}`;
          break;
        case "PART_APPLY_CHECK":
          url = `/api/epm/part/part-apply/get-by-id/${this.id}`;
          break;
        case "PART_RETURN_CHECK":
          url = `/api/epm/part/part-return/get-by-id/${this.id}`;
          break;
        default:
          url = `/api/epm/eam/eqp-transfer/todo/get-by-id/${id}`;
          break;
      }

      return Promise.all([
        this.$axios.get(`${ApiConstant.PREFIX_CORE}${url}`), // 审核单详情
        this.$axios.get(
          `${ApiConstant.PREFIX_CORE}/api/sys/dict-data/DIC:CORE:MATERIAL_UNIT`
        ), // 数据字典（单位）
      ]).then(([detailRes, unitRes]) => {
        this.info = detailRes.data?.data;
        this.unitList = unitRes.data.data;
      });
    },
    // 保存
    handleSave() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let url = "";
          const params = {
            checkResult: this.form.checkResult, // 通过(PASS);驳回(REJECT)
            checkFormId: this.id,
            rejectReason: this.form.rejectReason,
          };
          switch (this.checkProcessType) {
            case "TRANSFER_CHECK":
              url = `/api/epm/eam/eqp-transfer/todo/check`;
              break;
            case "MOLD_SCRAP_CHECK":
              url = `/api/epm/mam/mold-scrap/todo/check`;
              break;
            case "BORROW_CHECK":
              url = `/api/epm/eam/eqp-borrow/todo/check`;
              break;
            case "EQP_SCRAP_CHECK":
              url = `/api/epm/eam/eqp-scrap/todo/check`;
              break;
            case "PART_SCRAP_CHECK":
              url = `/api/epm/part/part-scrap/todo/check`;
              break;
            case "PART_APPLY_CHECK":
              url = `/api/epm/part/part-apply/todo/check`;
              break;
            case "PART_RETURN_CHECK":
              url = `/api/epm/part/part-return/todo/check`;
              break;
            default:
              url = `/api/epm/eam/eqp-scrap/todo/check`;
              break;
          }
          this.$axios
            .put(`${ApiConstant.PREFIX_CORE}${url}`, {
              ...params,
            })
            .then(() => {
              this.confirmLoading = false;
              this.$message.success(this.$t("SAVE_SUCCESS"));
              this.handleCancel();
            })
            .catch((e) => {
              this.confirmLoading = false;
            });
        }
      });
    },
    // 取消
    handleCancel() {
      this.$store.dispatch("tagsView/jumpBeforeView", {
        to: {
          path: "/eam/to_do/examine",
        },
        vm: this,
      });
    },
    // 页面埋点
    handleFuncUseRate(data) {
      this.$axios
        .put(`${ApiConstant.PREFIX_CORE}/api/epm/eam/use`, data)
        .then(() => {});
    },
  },
};
</script>
<style lang="less" scoped>
@import "@/assets/css/fault_repair.less";
/deep/ .mega-design-business-table .mega-design-pro-table {
  padding: 0 !important;
  margin-bottom: 24px;
}
</style>
