import React from 'react';
import { AutoComplete, Icon, Button, Input, InputNumber, Select, Spin, Table, message, Modal, Row, Col, DatePicker } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import moment from 'moment';
import './techDemand.less';
import { orderStatusList } from '../../dataDic.js';
import { befoFile, getSearchUrl, getOrderStatus, companySearch } from '../../tools.js';
import BatchImport from './batchImport';
import throttle from '../../throttle.js';
const DemandDetail = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            columns: [
                {
                    title: '状态',
                    dataIndex: 'status',
                    key: 'status',
                    render: text => { return getOrderStatus(text) }
                }, {
                    title: '操作时间',
                    dataIndex: 'recordTimeFormattedDate',
                    key: 'recordTimeFormattedDate',
                }, {
                    title: '管理员',
                    dataIndex: 'operator',
                    key: 'operator',
                }, {
                    title: '备注',
                    dataIndex: 'comment',
                    key: 'comment',
                }
            ],
            dataSource: [],
        };
    },
    loadLogs(id) {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/portal/order/demandOrderLog",
            data: {
                demandOrderId: id
            },
            success: function (data) {
                let theArr = [];
                if (!data.data) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                } else {
                    for (let i = 0; i < data.data.length; i++) {
                        let thisdata = data.data[i];
                        theArr.push({
                            key: i,
                            recordTime: thisdata.recordTime,
                            status: thisdata.status,
                            comment: thisdata.comment,
                            operator: thisdata.operator,
                            recordTimeFormattedDate: thisdata.recordTimeFormattedDate
                        });
                    };
                };
                this.setState({
                    dataSource: theArr
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    handleSubmit(e) {
        //keyword长度判断
        if (this.state.orderStatus || this.state.comment || this.state.recordTime) {
            if (this.state.orderStatus && this.state.comment && this.state.recordTime) {
            } else {
                message.warning('请填写完整的状态流转记录!');
                return;
            };
        };
        this.setState({
            loading: true
        });
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/admin/portal/order/updateDemandOrder',
            data: {
                id: this.props.data.id,
                demandId: this.props.data.demandId,
                enclosureUrl: this.props.data.enclosureUrl,
                status: this.state.orderStatus || this.props.data.status,
                recordTimeFormattedDate: this.state.recordTime ? this.state.recordTime.format("YYYY-MM-DD HH:mm:ss") : undefined,
                comment: this.state.comment,
                intentionMoney: this.state.intentionMoney * 10000,
                contractMoney: this.state.contractMoney * 10000,
                commission: this.state.commission * 10000,
                serviceMoney: this.state.serviceMoney * 10000,
            }
        }).done(function (data) {
            this.setState({
                loading: false
            });
            if (!data.error.length) {
                message.success('保存成功!');
                this.handleOk();
            } else {
                message.warning(data.error[0].message);
            };
        }.bind(this));
    },
    handleCancel(e) {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false);
    },
    handleOk(e) {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false, true);
    },
    componentWillReceiveProps(nextProps) {
        if (!this.state.visible && nextProps.showDesc && nextProps.data) {
            this.loadLogs(nextProps.data.id);
            this.state.intentionMoney = nextProps.data.intentionMoney;
            this.state.contractMoney = nextProps.data.contractMoney;
            this.state.commission = nextProps.data.commission;
            this.state.serviceMoney = nextProps.data.serviceMoney;
            this.state.orderStatus = undefined;
            this.state.comment = undefined;
            this.state.recordTime = undefined;
        };
        this.state.visible = nextProps.showDesc;
    },
    render() {
        let theData = this.props.data;
        if (theData) {
            return (
                
            );
        } else {
            return 
        }
    },
});
const DemandOrderList = React.createClass({
    loadData(pageNo, apiUrl) {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + (apiUrl || this.props['data-listApiUrl']),
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                status: this.state.status,
                username: ((apiUrl || this.props['data-listApiUrl']).indexOf('org') != -1) ? null : this.state.searchName,
                unitName: ((apiUrl || this.props['data-listApiUrl']).indexOf('org') != -1) ? this.state.searchName : null
            },
            success: function (data) {
                let theArr = [];
                if (!data.data || !data.data.list) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                } else {
                    for (let i = 0; i < data.data.list.length; i++) {
                        let thisdata = data.data.list[i];
                        theArr.push({
                            key: i,
                            id: thisdata.id,  //线索单/意向单ID
                            serialNumber: thisdata.serialNumber,  //线索单/意向单编号
                            demandId: thisdata.demandId, //科技需求ID
                            createTime: thisdata.createTime,
                            enclosureUrl: thisdata.enclosureUrl,
                            status: thisdata.status, 	//线索单/意向单状态
                            intentionMoney: thisdata.intentionMoney / 10000, 	//意向金
                            contractMoney: thisdata.contractMoney / 10000, 	//合同金
                            commission: thisdata.commission / 10000, 	//佣金
                            serviceMoney: thisdata.serviceMoney / 10000, 	//服务金
                            name: thisdata.name, //需求名称,
                            demandSerialNumber: thisdata.demandSerialNumber, //科技需求编号
                            deletedSign: thisdata.deletedSign, 	//科技需求删除标记
                            auditStatus: thisdata.auditStatus, 	//科技需求审核状态
                            theName: thisdata.username || thisdata.unitName, 	//个人用户名称
                            uid: thisdata.uid,	//个人用户ID
                            createTimeFormattedDate: thisdata.createTimeFormattedDate,	//接单时间
                        });
                    };
                }
                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));
    },
    getInitialState() {
        return {
            selectedRowKeys: [],
            selectedRows: [],
            searchData: [],
            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: 'serialNumber',
                    key: 'serialNumber',
                }, {
                    title: '需求名称',
                    dataIndex: 'name',
                    key: 'name',
                }, {
                    title: '需求编号',
                    dataIndex: 'demandSerialNumber',
                    key: 'demandSerialNumber',
                }, {
                    title: '雇主编号',
                    dataIndex: 'theName',
                    key: 'theName',
                }, {
                    title: '订单状态',
                    dataIndex: 'auditStatus',
                    key: 'auditStatus',
                    render: (text, record) => {
                        if (record.deletedSign == 1) {
                            return "已删除"
                        } else if (record.auditStatus != 3) {
                            return "已下架"
                        } else {
                            return "正常"
                        }
                    }
                }, {
                    title: '流程状态',
                    dataIndex: 'status',
                    key: 'status',
                    render: (text, record) => { return getOrderStatus(text); }
                }, {
                    title: '意向金(万元)',
                    dataIndex: 'intentionMoney',
                    key: 'intentionMoney',
                }, {
                    title: '合同金(万元)',
                    dataIndex: 'contractMoney',
                    key: 'contractMoney',
                }, {
                    title: '佣金(万元)',
                    dataIndex: 'commission',
                    key: 'commission',
                }, {
                    title: '服务金(万元)',
                    dataIndex: 'serviceMoney',
                    key: 'serviceMoney',
                }, {
                    title: '接单时间',
                    dataIndex: 'createTimeFormattedDate',
                    key: 'createTimeFormattedDate',
                }
            ],
            dataSource: [],
        };
    },
    handleSearch(e) {
        if (this.props['data-detailApiUrl'].indexOf('org') != -1) {
            $.ajax({
                method: "get",
                dataType: "json",
                crossDomain: false,
                url: globalConfig.context + "/api/admin/achievement/orgOwner",
                data: { name: e },
                success: function (data) {
                    let theArr = [];
                    let theObj = {};
                    if (!data.data) {
                        if (data.error && data.error.length) {
                            message.warning(data.error[0].message);
                        };
                    } else if (!$.isEmptyObject(data.data)) {
                        data.data.map(function (item) {
                            theArr.push(item.unitName);
                            theObj[item.unitName] = item.uid;
                        });
                    };
                    this.setState({
                        searchData: theArr,
                        dataSourceObj: theObj
                    });
                }.bind(this),
            });
        } else {
            $.ajax({
                method: "get",
                dataType: "json",
                crossDomain: false,
                url: globalConfig.context + "/api/admin/achievement/userOwner",
                data: { name: e },
                success: function (data) {
                    let theArr = [];
                    let theObj = {};
                    if (!data.data) {
                        if (data.error && data.error.length) {
                            message.warning(data.error[0].message);
                        };
                    } else if (!$.isEmptyObject(data.data)) {
                        for (let item in data.data) {
                            theArr.push(data.data[item]);
                            theObj[data.data[item]] = item;
                        };
                    };
                    this.setState({
                        dataSource: theArr,
                        dataSourceObj: theObj
                    });
                }.bind(this),
            });
        };
    },
    componentWillMount() {
        this.state.therottleSearch = throttle(this.handleSearch, 1000, { leading: false }).bind(this);
        let theArr = [], typeArr = [];
        orderStatusList.map(function (item) {
            theArr.push(
                {item.key}
            )
        });
        this.state.demandTypeOption = typeArr;
        this.state.orderStatusOption = theArr;
        // if (window.location.search) {
        //     let theObj = getSearchUrl(window.location.search);
        //     if (theObj.rid) {
        //         theObj.id = theObj.rid;
        //         if (theObj.rid != 'null') {
        //             this.tableRowClick(theObj);
        //         };
        //     };
        // };
        this.loadData();
    },
    componentWillReceiveProps(nextProps) {
        if (this.props['data-listApiUrl'] != nextProps['data-listApiUrl']) {
            this.state.selectedRowKeys = [];
            this.state.selectedRows = [];
            this.state.status = undefined;
            this.state.searchName = null;
            this.state.searchData = [];
            this.loadData(null, nextProps['data-listApiUrl']);
        };
    },
    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/demand/delete",
            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));
    },
    addClick() {
        this.state.RowData = {};
        this.setState({
            showDesc: true
        });
    },
    closeDesc(e, s) {
        this.state.showDesc = e;
        if (s) {
            this.loadData();
        };
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.searchName = undefined;
        this.state.status = undefined;
        this.loadData();
    },
    render() {
        const rowSelection = {
            selectedRowKeys: this.state.selectedRowKeys,
            onChange: (selectedRowKeys, selectedRows) => {
                this.setState({
                    selectedRows: selectedRows.splice(-1),
                    selectedRowKeys: selectedRowKeys.splice(-1)
                });
            }
        };
        const hasSelected = this.state.selectedRowKeys.length > 0;
        return (
            
                
                    {this.props['data-listApiUrl'].indexOf('org') == -1 ? 个人需求订单管理 : 组织需求订单管理}
                
                
                    
                     {
                            this.state.searchName = e;
                        }} />
                    
                    
                 
                
                
             
        );
    }
});
export default DemandOrderList;
//