import React from 'react';
import { Icon, Spin, Table, message } from 'antd';
import { getPatentType } from '../../../tools.js';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import './comprehensive.less';
import ExpressDesc from './expressDesc.jsx';

const Express = React.createClass({
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        let page = pageNo || 1,
        pageSize = this.state.pagination.pageSize;
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/patent/getRecieveSendList?pageNo=" + page + "&pageSize=" + pageSize,
            success: function (data) {
                if (data.error.length || !data.data || !data.data.list) {
                    message.warning(data.error[0].message);
                    return;
                }
                for (let i = 0; i < data.data.list.length; i++) {
                    let thisdata = data.data.list[i];
                    this.state.data.push({
                        key: i,
                        number: thisdata.serialNumber,
                        patentNumber: thisdata.patentNumber,
                        unitName: thisdata.unitName,
                        patentName: thisdata.patentName,
                        patentType: thisdata.patentCatagory,
                        acceptanceReceiveTime:thisdata.acceptanceReceiveTimeFormattedDate,
                        acceptanceIssueTime:thisdata.acceptanceIssueTimeFormattedDate,
                        acceptanceTrackingNumber:thisdata.acceptanceTrackingNumber,
                        acceptance:[thisdata.acceptanceReceiveTimeFormattedDate,thisdata.acceptanceIssueTimeFormattedDate],
                        authorizationReceiveTime:thisdata.authorizationReceiveTimeFormattedDate,
                        authorizationIssueTime:thisdata.authorizationIssueTimeFormattedDate,
                        authorizationTrackingNumber:thisdata.authorizationTrackingNumber,
                        authorization:[thisdata.authorizationReceiveTimeFormattedDate,thisdata.authorizationIssueTimeFormattedDate],
                        certificateRecieveTime:thisdata.certificateRecieveTimeFormattedDate,
                        certificateIssueTime:thisdata.certificateIssueTimeFormattedDate,
                        certificateTrackingNumber:thisdata.certificateTrackingNumber,
                        certificate:[thisdata.certificateRecieveTimeFormattedDate,thisdata.certificateIssueTimeFormattedDate],
                        rid:thisdata.rid,
                        pid:thisdata.pid
                    });
                };
                this.state.pagination.current = data.data.pageNo;
                this.state.pagination.total = data.data.totalCount;
                this.setState({
                    dataSource: this.state.data,
                    pagination: this.state.pagination
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            number: '',
            patentNumber: '',
            province: '',
            unitName: '',
            patentType: '',
            patentName: '',
            data: [],
            loading: false,
            pagination: {
                defaultCurrent: 1,
                defaultPageSize: 10,
                showQuickJumper: true,
                pageSize: 10,
                onChange: function (page) {
                    this.loadData(page);
                }.bind(this),
                showTotal: function (total) {
                    return '共' + total + '条数据';
                }
            },
            columns: [
                {
                    title: '编号',
                    dataIndex: 'number',
                    key: 'number',
                }, {
                    title: '申请号/专利号',
                    dataIndex: 'patentNumber',
                    key: 'patentNumber',
                }, {
                    title: '省份',
                    dataIndex: 'province',
                    key: 'province',
                }, {
                    title: '公司名称',
                    dataIndex: 'companyName',
                    key: 'companyName',
                }, {
                    title: '专利类型',
                    dataIndex: 'patentType',
                    key: 'patentType',
                    render: text => { return getPatentType(text) },
                }, {
                    title: '专利名称',
                    dataIndex: 'patentName',
                    key: 'patentName',
                }, {
                    title: '受理书',
                    dataIndex: 'acceptance',
                    key: 'acceptance',
                    render: text => { 
                        if (text[1]) {
                            return <div>已发出</div>
                        } else {
                            if ( text[0] ) {
                                return <div>已收到</div>
                            };
                        };
                        return <div>未收到</div> 
                    },
                }, {
                    title: '授权书',
                    dataIndex: 'authorization',
                    key: 'authorization',
                    render: text => { 
                        if (text[1]) {
                            return <div>已发出</div>
                        } else {
                            if ( text[0] ) {
                                return <div>已收到</div>
                            };
                        };
                        return <div>未收到</div> 
                    },
                }, {
                    title: '证书',
                    dataIndex: 'certificate',
                    key: 'certificate',
                    render: text => { 
                        if (text[1]) {
                            return <div>已发出</div>
                        } else {
                            if ( text[0] ) {
                                return <div>已收到</div>
                            };
                        };
                        return <div>未收到</div> 
                    },
                }
            ],
            dataSource: []
        };
    },
    componentWillMount() {
        this.loadData();
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        this.setState({
            showDesc: true
        });
    },
    closeDesc(e, s) {
        this.state.showDesc = e;
        if (s) {
            this.loadData();
        };
    },
    render() {
        return (
            <div className="patent-content" >
                <div className="content-title">
                    <span>专利纸件收发登记</span>
                </div>
                <div className="patent-table">
                    <Spin spinning={this.state.loading}>
                        <Table columns={this.state.columns}
                            dataSource={this.state.dataSource}
                            pagination={this.state.pagination}
                            onRowClick={this.tableRowClick} />
                    </Spin>
                </div>
                <ExpressDesc data={this.state.RowData} showDesc={this.state.showDesc} closeDesc={this.closeDesc} />
            </div >
        );
    }
});

export default Express;