import React from 'react';
import { Input, Modal, Button, message } from 'antd';
import { achievementCategoryList } from '../../dataDic.js';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';

const BatchImport = React.createClass({
    getInitialState: function () {
        return {
            loading: false,
            error: false
        };
    },
    showModal () {
        this.setState({
            visible: true,
        });
    },
    handleOk (e) {
        this.setState({
            visible: false,
        });
    },
    handleCancel (e) {
        this.setState({
            visible: false,
        });
    },
    //数据反向转化
    getDataCategory (e, row) {
        switch (e) {
            case "成果":
                return 0;
            case "技术":
                return 1;
            case "项目":
                return 2;
            default:
                message.warning("第 " + row + " 行中没有对应的数据类型！");
                this.state.error = true;
                break;
        }
    },
    getOwnerType (e, row) {
        switch (e) {
            case "个人":
                return 0;
            case "组织":
                return 1;
            default:
                message.warning("第 " + row + " 行中没有对应的所有人类型！");
                this.state.error = true;
                break;
        }
    },
    getCategory (e, row) {
        let theType = null, _me = this;
        if (e) {
            achievementCategoryList.map(function (item) {
                if (item.key == e) {
                    theType = Number(item.value);
                };
            });
            if (theType == null) {
                message.warning("第 " + row + " 行中没有对应的成果类型！");
                _me.state.error = true;
            }
            return theType;
        };
    },
    getCooperationMode (e, row) {
        switch (e) {
            case "技术转让":
                return 0;
            case "授权生产":
                return 1;
            default:
                message.warning("第 " + row + " 行中没有对应的合作方式！");
                this.state.error = true;
                break;
        }
    },
    //提交
    handleSubmit () {
        let rowsArr = this.state.inputValue.split(/[\r\n]/g);
        let columnArr = [], _me = this;
        if (!rowsArr.length) {
            return;
        };
        let columnLength = 12;//定义数据正常长度
        for (let i = 0; i < rowsArr.length; i++) {
            if (_me.state.error) {
                this.state.error = false;
                return;
            };
            let theArr = rowsArr[i].split(/\t/);
            if (theArr.length == columnLength) {
                columnArr.push({
                    dataCategory: _me.getDataCategory(theArr[0], i + 1),
                    name: theArr[1],
                    keyword: theArr[2],
                    category: _me.getCategory(theArr[3], i + 1),
                    ownerName: theArr[4],
                    ownerType: _me.getOwnerType(theArr[5], i + 1),
                    ownerIdNumber: theArr[6],
                    ownerMobile: theArr[7],
                    ownerEmail: theArr[8],
                    ownerPostalAddress: theArr[9],
                    cooperationMode: _me.getCooperationMode(theArr[10], i + 1),
                    introduction: theArr[11],
                });
            } else if (rowsArr[i].replace(/(^\s+)|(\s+$)/g, "").length) {
                message.warning("第" + (i + 1) + "行数据长度错误！");
                return;
            };
        };
        // for (let i = 0; i < columnArr.length; i++) {
        //     if (columnArr[i].dataCategory.toString().length > 1) { console.log((i + 1) + "行：" + "dataCategory") }
        //     if (columnArr[i].name.toString().length > 31) { console.log((i + 1) + "行：" + "name") }
        //     if (columnArr[i].category.toString().length > 1) { console.log((i + 1) + "行：" + "category") }
        //     if (columnArr[i].ownerName.toString().length > 31) { console.log((i + 1) + "行：" + "ownerName") }
        //     if (columnArr[i].ownerIdNumber.toString().length > 35) { console.log((i + 1) + "行：" + "ownerIdNumber") }
        //     if (columnArr[i].ownerMobile.toString().length > 11) { console.log((i + 1) + "行：" + "ownerMobile") }
        //     if (columnArr[i].ownerEmail.toString().length > 254) { console.log((i + 1) + "行：" + "ownerEmail") }
        //     if (columnArr[i].ownerPostalAddress.toString().length > 254) { console.log((i + 1) + "行：" + "ownerPostalAddress") }
        //     if (columnArr[i].cooperationMode.toString().length > 1) { console.log((i + 1) + "行：" + "cooperationMode") }
        //     if (columnArr[i].introduction.toString().length > 298) { console.log((i + 1) + "行：" + "introduction") }
        //     if (columnArr[i].keyword.toString().length > 44) { console.log((i + 1) + "行：" + "ownerType") }
        //     if (columnArr[i].ownerType.toString().length > 1) { console.log((i + 1) + "行：" + "ownerType") }
        // }
        if (columnArr.length > 1000) {
            message.warning("数据条数不能多于1000条！");
            return;
        };
        this.setState({
            loading: true
        });
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/admin/achievement/importAchievement',
            data: {
                data: JSON.stringify(columnArr)
            }
        }).done(function (data) {
            this.setState({
                loading: false
            });
            if (!data.error.length) {
                message.success('导入成功！');
                this.props.closeDesc(false, true);
                this.handleOk();
            } else {
                message.warning(data.error[0].message);
            };
        }.bind(this));
    },
    render () {
        return (
            <div>
                <Button type="primary" onClick={this.showModal}>批量导入</Button>
                <Modal
                    title="批量导入"
                    visible={this.state.visible}
                    onCancel={this.handleCancel}
                    width='1200px'
                    footer={[
                        <Button key="ok" className="set-submit" type="primary" loading={this.state.loading} onClick={this.handleSubmit}>保存</Button>,
                        <Button key="clancel" className="set-submit" type="ghost" onClick={this.handleCancel}>取消</Button>
                    ]} >
                    <Input type="textarea" rows={12} onChange={(e) => { this.state.inputValue = e.target.value; }} />
                </Modal>
            </div>
        );
    }
});

export default BatchImport;