import React from 'react';
import { Icon, Button, Select, Spin, Table, message } from 'antd';
import { provinceArr } from '../../../dataDic.js';
import { companySearch, getTechnologyState } from '../../../tools.js';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import './technology.less';

import ApplyAdd from './applyAdd.jsx';
import ApplyChange from './applyChange.jsx';


const ApplyManage = React.createClass({
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admintechproject/listTechProject",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                province: this.state.province,
                uid: 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];
                    switch (thisdata.contacts){
                    	case 1:
                    		thisdata.contacts = thisdata.firstContacts;
                    		break;
                    	case 2:
                    		thisdata.contacts = thisdata.secondContacts;
                    		break;
                    	case 3:
                    		thisdata.contacts = thisdata.thirdContacts;
                    		break;
                    }
                    this.state.data.push({
                        key: i,
                        pid: thisdata.id,
                        uid: thisdata.uid,
                        serialNumber: thisdata.serialNumber,
                        province: thisdata.province,
                        unitName: thisdata.unitName,
                        contacts: thisdata.contacts,
                        projectName: thisdata.projectName,
                        projectCatagory: thisdata.projectCatagory,
                        techField: thisdata.techField,
                        state: thisdata.state,
                        createTime: thisdata.createTimeFormattedDate,
                        website: thisdata.website,
                        accountNumber: thisdata.accountNumber,
                        password: thisdata.password,
                        consultant: thisdata.consultant
                    });
                };
                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 + "/techservice/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 + "/techservice/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={data.data[item]} key={data.data[item]}>{data.data[item]}</Select.Option>
                    )
                    _me.state.companyAddOption.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: [],
            companyAddOption: [],
            authorOption: [],
            selectedRows: [],
            selectedRowKeys: [],
            loading: false,
            showAdd: false,
            showDesc: 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: 'serialNumber',
                    key: 'serialNumber'
                }, {
                    title: '省份',
                    dataIndex: 'province',
                    key: 'province'
                }, {
                    title: '公司',
                    dataIndex: 'unitName',
                    key: 'unitName'
                }, {
                    title: '联系方式',
                    dataIndex: 'contacts',
                    key: 'contacts'
                }, {
                    title: '项目名称',
                    dataIndex: 'projectName',
                    key: 'projectName'
                }, {
                    title: '项目类型',
                    dataIndex: 'projectCatagory',
                    key: 'projectCatagory'
                },{
                    title: '技术领域',
                    dataIndex: 'techField',
                    key: 'techField'
                }, {
                    title: '项目情况',
                    dataIndex: 'state',
                    key: 'state',
                    render: text => { return getTechnologyState(text) },
                },{
                    title: '派单日',
                    dataIndex: 'createTime',
                    key: 'createTime'
                }, {
                    title: '网址',
                    dataIndex: 'website',
                    key: 'website'
                }, {
                    title: '用户名',
                    dataIndex: 'accountNumber',
                    key: 'accountNumber'
                }, {
                    title: '密码',
                    dataIndex: 'password',
                    key: 'password'
                }, {
                    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();
        //        this.getContactsList();
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        this.setState({
            showDesc: true
        });
    },
    addClick() {
        this.setState({
            showAdd: 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/admintechproject/deleteTechProject",
            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) {
        this.state.showDesc = e;
        this.loadData();
    },
    closeAdd(e) {
        this.state.showAdd = e;
        this.loadData();
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.province = undefined;
        this.state.unitName = 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="foster-content" >
                <div className="content-title">
                    <span>科技项目申报管理</span>
                    <div className="foster-query" style={{ float: "right" }}>
	                    <div>
	                        <Button style={{ background: "#ea0862", border: "none", color: "#fff" }}
	                            onClick={this.addClick}>申请新科技项目<Icon type="plus" /></Button>
	                    </div>
	                </div>
                </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="patent-table">
                    <Spin spinning={this.state.loading}>
                        <Table columns={this.state.columns}
                            rowSelection={rowSelection}
                            dataSource={this.state.dataSource}
                            pagination={this.state.pagination}
                            onRowClick={this.tableRowClick} />
                    </Spin>
                </div>
                <ApplyChange
                    data={this.state.RowData}
                    authorOption={this.state.authorOption}
                    showDesc={this.state.showDesc}
                    closeDesc={this.closeDesc} />
                <ApplyAdd
                    authorOption={this.state.authorOption}
                    companyAddOption={this.state.companyAddOption}
                    showAdd={this.state.showAdd}
                    closeAdd={this.closeAdd} />
            </div >
        );
    }
});

export default ApplyManage;