import React from 'react';
import { Icon, Button, Input, Select, Spin, Table, Switch, DatePicker, message } from 'antd';
import { patentTypeList, patentStateList, provinceArr } from '../../../dataDic.js';
import { getTime, getPatentState } from '../../../tools.js';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import './comprehensive.less';
import PatentAdd from './comPatentAdd.jsx';
import PatentDesc from './comPatentDesc.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/managePatentList",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                serialNumber: this.state.number,
                patentNumber: this.state.patentNumber,
                patentName: this.state.patentName,
                //office: 事务所,
                locationProvince: this.state.province,
                unitName: this.state.unitName,
                patentCatagory: this.state.patentType,
                patentState: this.state.patentState,
                createTime: this.state.createTime,
                patentApplicationDate: this.state.startTime,
                authorizedDate: this.state.endTime,
                author: this.state.author
            },
            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,
                        pid: thisdata.pid,
                        oid: thisdata.oid,
                        number: thisdata.serialNumber || '',
                        patentNumber: thisdata.patentNumber || '',
                        unitName: thisdata.unitName || '',
                        patentName: thisdata.patentName || '',
                        patentType: thisdata.patentCatagory || '0',
                        patentState: thisdata.patentState || '0',
                        patentField: thisdata.patentField || '0',
                        patentDes: thisdata.patentDes || '',
                        patentProryStatementUrl: thisdata.patentProryStatementUrl, //专利代理委托书
                        patentWritingUrl: thisdata.patentWritingUrl, //专利稿件
                        authorizationNoticeUrl: thisdata.authorizationNoticeUrl, //授权通知书
                        patentCertificateUrl: thisdata.patentCertificateUrl, //专利证书
                        lastYearTaxReportUrl: thisdata.lastYearTaxReportUrl, //上年度纳税表
                        startTime: thisdata.patentApplicationDate,
                        endTime: thisdata.authorizedDate,
                        orgCode: thisdata.orgCode,
                        province:thisdata.locationProvince,
                        companyAddress: thisdata.locationProvince || thisdata.locationCity || thisdata.locationArea ? thisdata.locationProvince + '/' + thisdata.locationCity + '/' + thisdata.locationArea : '',
                        companyContacts: thisdata.contacts, //联系人
                        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));
    },
    getAuthorList() {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/techservice/patent/getAdmin",
            success: function (data) {
                if (data.error.length || !data.data) {
                    return;
                };
                let _me = this;
                for (var item in data.data) {
                    _me.state.authorOption.push(
                        <Select.Option value={item} key={item}>{data.data[item]}</Select.Option>
                    )
                };
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getCompanyList() {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/techservice/patent/getUnitNames",
            success: function (data) {
                if (data.error.length || !data.data) {
                    return;
                };
                let _me = this;
                for (var item in data.data) {
                    _me.state.companyOption.push(
                        <Select.Option value={item} key={item}>{data.data[item]}</Select.Option>
                    )
                };
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            number: '',
            patentNumber: '',
            //office: 事务所,
            province: '',
            unitName: '',
            patentType: '',
            patentName: '',
            patentState: '',
            createTime: [],
            endTime: [],
            startTime: [],
            author: '',
            AlertDis: 'block',
            patentTypeOption: [],
            patentStateOption: [],
            companyOption: [],
            authorOption: [],
            provinceOption: [],
            searchMore: true,
            data: [],
            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: 'office',
                    key: 'office',
                }, {
                    title: '省份',
                    dataIndex: 'province',
                    key: 'province',
                }, {
                    title: '公司名称',
                    dataIndex: 'unitName',
                    key: 'unitName',
                }, {
                    title: '专利名称',
                    dataIndex: 'patentName',
                    key: 'patentName',
                }, {
                    title: '专利状态',
                    dataIndex: 'patentState',
                    key: 'patentState',
                    render: text => { return getPatentState(text) },
                }, {
                    title: '派单日',
                    dataIndex: 'createTime',
                    key: 'createTime',
                    render: text => { return getTime(text) },
                }, {
                    title: '申请日',
                    dataIndex: 'startTime',
                    key: 'startTime',
                    render: text => { return getTime(text) },
                }, {
                    title: '授权日',
                    dataIndex: 'endTime',
                    key: 'endTime',
                    render: text => { return getTime(text) },
                }, {
                    title: '资料撰写人',
                    dataIndex: 'author',
                    key: 'author',
                }
            ],
            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>
            )
        });
        provinceArr.map(function (item) {
            _me.state.provinceOption.push(
                <Select.Option value={item} key={item}>{item}</Select.Option>
            )
        });
        this.loadData();
        this.getAuthorList();
        this.getCompanyList();
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        this.setState({
            showDesc: true
        });
    },
    closeDesc(e) {
        this.state.showDesc = e;
        this.loadData();
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.number = '';
        this.state.patentNumber = '';
        this.state.province = '';
        this.state.unitName = '';
        this.state.patentType = '';
        this.state.patentName = '';
        this.state.patentState = '';
        this.state.createTime = [];
        this.state.endTime = [];
        this.state.startTime = [];
        this.state.author = '';
        this.state.searchKey = '';
        this.state.searchValue = '';
        this.loadData();
    },
    exportComposite() {
        window.open(globalConfig.context + "/techservice/patent/exportComposite" +
            "?" + "serialNumber" + "=" + this.state.number +
            "&" + "patentNumber" + "=" + this.state.patentNumber +
            "&" + "patentName" + "=" + this.state.patentName +
            "&" + "locationProvince" + "=" + this.state.locationProvince +
            "&" + "unitName" + "=" + this.state.unitName +
            "&" + "patentCatagory" + "=" + this.state.patentCatagory +
            "&" + "patentState" + "=" + this.state.patentState +
            "&" + "createTime" + "=" + this.state.createTime +
            "&" + "patentApplicationDate" + "=" + this.state.startTime +
            "&" + "author" + "=" + this.state.author);
    },
    searchSwitch() {
        this.setState({
            searchMore: !this.state.searchMore
        });
    },
    searchcreateTime(date, dateString) {
        this.state.createTime = dateString;
    },
    searchstartTime(date, dateString) {
        this.state.startTime = dateString;
    },
    searchSelect(e) {
        let _me = this;
        this.setState({
            searchValue: e.target.value
        });
        switch (_me.state.searchKey) {
            case "patentNumber":
                _me.state.patentNumber = e.target.value;
                _me.state.number = '';
                _me.state.patentName = '';
                break;
            case "serialNumber":
                _me.state.number = e.target.value;
                _me.state.patentNumber = '';
                _me.state.patentName = '';
                break;
            case "patentName":
                _me.state.patentNumber = '';
                _me.state.number = '';
                _me.state.patentName = e.target.value;
                break;
        }
    },
    render() {
        const { MonthPicker, RangePicker } = DatePicker;
        return (
            <div className="patent-content" >
                <div className="content-title">
                    <span>专利综合管理</span>
                    <PatentAdd />
                </div>
                <div className="patent-query">
                    <Select placeholder="请选择" value={this.state.searchKey} style={{ width: 200 }} onChange={(e) => { this.setState({ searchKey: e, searchValue: '' }); }}>
                        <Select.Option value="patentNumber">专利号</Select.Option>
                        <Select.Option value="serialNumber">编号</Select.Option>
                        <Select.Option value="patentName">专利名称</Select.Option>
                    </Select>
                    <Input value={this.state.searchValue} onChange={this.searchSelect} />
                    <Select placeholder="专利状态" value={this.state.patentState} style={{ width: 200 }} onChange={(e) => { this.setState({ patentState: e }); }}>{this.state.patentStateOption}</Select>
                    <Button type="primary" onClick={this.search}>搜索</Button>
                    <Button onClick={this.reset}>重置</Button>
                    <Button type="ghost" onClick={this.exportComposite}>导出</Button>
                    <span>更多搜索<Switch defaultChecked={false} onChange={this.searchSwitch} /></span>
                    <div className="search-more" style={this.state.searchMore ? { display: 'none' } : {}}>
                        <p>
                            <span>派单日：</span><RangePicker onChange={this.searchcreateTime} />
                            <span>申请日：</span><RangePicker onChange={this.searchstartTime} />
                        </p>
                        <div>
                            <Select placeholder="选择省份" style={{ width: 200 }} onChange={(e) => { this.state.province = e }}>{this.state.provinceOption}</Select>
                            <Select placeholder="选择撰写人" style={{ width: 200 }} onChange={(e) => { this.state.author = e }}>{this.state.authorOption}</Select>
                            <Select placeholder="选择公司" style={{ width: 200 }} onChange={(e) => { this.state.companyName = e }}>{this.state.companyOption}</Select>
                        </div>
                    </div>
                </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;