import React, { useState, useEffect } from 'react';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { Form, Pagination, Button, Icon, message, Modal, Table, Select, Input, DatePicker } from 'antd';
import router from 'umi/router';
import moment from 'moment';
import className from 'classnames';
import Cell from '../../components/Cell';
import styles from './style.less';
import { fetch, apis } from '../../utils/request';
import request from '../../utils/request';
import AuthButton from '@/components/AuthButton';

const { MonthPicker, RangePicker, WeekPicker } = DatePicker;

function header(props) {
    // 获取初始化数据
    const [data, setData] = useState({})
    const [demandIdList, setDemandIdList] = useState([])
    const [visible, setVisible] = useState(false)

    useEffect(() => {
        getList({ pageNum: 1, pageSize: 10 });
    }, [])

    // 查询列表
    const getList = (params) => {
        request({ ...apis.sample.list, params: { ...params } }).then((data) => {
            console.log(data)
            setData(data)
        })
    }


    // 提交事件
    const handleSubmit = (e, props) => {
        e.preventDefault();
        props.form.validateFields((err, values) => {
            if (!err) {
                let { createDate, ...submitValue } = values
                if (null != createDate && createDate.length > 0) {
                    const [startCreateDate, endCreateDate] = createDate
                    submitValue.startCreateDate = moment(startCreateDate).format('YYYY-MM-DD');
                    submitValue.endCreateDate = moment(endCreateDate).format('YYYY-MM-DD');
                } else {
                    submitValue.startCreateDate = null
                    submitValue.endCreateDate = null
                }
                getList({ pageNum: 1, pageSize: 10, ...submitValue })
            }
        });
    }

    const changePageNum = (pageNumber) => {
        let { createDate, ...submitValue } = props.form.getFieldsValue()
        if (null != createDate && createDate.length > 0) {
            const [startCreateDate, endCreateDate] = createDate
            submitValue.startCreateDate = moment(startCreateDate).format('YYYY-MM-DD');
            submitValue.endCreateDate = moment(endCreateDate).format('YYYY-MM-DD');
        } else {
            submitValue.startCreateDate = null
            submitValue.endCreateDate = null
        }
        getList({ pageNum: pageNumber, pageSize: 10, ...submitValue })
    }

    /**
     *
     *
     * @param {*} props
     * @returns
     */
    const columns = [
        {
            title: '订单编号',
            dataIndex: 'sampleName',
            key: 'sampleName',
            align: 'center',
        },
        {
            title: '退款组织',
            dataIndex: 'orgName',
            key: 'orgName',
            align: 'center',
        },
        {
            title: '退款金额',
            dataIndex: 'orderer',
            key: 'orderer',
            align: 'center',
        },
        {
            title: '退款创建时间',
            dataIndex: 'createDate',
            key: 'createDate',
            align: 'center',
            render: (x, row) => <><span>{`${moment(row.createDate).format('YYYY-MM-DD HH:mm:ss')}`}</span></>,
        },
        {
            title: '退款状态',
            dataIndex: 'orderer',
            key: 'orderer1',
            align: 'center',
        },
        {
            title: '驳回原因',
            dataIndex: 'orderer',
            key: 'ordere2r',
            align: 'center',
        },
        {
            title: '退款凭证',
            dataIndex: 'orderer',
            key: 'o2rderer',
            align: 'center',
        },
        {
            title: '操作',
            dataIndex: 'createDate',
            key: 'crea2teDate',
            align: 'center',
            width: '120px',
            render: (x, row) => (
                <>
                    <span style={{ color: '#FF925C', cursor: 'pointer', marginRight: '16px' }} >
                        退款
                    </span>
                    <span style={{ color: '#FF925C', cursor: 'pointer' }} onClick={() => setVisible(true)}>
                        驳回
                    </span>
                </>
            ),
        },

    ];
    function handleReset() {
        props.form.resetFields();
        getList({ pageNum: 1, pageSize: 10 })
    }
    const handleOk = () => {

        setVisible(false)

    }
    const handleCancel = () => {
        setVisible(false)
    }

    const { getFieldDecorator } = props.form
    return (

        <>
            <Modal
                visible={visible}
                title="驳回退款"
                onOk={() => handleOk()}
                onCancel={() => handleCancel()}
                footer={[
                    <Button key="back" size="large" onClick={() => handleCancel()}>取消</Button>,
                    <Button key="submit" type="primary" size="large" onClick={() => handleOk()}>确认</Button>,
                ]}
            >
                <div style={{ display: 'flex', alignItems: 'center', margin: '30px 0' }}>驳回原因
                <span style={{ color: 'red' }}>*</span><Input style={{ marginLeft: '10px', width: '80%' }} placeholder="请输入驳回原因" />
                </div>
            </Modal>
            <Form layout="inline" onSubmit={e => handleSubmit(e, props)} style={{ marginBottom: '16px' }}>
                <Form.Item>
                    {getFieldDecorator('sampleName')(
                        <Input
                            prefix={<Icon type="text" style={{ color: 'rgba(0,0,0,.25)' }} />}
                            placeholder="订单编号"
                        />,
                    )}
                </Form.Item>
                <Form.Item>
                    {getFieldDecorator('orgName')(
                        <Input
                            prefix={<Icon type="text" style={{ color: 'rgba(0,0,0,.25)' }} />}
                            placeholder="退款组织"
                        />,
                    )}
                </Form.Item>
                <Form.Item>
                    <span style={{ marginRight: '10px' }}>退款时间段：</span>
                    {getFieldDecorator('createDate')(
                        <RangePicker placeholder={['开始时间', '结束时间']} />
                    )}
                </Form.Item>
                <Form.Item>
                    <Button type="primary" htmlType="submit" className={styles.searchBtn}>
                        搜索
                    </Button>
                    <Button style={{ marginLeft: 8 }} onClick={handleReset}>
                        重置
                    </Button>
                </Form.Item>
            </Form>
            <Table rowKey="newsType" dataSource={data.records} columns={columns} pagination={false} />
            <div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '30px' }}>
                <Pagination showQuickJumper defaultCurrent={1} total={data.total} onChange={changePageNum} current={data.current} />
            </div>
        </>
    )
}
const WrappedHeader = Form.create({ name: 'header' })(header);

export default WrappedHeader