import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
} from '@/common';
import { Selects } from '@/components/Selects';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import Create from './modals/Create';
import CreateCounters from './modals/CreateCounters';
import Update from './modals/Update';
import UpdateCounters from './modals/UpdateCounters';

export default function Index({ title = '商户' }) {
  const ExpandedRowRender = (e: any) => {
    const columns = [
      { title: 'id', dataIndex: 'id' },
      { title: '上游', dataIndex: ['market', 'name'] },
      { title: '名称', dataIndex: 'name' },
      { title: '上游商家编号', dataIndex: 'up_merchant_no' },
      { title: '平台商户号', dataIndex: 'plat_merchant_no' },
      { title: '平台柜台号', dataIndex: 'plat_counter_no' },
      MyColumns.Option({
        render: (_, item: any, index, action) => (
          <Space key={index}>
            <UpdateCounters item={item} reload={action?.reload} title="柜台" />
            <MyButtons.Delete
              onConfirm={() =>
                Apis.MerchantCounters.Delete({ id: item.id }).then(() =>
                  action?.reload(),
                )
              }
            />
          </Space>
        ),
      }),
    ];
    return (
      <ProTable
        {...MyProTableProps.props}
        pagination={false}
        columns={columns}
        request={async (params, sort) =>
          MyProTableProps.request(
            { ...params, merchants_id: e?.id },
            sort,
            Apis.MerchantCounters.List,
          )
        }
        toolBarRender={(action) => [
          <CreateCounters
            key="Create"
            reload={action?.reload}
            title="添加柜台"
            item={{ merchants_id: e?.id }}
          />,
        ]}
        headerTitle="柜台信息"
        search={false}
      />
    );
  };

  return (
    <MyPageContainer title={title}>
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Merchants.List)
        }
        toolBarRender={(action) => [
          <Create key="Create" reload={action?.reload} title={title} />,
        ]}
        expandable={{
          expandedRowRender: ExpandedRowRender,
        }}
        columns={[
          MyColumns.ID(),
          Selects.Agents({ dataIndex: 'agents_name' }),
          Selects.Bosses({ dataIndex: 'bosses_name' }),
          Selects.Factories({ dataIndex: 'factories_name' }),
          Selects.Platforms({ dataIndex: 'platforms_name' }),
          // {
          //   title: '代理',
          //   dataIndex: 'agents_name',
          //   search: false,
          // },
          // {
          //   title: '厂家',
          //   dataIndex: 'factories_name',
          //   search: false,
          // },
          // {
          //   title: '平台',
          //   dataIndex: 'platforms_name',
          //   search: false,
          // },
          // {
          //   title: '老板',
          //   dataIndex: 'bosses_name',
          //   search: false,
          // },
          {
            title: '简称',
            dataIndex: 'name',
          },
          {
            title: '全称',
            dataIndex: 'full_name',
          },
          {
            title: '平台商户号',
            dataIndex: 'plat_merchant_no',
          },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <Update item={item} reload={action?.reload} title={title} />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Merchants.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
