import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  CompanySuppliersCounterpartyEnum,
  CompanySuppliersGradeEnum,
  CompanySuppliersSupplierTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import dayjs from 'dayjs';
import AddBlack from './modals/AddBlack';
import AddGrade from './modals/AddGrade';
import Create from './modals/Create';
import Update from './modals/Update';

export default function Index({ title = '供应商' }) {
  const navigate = useNavigate();

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="company_suppliers_list"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              counterparty: CompanySuppliersCounterpartyEnum.Supplier.value,
            },
            sort,
            Apis.CompanySupplier.CompanySuppliers.List,
          )
        }
        toolBarRender={(action) => [
          <Create key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          // MyColumns.ID({ search: false }),
          {
            title: '供应商名称',
            dataIndex: 'name',
          },
          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'supplier_type',
            valueEnum: CompanySuppliersSupplierTypeEnum,
          }),
          // MyColumns.EnumTag({
          //   title: '证件类型',
          //   dataIndex: 'certificate_type',
          //   valueEnum: CompanySuppliersCertificateTypeEnum,
          //   search: false,
          // }),
          {
            title: '所在城市',
            search: false,
            render: (_, item: any) => {
              return `${item.city || ''} `;
            },
            // render: (_, item: any) => {
            //   return `${item.province} ${item.city || ''} ${item.area || ''} ${
            //     item.street || ''
            //   }${item.address || ''}`;
            // },
          },
          MyColumns.EnumTag({
            title: '等级',
            dataIndex: 'grade',
            valueEnum: CompanySuppliersGradeEnum,
          }),
          {
            title: '上一次评级',
            dataIndex: 'grade_time',
            render: (_, item: any) => {
              if (!item.grade_time) return '';
              return dayjs(item.grade_time).format('YYYY-MM-DD');
            },
            search: false,
          },
          //是否黑名单
          MyColumns.Boolean({
            dataIndex: 'is_blacklisted',
            title: '是否拉黑',
          }),

          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyButtons.View
                  title="查看"
                  onClick={() => {
                    navigate(`/supplier/outside/show/${item.id}`);
                  }}
                />
                <Update item={item} reload={action?.reload} title={title} />
                <AddGrade item={item} reload={action?.reload} title={title} />
                {!item.is_blacklisted && (
                  <AddBlack item={item} reload={action?.reload} title={title} />
                )}
                {item.is_blacklisted === 1 && (
                  <MyButtons.Default
                    title="设白名单"
                    isConfirm={true}
                    onConfirm={() =>
                      Apis.CompanySupplier.CompanySuppliers.Update({
                        id: item.id ?? 0,
                        is_blacklisted: false,
                        counterparty: item.counterparty ?? 0,
                        supplier_type: item.supplier_type ?? 0,
                        name: item.name ?? '',
                        reason: '',
                      }).then(() => action?.reload())
                    }
                  />
                )}
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.CompanySupplier.CompanySuppliers.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
