import React, { useState, useEffect } from 'react';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { Form, Pagination, Button, Icon, message, Modal, Table, Input } 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';
import SelectContact from './components/SelectContact';


function header(props) {
    // 获取初始化数据
    const [data, setData] = useState({})
    const [contactList, setContactList] = useState([])

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

    // 查询列表
    const getList = (params) => {
        request({ ...apis.fund.contactList, params: { ...params, contentType: 'finance' } }).then((data) => {
            console.log(data)
            setData(data)
        })
    }


    // 提交事件
    const handleSubmit = (e, props) => {
        e.preventDefault();
        props.form.validateFields((err, values) => {
            if (!err) {
                let { ...submitValue } = values

                getList({ pageNum: 1, pageSize: 10, ...submitValue })
            }
        });
    }

    const changePageNum = (pageNumber) => {
        let { ...submitValue } = props.form.getFieldsValue()

        getList({ pageNum: pageNumber, pageSize: 10, ...submitValue })
    }

    /**
     *
     *
     * @param {*} props
     * @returns
     */
    const columns = [
        {
            title: '姓名',
            dataIndex: 'contactName',
            key: 'contactName',
            align: 'center',
        },
        {
            title: '性别',
            dataIndex: 'sex',
            key: 'sex',
            align: 'center',
            render: (text, record) => <span>{record.sex == '1' ? '男' : record.sex == '2' ? '女' : '未知'}</span>

        },
        {
            title: '头像',
            dataIndex: 'avatar',
            key: 'avatar',
            align: 'center',
            render: (text, record) => <img src={record.avatar} className={styles.touxiang} />,
        },
        {
            title: '固话',
            dataIndex: 'telephone',
            key: 'telephone',
            align: 'center',
        },
        {
            title: '手机号',
            dataIndex: 'phone',
            key: 'phone',
            align: 'center',
        },
        {
            title: '对外头衔',
            dataIndex: 'appellation',
            key: 'appellation',
            align: 'center',
        },
        {
            title: '权重',
            dataIndex: 'orderNo',
            key: 'orderNo',
            align: 'center',
        },

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


    const rowSelection = {
        onChange: (selectedRowKeys, selectedRows) => {
            console.log('selectedRowKeys:', selectedRowKeys, 'selectedRows: ', selectedRows);
            setContactList(selectedRows)
        },
    };

    const toDel = () => {
        if (contactList.length < 1) {
            message.info('请至少选择一条数据')
            return
        }
        Modal.confirm({
            title: `确认将所选的${contactList.length}条数据删除`,
            okText: '确定',
            cancelText: '取消',
            onOk() {
                request({ ...apis.fund.delContact, data: contactList, }).then((data) => {
                    message.info("操作成功")
                    getList({ pageNum: 1, pageSize: 10 });
                    setContactList([])
                }).catch((err) => {

                })
            },
            onCancel() { },
        });
    }
    const { getFieldDecorator } = props.form
    return (

        <>
            <Form layout="inline" onSubmit={e => handleSubmit(e, props)} >
                <Form.Item>
                    {getFieldDecorator('contactName')(
                        <Input
                            prefix={<Icon type="text" style={{ color: 'rgba(0,0,0,.25)' }} />}
                            placeholder="姓名"
                        />,
                    )}
                </Form.Item>
                <Form.Item>
                    {getFieldDecorator('telephone')(
                        <Input
                            prefix={<Icon type="text" style={{ color: 'rgba(0,0,0,.25)' }} />}
                            placeholder="固话"
                        />,
                    )}
                </Form.Item>
                <Form.Item>
                    {getFieldDecorator('phone')(
                        <Input
                            prefix={<Icon type="text" style={{ color: 'rgba(0,0,0,.25)' }} />}
                            placeholder="手机号"
                        />,
                    )}
                </Form.Item>
                <Form.Item>
                    {getFieldDecorator('appellation')(
                        <Input
                            prefix={<Icon type="text" style={{ color: 'rgba(0,0,0,.25)' }} />}
                            placeholder="对外头衔"
                        />,
                    )}
                </Form.Item>
                <Form.Item>
                    <Button type="primary" htmlType="submit" className={styles.searchBtn}>
                        搜索
                    </Button>
                    <Button style={{ marginLeft: 8 }} onClick={handleReset}>
                        重置
                    </Button>
                </Form.Item>
            </Form>
            <div style={{ margin: '10px 0 16px 0' }}>
                <Button type="danger" className={styles.addBtn} style={{ padding: '0' }} ><SelectContact onClick={() => getList({ pageNum: 1, pageSize: 10 })} /></Button>
                <Button type="primary" className={styles.addBtn} onClick={() => toDel()} style={{ marginLeft: '30px' }} >删除</Button>
            </div>
            <Table rowSelection={rowSelection} rowKey={r => r.contactId} 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