<template>
  <div class="page-wrapper app">
    <breadcrumb v-if="!multiPage" :breadcrumb-data="breadcrumbData" />
    <div class="page-container">
      <a-form-model
        slot="main"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="{ span: 7 }"
        :wrapper-col="{ span: 17 }"
      >
        <!-- 基础信息 -->
        <a-collapse
          v-model="activeKeyA"
          expand-icon-position="right"
          class="collapse-header-divide"
        >
          <a-collapse-panel key="a" :header="$t('BASE_INFO')">
            <a-row :gutter="24" type="flex">
              <!-- 编码 开始 -->
              <a-col :span="8">
                <a-form-model-item :label="`${$t('CODE')}`" prop="code">
                  <a-input
                    v-model="form.code"
                    disabled
                    :placeholder="$t('PLACEENTER')"
                  /> </a-form-model-item
              ></a-col>
              <!-- 编码 结束 -->
              <!-- 标题 开始 -->
              <a-col :span="8">
                <a-form-model-item :label="`${$t('TITLE')}`" prop="name">
                  <a-input
                    v-model="form.name"
                    disabled
                    :placeholder="$t('PLACEENTER')"
                  /> </a-form-model-item
              ></a-col>
              <!-- 标题 结束 -->
              <!-- 报废仓 开始 -->
              <a-col :span="8">
                <a-form-model-item
                  :label="$t('SCRAP_WAREHOUSE')"
                  prop="locationFirstIdArr"
                >
                  <a-cascader
                    v-model="form.locationFirstIdArr"
                    disabled
                    :placeholder="$t('PLACESELECT')"
                    :options="currentOptions"
                    :field-names="fieldNames"
                  />
                </a-form-model-item>
              </a-col>
              <!-- 报废仓 结束 -->
              <!-- 审核流程 开始 -->
              <a-col :span="8">
                <a-form-model-item
                  :label="$t('CHECK_PROCESS')"
                  prop="checkProcessId"
                >
                  <SelectSearch
                    :show-multiple="false"
                    disabled
                    query-list-key="checkProcessName"
                    request-api="/api/epm/ebs/epm-check-process/search?checkProcessTypeList=EQP_SCRAP_CHECK"
                    option-label="checkProcessName"
                    :default-selected-keys="form.checkProcessId"
                    :default-selected-names="form.checkProcessName"
                    :search-params="searchParams"
                  />
                </a-form-model-item>
              </a-col>
              <!-- 审核流程 结束 -->
            </a-row>
            <a-row v-if="form.hasOwnProperty('remark')" :gutter="21">
              <!-- 备注 开始 -->
              <a-col :span="18">
                <a-form-model-item
                  :label="$t('REMARK')"
                  :label-col="{ span: 3 }"
                  :wrapper-col="{ span: 19 }"
                  prop="remark"
                >
                  <a-textarea
                    v-model="form.remark"
                    disabled
                    :rows="2"
                    :placeholder="$t('PLACEENTER')"
                    onkeyup="value=value.slice(0, 50)"
                  />
                </a-form-model-item>
              </a-col>
              <!-- 备注 结束 -->
            </a-row>
          </a-collapse-panel>
        </a-collapse>
        <a-collapse
          v-model="activeKeyB"
          expand-icon-position="right"
          class="collapse-header-divide"
        >
          <a-collapse-panel key="b" :header="`${$t('EQUIPMENT_LIST')}`">
            <MomTable
              ref="MomTable"
              row-key="itemName"
              :action-buttons="actionButtons"
              :columns="columns"
              :mega-extend="{
                rowAction: false,
              }"
              :data-source="eqpList"
            >
              <template slot="assetsStatus" slot-scope="text, record">
                <span class="status-box" :class="text">{{
                  getAttrValue(assetsStatusList, record.assetsStatus)
                }}</span>
              </template>
            </MomTable>
          </a-collapse-panel>
          <!-- 扩展字段 -->
          {{
            fieldExRenderFn($createElement, {
              layout: "grid",
              itemSpan: 8,
              disabled: true,
            })
          }}
          <slot name="RenderFieldExtendDom"></slot>
        </a-collapse>
      </a-form-model>
    </div>
    <div class="page-bottom-op">
      <a-button @click="handleCancel()">
        <i class="iconfont icon-quxiao" />
        {{ $t("BACK") }}
      </a-button>
    </div>
  </div>
</template>

<script lang="js">
import { mapGetters, mapState } from "vuex";
import { deleteLeafArray } from "@mega-apps/utils";
import ApiConstant from "@/api/ApiConstant";
import breadcrumb from "@/components/breadcrumb/index.vue";
import FieldExtendMixin from "@/mixins/fieldExtend";

export default {
  components: { breadcrumb },
  mixins: [FieldExtendMixin],
  data() {
    return {
      breadcrumbData: [
        {
          name: this.$t("MOLD_TYPE"),
          url: "/eam/mold_management/mold_type",
        },
        { name: this.$t("EQUIPMENT_TYPE"), url: "" },
      ],
      activeKeyA: "a",
      activeKeyB: "b",
      confirmLoading: false,
      actionButtons: [],
      form: {
        id: null,
        code: "",
        name: "",
        locationFirstIdArr: [], // 报废仓
        checkProcessId: "", // 工作流
        remark: "",
      },
      // 申请部门
      treeData: [],
      treeProps: {
        value: "id",
        children: "children",
        label: "label",
      },
      selectedUser: [{ name: "" }],
      typeList: [],
      assetsStatusList: [],
      columns: [
        {
          dataIndex: "eqpCode", // 设备编码
          sorter: false,
          ellipsis: true,
          title: () => this.$t("EPMS_EQP_CODE"),
        },
        {
          dataIndex: "eqpName", // 设备名称
          title: () => this.$t("EQP_NAME"),
          sorter: false,
          ellipsis: true,
        },
        {
          dataIndex: "eqpTypeName", // 设备类型
          align: "left",
          sorter: false,
          ellipsis: true,
          width: 180,
          title: () => this.$t("EQP_TYPE"),
        },
        {
          dataIndex: "assetsCode", // 资产编码
          sorter: false,
          ellipsis: true,
          title: () => this.$t("EPMS_ASSETS_CODE"),
        },
        {
          dataIndex: "assetsStatus", // 资产状态
          sorter: false,
          ellipsis: true,
          width: 180,
          title: () => this.$t("ASSETS_STATUS"),
          scopedSlots: { customRender: "assetsStatus" },
        },
        {
          dataIndex: "eqpModel", // 规格型号
          sorter: false,
          ellipsis: true,
          width: 180,
          title: () => this.$t("EQP_MODEL"),
        },
        {
          dataIndex: "ownerName", // 设备负责人
          sorter: false,
          ellipsis: true,
          title: () => this.$t("LEADER_NAME"),
        },
        {
          dataIndex: "location", // 位置
          sorter: false,
          ellipsis: true,
          width: 360,
          title: () => this.$t("CURRENT_LOCATION"),
        },
      ],
      eqpList: [], // 设备列表
      searchParams: {
        deptId: "",
        checkProcessTypeList: "MOLD_SCRAP_CHECK",
      },
      fieldNames: { label: "label", value: "code", children: "children" },
      currentOptions: [],
      tableNameList: [
        {
          key: "epm_eqp_scrap",
          tab: "设备报废",
        },
      ],
      dataAttr: {
        form: "form",
      },
    };
  },
  computed: {
    ...mapState("setting", ["multiPage"]),
    ...mapGetters(["modules"]),
    rules() {
      return {
        name: [
          {
            required: true,
            message: `${this.$t("PLACEENTER")}`,
            trigger: "blur",
          },
        ],
        locationFirstIdArr: [
          {
            required: true,
            message: () => this.$t("PLACESELECT"),
            trigger: "change",
          },
        ],
        checkProcessId: [
          {
            required: true,
            message: () => this.$t("PLACESELECT"),
            trigger: "change",
          },
        ],
      };
    },
  },
  mounted() {
    const { id } = this.$route.query;
    if (id) {
      this.$axios
        .get(
          `${ApiConstant.PREFIX_CORE}/api/epm/eam/eqp-scrap/get-by-id/${id}`
        )
        .then((res) => {
          this.form = { ...res?.data?.data };
          const { eqpScrapItemBPOList, whId, whCateId, locationId } = { ...res?.data?.data };
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          this.form.locationFirstIdArr = [
            "WAREHOUSE",
            whId,
            whCateId,
            locationId,
          ];
          this.eqpList = [...eqpScrapItemBPOList];
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          this.queryFieldExtend().then(() => {});
        });
    } else {
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      this.queryFieldExtend().then(() => {});
    }
    // 初始化数据
    this.init();
  },
  methods: {
    // 初始化数据，获取数据字典数据
    init() {
      return Promise.all([
        this.$axios.get(
          `${ApiConstant.PREFIX_CORE}/api/epm/eam/eqp/location-tree`
        ), // 获取当前位置的三级树
        this.$axios.get(`${ApiConstant.PREFIX_CORE}/api/sys/org/tree`), // 部门树结构
        this.$axios.get(
          `${ApiConstant.PREFIX_CORE}/api/sys/dict-data/DIC:EPM:ASSETS_STATUS`
        ), // 资产状态
      ]).then(([treeRes, orgTreeRes, assetsStatusRes]) => {
        deleteLeafArray(treeRes.data.data.lineTree, "children");
        deleteLeafArray(treeRes.data.data.warehouseTree, "children");
        this.currentOptions = treeRes.data.data.warehouseTree;
        this.treeData = orgTreeRes.data.data;
        this.assetsStatusList = assetsStatusRes.data.data;
      });
    },
    // 监听取消点击事件
    handleCancel() {
      this.$store.dispatch("tagsView/jumpBeforeView", {
        to: {
          path: "/eam/account_management/equipment_scrap",
        },
        vm: this,
      });
    },
  },
};
</script>
