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

function header(props) {
    
    const [taNoticeList, setTaNoticeList] = useState([])
    const [ data, setData ] = useState({})

    useEffect(() => {
        getList({ pageNum: 1, pageSize: 10 });
    }, [])
    // 查询列表

    const getList = params => {
        request({ ...apis.openScreen.list, params: { ...params } }).then(data => {
            setData(data)
        })
    }

    const toEditResource = (id) => () => {
        if(id){
        router.push({
            pathname: '/resource/openScreen/edit',
              query: {
                id
              },
        });
    }else{
            router.push({
                pathname: '/resource/openScreen/add',
                  query: {
                    id
                  },
            });
        }
    }

    const handleSubmit = (e, props) => {
        e.preventDefault();
        getList({ pageNum: 1, pageSize: 10, ...props.form.getFieldsValue() });
    }

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

    const toDelBatch = rowData => () => {
        console.log(taNoticeList, 'taNoticeList')
        if (taNoticeList.length < 1) {
            message.error('请至少选择一条数据')
            return
        }

        Modal.confirm({
            title: '确认将所选数据删除？',
            okText: '确定',
            cancelText: '取消',
            onOk() {
                request({ ...apis.openScreen.delete, data: taNoticeList, }).then((data) => {
                  message.info("操作成功")
                  getList({ pageNum: 1, pageSize: 10 });
                  setTaNoticeList([])
                }).catch((err) => {
                })
            },
        });
    }

    const changePageNum = pageNumber => {
        getList({ pageNum: pageNumber, pageSize: 10, ...props.form.getFieldsValue() })
    }


    const columns = [
        {
            title: '通知标题',
            dataIndex: 'title',
            key: 'title',
            align: 'center',

        },
        {
            title: '通知图',
            dataIndex: 'noticeImg',
            key: 'noticeImg',
            align: 'center',
            render: (text, record) => <img style={{width:'140px',height:'92px'}} src={record.noticeImg} className={styles.touxiang} />,
        },
        {
            title: '关联业务类型',
            dataIndex: 'targetType',
            key: 'targetType',
            align: 'center',
            render: (x, row) => <><span>{row.targetType === 'H5' ? 'H5样例' :''}</span></>
        },
        {
            title: '关联业务',
            dataIndex: 'targetName',
            key: 'targetName',
            align: 'center',
        },
        {
            title: '发布状态',
            dataIndex: 'status',
            key: 'status',
            align: 'center',
            render: (x, row) => <><span>{row.status === 1 &&moment(row.invalidTime) > moment() ? '是' :'否'}</span></>
        },
        {
            title: '自动下架时间',
            dataIndex: 'invalidTime',
            key: 'invalidTime',
            align: 'center',
            render: (x, row) => <><span>{row.invalidTime?`${moment(row.invalidTime).format('YYYY-MM-DD HH:mm:ss')}`:''}</span></>,
        },
        {
            title: '权重',
            dataIndex: 'orderNo',
            key: 'orderNo',
            align: 'center',

        },
        {
            title: '新增时间',
            dataIndex: 'createDate',
            key: 'createDate',
            align: 'center',
            render: (x, row) => <><span>{row.createDate?`${moment(row.createDate).format('YYYY-MM-DD HH:mm:ss')}`:''}</span></>,
        },
        {
            title: '操作',
            dataIndex: 'handle',
            key: 'handle',
            align: 'center',
            render: (x, row) => (
                <>


                    <span style={{ color: '#FF925C', cursor: 'pointer' }} onClick={toEditResource(row.noticeId)}>
                        查看详情<Icon type="form" className={styles.edit} />
                    </span>

                </>
            ),
        },
    ];

    const { getFieldDecorator } = props.form

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

        <>
            <div>
                <Form layout="inline" onSubmit={e => handleSubmit(e, props)}>
                    <Form.Item>
                        {getFieldDecorator('title')(
                            <Input
                                prefix={<Icon type="text" style={{ color: 'rgba(0,0,0,.25)' }} />}
                                placeholder="通知标题"
                            />,
                        )}
                    </Form.Item>
                    <Form.Item>
                        {getFieldDecorator('targetType')(
                            <Select style={{ width: '180px' }} placeholder="关联业务类型">
                                   <Option value='H5'>H5样例</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>
                        {getFieldDecorator('status')(

                            <Select style={{ width: '180px' }} placeholder="发布状态">
                                 <Option value="">全部</Option>
                                {/* <Option value="-1">删除</Option> */}
                                <Option value="0">否</Option>
                                <Option value="1">是</Option>

                            </Select>,

                        )}
                    </Form.Item>
                    <Form.Item>

                        <Button type="primary" htmlType="submit" className={styles.searchBtn}>
                            搜索
              </Button> 
                        {/*  */}
                        <Button style={{ marginLeft: 8 }} onClick={handleReset}>
                            重置
            </Button>
                    </Form.Item>
                </Form>

                <Button type="danger" className={styles.addBtn} onClick={toEditResource()}>新增</Button>

                <Button type="primary" className={styles.addBtn} onClick={toDelBatch()} style={{ marginLeft: '30px' }} >删除</Button>

                <Table id='noticeTable' rowSelection={rowSelection} rowKey={r => r.noticeId} dataSource={data.records} columns={columns} pagination={false} />

                <div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '30px' }}>
                    <Pagination showQuickJumper defaultCurrent={1} total={data.total} onChange={e => changePageNum(e)} current={data.current} />
                </div>
            </div>
        </>
    )
}

const WrappedHeader = Form.create({ name: 'header' })(header);

export default WrappedHeader