import React,{Component} from 'react';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import moment from 'moment';
import './public.less';
import { Icon, Button,Form, Input, Select, Spin, Table, Switch, message, DatePicker, Modal } from 'antd';
import OrderDesc from '@/manageCenter/financialManage/orderDetail/orderDesc';
import Assign from '@/manageCenter/publicComponent/assign';
import {getProcessStatus,getApproval} from '@/tools';
const FormItem = Form.Item;

const PaiDan = React.createClass({
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            selectedRowKeys:[],
            selectedRowKey:[],
            page:pageNo,
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/financial/allUnassignedOrder",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                buyerName: this.state.nameSearch,//客户名称
                orderNo:this.state.orderNoSearch,
                startTime: this.state.releaseDate[0],
                endTime: this.state.releaseDate[1],
				processStatus:this.state.processStatus
            },
            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,
                            orderNo: thisdata.orderNo,
                            approval:thisdata.approval,
		                    orderType:thisdata.orderType,
		                    processStatus:thisdata.processStatus,
                            signTotalAmount:thisdata.signTotalAmount,
                            buyerName:thisdata.buyerName,
                            sellerName:thisdata.sellerName,
                            createTime:thisdata.createTime?new Date(thisdata.createTime).toLocaleString():'',
                            signTime:thisdata.signTime,
							contractNo:thisdata.contractNo
                        });
                    };
                }
                this.state.pagination.current = data.data.pageNo;
                this.state.pagination.total = data.data.totalCount;
                if(data.data&&data.data.list&&!data.data.list.length){
					this.state.pagination.current=0
					this.state.pagination.total=0
				}
                this.setState({
                    totalPage:data.data.totalPage,
                    dataSource: theArr,
                    pagination: this.state.pagination
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            searchMore: true,
            assignVisible:false,
            releaseDate: [],
            totalPage:0,
            boHuivisible:false,
            selectedRowKeys: [],
            selectedRows: [],
            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: 'orderNo',
                    key: 'orderNo',
                }, {
                    title: '合同编号',
                    dataIndex: 'contractNo',
                    key: 'contractNo',
                }, {
                    title: '下单时间',
                    dataIndex: 'createTime',
                    key: 'createTime',
                },  {
                    title: '签单时间',
                    dataIndex: 'signTime',
                    key: 'signTime',
                },
                {
                    title: '签单客户',
                    dataIndex: 'buyerName',
                    key: 'buyerName',
                },
                {
                    title: '流程状态',
                    dataIndex: 'processStatus',
                    key: 'processStatus',
                    render: text => { return getProcessStatus(text)}
                },
                {
                    title: '订单总金额(万)',
                    dataIndex: 'signTotalAmount',
                    key: 'signTotalAmount'
                },
                {
                    title: '是否特批',
                    dataIndex: 'approval',
                    key: 'approval',
                    render: text => { return getApproval(text)}
                },
                {
                    title: '订单负责人',
                    dataIndex: 'sellerName',
                    key: 'sellerName'
                },
                {
                    title:'操作',
                    dataIndex: 'caozuo',
                    key: 'caouzo',
                    render:(text,recard)=>{
                        return <div>
                            <Button type="primary" onClick={(e)=>{e.stopPropagation(),this.evaluate(recard)}}>分派</Button>
                        </div>
                    }
                }
            ],
            dataSource: [],
            searchTime: [,]
        }
    },
    /* 分派 */
    evaluate(recard){
        this.state.assignData=recard;
        this.setState({
            assignVisible:true
        })
    },
    /* 驳回 */
    reject(recard){
       this.setState({
           bohuiData:recard,
           boHuiVisible:true,
           content:''
       })
    },
    boHuiOk(){
        this.setState({
            boHuiVisible:false
        });
        if((this.state.pagination.current==this.state.totalPage)&&(this.state.pagination.total)%10==1){
            this.loadData((this.state.page)-1);
        }else{
            this.loadData(this.state.page);
        };
    },
    boHuiCancel(e){
        this.setState({
            boHuiVisible:false
        })
    },
    componentWillMount() {
        this.loadData();
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        this.setState({
            showDesc: true,
        });
    },
    closeDesc(e, s) {
        this.state.showDesc = e;
        if (s) {
            this.loadData(this.state.page);
        };
    },
    closeAssign(e, s) {
        this.state.assignVisible = e;
        if (s) {
            if((this.state.pagination.current==this.state.totalPage)&&(this.state.pagination.total)%10==1){
                this.loadData((this.state.page)-1);
            }else{
                this.loadData(this.state.page);
            }
        };
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.nameSearch = '';
        this.state.releaseDate = [];
        this.state.orderNoSearch='';
		this.state.processStatus = [];
        this.loadData();
    },
    searchSwitch() {
        this.setState({
            searchMore: !this.state.searchMore
        });
    },
    render() { 
        const rowSelection = {
            selectedRowKeys: this.state.selectedRowKeys,
            onChange: (selectedRowKeys, selectedRows) => {
                this.setState({
                    selectedRows: selectedRows.slice(-1),
                    selectedRowKeys: selectedRowKeys.slice(-1)
                });
            }
        };
        const hasSelected = this.state.selectedRowKeys.length > 0;
        const { RangePicker } = DatePicker;
        return ( <div className="user-content">
            <div className="content-title">
                    <span>派单管理</span>
                </div>
                <div className="user-search">
                    <Input placeholder="客户名称"
                        value={this.state.nameSearch}
                        onChange={(e) => { this.setState({ nameSearch: e.target.value }); }} />
                    <Input placeholder="订单编号"
                        value={this.state.orderNoSearch}
                        onChange={(e) => { this.setState({ orderNoSearch: e.target.value }); }} />
					<Select placeholder="流程状态"
							onChange={(e)=>{this.setState({processStatus:e})}} 
							style={{width:160,marginRight:5}} 
							value={this.state.processStatus}>
						<Option value="2">未分配</Option>
						<Option value="3">已分配</Option>
					</Select>
                    <span style={{ marginRight:10}}>下单时间 :</span>
                        <RangePicker
                            value={[this.state.releaseDate[0] ? moment(this.state.releaseDate[0]) : null,
                            this.state.releaseDate[1] ? moment(this.state.releaseDate[1]) : null]}
                            onChange={(data, dataString) => { this.setState({ releaseDate: dataString }); }} />
                    <Button type="primary" onClick={this.search} style={{ marginLeft:10}}>搜索</Button>
                    <Button onClick={this.reset}>重置</Button>
                </div>
                <div className="patent-table">
                    <Spin spinning={this.state.loading}>
                        <Table columns={this.state.columns}
                            dataSource={this.state.dataSource}
                            rowSelection={false}
                            pagination={this.state.pagination}
                            onRowClick={this.tableRowClick.bind(this)} />
                    </Spin>
                </div>
                <OrderDesc
                    data={this.state.RowData}
                    showDesc={this.state.showDesc}
                    closeDesc={this.closeDesc.bind(this)}
                />
                <Assign
                    title='订单'
                    selApi="/api/admin/financial/distributionFinance"
                    data={this.state.assignData}
                    showDesc={this.state.assignVisible}
                    closeDesc={this.closeAssign.bind(this)
					}
					requestMethod={"post"}
                />
                <Modal visible={this.state.boHuiVisible} 
                        width="400px" 
                        title="驳回备注"
                        footer=''
                        onOk={this.boHuiOk} 
                        onCancel={this.boHuiCancel}
                >
                    <Form layout="horizontal" onSubmit={this.boHuiSubmit}>
			                <Spin spinning={this.state.loading}>
                                <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label={<span><strong style={{color:'#f00'}}>*</strong>驳回原因</span>}>
									<Input
										type="textarea"
										rows={4}
										placeholder="请输入驳回原因"
										value={this.state.content}
										onChange={(e) => {
											this.setState({ content: e.target.value });
										}}
									/>
								</FormItem>   
                                <FormItem wrapperCol={{ span: 12, offset:5 }}>  
                                        <Button type="primary" htmlType="submit"  style={{marginRight:20}}>驳回</Button>
                                        <Button type="default" onClick={()=>{this.boHuiCancel()}}>取消</Button>
                                </FormItem>        
                            </Spin>
                        </Form>
                </Modal>
        </div> )
    }
})
 
export default PaiDan;