import { queryAllRoleUsingPost } from '@/services/pop-b2b2c/pbcRoleController';
import { exportOperationalDashboardUsingPost, operationalDashboardUsingPost } from '@/services/pop-b2b2c/pbcUserRecordLogController';
import { ActionType, ProColumns, ProFormInstance, ProTable } from '@ant-design/pro-components';
import { PageContainer } from '@ant-design/pro-layout';
import { Link } from '@umijs/max';
import { Button, message } from 'antd';
import React, { useRef } from 'react';

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

const handleExport = async (values?: API.PbcOperationalDashboardDTO) => {
  
  const hide = message.loading('正在处理', 0);
  try {
    await exportOperationalDashboardUsingPost(
      { ...values },
      {
        responseType: 'blob',
        getResponse: true,
        parseResponse: false,
        data: { ...values, fileName: '导出' },
      },
    );
    hide();
    return false;
  } catch (error) {
    console.log(error);
    hide();
    message.error('处理失败，请重试');
    return false;
  }
};

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

  const columns: ProColumns<API.PbcUserOperationalDataVO>[] = [
    {
      title: '序号',
      dataIndex: 'serialNumber',
      search: false
    },
    {
      title: '人员姓名',
      dataIndex: 'pbcUserName'
    },
    {
      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: 'registeredMerchantCount',
      search: false,
      render: (text, record) => <Link to={`business-detail?userId=${record.pbcUserId}&userName=${record.pbcUserName}`}>{text}</Link>
    },
    {
      title: '上传商品数',
      dataIndex: 'productUploadCount',
      search: false,
      render: (text, record) => <Link to={`product-detail?userId=${record.pbcUserId}&userName=${record.pbcUserName}`}>{text}</Link>
    },
  ];
  return (
    <PageContainer
      header={{
        title: '',
        breadcrumb: {},
      }}
    >
      <ProTable<API.PbcUserOperationalDataVO>
        columns={columns}
        actionRef={actionRef}
        request={(param: any) => {
          const queryParam = {
            userName: param.pbcUserName,
            roleId: param.pbcUserRole
          }
          return fetchData(queryParam);
        }}
        rowKey="pbcId"
        size="small"
        bordered
        search={{
          labelWidth: 'auto',
          span: 6,
        }}
        pagination={{
          defaultPageSize: 20,
          showSizeChanger: true,
        }}
        formRef={ref}
        toolbar={{
          actions: [
            <Button type="primary" key="export" onClick={() => {
              const values = ref.current?.getFieldsValue();
              const queryParam = {
                userName: values?.pbcUserName,
                roleId: values?.pbcUserRole
              }
              handleExport(queryParam)
            }}>导出</Button>
          ]
        }}
        scroll={{
          y: 'calc(100vh - 320px)',
        }}
        dateFormatter="string"
        options={false}
        toolBarRender={() => []}
      />
    </PageContainer>
  );
};

export default TableList;
