import { PlusOutlined } from '@ant-design/icons';
import { Button, message, Input, Drawer, Popconfirm, Tooltip } from 'antd';
import React, { useState, useRef, useEffect } from 'react';
import { PageContainer, FooterToolbar } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import ProDescriptions from '@ant-design/pro-descriptions';
import CreateForm from './components/CreateForm';

import { add, page, Start } from './service';

/**
 * 添加节点
 *
 * @param fields
 */
const handleAdd = async (fields) => {
  const hide = message.loading('正在添加');
  try {
    await add(JSON.stringify(fields)).then((r) => {
      if (r.data) {
        message.success('添加成功');
      } else {
        message.error('添加失败请重试！');
      }
    });
    hide();
    return true;
  } catch (error) {
    hide();
    message.error('添加失败请重试！');
    return false;
  }
};

/**
 * 删除节点
 *
 * @param selectedRows
 */

const handleStart = async (value) => {
  const hide = message.loading('正在发起退款');
  try {
    await Start(value);
    hide();
    message.success('发起退款成功，即将刷新');
    return true;
  } catch (error) {
    hide();
    message.error('发起退款失败，请重试');
    return false;
  }
};

const GoodsManage = () => {
  /** 新建/更新窗口的弹窗 */
  const [createModalVisible, handleModalVisible] = useState(false);
  /** 分布更新窗口的弹窗 */
  const actionRef = useRef();
  const [currentRow, setCurrentRow] = useState();
  /** 国际化配置 */

  const columns = [
    {
      title: '主键',
      dataIndex: 'id',
      hideInTable: true,
      hideInSearch: true,
      tip: '主键',
      render: (dom, entity) => {
        return (
          <a
            onClick={() => {
              setCurrentRow(entity);
              setShowDetail(true);
            }}
          >
            {dom}
          </a>
        );
      },
    },
    {
      title: '交易号',
      dataIndex: 'trade_no',
      ellipsis: {
        showTitle: false,
      },
    },
    {
      title: '订单号',
      dataIndex: 'orderNumId',
      ellipsis: {
        showTitle: false,
      },
    },
    {
      title: '客户单号',
      dataIndex: 'orderCustomerNumId',
      hideInSearch: true,
      ellipsis: {
        showTitle: false,
      },
    },

    {
      title: '退款状态',
      dataIndex: 'isRefund',
      hideInForm: true,
      valueEnum: {
        true: {
          text: '已经退款',
          status: 'Processing',
        },
        false: {
          text: '等待退款',
          status: 'Success',
        },
      },
    },
    {
      title: '发起退款时间',
      key: 'since',
      dataIndex: 'createAt',
      valueType: 'dateTime',

      hideInSearch: true,
    },
    {
      title: '发起退款时间',
      key: 'since',
      dataIndex: 'createAt',
      valueType: 'dateRange',
      hideInTable: true,
      search: {
        transform: (value) => {
          return {
            CreateTime: value[0],
            EndTime: value[1],
          };
        },
      },
    },
    {
      title: '退款类型',
      dataIndex: 'refundType',
      hideInSearch: true,
      valueEnum: {
        1: {
          text: '帮客户退款',
          status: 'Processing',
        },
        2: {
          text: '测试退款',
          status: 'Success',
        },
      },
    },
    {
      title: '退款备注',
      dataIndex: 'remark',
      valueType: 'textarea',
      ellipsis: {
        showTitle: false,
      },
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => [
        record.isRefund === false && (
          <a
            key="config"
            onClick={() => {
              handleModalVisible(true);
              setCurrentRow(record);
            }}
          >
            更新
          </a>
        ),
        record.isRefund === false && (
          <Popconfirm
            type="Start"
            key="Start"
            title="确认退款"
            okText="是"
            cancelText="否"
            onConfirm={() => {
              handleStart(record);
            }}
            onCancel={() => {}}
          >
            <a href="#">退款</a>
          </Popconfirm>
        ),
      ],
    },
  ];
  return (
    <PageContainer>
      <ProTable
        headerTitle="退款记录"
        actionRef={actionRef}
        rowKey="id"
        search={{
          labelWidth: 120,
        }}
        toolBarRender={() => [
          <Button
            type="primary"
            key="primary"
            onClick={() => {
              handleModalVisible(true);
            }}
          >
            <PlusOutlined /> 新增退款
          </Button>,
        ]}
        request={async (params) => {
          var Data = [];
          var total = 0;
          await page(params).then((r) => {
            Data = r.data.data;
            total = r.data.total;
          });
          return {
            data: Data,
            success: true,
            total: total,
          };
        }}
        columns={columns}
      />
      <CreateForm
        onFinish={async (value) => {
          var success = false;
          if (value.id) {
            success = await handleAdd(value);
          } else {
            success = await handleAdd(value);
          }
          if (success) {
            handleModalVisible(false);
            setCurrentRow(undefined);

            if (actionRef.current) {
              actionRef.current.reload();
            }
          }
        }}
        onCancel={() => {
          handleModalVisible(false);
          setCurrentRow(undefined);
        }}
        createModalVisible={createModalVisible}
        values={currentRow || {}}
      />
    </PageContainer>
  );
};

export default GoodsManage;
