import React from 'react';
import { Icon, Button, Select, Spin, Table, message, Input, Cascader } from 'antd';
import { provinceSelect, getProvince } from '../../../NewDicProvinceList';
import { companySearch, getTechnologyState, getSearchUrl } 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/admin/techproject/listTechProject",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                province: this.state.province ? this.state.province[0] : undefined,
                uid: this.state.unitName,
                contractId: this.state.contractId
            },
            success: function (data) {
                let theArr = [];
                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;
                    }
                    theArr.push({
                        key: i,
                        id: thisdata.id,
                        uid: thisdata.uid,
                        serialNumber: thisdata.serialNumber,
                        contractNumber: thisdata.contractNumber,
                        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,
                        founder: thisdata.founder, //创建人
                        techPrincipal: thisdata.techPrincipal //技术员
                    });
                };
                this.state.pagination.current = data.data.pageNo;
                this.state.pagination.total = data.data.totalCount;
                this.setState({
                    dataSource: theArr,
                    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 + "/api/admin/techproject/getPrincipal",
            success: function (data) {
                if (!data.data) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                    return;
                };
                let _me = this;
                for (var item in data.data) {
                    _me.state.authorOption.push(
                        {data.data[item]}
                    )
                };
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getSalesmanList() {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/salesman",
            success: function (data) {
                if (!data.data) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    }
                    return;
                };
                let _me = this, theArr = [];
                for (var item in data.data) {
                    theArr.push(
                        {data.data[item]}
                    )
                };
                this.setState({
                    salesmanOption: theArr
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getConsultant() {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/techproject/getConsultant",
            success: function (data) {
                if (!data.data) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                    return;
                };
                let _me = this;
                for (var item in data.data) {
                    _me.state.consultantOption.push(
                        {data.data[item]}
                    )
                };
            }.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 + "/api/admin/getUnitNames",
            success: function (data) {
                if (!data.data) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    }
                    return;
                };
                let _me = this;
                for (var item in data.data) {
                    _me.state.companyOption.push(
                        {data.data[item]}
                    )
                    _me.state.companyAddOption.push(
                        {data.data[item]}
                    )
                };
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getContractList() {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/contract/serialNumber",
            success: function (data) {
                if (!data.data) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                    return;
                };
                let theArr = [];
                for (let i = 0; i < data.data.length; i++) {
                    let item = data.data[i];
                    theArr.push(
                        {item.serialNumber}
                    );
                };
                this.setState({
                    contractListOption: theArr
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            companyOption: [],
            companyAddOption: [],
            authorOption: [],
            consultantOption: [],
            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: 'contractNumber',
                    key: 'contractNumber'
                }, {
                    title: '省份',
                    dataIndex: 'province',
                    key: 'province',
                    render: text => { return getProvince(text) }
                }, {
                    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: 'founder',
                    key: 'founder'
                }, {
                    title: '技术员',
                    dataIndex: 'techPrincipal',
                    key: 'techPrincipal',
                }
            ],
            dataSource: []
        };
    },
    componentWillMount() {
        this.getAuthorList();
        this.getSalesmanList();
        this.getCompanyList();
        //this.getConsultant();
        this.getContractList();
        if (window.location.search) {
            let theObj = getSearchUrl(window.location.search);
            if (theObj.rid) {
                theObj.id = theObj.rid;
                if (theObj.rid != 'null' && theObj.uid != 'null') {
                    this.tableRowClick(theObj);
                };
            } else {
                if (theObj.uid) {
                    this.state.unitName = theObj.uid;
                } else if (theObj.contractId) {
                    this.state.contractId = theObj.contractId;
                };
            };
        };
        this.loadData();
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        if(index!=undefined){
        	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/admin/techproject/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, s) {
        this.state.showDesc = e;
        if (s) {
            this.loadData();
        }
    },
    closeAdd(e) {
        this.state.showAdd = e;
        this.loadData();
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.province = undefined;
        this.state.unitName = undefined;
        this.state.contractId = 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 (
            
                
                    科技项目申报管理
                
                
                     { this.setState({ province: e }) }} />
                    
                    
                    
                    
                    
                
                
                
                
             
        );
    }
});
export default ApplyManage;
//