<template>
  <div class="flex flex-col h-full">
    <div v-if="perm.BUTTON_Add" class="page-action page-header">
      <t-button theme="primary" @click="handleAdd">
        <template #icon><svg-icon name="add-circle" color="#fff" /></template
        >新增供应商
      </t-button>
    </div>
    <div class="flex-1 page-wrap">
      <t-table
        size="small"
        row-key="id"
        :columns="columns"
        :data="tableData"
        bordered
        :pagination="{
          defaultCurrent: 1,
          defaultPageSize: 10,
          onChange,
          showJumper: true,
          showPageSize: false,
          total: pagination.total,
        }"
        v-loading="tableLoading"
      >
        <template #type="{ row }">
          {{ supplierTypeFilter(row.type) }}
        </template>
        <template #enable="{ col, row }">
          <status-tag :value="row[col.colKey]" type="enable"></status-tag>
        </template>
        <template #operate="{ row }">
          <div class="table-operations">
            <t-link
              v-if="perm.LINK_Update"
              theme="primary"
              hover="color"
              @click="handleEdit(row)"
            >
              修改
            </t-link>
            <t-link
              v-if="perm.LINK_Enable"
              theme="primary"
              hover="color"
              @click="handleEnable(row)"
              >{{ enableFilter(!row.enable) }}</t-link
            >
          </div>
        </template>
      </t-table>
    </div>

    <!-- EditSupplierDialog -->
    <edit-supplier-dialog
      v-if="perm.LINK_Update || perm.BUTTON_Add"
      v-model:visible="showEditSupplierDialog"
      :curRow="curRow"
      @success="fetchData"
    ></edit-supplier-dialog>
  </div>
</template>

<script setup name="SupplierManage">
import { ref } from 'vue';
import { MessagePlugin } from 'tdesign-vue-next';
import useFetchTable from '@/hooks/useFetchTable';
import EditSupplierDialog from './edit-supplier-dialog.vue';
import { supplierQueryApi, supplierEnableApi } from '@/api/system';
import { enableFilter, supplierTypeFilter } from '@/utils/filter';
import usePermission from '@/hooks/usePermission';
const { perm } = usePermission();

const showEditSupplierDialog = ref(false);
const curRow = ref(null);

const columns = [
  { colKey: 'code', title: '供应商编号' },
  { colKey: 'name', title: '供应商名称' },
  { colKey: 'type', title: '类型', cell: 'type', width: 120 },
  { colKey: 'remark', title: '备注' },
  { colKey: 'enable', title: '启用/禁用', cell: 'enable', width: 120 },
  {
    title: '管理',
    colKey: 'operate',
    fixed: 'right',
    width: 120,
  },
];
const {
  loading: tableLoading,
  pagination,
  tableData,
  fetchData,
  onChange,
} = useFetchTable(supplierQueryApi);

const handleAdd = () => {
  curRow.value = null;
  showEditSupplierDialog.value = true;
};
const handleEdit = (row) => {
  curRow.value = row;
  showEditSupplierDialog.value = true;
};
const handleEnable = async (row) => {
  const res = await supplierEnableApi(row.id).catch(() => {});

  if (!res) return;
  MessagePlugin.success('操作成功');
  row.enable = !row.enable;
};
</script>
