import { queryAllRoleUsingPost } from '@/services/pop-b2b2c/pbcRoleController';
import {
  pbcUsersPageUsingPost,
} from '@/services/pop-b2b2c/pbcUsersController';
import { handlePageQuery } from '@/utils/utils';
import { ActionType, ProColumns, ProTable } from '@ant-design/pro-components';
import { PageContainer } from '@ant-design/pro-layout';
import React, { useRef } from 'react';

/**
 * 查询表格
 * @param param0
 */
const fetchData = async (params: API.PageVO) => {
  const msg = await pbcUsersPageUsingPost(params);
  return {
    data: msg.data?.records,
    total: msg.data?.total,
    success: msg.retcode,
  } as any;
};

// eslint-disable-next-line @typescript-eslint/ban-types
const TableList: React.FC<{}> = () => {
  const actionRef = useRef<ActionType>();

  const columns: ProColumns<API.PbcUsers>[] = [
    {
      title: '人员姓名',
      dataIndex: 'pbcUserNickName'
    },
    {
      title: '手机号',
      dataIndex: 'pbcUserMobile',
      search: false
    },
    {
      title: '角色',
      dataIndex: 'pbcUserRole',
      valueType: 'select',
      request: async () => {
        const msg = await queryAllRoleUsingPost();
        if (msg.retcode && msg.data) {
          return msg.data.map((e) => {
            return {
              label: e.roleName,
              value: e.pbcId,
            };
          });
        }
        return [];
      },
    },
    {
      title: '注册商家数',
      dataIndex: 'pbcUserMobile',
      search: false
    },
    {
      title: '上传商品数',
      dataIndex: 'pbcUserMobile',
      search: false
    },
  ];
  return (
    <PageContainer
      header={{
        title: '',
        breadcrumb: {},
      }}
    >
      <ProTable<API.PbcUsers>
        columns={columns}
        actionRef={actionRef}
        request={(param: any) => {
          console.log(param)
          let hasUserType = false
          if (param.pbcUserType) {
            param.pbcUserType = parseInt(param.pbcUserType)
            hasUserType = true
          }
          const queryParam = handlePageQuery(param);
          if (!hasUserType && queryParam.filters) {
            queryParam.filters.push({
              key: 'pbcUserType'.replace(/([A-Z])/g, '_$1').toLowerCase(),
              value: 1,
              action: '<=',
            });
          }
          return fetchData(queryParam);
        }}
        rowKey="pbcId"
        size="small"
        bordered
        search={{
          labelWidth: 'auto',
          span: 6,
        }}
        pagination={{
          defaultPageSize: 20,
          showSizeChanger: true,
        }}
        scroll={{
          y: 'calc(100vh - 320px)',
        }}
        dateFormatter="string"
        options={false}
        toolBarRender={() => []}
      />
    </PageContainer>
  );
};

export default TableList;
