<template>
  <div>
    <MomTable
      ref="MomTable"
      :row-selection="rowSelection"
      :columns="tableColumns"
      :data-source="tableData"
      :query-param="queryForm"
      :query-param-item="queryFormItem"
      row-key="id"
      :page.sync="page"
      :total="total"
      :action-buttons="actionButtons"
      :show-enable-flag="true"
      @search="searchPageData"
      @edit="handEdit"
      @delete="handDelete"
      @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'
          )
      "
    >
      <template slot="toolbar">
        <a-button v-if="hasAuth('BUSINESS_COMPONENT:ADD')" @click="handleAdd">
          <a-icon type="plus" /> {{ $t("ADD") }}
        </a-button>
        <a-button
          v-if="hasAuth('BUSINESS_COMPONENT:BATCH_DELETE')"
          type="danger"
          icon="delete"
          @click="handleDelete"
        >
          {{ $t("BATCH_DELETE") }}
        </a-button>
      </template>
      <div slot="category" slot-scope="text, record">
        <span>{{ formatData(flowList, record.category) }}</span>
      </div>
      <div slot="source" slot-scope="text, record">
        <span>{{ formatData(sourceList, record.source) }}</span>
      </div>
      <div slot="enableFlag" slot-scope="text, record">
        <span v-if="record.enableFlag == 'Y'" style="color: #0fd19c">{{
          formatData(stateList, record.enableFlag)
        }}</span>
        <span v-else style="color: #d45f5f">{{
          formatData(stateList, record.enableFlag)
        }}</span>
      </div>
    </MomTable>
  </div>
</template>

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

export default {
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      stateList: [],
      flowList: [],
      sourceList: [],
      page: 1, // 页码
      total: 0, // 总条数
      dialogVisble: false, // 弹框的显示 隐藏
      // 查询的表单的配置项
      queryForm: {
        componentName: "",
        componentKey: "",
        category: "",
      },
      actionButtons: [
        this.hasAuth("BUSINESS_COMPONENT:EDIT") && "edit",
        this.hasAuth("BUSINESS_COMPONENT:DELETE") && "delete",
      ],
      selectedRowKeys: [],
    };
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = selectedRowKeys;
        },
      };
    },
    // 表格筛选项
    queryFormItem() {
      return this.$observable("queryFormItem", {
        componentName: {
          label: () => this.$t("COMPONENT_NAME"),
        },
        componentKey: {
          label: () => this.$t("COMPONENT_IDENTIFICATION"),
        },
        category: {
          itemType: "select",
          label: () => this.$t("TAGCATEGORY_LABEL"),
          labelWidth: "90",
          optionsProps: {
            label: "label",
            value: "value",
          },
          options: [],
        },
      });
    },
    // 表格配置项
    tableColumns() {
      return [
        {
          dataIndex: "componentName",
          title: () => this.$t("COMPONENT_NAME"),
          width: 200,
          sorter: false,
        },
        {
          dataIndex: "componentKey",
          title: () => this.$t("COMPONENT_IDENTIFICATION"),
          options: [],
          sorter: false,
          minWidth: 240,
        },
        {
          dataIndex: "category",
          title: () => this.$t("TAGCATEGORY_LABEL"),
          scopedSlots: { customRender: "category" },
          width: 200,
        },
        {
          key: "enableFlag",
          title: () => this.$t("STATUS"),
          scopedSlots: { customRender: "enableFlag" },
          sorter: false,
          width: 200,
        },
        {
          dataIndex: "componentClass",
          title: () => this.$t("COMPONENT_CLASS_NAME"),
          sorter: false,
          width: 200,
        },
        {
          dataIndex: "remark",
          title: () => this.$t("REMARK"),
          sorter: false,
          width: 220,
        },
        {
          dataIndex: "source",
          title: () => this.$t("COMPONENT_SOURCE"),
          scopedSlots: { customRender: "source" },
          sorter: false,
          width: 200,
        },
        {
          dataIndex: "updateBy",
          title: () => this.$t("UPDATE_BY"),
          sorter: false,
          width: 220,
        },
        {
          dataIndex: "updateTime",
          title: () => this.$t("UPDATE_TIME"),
          width: 220,
        },
      ];
    },
  },
  mounted() {
    // 初始化字典数据
    this.initDictData();
  },
  methods: {
    // 初始化字典数据
    initDictData() {
      Promise.all([
        this.getDictByCode("DIC:COMMON:ENABLE_FLAG"),
        this.getDictByCode("DIC:SYS:FLOW_CATEGORY"),
        this.getDictByCode("DIC:SYS:FLOW_SOURCE"),
      ]).then(([statusRes, flowRes, sourceRes]) => {
        this.stateList = statusRes.data.data;
        this.flowList = flowRes.data.data;
        this.queryFormItem.category.options = flowRes.data.data;
        this.sourceList = sourceRes.data.data;
      });
    },
    handleAdd() {
      this.$router.push({
        path: "/upms/operationFlow/businessComponent/add",
      });
    },
    handEdit(row) {
      this.$router.push({
        path: "/upms/operationFlow/businessComponent/edit",
        query: {
          id: row.id,
        },
      });
    },
    // 启用、禁用
    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/sys/flow-component/enable/${id}/${status}`
            )
            .then((res) => {
              this.$message.success(this.$t(tips));
              this.$refs.MomTable?.query();
            });
        },
      });
    },
    handleDelete() {
      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/sys/flow-component/batch-delete`,
                {
                  params: {
                    ids: this.selectedRowKeys.join(","),
                  },
                }
              )
              .then((res) => {
                this.$message.success(this.$t("SUBMITTED_FOR_DELETION"));
                this.$refs.MomTable?.query();
              });
          },
        });
      } else {
        this.$message.error(this.$t("SELECT_ONE_DATA"));
      }
    },
    handDelete({ 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/sys/flow-component/${id}`)
            .then((res) => {
              this.$message.success(this.$t("SUBMITTED_FOR_DELETION"));
              this.$refs.MomTable?.query();
            });
        },
      });
    },
    // 后台分页查询
    searchPageData(params) {
      this.$axios
        .get(`${ApiConstant.PREFIX_CORE}/api/sys/flow-component/search`, {
          params,
        })
        .then((res) => {
          this.tableData = res.data.data.records;
          this.total = res.data.data.total;
        });
    },
    formatData(data, status) {
      const result = data?.find((item) => item.value === status?.toString());
      return result ? result.label : "";
    },
  },
};
</script>
