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 { Button,Form, Input, Spin, Table, Switch, message, DatePicker, Modal } from 'antd';
import OrderDesc from '../orderDetail/orderDesc'
import Receivable from './receivable';
import {getLiquidationStatus,getNewOrderStatus} from '@/tools';
const FormItem = Form.Item;

const ShouKuang = React.createClass({
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            page:pageNo,
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/financial/financeList",
            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],
            },
            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,
		                    orderType:thisdata.orderType,
		                    orderStatus:thisdata.orderStatus,
		                    sellerName:thisdata.processStatus,
		                    liquidationStatus:thisdata.liquidationStatus,
		                    approval:thisdata.approval,
		                    buyerName:thisdata.buyerName,
                            actuallyTotalAmount:thisdata.actuallyTotalAmount,
                            signTime:thisdata.releaseDate?(new Date(thisdata.releaseDate)).toLocaleString():''
                        });
                    };
                }
                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({
                    dataSource: theArr,
                    pagination: this.state.pagination
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            searchMore: true,
            assignVisible:false,
            releaseDate: [],
            assignData:[],
            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: 'signTime',
                    key: 'signTime',
                },
                {
                    title: '已收款',
                    dataIndex: 'actuallyTotalAmount',
                    key: 'actuallyTotalAmount',
                },
                {
                    title: '客户名称',
                    dataIndex: 'buyerName',
                    key: 'buyerName',
                },
                {
                    title: '订单状态',
                    dataIndex: 'orderStatus',
                    key: 'orderStatus',
                    render: text => { return getNewOrderStatus(text)}
                },
                {
                    title: '订单负责人',
                    dataIndex: 'sellerName',
                    key: 'sellerName'
                },
                {
                    title: '结算状态',
                    dataIndex: 'liquidationStatus',
                    key: 'liquidationStatus',
                    render: text => { return getLiquidationStatus(text)}
                },
                {
                    title:'操作',
                    dataIndex: 'caozuo',
                    key: 'caouzo',
                    render:(text,recard)=>{
                        return <div>
                            <Button type="primary" style={{margin:'0 10px'}} onClick={(e)=>{e.stopPropagation(),this.evaluate(recard)}}>添加收款记录</Button>
                            <Button type="danger" onClick={(e)=>{e.stopPropagation(),this.reject(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
        })
        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.assignData={};
        this.state.assignVisible = e;
        if (s) {
            this.loadData(this.state.page);
        };
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.nameSearch = '';
        this.state.releaseDate = [];
        this.state.orderNoSearch='';
        this.loadData();
    },
    //驳回
    boHuiSubmit(e){
        e.preventDefault();
        if(!this.state.content){
            message.warning('请填写驳回原因');
            return false;
        }
        this.setState({
            loading:true
        })
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/user/achievement/delete",
            data: {
                id: this.state.bohuiData.id,
                reason:this.state.content
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success('驳回成功！');
                this.setState({
                    loading: false
                });
                if((this.state.total)%10==1){
                    this.loadData((this.state.page)-1);
                }else{
                    this.loadData(this.state.page);
                }
            } else {
                message.warning(data.error[0].message);
            };
        }.bind(this));
    },
    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;
        const hasSelect = this.state.selectedRowKeys.length;
        return ( <div className="user-content">
            <div className="content-title">
                    <span>收款结算</span>
                </div>
                <div className="user-search">
                    <Input placeholder="订单编号"
                        value={this.state.orderNoSearch}
                        onChange={(e) => { this.setState({ orderNoSearch: e.target.value }); }} />  
                    <Input placeholder="客户名称"
                        value={this.state.nameSearch}
                        onChange={(e) => { this.setState({ nameSearch: e.target.value }); }} />
                    <Button type="primary" onClick={this.search}>搜索</Button>
                    <Button onClick={this.reset}>重置</Button>
                    <span>更多搜索<Switch defaultChecked={false} onChange={this.searchSwitch.bind(this)} /></span>
                    <div className="search-more" style={this.state.searchMore ? { display: 'none' } : {}}>
                        <span>订单时间 :</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 }); }} />
                    </div>
                </div>
                <div className="patent-table">
                    <Spin spinning={this.state.loading}>
                        <Table columns={this.state.columns}
                            dataSource={this.state.dataSource}
                            rowSelection={rowSelection}
                            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)}
                />
                <Receivable
                    data={this.state.assignData}
                    showDesc={this.state.assignVisible}
                    closeDesc={this.closeAssign.bind(this)}
                />
                <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 ShouKuang;