import React from 'react';
import { Switch, Icon, Button, Input, Spin, Table, message, Select, Modal, DatePicker } from 'antd';
import { technicalSourceList } from '../../../../dataDic.js';
import { getCatagory, getIntellectualObtainWay, getProportion, saveProportion } from '../../../../tools.js';
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: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/intellectualList",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                uid: this.props.data.uid,
                startDate: this.state.startDateFormattedDate,
                endDate: this.state.endDateFormattedDate
            }
        }).done((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,
                    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,
                    type: thisdata.type
                });
            };
            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));
        let _me = this;
        getProportion(this.props.data.uid, function (data) { _me.setState({ proportion: data }); });
    },
    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/admin/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>
                    <span className="proportion"> 是否已完成：</span>
                    <Switch
                        checked={this.state.proportion && this.state.proportion.intellectualProperty == 1 ? true : false}
                        checkedChildren={'已完成'}
                        unCheckedChildren={'未完成'}
                        onChange={(e) => {
                            e ? this.state.proportion.intellectualProperty = 1 : this.state.proportion.intellectualProperty = 0;
                            saveProportion(this.state.proportion.id, this.props.data.uid, 'intellectualProperty', this.state.proportion.intellectualProperty);
                            this.setState({ proportion: this.state.proportion });
                        }} />
                    <span className="link">
                        <a href="http://www2.soopat.com/Home/IIndex" target="_blank">SOOPAT</a>
                        <a href="http://www.baiten.cn/" target="_blank">佰腾网</a>
                        <a href="http://www.sipo.gov.cn/" target="_blank">国家知识产权局</a>
                        <a href="http://www.ccopyright.com.cn/" target="_blank">中国版权保护中心</a>
                    </span>
                </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>
                    <p>
                        <Button style={{ background: "#ea0862", border: "none", color: "#fff" }}
                            onClick={this.tableRowClick}>添加<Icon type="plus" /></Button>
                        <Button style={{ background: "#3fcf9e", border: "none", color: "#fff" }}
                            disabled={!hasSelected}
                            onClick={this.delectRow}>删除<Icon type="minus" /></Button>
                    </p>
                </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 uid={this.props.data.uid} data={this.state.RowData} showDesc={this.state.showDesc} closeDesc={this.closeDesc} />
            </div >
        );
    }
});

export default Intellectual;