import React from 'react';
import { Icon, Button, Input, Select, Spin, Table, message, DatePicker, Modal } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import './userList.less';


const OrderList = React.createClass({
    loadData (pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/orgList",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize
            },
            success: function (data) {
                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];
                        this.state.data.push({
                            key: i,
                            id: thisdata.id,
                            mobile: thisdata.mobile,
                            email: thisdata.email,
                            createTime: thisdata.createTime,
                            aftUsername: thisdata.aftUsername,
                            managerName: thisdata.managerName,
                            number: thisdata.number,
                            orgAuditStatus: thisdata.orgAuditStatus,
                            createTimeFormattedDate: thisdata.createTimeFormattedDate,
                            adminName: thisdata.adminName,
                            unitName: thisdata.unitName
                        });
                    };
                    this.state.pagination.current = data.data.pageNo;
                    this.state.pagination.total = data.data.totalCount;
                };
                this.setState({
                    dataSource: this.state.data,
                    pagination: this.state.pagination
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState () {
        return {
            searchMore: true,
            data: [],
            searchTime: [],
            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: 'number',
                    key: 'number',
                }, {
                    title: '账号',
                    dataIndex: 'mobile',
                    key: 'mobile',
                }, {
                    title: '认证名称',
                    dataIndex: 'unitName',
                    key: 'unitName',
                }, {
                    title: '订单状态',
                    dataIndex: 'orderStatus',
                    key: 'orderStatus',
                }, {
                    title: '生成时间',
                    dataIndex: 'createTimeFormattedDate',
                    key: 'createTimeFormattedDate',
                }
            ],
            dataSource: []
        };
    },
    componentWillMount () {
        this.loadData();
    },
    search () {
        this.loadData();
    },
    reset () {
        this.state.searchOrderNumber = undefined;
        this.state.searchAftNumber = undefined;
        this.state.searchOrderStatus = undefined;
        this.state.searchName = undefined;
        this.state.searchTime = [];
        this.loadData();
    },
    render () {
        const { RangePicker } = DatePicker;
        const Option = Select.Option;
        return (
            <div className="user-content" >
                <div className="content-title">
                    <span>用户订单列表</span>
                </div>
                <div className="user-search">
                    <Input placeholder="订单编号" value={this.state.searchOrderNumber}
                        onChange={(e) => { this.setState({ searchOrderNumber: e.target.value }); }} />
                    <Input placeholder="用户账号" value={this.state.searchAftNumber}
                        onChange={(e) => { this.setState({ searchAftNumber: e.target.value }); }} />
                    <Input placeholder="认证名称" value={this.state.searchName}
                        onChange={(e) => { this.setState({ searchName: e.target.value }); }} />
                    <Select placeholder="订单状态" value={this.state.searchOrderStatus} style={{ width: 120 }}
                        onChange={(e) => { this.setState({ searchOrderStatus: e }) }}>
                        <Option value="0">未付款</Option>
                        <Option value="1">已付款</Option>
                    </Select>
                    <div className="search-div">
                        <span>订单生成时间：</span>
                        <RangePicker
                            allowClear={false}
                            value={[this.state.searchTime[0] ? moment(this.state.searchTime[0]) : null,
                            this.state.searchTime[1] ? moment(this.state.searchTime[1]) : null]}
                            onChange={(data, dataString) => { this.setState({ searchTime: dataString }); }} />
                    </div>
                    <Button type="primary" onClick={this.search}>搜索</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}
                            pagination={this.state.pagination}
                            onRowClick={this.tableRowClick} />
                    </Spin>
                </div>
            </div >
        );
    }
});

export default OrderList;