import React from 'react';
import ReactDom from 'react-dom';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import { Form,Radio, Icon, Button, Input, Select, Spin, Table, Switch, message, Modal, Upload ,Popconfirm,Checkbox} from 'antd';
import MyBillDetaile from "./myBillDetaile.jsx";
import moment from 'moment';
import '../../userMangagement.less';
import {lvl,intentProgress,transactionProgress,contractProgress,projectProgress} from '../../../../dataDic.js';
import {getIntentProgress,getTransactionProgress,getContractProgress,getProjectProgress,getTransactionChannel,getTransactionProject} from '../../../../tools.js';

const MyBill=Form.create()(React.createClass({
	loadData(pageNo) {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/admin/order/queryAllBill',
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                payerName:this.state.draweeName,//付款人名称
                depId:this.state.departmenttSearch,//部门
                confirmSign:this.state.confirmSignSearch,//流水确认
                payeeName:this.state.payeeName,//收款人名称
                deleteSign:this.state.term,//是否有效
                orderNo:this.state.orderNumber,//订单编号
                salesmanName:this.state.orderLeader,//订单负责人
            },
            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.list.length; i++) {
                        let thisdata = data.data.list[i];
                        theArr.push({
                             key: i,
	                         billNo: thisdata.billNo,
	                         transactionAmount:thisdata.transactionAmount,
	                         createTime:thisdata.createTime, 
	                         transactionAmount:thisdata.transactionAmount,
	                         payerName:thisdata.payerName,
	                         payeeName:thisdata.payeeName,
	                         transactionSubject:thisdata.transactionSubject,
	                         transactionChannel:thisdata.transactionChannel,
	                         financialPayNo:thisdata.financialPayNo,
	                         financialPayTime:thisdata.financialPayTime,
	                         orderNo:thisdata.orderNo,
	                         departmentName:thisdata.departmentName,
	                         salesmanName:thisdata.salesmanName,
	                         financeName:thisdata.financeName,
	                         confirmSign:thisdata.confirmSign,
	                         deleteSign:thisdata.deleteSign,
	                         confirmTime:thisdata.confirmTime
                        });
                    };
                    this.state.pagination.current = data.data.pageNo;
                    this.state.pagination.total = data.data.totalCount;
                };
                if(!data.data.list.length){
					this.state.pagination.current=0
					this.state.pagination.total=0
				}
                this.setState({
                	pageNo:pageNo,
                    dataSource: theArr,
                    pagination: this.state.pagination,
                    selectedRowKeys:[]
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    //部门
	departmentList() {
		this.setState({
			loading: true
		});
		$.ajax({
			method: "post",
			dataType: "json",
			crossDomain: false,
			url: globalConfig.context + "/api/admin/organization/selectSuperId",
			data: {
				
			},
			success: function(data) {
				let thedata = data.data;
				let theArr=[];
				if(!thedata) {
					if(data.error && data.error.length) {
						message.warning(data.error[0].message);
					};
					thedata = {};
				}else{
					thedata.map(function(item,index){
						theArr.push({
							key:index,
							name:item.name,
							id:item.id,
						})
					})
				}
				this.setState({
					departmentArr:theArr,
				})
			}.bind(this),
			}).always(function() {
				this.setState({
					loading: false
				});
			}.bind(this));
	},
    getInitialState() {
        return {
        	datauser:{},
            selectedRowKeys: [],
            selectedRows: [],
            searchMore: true,
            departmentArr:[],
            loading: false,
            visible: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: 'billNo',
                    key: 'billNo'
                }, {
                    title: '平台流水时间',
                    dataIndex: 'createTime',
                    key: 'createTime'
                }, {
                    title: '流水金额',
                    dataIndex: 'transactionAmount',
                    key: 'transactionAmount'
                },{
                    title: '付款人名称',
                    dataIndex: 'payerName',
                    key: 'payerName'
                }, {
                    title: '收款人名称',
                    dataIndex: 'payeeName',
                    key: 'payeeName'
                },
                {
                    title: '流水科目',
                    dataIndex: 'transactionSubject',
                    key: 'transactionSubject',
                    render:(text)=>{return getTransactionProject(text) }
                }, {
                    title: '流水渠道',
                    dataIndex: 'transactionChannel',
                    key: 'transactionChannel',
                    render:(text)=>{return getTransactionChannel(text) }
                },{
                    title: '财务流水号',
                    dataIndex: 'financialPayNo',
                    key: 'financialPayNo',
                }, {
                    title: '财务流水时间',
                    dataIndex: 'financialPayTime',
                    key: 'financialPayTime',
                }, {
                    title: '流水确认',
                    dataIndex: 'confirmSign',
                    key: 'confirmSign',
                    render:(text)=>{return text?'已确认':'待确认' }
                },{
                    title: '确认时间',
                    dataIndex: 'confirmTime',
                    key: 'confirmTime',
                },{
                    title: '订单编号',
                    dataIndex: 'orderNo',
                    key: 'orderNo',
                },{
                    title: '订单负责人',
                    dataIndex: 'salesmanName',
                    key: 'salesmanName',
                },{
                    title: '是否有效',
                    dataIndex: 'deleteSign',
                    key: 'deleteSign',
                    render:(text)=>{return !text?'有效':'锁定/作废' }
                },
            ],
            dataSource: [],
        };
    },
    //驳回
    reject(record){
    	this.state.userDetaile=true;
        this.state.datauser = record;
        this.setState({
        	rowClick:false,
            showDesc: true
        });
    },
    componentWillMount() {
    	this.loadData()
        this.departmentList();
    },
    closeDesc(e, s) {
    	this.state.userDetaile=false;
        this.state.showDesc = e;
        if (s) {
            this.loadData(this.state.pageNo);
        };
    },
    searchSwitch() {
		this.setState({
			searchMore: !this.state.searchMore
		});
	},
    search() {
        this.loadData();
    },
    reset() {
    	this.state.draweeName='';
    	this.state.payeeName='';
		this.state.term = undefined;
		this.state.orderNumber = '';
		this.state.confirmSignSearch=undefined;
		this.state.departmenttSearch = undefined;
		this.state.orderLeader = '';
        this.loadData();       
    },
    tableRowClick(record, index) {
    	this.state.userDetaile=true;
        this.state.datauser = record;
        this.setState({
        	rowClick:true,
        	signBillState:true,
            showDesc: true
        });
    },
    render() {
        const rowSelection = {
            selectedRowKeys: this.state.selectedRowKeys,
            onChange: (selectedRowKeys, selectedRows) => {
	                this.setState({
	                    selectedRows: selectedRows.slice(-1),
	                    selectedRowKeys: selectedRowKeys.slice(-1)
	                }); 
            },
            onSelectAll: (selected, selectedRows, changeRows) => {
			   this.setState({
			   		selectedRowKeys:[]
			   })
			},
        };
        let departmentArr = this.state.departmentArr || [];
        const hasSelected = this.state.selectedRowKeys.length > 0;
        return (
            <div className="user-content" >
                <div className="content-title">
                	<span style={{fontSize:'16px'}}>结算流水管理</span>	
	                <div className="user-search">
	                    <Input placeholder="付款人名称" style={{width:'150px'}}
	                        value={this.state.draweeName}
	                        onChange={(e) => { this.setState({ draweeName: e.target.value }); }} />
	                    <Input placeholder="收款人名称" style={{width:'150px'}}
	                        value={this.state.payeeName}
	                        onChange={(e) => { this.setState({ payeeName: e.target.value }); }} />    
                        <Select placeholder="流水确认"
	                        style={{ width: 150 ,marginRight:'10px'}}
	                        value={this.state.confirmSignSearch}
	                        onChange={(e) => { this.setState({ confirmSignSearch: e }) }}>
	                        <Select.Option value='0'>待确认</Select.Option>
	                        <Select.Option value='1'>已确认</Select.Option>
                  		</Select>
                  		<Select placeholder="是否有效"
	                        style={{ width: 150 ,marginRight:'10px'}}
	                        value={this.state.term}
	                        onChange={(e) => { this.setState({ term: e }) }}>
	                        <Select.Option value='0'>作废</Select.Option>
	                        <Select.Option value='1'>有效</Select.Option>
                  		</Select>
	                    <Button type="primary" onClick={this.search} style={{marginLeft:'10px'}}>搜索</Button>
	                    <Button onClick={this.reset}>重置</Button>
	                    <span style={{marginLeft:'10px',marginRight:'20px'}}>更多搜索<Switch defaultChecked={false} onChange={this.searchSwitch} /></span>    
	                </div>
	                <div className='clearfix' style={{marginTop:'5px'}}>
	                    <div className="search-more" style={this.state.searchMore ? { display: 'none' } : {}}> 
	                  		<Input placeholder="订单编号" style={{width:'150px'}}
		                        value={this.state.orderNumber}
		                        onChange={(e) => { this.setState({ orderNumber: e.target.value }); }} />
		                    <Select placeholder="订单部门"
	                            style={{ width: 150 ,marginRight:'10px',marginLeft:'10px'}}
	                            value={this.state.departmenttSearch}
	                            onChange={(e) => { this.setState({ departmenttSearch: e }) }}>
	                            {
                                    departmentArr.map(function (item) {
                                            return <Select.Option key={item.id} >{item.name}</Select.Option>
                                    })
                                }
	                    	</Select> 
		                    <Input placeholder="订单负责人" style={{width:'150px',marginBottom:'10px'}}
		                        value={this.state.orderLeader}
		                        onChange={(e) => { this.setState({ orderLeader: e.target.value }); }} />    
	                    </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}
                            />
	                    </Spin>
	                </div>
	                <MyBillDetaile
	                	rowClick={this.state.rowClick}
	                    userDetaile={this.state.userDetaile}
	                    datauser={this.state.datauser}
	                    showDesc={this.state.showDesc}
	                    closeDesc={this.closeDesc} />
	            </div >
            </div>
        );
    }
}));

export default MyBill;

