import React, { useRef, useEffect, useState } from 'react';
import { Select, Space, Image, Button } from 'antd';
import {
  PageContainer,
  ProTable,
} from '@ant-design/pro-components';

export default () => {

  const initRow = {
    shop_id: '',
    todate: '',
  };

  const initRowCancellationModal = {
    username: "",
    mobile: "",
    code: ""
  }


  const [row, setRow] = useState(initRow);



  const [rowCancellationModal, setRowCancellationModal] = useState(initRowCancellationModal);


  const actionRef = useRef();

  const [shopOption, setShopOption] = useState([]);
  const [searchParams, setSearchParams] = useState(null);

  const searchShop = (value) => {
    shopItems({
      username: value
    }).then(({ data }) => {
      const arr = [];
      data?.data?.map((item) => {
        item.label = `${item.username}   ${item.mobile}`;
        arr.push(item);
      });
      setShopOption(arr);
    });
  }

  const columns = [
    {
      title: '活动名称',
      dataIndex: 'title',
      search: false,
    },
    {
      title: '投资人',
      dataIndex: 'shop_id',
      render: (_, record) => {
        return <>
          <div>用户名：{record?.shop?.username}</div>
          <div>手机号：{record?.shop?.mobile}</div>
        </>
      },
      renderFormItem: (
        _,
        { type, defaultRender, formItemProps, fieldProps, ...rest },
        form,
      ) => {
        return <Select
          {...fieldProps}
          allowClear
          showSearch
          placeholder="请输入用户名"
          style={{ width: "100%" }}
          filterOption={false}
          onSearch={
            (e) => {
              if (!e) {
                return;
              }
              searchShop(e);
            }
          }
          fieldNames={{
            label: "label",
            value: "id"
          }}
          options={shopOption}
        />
      },
    },
    {
      title: '认领时间',
      dataIndex: 'todate',
      search: false,
    },
    {
      title: '充电桩',
      dataIndex: 'device',
      search: false,
      render: (_, record) => {
        let ids = record?.device.map((item) => {
          return item.id
        })
        return ids.join(',')
      },
    },
    {
      title: '创建时间',
      dataIndex: 'created_at',
      search: false,
    }
  ];

  return (
    <PageContainer
      ghost
    >
      <>
        <ProTable
          actionRef={actionRef}
          rowKey="id"
          search={{
            defaultCollapsed: false,
          }}
          toolBarRender={() => [
            // <AuthConsumer action={'admin/shop_activity/create'} key="admin/shop_activity/create">
            //   <Button
            //     type="primary"
            //     style={{ background: '#67c23a' }}
            //     onClick={() => {
            //       setCreateFormModal(true)
            //     }}
            //   >
            //     添加
            //   </Button>
            // </AuthConsumer>,
            // <AuthConsumer action={'admin/shop_activity/export'} key="admin/shop_activity/export">
            //   <Button type="primary" onClick={() => {
            //     window.location.href = exportLink(searchParams, '/admin/shop_activity/export');
            //   }}>导出表格</Button>
            // </AuthConsumer>,
            // <AuthConsumer action={'admin/shop_activity/delete'} key="admin/shop_activity/delete">
            //   <Button
            //     danger
            //     type="primary"
            //     onClick={() => {
            //       setCancellationModal(true)
            //     }}
            //   >
            //     作废
            //   </Button>
            // </AuthConsumer>
          ]}
          request={async (params, sorter, filter) => {
            setSearchParams({ token: '', ...params });
            const { data, success } = await shopActivityItems813({
              ...params,
              sorter,
              filter,
            });
            return {
              data: data?.items || [],
              total: data?.total,
              success,
            };
          }}
          columns={columns}
        />
      </>
    </PageContainer>
  );
};
