import React from 'react';
import { Icon, Button, Input, Select, Spin, Table, DatePicker, message, Cascader } from 'antd';
import { provinceArr } from '../../../dataDic.js';
import { getTime, getPatentState, companySearch } from '../../../tools.js';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import './highTechFoster.less';
import FosterDesc from './fosterDesc/content.jsx';

const Patent = React.createClass({
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/listCognizance",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                locationProvince: this.state.province,
                unitName: this.state.unitName,
            },
            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,
                        uid: thisdata.uid,
                        cid: thisdata.cid,
                        serialNumber: thisdata.serialNumber,
                        locationProvince: thisdata.locationProvince,
                        unitName: thisdata.unitName,
                        contacts: [thisdata.contacts, {
                            '1': thisdata.firstContacts,
                            '2': thisdata.secondContacts,
                            '3': thisdata.thirdContacts
                        }],
                        createTime: thisdata.createTime,
                        comment: thisdata.comment,
                        state: thisdata.state,
                        certificateNumber: thisdata.certificateNumber,
                        issuingDate: thisdata.issuingDate,
                        consultant: thisdata.consultant,
                        createTimeFormattedDate: thisdata.createTimeFormattedDate,
                        issuingDateFormattedDate: thisdata.issuingDateFormattedDate,
                        endtime: getTime(thisdata.issuingDateFormattedDate, 36)
                    });
                };
                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 + "/api/user/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 + "/api/user/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 {
            provinceOption: [],
            companyOption: [],
            authorOption: [],
            fosterDescDisplay: 'none',
            fosterDisplay: 'block',
            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: 'locationProvince',
                    key: 'locationProvince',
                }, {
                    title: '公司名称',
                    dataIndex: 'unitName',
                    key: 'unitName',
                }, {
                    title: '联系人',
                    dataIndex: 'contacts',
                    key: 'contacts',
                    render: (text) => {
                        for (let item in text[1]) {
                            if (text[0] == item) {
                                return text[1][item];
                            };
                        };
                    }
                }, {
                    title: '状态',
                    dataIndex: 'state',
                    key: 'state',
                    render: text => { return getPatentState(text) }
                }, {
                    title: '证书编号',
                    dataIndex: 'certificateNumber',
                    key: 'certificateNumber',
                }, {
                    title: '发证日期',
                    dataIndex: 'issuingDateFormattedDate',
                    key: 'issuingDateFormattedDate',
                }, {
                    title: '到期日期',
                    dataIndex: 'endtime',
                    key: 'endtime'
                }, {
                    title: '咨询师',
                    dataIndex: 'consultant',
                    key: 'consultant',
                }
            ],
            dataSource: []
        };
    },
    componentWillMount() {
        let _me = this;
        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({
            fosterDisplay: 'none',
            fosterDescDisplay: 'block'
        });
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.province = undefined;
        this.state.unitName = undefined;
        this.loadData();
    },
    returnBtn() {
        this.setState({
            fosterDisplay: 'block',
            fosterDescDisplay: 'none'
        });
        this.loadData();
    },
    render() {
        return (
            <div className="foster-box">
                <div className="foster-content" style={{ display: this.state.fosterDisplay }}>
                    <div className="content-title">
                        <span>高企认定培育</span>
                    </div>
                    <div className="foster-query">
                        <Select placeholder="选择省份"
                            style={{ width: 200 }}
                            value={this.state.province}
                            showSearch
                            filterOption={companySearch}
                            onChange={(e) => { this.setState({ province: e }) }}>
                            {this.state.provinceOption}
                        </Select>
                        <Select placeholder="选择公司"
                            style={{ width: 200 }}
                            value={this.state.unitName}
                            showSearch
                            filterOption={companySearch}
                            onChange={(e) => { this.setState({ unitName: e }) }}>
                            {this.state.companyOption}
                        </Select>
                        <Button type="primary" onClick={this.search}>搜索</Button>
                        <Button onClick={this.reset}>重置</Button>
                    </div>
                    <div className="foster-table">
                        <Spin spinning={this.state.loading}>
                            <Table columns={this.state.columns}
                                dataSource={this.state.dataSource}
                                pagination={this.state.pagination}
                                onRowClick={this.tableRowClick} />
                        </Spin>
                    </div>
                </div >
                <FosterDesc
                    style={this.state.fosterDescDisplay}
                    data={this.state.RowData}
                    returnBtn={this.returnBtn} />
            </div>
        );
    }
});

export default Patent;