<template>
  <PageWrapper dense fixedHeight contentFullHeight>
    <SupplierInfo :id="recordId" @return-page="isView = false" v-if="isView" />
    <BasicTable @register="registerTable" v-else>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'type' && !!record.type">
          <a-tag
            v-for="(item, index) in record.type.split(',')"
            :key="index"
            :color="getType(item, false)"
          >
            {{ getType(item, true) }}
          </a-tag>
        </template>
        <template v-if="column.dataIndex == 'action'">
          <a-button
            type="link"
            class="actionTxt"
            v-auth="'eliminate:edit'"
            @click="handleEdit(record)"
          >
            编辑
          </a-button>
          <a-button type="link" class="actionTxt" @click="handleView(record)">详情查看</a-button>
          <a-button type="link" class="actionTxt" @click="handleRegain(record)">恢复</a-button>
          <a-button
            type="link"
            class="actionTxt"
            v-auth="'eliminate:delete'"
            @click="handleDelete(record)"
          >
            删除
          </a-button>
        </template>
      </template>
    </BasicTable>
    <PotentialModal @register="registerModal" @success="reload" />
    <RegainModal @register="registerRegainModal" @success="reload" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { createVNode, ref, onMounted } from 'vue';
  import { BasicTable, useTable, BasicColumn, FormSchema } from '/@/components/Table';

  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { PageWrapper } from '/@/components/Page';
  import { getSupplierPageList, deleteSupplier } from '/@/api/erp/supplier/list';
  import { getDicDetailList } from '/@/api/system/dic';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import PotentialModal from './components/PotentialModal.vue';
  import RegainModal from './components/RegainModal.vue';
  import SupplierInfo from './components/SupplierInfo.vue';
  import { usePermission } from '/@/hooks/web/usePermission';

  const { t } = useI18n();
  const columns: BasicColumn[] = [
    {
      title: '供应商编码',
      dataIndex: 'number',
      resizable: true,
    },
    {
      title: '供应商名称',
      dataIndex: 'name',
      resizable: true,
    },
    {
      title: '经营范围',
      dataIndex: 'scope',
      resizable: true,
    },
    {
      title: '负责人',
      dataIndex: 'person',
      resizable: true,
    },
    {
      title: '手机号',
      dataIndex: 'phone',
      resizable: true,
    },
    {
      title: '供应商类型',
      dataIndex: 'type',
      width: 220,
      resizable: true,
    },
  ];

  const searchFormSchema: FormSchema[] = [
    {
      field: 'name',
      label: '供应商名称',
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请输入供应商名称',
      },
    },
    {
      field: 'type',
      label: '供应商类别',
      component: 'XjrSelect',
      colProps: { span: 8 },
      componentProps: {
        datasourceType: 'dic',
        params: { itemId: '1676483934342787074' },
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择供应商类别',
        getPopupContainer: () => document.body,
      },
    },
  ];

  const { notification } = useMessage();
  const { hasPermission } = usePermission();

  const [registerModal, { openModal }] = useModal();
  const [registerRegainModal, { openModal: openRegainModal }] = useModal();

  const recordId = ref('');
  const isView = ref(false);

  const typeOptions = ref<Recordable[]>([]);

  const [registerTable, { reload }] = useTable({
    title: '淘汰供应商',
    api: getSupplierPageList,
    rowKey: 'id',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
    },
    beforeFetch: (params) => {
      return { ...params, state: 2 };
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    actionColumn: {
      width: 220,
      title: t('操作'),
      dataIndex: 'action',
    },
    customRow: (record) => {
      return {
        ondblclick: () => {
          if (hasPermission('eliminate:edit')) {
            handleEdit(record);
          }
        },
      };
    },
    tableSetting: {
      size: false,
    },
  });

  onMounted(async () => {
    typeOptions.value = await getDicDetailList({ itemId: '1676483934342787074' });
  });

  const getType = (type, isText) => {
    const data = typeOptions.value.filter((x) => x.id === type);
    if (!data.length) return '';
    if (isText) return data[0].name;
    switch (data[0].name) {
      case '原料':
        return 'processing';
      case '成品':
        return 'error';
      case '半成品':
        return 'success';
      case '其它':
        return 'warning';
    }
  };

  const handleEdit = (record) => {
    openModal(true, {
      id: record.id,
      isUpdate: true,
      title: '淘汰',
    });
  };

  const handleRegain = (record) => {
    openRegainModal(true, {
      id: record.id,
      name: record.name,
      number: record.number,
    });
  };

  const handleView = (record) => {
    isView.value = true;
    recordId.value = record.id;
  };

  const handleDelete = (record) => {
    Modal.confirm({
      title: t('提示信息'),
      icon: createVNode(ExclamationCircleOutlined),
      content: t('是否确认删除？'),
      okText: t('确认'),
      cancelText: t('取消'),
      async onOk() {
        await deleteSupplier(record.id);
        notification.success({
          message: t('提示'),
          description: t('删除成功'),
        });
        reload();
      },
      onCancel() {},
    });
  };
</script>
<style lang="less" scoped>
  .actionTxt {
    padding: 0 7px;

    &:last-child {
      color: #f56c6c;
    }
  }

  :deep(.ant-tag) {
    margin-right: 5px;
  }
</style>
