<!-- 组件管理 -->
<template>
  <div class="component-mgmt-page">
    <MomTable
      ref="MomTable"
      row-key="id"
      :row-selection="rowSelection"
      :columns="tableColumns"
      :data-source="tableData"
      :query-param="queryParam"
      :query-param-item="queryFormItem"
      :action-buttons="actionButtons"
      :total="total"
      :page.sync="page"
      :show-enable-flag="hasAuth('COMPONENT_MGMT:ENABLED')"
      @beforeSearch="beforeSearch"
      @search="searchPageData"
      @edit="handleEdit"
      @delete="handleDelete"
      @enable="
        (record) =>
          handleStatus(
            { status: 'Y', id: record.id },
            'ENABLED',
            'ENABLED_DATA_INFO',
            'ENABLE_SUCCESS'
          )
      "
      @disable="
        (record) =>
          handleStatus(
            { status: 'N', id: record.id },
            'DISABLED',
            'DISABLED_DATA_INFO',
            'DISABLE_SUCCESS'
          )
      "
    >
      <a-row slot="toolbar" class="button-top-table">
        <a-button v-if="hasAuth('COMPONENT_MGMT:ADD')" @click="handleAdd">
          <a-icon type="plus" /> {{ $t("ADD") }}
        </a-button>
        <a-button
          v-if="hasAuth('COMPONENT_MGMT:BATCH_DELETE')"
          type="danger"
          icon="delete"
          @click="handDelete"
        >
          {{ $t("BATCH_DELETE") }}
        </a-button>
      </a-row>
      <template slot="enableFlag" slot-scope="text">
        <span :class="text === 'Y' ? 'text-success' : 'text-error'">
          {{ getAttrValue(enableFlagList, text) }}
        </span>
      </template>
    </MomTable>
  </div>
</template>

<script>
import ApiConstant from "@/api/ApiConstant";

export default {
  data() {
    return {
      enableFlagList: [],
      // 查询的表单的配置项
      queryParam: {
        componentName: undefined,
        componentType: undefined,
      },
      // 表格的配置项
      tableColumns: [
        {
          dataIndex: "componentCode",
          minWidth: 240,
          title: () => this.$t("COMPONENT_CODE"), // 组件编码
        },
        {
          dataIndex: "componentName",
          minWidth: 240,
          title: () => this.$t("COMPONENT_NAME"), // 组件名称
        },
        {
          dataIndex: "componentTypeText",
          width: 130,
          sorter: false,
          title: () => this.$t("COMPONENT_TYPE"), // 组件类型
        },
        {
          dataIndex: "remark",
          sorter: false,
          width: 280,
          ellipsis: true,
          title: () => this.$t("REMARK"), // 备注
        },
        {
          dataIndex: "enableFlag",
          sorter: false,
          width: 80,
          title: () => this.$t("STATUS"), // 状态
          scopedSlots: { customRender: "enableFlag" },
        },
        {
          dataIndex: "updateBy", // 更新人员
          title: () => this.$t("UPDATE_BY"),
          width: 120,
          sorter: false,
        },
        {
          dataIndex: "updateTime", // 更新时间
          title: () => this.$t("UPDATE_TIME"),
          width: 200,
        },
      ],
      actionButtons: [
        this.hasAuth("COMPONENT_MGMT:EDIT") && "edit",
        this.hasAuth("COMPONENT_MGMT:DELETE") && "delete",
      ],
      page: 1, // 页码
      total: 0, // 每页个数
      tableData: [],
      selectedRowKeys: [], // 选中的行Keys
    };
  },
  computed: {
    // 表格搜索配置
    queryFormItem() {
      return this.$observable("queryFormItem", {
        componentName: {
          itemType: "input",
          label: () => this.$t("COMPONENT_NAME"),
        },
        componentType: {
          itemType: "select",
          label: () => this.$t("COMPONENT_TYPE"),
          options: [],
          optionsProps: {
            label: "label",
            value: "value",
          },
        },
      });
    },
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = selectedRowKeys;
        },
      };
    },
  },
  methods: {
    beforeSearch() {
      return Promise.all([
        this.getDictByCode("DIC:IDE:COMPONENT_TYPE"), // 组件类型
        this.getDictByCode("DIC:COMMON:ENABLE_FLAG"), // 是否启用
      ]).then((res) => {
        this.queryFormItem.componentType.options = res[0]?.data?.data || [];
        this.enableFlagList = res[1]?.data?.data || [];
      });
    },
    // 后台分页查询
    searchPageData(params) {
      this.$axios
        .get(`${ApiConstant.PREFIX_CORE}/api/idewebcomponent/search`, {
          params,
        })
        .then((res) => {
          this.tableData = res.data.data.records.map((item) => ({
            ...item,
            componentTypeText: this.getAttrValue(
              this.queryFormItem.componentType.options,
              item.componentType
            ),
          }));
          this.total = res.data.data.total;
        });
    },
    // 启用、禁用
    handleStatus(record, titleText, contentText, tips) {
      this.$confirm({
        title: this.$t(titleText),
        content: this.$t(contentText),
        okText: this.$t("CONFIRM"),
        cancelText: this.$t("CANCEL"),
        centered: false,
        icon: "exclamation-circle",
        onOk: () => {
          const { id, status } = record;
          this.$axios
            .put(
              `${ApiConstant.PREFIX_CORE}/api/idewebcomponent/enable/${id}/${status}`
            )
            .then((res) => {
              this.$message.success(this.$t(tips));
              this.$refs.MomTable?.query();
            });
        },
      });
    },
    // 点击新增
    handleAdd() {
      this.$router.push({
        path: "/upms/devCenter/tools/component-mgmt/add",
      });
    },
    // 点击表格行内的编辑
    handleEdit(row) {
      this.$router.push({
        path: "/upms/devCenter/tools/component-mgmt/edit",
        query: {
          id: row.id,
        },
      });
    },
    handDelete() {
      if (this.selectedRowKeys.length > 0) {
        this.$confirm({
          title: this.$t("DELETE"),
          content: this.$t("DELETE_INFO"),
          okType: "warning",
          okText: this.$t("CONFIRM"),
          cancelText: this.$t("CANCEL"),
          centered: false,
          icon: "close-circle",
          onOk: () => {
            this.$axios
              .delete(
                `${ApiConstant.PREFIX_CORE}/api/idewebcomponent/batch-delete`,
                {
                  params: {
                    ids: this.selectedRowKeys.join(","),
                  },
                }
              )
              .then((res) => {
                this.$message.success(this.$t("DELETE_SUCCESS"));
                this.$refs.MomTable?.query();
                this.selectedRowKeys = [];
              });
          },
        });
      } else {
        this.$message.error(this.$t("SELECT_ONE_DATA"));
      }
    },
    // 点击表格行内的删除
    handleDelete({ id }) {
      this.$confirm({
        title: this.$t("DELETE"),
        content: this.$t("DELETE_INFO"),
        okType: "warning",
        okText: this.$t("CONFIRM"),
        cancelText: this.$t("CANCEL"),
        centered: false,
        icon: "close-circle",
        onOk: () => {
          this.$axios
            .delete(`${ApiConstant.PREFIX_CORE}/api/idewebcomponent/${id}`)
            .then((res) => {
              this.$message.success(this.$t("DELETE_SUCCESS"));
              this.$refs.MomTable?.query();
            });
        },
      });
    },
  },
};
</script>
