import React from 'react';
import { Icon, Alert, Button, Input, Select, Spin, Table, message } from 'antd';
import { patentTypeList, patentStateList } from '../../dataDic.js';
import { getPatentType, getPatentState, getTime } from '../../tools.js';
import ajax from 'jquery/src/ajax/xhr.js'
import $ from 'jquery/src/ajax';
import './patent.less';
import PatentAdd from './patentAdd.jsx';
import PatentDesc from './patentDesc.jsx';

const Patent = React.createClass({
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/techservice/patent/clientApplyList",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                patentNumber: this.state.searchNum,
                patentName: this.state.searchName,
                patentCatagory: this.state.searchType,
                patentState: this.state.searchState
            },
            success: function (data) {
                if (!data.data) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                        this.state.ButtonDisabled = true;
                    }
                    return;
                };
                for (let i = 0; i < data.data.list.length; i++) {
                    let thisdata = data.data.list[i];
                    this.state.data.push({
                        key: i,
                        pid: thisdata.id,
                        number: thisdata.serialNumber,
                        patentNum: thisdata.patentNumber,
                        patentName: thisdata.patentName,
                        patentType: getPatentType(thisdata.patentCatagory),
                        patentState: getPatentState(thisdata.patentState),
                        patentField: thisdata.patentField,
                        patentDes: thisdata.patentDes,
                        patentProryStatementUrl: thisdata.patentProryStatementUrl, //专利代理委托书
                        patentWritingUrl: thisdata.patentWritingUrl, //专利稿件
                        authorizationNoticeUrl: thisdata.authorizationNoticeUrl, //授权通知书
                        patentCertificateUrl: thisdata.patentCertificateUrl, //专利证书
                        confirmState: thisdata.confirmState, //稿件确认状态
                        authorizedTime: getTime(thisdata.authorizedDate),
                        startTime: getTime(thisdata.patentApplicationDate),
                        endTime: getTime(thisdata.patentApplicationDate, 2),
                        firstInventorName: thisdata.firstInventorName,
                        firstInventorNationality: thisdata.firstInventorNationality, //国籍
                        firstInventorIdNumber: thisdata.firstInventorIdNumber,   //id
                        firstInventorIsPublish: thisdata.firstInventorIsPublish, //是否公布
                        secondInventorName: thisdata.secondInventorName,
                        secondInventorNationality: thisdata.secondInventorNationality,
                        secondInventorIsPublish: thisdata.secondInventorIsPublish,
                        thirdInventorName: thisdata.thirdInventorName,
                        thirdInventorNationality: thisdata.thirdInventorNationality,
                        thirdInventorIsPublish: thisdata.thirdInventorIsPublish,
                        createTime: thisdata.createTime,   //派单日
                        author: thisdata.author, //撰写人
                        office: thisdata.office, //事务所
                        principal: thisdata.principal //负责人
                    });
                };
                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 {
            warningText: 'warning text! warning text! warning text!',
            AlertDis: 'block',
            patentTypeOption: [],
            patentStateOption: [],
            data: [],
            //dataSource: [],
            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: 'patentNum',
                    key: 'patentNum',
                }, {
                    title: '专利类型',
                    dataIndex: 'patentType',
                    key: 'patentType',
                }, {
                    title: '专利名称',
                    dataIndex: 'patentName',
                    key: 'patentName',
                }, {
                    title: '专利状态',
                    dataIndex: 'patentState',
                    key: 'patentState',
                }, {
                    title: '申请日',
                    dataIndex: 'startTime',
                    key: 'startTime',
                }, {
                    title: '授权缴费截止日',
                    dataIndex: 'endTime',
                    key: 'endTime',
                }
            ],
            dataSource: []
        };
    },
    componentWillMount() {
        let _me = this;
        patentTypeList.map(function (item) {
            _me.state.patentTypeOption.push(
                <Select.Option value={item.value} key={item.key}>{item.key}</Select.Option>
            )
        });
        patentStateList.map(function (item) {
            _me.state.patentStateOption.push(
                <Select.Option value={item.value} key={item.key}>{item.key}</Select.Option>
            )
        });
        this.loadData();
    },
    alertClose(e) {
        console.log(e);
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        this.setState({
            showDesc: true
        });
    },
    closeDesc(e, s) {
        this.state.showDesc = e;
        if (s) {
            this.loadData();
        };
    },
    search() {
        this.state.searchNum;
        this.state.searchName;
        this.state.searchType;
        this.state.searchState;
        this.loadData();
    },
    reset() {
        this.state.searchNum = "";
        this.state.searchName = "";
        this.state.searchType = "";
        this.state.searchState = "";
        this.loadData();
    },
    render() {
        return (
            <div className="patent-content" >
                <div className="content-title">
                    <span>专利申请管理</span>
                    {userData.type == '0' ? <span></span> : <PatentAdd ButtonDisabled={this.state.ButtonDisabled} closeDesc={this.closeDesc} />}
                </div>
                <Alert style={{ display: this.state.AlertDis }} className="patent-alert" message={this.state.warningText}
                    type="info"
                    showIcon={true}
                    closable
                    onClose={this.alertClose}
                />
                <div className="patent-query">
                    <Input placeholder="专利号" value={this.state.searchNum} onChange={(e) => { this.setState({ searchNum: e.target.value }); }} />
                    <Input placeholder="专利名称" value={this.state.searchName} onChange={(e) => { this.setState({ searchName: e.target.value }); }} />
                    <Select placeholder="专利类型" value={this.state.searchType} style={{ width: 200 }} onChange={(e) => { this.setState({ searchType: e }); }}>{this.state.patentTypeOption}</Select>
                    <Select placeholder="专利状态" value={this.state.searchState} style={{ width: 200 }} onChange={(e) => { this.setState({ searchState: e }); }}>{this.state.patentStateOption}</Select>
                    <Button type="primary" onClick={this.search}>搜索</Button>
                    <Button type="ghost" onClick={this.reset}>重置</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>
                <PatentDesc data={this.state.RowData} showDesc={this.state.showDesc} closeDesc={this.closeDesc} />
            </div >
        );
    }
});

export default Patent;