import {
  MyButtons,
  MyColumns,
  MyImportModal,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  HouseOccupantsCardTypeEnum,
  HouseOccupantsResidentialRelationEnum,
  HouseOccupantsStatusEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';

export default function Index({ title = '客户列表' }) {
  const navigate = useNavigate();
  const maskFirst3Last4 = (value?: string) => {
    const s = value ? String(value) : '';
    if (!s) return '-';
    if (s.length <= 11) return `${s.slice(0, 3)}${'*'.repeat(4)}${s.slice(-4)}`;
    return `${s.slice(0, 4)}${'*'.repeat(4)}${s.slice(-6)}`;
  };

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="customer-list"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle={title}
        request={async (params, sort) =>
          MyProTableProps.request(
            { ...params, status: HouseOccupantsStatusEnum.Normal.value },
            sort,
            Apis.Archive.HouseOccupants.List,
          )
        }
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              import: (
                <MyImportModal
                  key="ImportHouse"
                  title="批量导入"
                  type="default"
                  size="middle"
                  templateApi={Apis.Archive.HouseOccupants.DownloadTemplate}
                  importApi={Apis.Archive.HouseOccupants.Import}
                  reload={action?.reload}
                />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '项目名称',
            dataIndex: ['asset_project', 'name'],
            search: {
              transform: (value) => {
                return { project_name: value };
              },
            },
          },
          {
            title: '房屋名称',
            dataIndex: ['asset_house', 'full_name'],
            ellipsis: true,
            // search: {
            //   transform: (value) => {
            //     return { house_name: value };
            //   },
            // },
            search: false,
          },
          {
            title: '姓名',
            dataIndex: 'name',
          },
          {
            title: '电话  ',
            dataIndex: 'phone',
            render: (text: any) => maskFirst3Last4(text),
          },
          MyColumns.EnumTag({
            title: '证件类型',
            dataIndex: 'card_type',
            valueEnum: HouseOccupantsCardTypeEnum,
            search: false,
          }),
          {
            title: '证件号码  ',
            dataIndex: 'id_card',
            render: (text: any) => maskFirst3Last4(text),
          },
          MyColumns.EnumTag({
            title: '关联身份',
            dataIndex: 'residential_relation',
            valueEnum: HouseOccupantsResidentialRelationEnum,
            search: false,
          }),
          MyColumns.Option({
            render: (_, item: any) => (
              <MyTableActions
                actions={{
                  view: (
                    <MyButtons.View
                      title="查看"
                      onClick={() => {
                        navigate(
                          `/customer/archive/show/${item.asset_houses_id}`,
                        );
                      }}
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
