<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('APPLY_DEPT')" prop="applyDeptId">
                  <a-tree-select
                    v-model="form.applyDeptId"
                    disabled
                    :replace-fields="treeProps"
                    tree-check-strictly
                    :tree-data="treeData"
                    allow-clear
                  />
                </a-form-model-item>
              </a-col>
              <!-- 部门 结束 -->
              <!-- 申请人 开始 -->
              <a-col :span="8">
                <a-form-model-item :label="$t('APPLY_USER')" prop="applyUserId">
                  <a-input
                    v-model="form.applyUserName"
                    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"
                    option-label="checkProcessName"
                    :default-selected-keys="form.checkProcessId"
                    :default-selected-names="form.checkProcessName"
                    :search-params="searchParams"
                  >
                  </SelectSearch>
                </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('MOLD_LIST')}`">
            <MomTable
              ref="MomTable"
              row-key="itemName"
              :action-buttons="actionButtons"
              :columns="columns"
              :mega-extend="{
                rowAction: false,
              }"
              :data-source="moldList"
            >
            </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="ts">
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
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: "",
        applyUserId: "",
        locationFirstIdArr: [], // 报废仓
        checkProcessId: "", // 工作流
        remark: "",
      },
      // 申请部门
      treeData: [],
      treeProps: {
        value: "id",
        children: "children",
        label: "label",
      },
      selectedUser: [{ name: "" }],
      typeList: [],
      columns: [
        {
          dataIndex: "moldCode", // 编码
          title: () => this.$t("MOLD_CODE"),
          ellipsis: true,
          width: 200,
        },
        {
          dataIndex: "moldName", // 名称
          title: () => `${this.$t("MOLD")}${this.$t("NAME")}` as string,
          ellipsis: true,
          scopedSlots: { customRender: "moldName" },
        },
        {
          dataIndex: "moldTypeName", // 模具类型名称
          title: () => `${this.$t("MOLD_TYPE")}${this.$t("NAME")}`,
          ellipsis: true,
        },
        {
          dataIndex: "moldHoleNum", // 模具穴数
          title: () => this.$t("NUMBER_OF_ACUPOINTS"),
          ellipsis: true,
        },
        {
          dataIndex: "manageType",
          align: "left",
          width: 150,
          ellipsis: true,
          title: () => this.$t("MOLD_CONTROL_MODE"), // 管控方式
          scopedSlots: { customRender: "manageType" },
        },
        {
          dataIndex: "initValue", // 当次初始时间/次数
          title: () => this.$t("INITIAL_VALUE_OF_CURRENT_USE"),
          sorter: false,
          ellipsis: true,
          scopedSlots: { customRender: "initValue" },
        },
        {
          dataIndex: "totalInitValue", // 历次累积时间/次数
          title: () => this.$t("INITIAL_VALUE_OF_PREVIOUS_USE"),
          sorter: false,
          ellipsis: true,
          scopedSlots: { customRender: "totalInitValue" },
        },
        {
          dataIndex: "enableFlag",
          align: "left",
          width: 100,
          ellipsis: true,
          title: () => this.$t("STATUS"), // 状态
          scopedSlots: { customRender: "enableFlag" },
        },
        {
          dataIndex: "updateBy", // 更新人员
          ellipsis: true,
          title: () => this.$t("UPDATE_BY"),
          width: 120,
        },
        {
          dataIndex: "updateTime", // 更新时间
          ellipsis: true,
          title: () => this.$t("UPDATE_TIME"),
          width: 200,
        },
      ],
      moldList: [], // 模具列表
      searchParams: {
        deptId: "",
        checkProcessTypeList: "MOLD_SCRAP_CHECK",
      },
      fieldNames: { label: "label", value: "code", children: "children" },
      currentOptions: [
        // {
        //   code: "LINE",
        //   label: "车间",
        //   children: [],
        // },
        {
          code: "WAREHOUSE",
          label: "仓库",
          children: [],
        },
      ],
      tableNameList: [
        {
          key: "epm_mold_scrap",
          tab: "模具报废",
        },
      ],
      dataAttr: {
        form: "form",
      },
    };
  },
  computed: {
    ...mapState("setting", ["multiPage"]),
    ...mapGetters(["modules"]),
    rules(): object {
      return {
        name: [
          {
            required: true,
            message: `${this.$t("PLACEENTER")}`,
            trigger: "blur",
          },
        ],
        applyUserId: [
          {
            required: true,
            message: () => this.$t("PLACESELECT"),
            trigger: "change",
          },
        ],
        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/mam/mold-scrap/get-by-id/${id}`
        )
        .then((res) => {
          this.form = { ...res?.data?.data };
          this.selectedUser = [
            { name: res?.data?.data?.applyUserName as string },
          ];
          const {
            detailList = [],
            whId = "",
            whCateId = "",
            locationId = "",
          } = { ...res?.data?.data };
          /* eslint-disable */
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          this.form.locationFirstIdArr = [
            "WAREHOUSE",
            whId,
            whCateId,
            locationId,
          ] as any;
          this.moldList = [...detailList] as any;
          // 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/sys/dict-data/DIC:EPM:MOLD_MANAGE_TYPE`
        ), // 模具管控方式
        this.$axios.get(
          `${ApiConstant.PREFIX_CORE}/api/epm/eam/eqp/location-tree`
        ), // 获取当前位置的三级树
        this.$axios.get(`${ApiConstant.PREFIX_CORE}/api/sys/org/tree`), // 部门树结构
      ]).then(([typeRes, treeRes, orgTreeRes]) => {
        this.typeList = typeRes.data.data;
        deleteLeafArray(treeRes.data.data.lineTree, "children");
        deleteLeafArray(treeRes.data.data.warehouseTree, "children");
        // this.currentOptions[0].children = treeRes.data.data.lineTree;
        this.currentOptions[0].children = treeRes.data.data.warehouseTree;
        this.treeData = orgTreeRes.data.data;
      });
    },
    // 获取选择用户组件返回数据
    getReturnData(
      selectedKeys: [],
      selectedRows: [{ name: string; userName: string }]
    ) {
      this.form.applyUserId = selectedRows[0]?.userName;
      this.selectedUser = selectedRows;
    },
    // 监听取消点击事件
    handleCancel() {
      this.$store.dispatch("tagsView/jumpBeforeView", {
        to: {
          path: "/eam/mold_management/mold_scrap",
        },
        vm: this,
      });
    },
  },
};
</script>
