import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} 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 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) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              add: (
                <Create key="Create" reload={action?.reload} title={title} />
              ),
            }}
          />,
        ]}
        columns={[
          {
            title: '供应商名称',
            dataIndex: 'name',
          },

          {
            title: '所在城市',
            search: false,
            render: (_, item: any) => {
              return `${item.city || ''} `;
            },
          },
          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'supplier_type',
            valueEnum: CompanySuppliersSupplierTypeEnum,
          }),
          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, __, action) => (
              <MyTableActions
                actions={{
                  view: (
                    <MyButtons.View
                      title="查看"
                      onClick={() => {
                        navigate(`/supplier/outside/show/${item.id}`);
                      }}
                    />
                  ),
                  update: (
                    <Update item={item} reload={action?.reload} title={title} />
                  ),
                  grade: (
                    <AddGrade
                      item={item}
                      reload={action?.reload}
                      title={title}
                    />
                  ),
                  black: (
                    <AddBlack
                      item={item}
                      reload={action?.reload}
                      title={title}
                    />
                  ),
                  whitelist:
                    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())
                        }
                      />
                    ) : null,
                  delete: (
                    <MyButtons.Delete
                      onConfirm={() =>
                        Apis.CompanySupplier.CompanySuppliers.Delete({
                          id: item.id,
                        }).then(() => action?.reload())
                      }
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
