import React, { useRef, useEffect, useState } from 'react';
import { history } from '@umijs/max';

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

import dayjs from 'dayjs';

import { exportLink } from '@/utils/func';

import AuthConsumer from '@/components/Authority';

import services from '@/services/admin';

const { shopWalletItems } = services.ShopWalletController;
const { shopItems } = services.ShopController;

export default () => {

  const actionRef = useRef();

  const [config, setConfig] = useState();

  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: 'order_no',
      search: false,
    },
    {
      title: '投资人',
      dataIndex: 'shop_id',
      render: (_, record) => {
        return <>
          <div>用户名：{record?.shop?.username}</div>
          <div>手机号：{record?.shop?.mobile}</div>
          <div>等级：{record?.vip}</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: 'type',
      search: false,
      render: (_, record) => {
        return config?.type[record.type];
      },
    },
    {
      title: '收益时间',
      dataIndex: 'todate',
      search: false,
    },
    {
      title: '金额',
      dataIndex: 'money',
      search: false,
    },
    {
      title: '当前金额',
      dataIndex: 'cur_money',
      search: false,
    },
    {
      title: '支付方式',
      dataIndex: 'pay_type',
      search: false,
      render: (_, record) => {
        return config?.pay_type[record?.pay_type];
      },
    },
    {
      title: '状态',
      dataIndex: 'status',
      search: true,
      renderFormItem: (
        _,
        { type, defaultRender, formItemProps, fieldProps, ...rest },
        form,
      ) => {
        const data = [];

        for (let index in config?.status) {
          if (config?.status.hasOwnProperty(index)) {
            data.push({
              label: config?.status[index],
              key: index
            })
          }
        }

        return <Select
          {...fieldProps}
          allowClear
          defaultValue="2"
          style={{ width: "100%" }}
          filterOption={false}
          fieldNames={{
            label: "label",
            value: "key"
          }}
          options={data}
        />
      },
      render: (_, record) => {
        return <>
          {config?.status[record?.status]}
        </>
      },
    },
    {
      title: '备注',
      dataIndex: 'note',
      search: false,
    },
    {
      title: '后台备注',
      dataIndex: 'admin_note',
      search: false,
    },
    {
      title: '付款时间',
      dataIndex: 'pay_time',
      valueType: 'dateRange',
      search: {
        transform: (value) => {
          return {
            start_date: value[0],
            end_date: value[1],
          };
        },
      },
      render: (_, record) => {
        return record?.pay_time ? dayjs(record?.pay_time * 1000).format('YYYY-MM-DD HH:mm:ss') : ''
      },
    },
    {
      title: '创建时间',
      dataIndex: 'created_at',
      search: false,
    },
  ];

  return (
    <PageContainer
      ghost
    >
      <>
        <ProTable
          actionRef={actionRef}
          rowKey="id"
          search={{
            defaultCollapsed: false,
          }}
          toolBarRender={() => [
            <AuthConsumer action={'admin/shop_wallet/export1'} key="admin/shop_wallet/export1">
              <Button type="primary" onClick={() => {
                window.location.href = exportLink(searchParams, '/admin/shop_wallet/export1');
              }}>导出表格</Button>
            </AuthConsumer>,
          ]}
          request={async (params, sorter, filter) => {
            if (!params?.status) {
              params.status = 2;
            }
            setSearchParams({ token: '', ...params });
            const { data, success } = await shopWalletItems({
              ...params,
              sorter,
              filter,
            });
            setConfig(data?.config);
            return {
              data: data?.items?.data || [],
              total: data?.items?.total,
              success,
            };
          }}
          columns={columns}
        />
      </>
    </PageContainer>
  );
};
