import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import Create from './modals/Create';
import Update from './modals/Update';

export default function Index({ title = '门禁卡' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="company_internal_list"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
            },
            sort,
            Apis.HouseCharge.HouseDoorCards.List,
          )
        }
        headerTitle={`${title}列表`}
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              create: <Create key="Create" reload={action?.reload} title={title} />,
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({
            search: false,
          }),
          {
            title: '客户姓名',
            dataIndex: 'name',
          },
          {
            title: '客户电话',
            dataIndex: 'phone',
          },
          {
            title: '门禁卡编号',
            dataIndex: 'card_number',
          },
          {
            title: '申请时间',
            dataIndex: 'process_date',
            search: false,
          },
          {
            title: '工本费',
            dataIndex: 'labor_cost',
            search: false,
            render: (_, item: any) => {
              return `¥${item?.labor_cost}`;
            },
          },
          MyColumns.Boolean({
            title: '启/禁用',
            key: 'is_enable',
            dataIndex: 'is_enable',
          }),
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, __, action) => (
              <MyTableActions
                actions={{
                  update: (
                    <Update
                      item={item}
                      key="update"
                      reload={action?.reload}
                      title={title}
                    />
                  ),
                  delete: (
                    <MyButtons.Delete
                      key="delete"
                      onConfirm={() =>
                        Apis.HouseCharge.HouseDoorCards.Delete({
                          id: item.id,
                        }).then(() => action?.reload())
                      }
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
