import React from 'react';
import { Icon, Button, Select, Spin, Table, message, Cascader } from 'antd';
import { provinceSelect, getProvince } from '../../../NewDicProvinceList';
import { getTime, getPatentType, getPatentState, companySearch } from '../../../tools.js';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import './comprehensive.less';
import PaymentDesc from './paymentDesc.jsx';

const Payment = React.createClass({
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/patent/pendingPaymentList",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                locationProvince: this.state.province ? this.state.province[0] : undefined
            },
            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,
                        cid: thisdata.cid,
                        uid: thisdata.uid,
                        number: thisdata.serialNumber,
                        province: thisdata.locationProvince,
                        patentNumber: thisdata.patentNumber,
                        unitName: thisdata.unitName,
                        patentName: thisdata.patentName,
                        patentType: thisdata.patentCatagory,
                        patentState: thisdata.patentState,
                        endTime: thisdata.authorizedDate,
                        annualFeeState: thisdata.annualFeeState
                    });
                };
                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 {
            download: globalConfig.context + '/api/warningreports/download',
            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',
                    render: text => { return getProvince(text) }
                }, {
                    title: '公司名称',
                    dataIndex: 'unitName',
                    key: 'unitName',
                }, {
                    title: '专利类型',
                    dataIndex: 'patentType',
                    key: 'patentType',
                    render: text => { return getPatentType(text) },
                }, {
                    title: '专利名称',
                    dataIndex: 'patentName',
                    key: 'patentName',
                }, {
                    title: '专利状态',
                    dataIndex: 'patentState',
                    key: 'patentState',
                    render: text => { return getPatentState(text) },
                }, {
                    title: '缴费状态',
                    dataIndex: 'annualFeeState',
                    key: 'annualFeeState',
                    render: text => {
                        if (text == 0) {
                            return '未缴费'
                        } else if (text == 1) {
                            return '已缴费'
                        };
                    },
                }, {
                    title: '缴费截止时间',
                    dataIndex: 'endTime',
                    key: 'endTime',
                    render: text => { return getTime(text, 2) },
                }
            ],
            dataSource: []
        };
    },
    componentWillMount() {
        this.loadData();
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        if (record.annualFeeState !== 1) {
            this.setState({
                showDesc: true
            });
        };
    },
    closeDesc(e, s) {
        this.state.showDesc = e;
        if (s) {
            this.loadData();
        };
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.province = undefined;
        this.loadData();
    },
    exportPending() {
        window.open(globalConfig.context + "/api/admin/patent/exportPending?locationProvince=" + (this.state.province ? this.state.province[0] : null))
    },
    render() {
        return (
            <div className="patent-content" >
                <div className="content-title">
                    <span>代缴年登印费专利管理</span>
                </div>
                <div className="patent-query">
                    <Cascader placeholder="选择省份"
                        style={{ width: 100, marginTop: "-4px", marginLeft: 0, marginRight: '20px' }}
                        value={this.state.province}
                        options={provinceSelect()}
                        showSearch
                        filterOption={companySearch}
                        onChange={(e) => { this.setState({ province: e }) }} />
                    <Button type="primary" onClick={this.search}>搜索</Button>
                    <Button onClick={this.reset}>重置</Button>
                    <Button type="ghost" onClick={this.exportPending}>导出</Button>
                </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>
                <PaymentDesc data={this.state.RowData} showDesc={this.state.showDesc} closeDesc={this.closeDesc} />
            </div >
        );
    }
});

export default Payment;