import React, { useState, useEffect } from 'react';
import { Form, Button, Table, Pagination, Select, Input, Icon } from 'antd';
import router from 'umi/router';
import moment from 'moment';
import apis from '../../../services/apis';
import request from '../../../utils/request'
import Prompt from 'umi/prompt';

const typeList = [
    {
        label: '报名活动',
        value: 'activity'
    },
    {
        label: '助力活动',
        value: 'help'
    },
    {
        label: '拼团活动',
        value: 'group'
    },
    {
        label: 'H5活动',
        value: 'h5'
    },
    {
        label: '直播活动',
        value: 'live'
    },
    {
        label: '在线选房',
        value: 'house'
    },
    {
        label: '项目',
        value: 'project'
    },
    {
        label: '资讯',
        value: 'news'
    },
    {
        label: '首页',
        value: 'main'
    },
    {
        label: '个人主页',
        value: 'card_share'
    },
]
const showTypeList = [
    {
        label: '直播活动',
        value: 'livePost',
    },
    {
        label: '直播活动',
        value: 'liveApp',
    },
    {
        label: '在线选房',
        value: 'housePost',
    },
    {
        label: '在线选房',
        value: 'houseApp',
    },
    {
        label: '个人主页',
        value: 'consultant',
    },
]

/**
  @param {*} props
  @returns
 */
const { Option } = Select;

function record(props) {
    const [radioVal, updateRadioVal] = useState(7)
    const [formData, updateFormData] = useState({
        pageNum: 1,
        pageSize: 10,
        ...props.location.query,
    })

    // 获取初始化数据
    const [data, setData] = useState({})
    const [loading, updataLoading] = useState(true)

    useEffect(() => {
        getList(formData);
    }, [])

    // 查询列表
    const getList = (params) => {
        request({ ...apis.stats.shareNum, params: { ...params }, }).then((data) => {
            setData(data)
            updataLoading(false)
        })
    }

    //重置搜索
    function handleReset() {
        props.form.resetFields();
        getList(formData);
    }

    // 提交事件
    const handleSubmit = (e) => {
        e.preventDefault();
        props.form.validateFields((err, values) => {
            if (!err) {
                console.log('提交数据: ', values, { ...formData, ...values })
                getList({ ...formData, ...values });
            }
        });
    }
    // 更新页码
    const changePageNum = (pageNumber) => {
        props.form.validateFields((err, values) => {
            if (!err) {
                getList({ ...formData, pageNum: pageNumber, ...values });
            }
        });
    }

    function getType(type) {
        return typeList.concat(showTypeList).filter(t => t.value == type)[0].label
    }

    // 表格数据
    const columns = [

        {
            title: '分享标题',
            dataIndex: 'targetType',
            key: 'targetType',
            align: 'center',
            render: (_, record) => <><span>{getType(record.targetType)} {record.targetName ? '【' + record.targetName + '】' : ''}</span></>
        },
        {
            title: '分享时间',
            dataIndex: 'visitTime',
            key: 'visitTime',
            align: 'center',
            render: (_, record) => <><span>{record.visitTime && moment(record.visitTime).format('YYYY-MM-DD HH:mm:ss')}</span></>,
        },

    ];

    const { getFieldDecorator } = props.form
    return (

        <>
            <div align="right"><Button onClick={() => router.go(-1)}>返回</Button></div>
            <Form layout="inline">
                <div style={{ display: 'flex', alignItems: 'center' }}>
                    <Form.Item>
                        {getFieldDecorator('targetType')(
                            <Select placeholder="分享类型" style={{ width: 150 }}>
                                {typeList.map((item, index) => (
                                    <Option key={index} value={item.value}>{item.label}</Option>
                                ))}
                            </Select>
                        )}
                    </Form.Item>
                    <Form.Item>
                        {getFieldDecorator('targetName')(
                            <Input
                                prefix={<Icon type="text" style={{ color: 'rgba(0,0,0,.25)' }} />}
                                placeholder="分享标题"
                            />,
                        )}
                    </Form.Item>
                    <Form.Item style={{ position: 'absolute', right: '38px' }}>
                        <Button type="primary" onClick={handleSubmit}>搜索</Button>
                        <Button style={{ marginLeft: 8 }} onClick={handleReset}>重置</Button>
                    </Form.Item>
                </div>
            </Form>
            <Table rowKey={(index => { index })}
            // <Table rowKey={record => (record.personId + record.buildingId || '')}
                style={{ marginTop: '30px' }}
                dataSource={data.records}
                columns={columns}
                pagination={false}
                loading={loading}
            />
            < div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '30px' }}>
                <Pagination showQuickJumper defaultCurrent={1} total={data.total} onChange={changePageNum} current={data.current} />
            </div>
            <Prompt message={location =>
        location.pathname.startsWith("/statistical/consultant")
          ? true
          : localStorage.removeItem("consultantPageParams")} />
        </>
    )
}
const WrappedHeader = Form.create({ name: 'record' })(record);

export default WrappedHeader
