import React from 'react';
import { Icon, Button, Input, Spin, Table, message, Select, Modal, DatePicker } from 'antd';
import { technicalSourceList } from '../../dataDic.js';
import { getCatagory, getIntellectualObtainWay } from '../../tools.js';
import './intellectual.less';
import moment from 'moment';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import IntellectualDesc from './intellectualDesc.jsx';

const Intellectual = React.createClass({
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/user/cognizance/intellectualList",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                startDate: this.state.startDateFormattedDate,
                endDate: this.state.endDateFormattedDate
            }
        }).done((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,
                    id: thisdata.id,
                    uid: thisdata.uid,
                    intellectualPropertyNumber: thisdata.intellectualPropertyNumber,
                    intellectualPropertyName: thisdata.intellectualPropertyName,
                    sortNumber: thisdata.sortNumber,
                    catagory: thisdata.catagory,
                    obtainWay: thisdata.obtainWay,
                    authorizationNumber: thisdata.authorizationNumber,
                    authorizationDate: thisdata.authorizationDate,
                    evaluationCategory: thisdata.evaluationCategory,
                    authorizationDateFormattedDate: thisdata.authorizationDateFormattedDate,
                    propertyRightUrl: thisdata.propertyRightUrl,
                    propertyRightDownloadFileName: thisdata.propertyRightDownloadFileName
                });
            };
            this.state.pagination.current = data.data.pageNo;
            this.state.pagination.total = data.data.totalCount;
            this.setState({
                dataSource: this.state.data,
                pagination: this.state.pagination
            });
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            selectedRowKeys: [],
            selectedRows: [],
            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: 'intellectualPropertyNumber',
                    key: 'intellectualPropertyNumber'
                }, {
                    title: '知识产权名称',
                    dataIndex: 'intellectualPropertyName',
                    key: 'intellectualPropertyName'
                }, {
                    title: '排序号',
                    dataIndex: 'sortNumber',
                    key: 'sortNumber'
                }, {
                    title: '知识产权类型',
                    dataIndex: 'catagory',
                    key: 'catagory',
                    render: text => { return getCatagory(text) }
                }, {
                    title: '获取方式',
                    dataIndex: 'obtainWay',
                    key: 'obtainWay',
                    render: text => { return getIntellectualObtainWay(text) }
                }, {
                    title: '授权号',
                    dataIndex: 'authorizationNumber',
                    key: 'authorizationNumber'
                }, {
                    title: '授权时间',
                    dataIndex: 'authorizationDateFormattedDate',
                    key: 'authorizationDateFormattedDate'
                }
            ],
            dataSource: []
        };
    },
    componentWillMount() {
        this.loadData();
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        this.setState({
            showDesc: true
        });
    },
    delectRow() {
        let deletedIds = [];
        for (let idx = 0; idx < this.state.selectedRows.length; idx++) {
            let rowItem = this.state.selectedRows[idx];
            if (rowItem.id) {
                deletedIds.push(rowItem.id)
            }
        }
        this.setState({
            selectedRowKeys: [],
            loading: deletedIds.length > 0
        });
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/user/cognizance/deleteIntellectual",
            data: {
                ids: deletedIds
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success('保存成功！');
                this.setState({
                    loading: false,
                });
            } else {
                message.warning(data.error[0].message);
            };
            this.loadData();
        }.bind(this));
    },
    closeDesc(e, s) {
        this.state.showDesc = e;
        if (s) {
            this.loadData();
        };
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.startDateFormattedDate = undefined;
        this.state.endDateFormattedDate = undefined;
        this.loadData();
    },
    render() {
        const rowSelection = {
            selectedRowKeys: this.state.selectedRowKeys,
            onChange: (selectedRowKeys, selectedRows) => {
                this.setState({
                    selectedRows: selectedRows,
                    selectedRowKeys: selectedRowKeys
                });
            }
        };
        const hasSelected = this.state.selectedRowKeys.length > 0;
        return (
            <div className="user-content" >
                <div className="content-title">
                    <span>知识产权列表</span>
                    <Button
                        disabled={this.state.ButtonDisabled}
                        style={{ background: "#ea0862", border: "none", color: "#fff", float: "right" }}
                        onClick={this.tableRowClick}>添加知识产权<Icon type="plus" /></Button>
                </div>
                <div className="user-search">
                    <span>开始日期:</span>
                    <DatePicker
                        value={this.state.startDateFormattedDate ? moment(this.state.startDateFormattedDate) : null}
                        onChange={(data, dataString) => { this.setState({ startDateFormattedDate: dataString }); }} />
                    <span>结束日期:</span>
                    <DatePicker
                        value={this.state.endDateFormattedDate ? moment(this.state.endDateFormattedDate) : null}
                        onChange={(data, dataString) => { this.setState({ endDateFormattedDate: dataString }); }} />
                    <Button type="primary" onClick={this.search}>搜索</Button>
                    <Button onClick={this.reset}>重置</Button>
                    <Button style={{ background: "#3fcf9e", border: "none", color: "#fff" }}
                        disabled={!hasSelected}
                        onClick={this.delectRow}>删除<Icon type="minus" /></Button>
                </div>
                <div className="patent-table">
                    <Spin spinning={this.state.loading}>
                        <Table columns={this.state.columns}
                            dataSource={this.state.dataSource}
                            pagination={this.state.pagination}
                            rowSelection={rowSelection}
                            onRowClick={this.tableRowClick} />
                    </Spin>
                </div>
                <IntellectualDesc data={this.state.RowData} showDesc={this.state.showDesc} closeDesc={this.closeDesc} />
            </div >
        );
    }
});

export default Intellectual;