import React from 'react';
import { Icon, Button, Input, Select, Spin, Table, DatePicker, message, Cascader, Switch } from 'antd';
import { copyrightStateList } from '../../../dataDic.js';
import { getTime, companySearch, getCopyrightState, getInUrgentTime, getVacations } from '../../../tools.js';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import PatentAdd from './comPatentAdd.jsx';
import PatentChange from './comPatentChange.jsx';

import moment from 'moment';
import './copyright.less';

const copyright = React.createClass({
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/user/copyright/list",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                copyrightName: this.state.copyrightName,
                status: this.state.status,
                createTime: this.state.createTime,
                acceptTime: this.state.acceptTime
            },
            success: function (data) {
                this.state.data = [];
                if (!data.data || !data.data.list) {
                    message.warning(data.error[0].message);
                } else {
                    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,
                            province: thisdata.province,
                            unitName: thisdata.unitName,
                            serialNumber: thisdata.serialNumber,
                            createTime: thisdata.createTime,
                            acceptTime: thisdata.acceptTime,
                            principal: thisdata.principal,
                            contact: thisdata.contact,
                            copyrightName: thisdata.copyrightName,
                            copyrightNumber: thisdata.copyrightNumber,
                            status: thisdata.status == '9' ? '8' : thisdata.status,
                            comment: thisdata.comment,
                            workIssue: thisdata.workIssue,
                            outsource: thisdata.outsource,
                            inUrgent: thisdata.inUrgent,
                            authorizedDate: thisdata.authorizedDate,
                            fisrtContact: thisdata.fisrtContact,
                            secondContact: thisdata.secondContact,
                            thirdContact: thisdata.thirdContact,
                            authorizedDateFormattedDate: thisdata.authorizedDateFormattedDate,
                            createTimeFormattedDate: thisdata.createTimeFormattedDate,
                            acceptTimeFormattedDate: thisdata.acceptTimeFormattedDate,
                            expectTime: [thisdata.acceptTime, thisdata.inUrgent]
                        });
                    };
                };
                this.state.pagination.defaultCurrent = 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 {
            visible: false,
            stateOption: [],
            data: [],
            loading: false,
            showAdd: false,
            showDesc: false,
            searchMore: true,
            createTime: [],
            acceptTime: [],
            authTime: [],
            pagination: {
                defaultCurrent: 1,
                defaultPageSize: 10,
                showQuickJumper: true,
                pageSize: 10,
                onChange: function (page) {
                    this.loadData(page);
                }.bind(this),
                showTotal: function (total) {
                    return '共' + total + '条数据';
                }
            },
            vacations: [],
            columns: [
                {
                    title: '编号',
                    dataIndex: 'serialNumber',
                    key: 'serialNumber',
                }, {
                    title: '认证状态',
                    dataIndex: 'status',
                    key: 'status',
                    render: (text) => {
                        if (text == '9') {
                            return '已完成'
                        } else {
                            return getCopyrightState(text)
                        }
                    }
                }, {
                    title: '软著名称',
                    dataIndex: 'copyrightName',
                    key: 'copyrightName',
                }, {
                    title: '加急天数',
                    dataIndex: 'inUrgent',
                    key: 'inUrgent',
                    render: (text) => {
                        switch (text) {
                            case 0:
                                return '不加急(45个工作日)';
                            case 3:
                                return '3个工作日';
                            case 5:
                                return '5个工作日';
                            case 10:
                                return '6-10个工作日';
                            case 15:
                                return '11-15个工作日';
                            case 20:
                                return '16-20个工作日';
                            case 25:
                                return '21-25个工作日';
                            case 30:
                                return '26-30个工作日';
                            case 35:
                                return '31-35个工作日';
                        }
                    }
                }, {
                    title: '负责人',
                    dataIndex: 'principal',
                    key: 'principal',
                }, {
                    title: '派单日',
                    dataIndex: 'createTimeFormattedDate',
                    key: 'createTimeFormattedDate',
                }, {
                    title: '受理日',
                    dataIndex: 'acceptTimeFormattedDate',
                    key: 'acceptTimeFormattedDate',
                }, {
                    title: '下证时间',
                    dataIndex: 'expectTime',
                    key: 'expectTime',
                    render: (text, record) => {
                        if (record.authorizedDate) {
                            return record.authorizedDateFormattedDate
                        } else {
                            if (text[0]) {
                                if (text[1]) {
                                    return getTime(getInUrgentTime(moment(text[0]), text[1], this.state.vacations))
                                } else {
                                    return getTime(getInUrgentTime(moment(text[0]), 45, this.state.vacations))
                                }
                            };
                        }
                    }
                }, {
                    title: '备注',
                    dataIndex: 'comment',
                    key: 'comment',
                }
            ],
            dataSource: []
        };
    },
    componentWillMount() {
        let _me = this;
        copyrightStateList.map(function (item) {
            if (item.value == '9') {
                _me.state.stateOption.push(
                    <Select.Option value={item.value} key={item.key}>已完成</Select.Option>
                )
            } else {
                _me.state.stateOption.push(
                    <Select.Option value={item.value} key={item.key}>{item.key}</Select.Option>
                )
            }
        });
        getVacations((data) => { this.setState({ vacations: data }); });
        this.loadData();
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        this.setState({
            showDesc: true
        });
    },
    closeDesc(e, s) {
        this.state.showDesc = e;
        if (s) {
            this.loadData();
        };
    },
    addClick() {
        this.setState({
            showAdd: true
        });
    },
    closeAdd(e, s) {
        this.state.showAdd = e;
        if (s) {
            this.loadData();
        };
    },
    searchSwitch() {
        this.setState({
            searchMore: !this.state.searchMore
        });
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.copyrightName = undefined;
        this.state.status = undefined;
        this.state.createTime = [];
        this.state.acceptTime = [];
        this.state.authTime = [];
        this.loadData();
    },
    render() {
        const { MonthPicker, RangePicker } = DatePicker;
        return (
            <div className="foster-box">
                <div className="foster-content">
                    <div className="content-title">
                        <span>软著申请管理</span>
                    </div>
                    <div className="foster-query">
                        <Input style={{ width: '160px' }}
                            placeholder="请输入软著名称"
                            value={this.state.copyrightName}
                            onChange={(e) => { this.setState({ copyrightName: e.target.value }); }} />
                        <Select style={{ width: '160px' }}
                            value={this.state.status}
                            onChange={(e) => { this.setState({ status: e }) }}
                            placeholder="选择一个状态">{this.state.stateOption}</Select>
                        <span>更多搜索<Switch defaultChecked={false} onChange={this.searchSwitch} /></span>
                        <Button type="primary" onClick={this.search}>搜索</Button>
                        <Button onClick={this.reset}>重置</Button>
                        <div className="search-more" style={this.state.searchMore ? { display: 'none' } : {}}>
                            <span>派单时间:</span>
                            <RangePicker style={{ width: '240px' }}
                                allowClear={false}
                                value={[this.state.createTime[0] ? moment(this.state.createTime[0]) : null,
                                this.state.createTime[1] ? moment(this.state.createTime[1]) : null]}
                                onChange={(date, dateString) => { this.setState({ createTime: dateString }) }} />
                            <span>受理时间:</span>
                            <RangePicker style={{ width: '240px' }}
                                allowClear={false}
                                value={[this.state.acceptTime[0] ? moment(this.state.acceptTime[0]) : null,
                                this.state.acceptTime[1] ? moment(this.state.acceptTime[1]) : null]}
                                onChange={(date, dateString) => { this.setState({ acceptTime: dateString }) }} />
                        </div>
                    </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>
                    <PatentChange
                        data={this.state.RowData}
                        showDesc={this.state.showDesc}
                        closeDesc={this.closeDesc} />
                </div >
            </div>
        );
    }
});

export default copyright;

// <PatentAdd
//     onClick={this.addClick}
//     closeAdd={this.closeAdd} />