import React from 'react';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import moment from 'moment';
import '../distribute/public.less';
import {  Button, Input, Spin, Table, Switch, message, DatePicker } from 'antd';
import {getTransactionProject} from '@/tools';
import WaterDesc from './waterDetail';

const MyWater = 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/myBillList",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                payeeName: this.state.payeeNameSearch,
                payerName:this.state.payerNameSearch,
                startTime: this.state.releaseDate[0],
                endTime: this.state.releaseDate[1],
                orderNo:this.state.orderNo
            },
            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,
		                    billNo:thisdata.billNo,
		                    transactionAmount:thisdata.transactionAmount,
		                    payeeName:thisdata.payeeName,
		                    payerName:thisdata.payerName,
		                    transactionSubject:thisdata.transactionSubject,
		                    transactionChannel:thisdata.transactionChannel,
                            financialPayTimes:thisdata.financialPayTimes?(new Date(thisdata.financialPayTimes)).toLocaleString():'',
                            financialPayNo:thisdata.financialPayNo,
		                    createrName:thisdata.createrName,
		                    saleName:thisdata.saleName,
                        });
                    };
                }
                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,
            releaseDate: [],
            RowData:{},
            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: 'billNo',
                    key: 'billNo',   
                },
                {
                    title: '金额(万元)',
                    dataIndex: 'transactionAmount',
                    key: 'transactionAmount',
                },
                {
                    title: '收款方',
                    dataIndex: 'payeeName',
                    key: 'payeeName',
                }, 
                {
                    title: '付款方',
                    dataIndex: 'payerName',
                    key: 'payerName',
                },
                {
                    title: '流水科目',
                    dataIndex: 'transactionSubject',
                    key: 'transactionSubject',
                    render: text => { return getTransactionProject(text)}
                },
                {
                    title: '财务流水时间',
                    dataIndex: 'financialPayTimes',
                    key: 'financialPayTimes',
                }, {
                    title: '财务流水号',
                    dataIndex: 'financialPayNo',
                    key: 'financialPayNo'
                },
                {
                    title: '订单编号',
                    dataIndex: 'orderNo',
                    key: 'orderNo'
                },{
                    title: '订单负责人',
                    dataIndex: 'saleName',
                    key: 'saleName'
                },
            ],
            dataSource: [],
            searchTime: [,]
        }
    },
    componentWillMount() {
        this.loadData();
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        this.setState({
            showDesc: true,
        });
    },
    closeDesc(e, s) {
        this.state.RowData={};
        this.state.showDesc = e;
        if (s) {
            this.loadData(this.state.page);
        };
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.orderNo = '';
        this.state.payeeNameSearch='';
        this.state.releaseDate = [];
        this.state.payerNameSearch='';
        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.orderNo}
                        onChange={(e) => { this.setState({ orderNo: e.target.value }); }} />
                    <Input placeholder="收款方"
                        value={this.state.payeeNameSearch}
                        onChange={(e) => { this.setState({ payeeNameSearch: e.target.value }); }} /> 
                    <Input placeholder="付款方"
                        value={this.state.payerNameSearch}
                        onChange={(e) => { this.setState({ payerNameSearch: 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={false}
                            pagination={this.state.pagination}
                            onRowClick={this.tableRowClick}
                             />
                    </Spin>
                </div>
                <WaterDesc
                    data={this.state.RowData}
                    showDesc={this.state.showDesc}
                    closeDesc={this.closeDesc.bind(this)}
                />
        </div> )
    }
})
 
export default MyWater;