import React from 'react';
import { Spin, Button, Tabs, Table, message } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js'
import $ from 'jquery/src/ajax';
import './index.less';
const TabPane = Tabs.TabPane;

const MessageModal = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            pagination: {
                defaultCurrent: 1,
                defaultPageSize: 10,
                showQuickJumper: true,
                pageSize: 10,
                onChange: function (page) {
                    this.loadReaded(page);
                }.bind(this),
                showTotal: function (total) {
                    return '共' + total + '条数据';
                }
            },
            UnreadPagination: {
                defaultCurrent: 1,
                defaultPageSize: 10,
                showQuickJumper: true,
                pageSize: 10,
                onChange: function (page) {
                    this.loadUnread(page);
                }.bind(this),
                showTotal: function (total) {
                    return '共' + total + '条数据';
                }
            },
            columns: [
                {
                    title: '编号',
                    dataIndex: 'key',
                    key: 'key'
                }, {
                    title: '类型',
                    dataIndex: 'noticeType',
                    key: 'noticeType',
                    render: (text, record) => {
                       return(<span>{record.noticeTypeName}</span>)  
                    }
                }, {
                    title: '时间',
                    dataIndex: 'createTimeFormattedDate',
                    key: 'createTimeFormattedDate'
                }, {
                    title: '内容',
                    dataIndex: 'content',
                    key: 'content',
                }, {
                    title: '公司',
                    dataIndex: 'unitName',
                    key: 'unitName',
                }, {
                    title: '业务员',
                    dataIndex: 'principle',
                    key: 'principle',
                }
            ],
            data: []
        };
    },
    loadReaded(pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/notice/readed1",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
            }
        }).done((data) => {
            if (!data.data) {
                if (data.error && data.error.length) {
                    message.warning(data.error[0].message);
                    return;
                };
            };
            this.state.data = [];
            for (let i = 0; i < data.data.list.length; i++) {
                let thisdata = data.data.list[i];
                this.state.data.push({
                    key: i + 1,
                    id: thisdata.id,
                    rid: thisdata.rid,
                    uid: thisdata.uid,
                    aid: thisdata.aid,
                    noticeType: thisdata.noticeType,
                    year: thisdata.year,
                    noticeTypeName: thisdata.noticeTypeName,
                    content: thisdata.content,
                    createTime: thisdata.createTime,
                    createTimeFormattedDate: thisdata.createTimeFormattedDate,
                    principle: thisdata.principle,
                    unitName: thisdata.unitName
                });
            };
            this.state.pagination.current = data.data.pageNo;
            this.state.pagination.total = data.data.totalCount;
            this.setState({
                dataSource: this.state.data,
                pagination: this.state.pagination
            });
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    loadUnread(pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/notice/unread2",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.UnreadPagination.pageSize,
            }
        }).done((data) => {
            if (!data.data) {
                if (data.error && data.error.length) {
                    message.warning(data.error[0].message);
                    return;
                };
            };
            this.state.data = [];
            for (let i = 0; i < data.data.list.length; i++) {
                let thisdata = data.data.list[i];
                this.state.data.push({
                    key: i + 1,
                    id: thisdata.id,
                    rid: thisdata.rid,
                    uid: thisdata.uid,
                    aid: thisdata.aid,
                    noticeType: thisdata.noticeType,
                    year: thisdata.year,
                    noticeTypeName: thisdata.noticeTypeName,
                    content: thisdata.content,
                    createTime: thisdata.createTime,
                    createTimeFormattedDate: thisdata.createTimeFormattedDate,
                    principle: thisdata.principle,
                    unitName: thisdata.unitName
                });
            };
            this.state.UnreadPagination.current = data.data.pageNo;
            this.state.UnreadPagination.total = data.data.totalCount;
            this.setState({
                unreadData: this.state.data,
                UnreadPagination: this.state.UnreadPagination
            });
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    componentWillMount() {
        this.loadReaded();
        this.loadUnread();
    },
    render() {
        return (
            <div className="set-content">
                <div className="set-title">
                    <span>消息中心</span>
                </div>
                <Spin spinning={this.state.loading} className='spin-box'>
                    <Tabs defaultActiveKey="1">
                        <Tabs.TabPane tab="新信息" key="1">
                            <Table columns={this.state.columns}
                                dataSource={this.state.unreadData}
                                pagination={this.state.UnreadPagination} />
                        </Tabs.TabPane>
                        <Tabs.TabPane tab="已读信息" key="2">
                            <Table columns={this.state.columns}
                                dataSource={this.state.dataSource}
                                pagination={this.state.pagination} />
                        </Tabs.TabPane>
                    </Tabs>
                </Spin>
            </div>
        );
    }
});

const Index = React.createClass({
    getInitialState() {
        return {
            loading: false
        };
    },
    render() {
        return (
            <div className="manage-content">
                <div>
                    <Spin spinning={this.state.loading} className='spin-box'>
                        <div className="index-content">
                            <MessageModal />
                        </div>
                    </Spin>
                </div>
            </div>
        )
    }
});

export default Index;