import React from 'react';
import { Icon, Button, Input, Select, Spin, Table, Switch, message, DatePicker } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import moment from 'moment';
import './userList.less';
import OrgDesc from './orgDesc.jsx';
import { auditStatusList } from '../../dataDic.js';

const UserList = 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,
                mobile: this.state.searchLoginId, //登录名
                email: this.state.searchEmail,
                createTime: this.state.searchTime,
                number: this.state.searchAftId, //阿凡提号
                aftUsername: this.state.searchAftUsername,
                auditStatus: this.state.searchAuditStatus
            },
            success: function (data) {
                if (data.error.length || !data.data || !data.data.list) {
                    message.warning(data.error[0].message);
                    return;
                }
                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,
                        number: thisdata.number,
                        auditStatus:thisdata.auditStatus,
                        createTimeFormattedDate: thisdata.createTimeFormattedDate
                    });
                };
                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: [],
            loading: false,
            searchTime:[,],
            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: 'auditStatus',
                    key: 'auditStatus',
                    render: text => {
                        switch (String(text)) {
                            case "0":
                                return "未提交审核";
                            case "1":
                                return "提交审核";
                            case "2":
                                return "审核未打款";
                            case "3":
                                return "审核已打款";
                            case "4":
                                return "认证未通过";
                            case "5":
                                return "已认证";
                        }
                    },
                }, {
                    title: '注册时间',
                    dataIndex: 'createTimeFormattedDate',
                    key: 'createTimeFormattedDate',
                }, {
                    title: '认证名称',
                    dataIndex: 'aftUsername',
                    key: 'aftUsername',
                }, {
                    title: '联系方式',
                    dataIndex: 'email',
                    key: 'email',
                }, {
                    title: '业务员',
                    dataIndex: 'author',
                    key: 'author',
                }
            ],
            dataSource: []
        };
    },
    componentWillMount() {
        this.loadData();
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        this.setState({
            showDesc: true
        });
    },
    closeDesc(e) {
        this.state.showDesc = e;
        this.loadData();
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.searchAftId = "";
        this.state.searchLoginId = "";
        this.state.searchAuditStatus = "";
        this.state.searchAftUsername = "";
        this.state.searchEmail = "";
        this.state.searchTime = [,];
        this.loadData();
    },
    searchSwitch() {
        this.setState({
            searchMore: !this.state.searchMore
        });
    },
    searchcreateTime(date, dateString) {
        this.state.createTime = dateString;
    },
    render() {
        const { RangePicker } = DatePicker;
        return (
            <div className="user-content" >
                <div className="content-title">
                    <span>组织用户管理</span>
                </div>
                <div className="user-search">
                    <Input placeholder="阿凡提号" value={this.state.searchAftId}
                        onChange={(e) => { this.setState({ searchAftId: e.target.value }); }} />
                    <Input placeholder="登录号" value={this.state.searchLoginId}
                        onChange={(e) => { this.setState({ searchLoginId: e.target.value }); }} />
                    <Select placeholder="选择认证状态" style={{ width: 140 }}
                        value={this.state.searchAuditStatus}
                        onChange={(e) => { this.setState({ searchAuditStatus: e }) }}>
                        {
                            auditStatusList.map(function (item, i) {
                                return <Select.Option key={i} value={item.value} >{item.key}</Select.Option>
                            })
                        }
                    </Select>
                    <Input placeholder="认证名称" value={this.state.searchAftUsername}
                        onChange={(e) => { this.setState({ searchAftUsername: e.target.value }); }} />
                    <Input placeholder="联系方式" value={this.state.searchEmail}
                        onChange={(e) => { this.setState({ searchEmail : e.target.value }); }} />
                    <Button type="primary" onClick={this.search}>搜索</Button>
                    <Button onClick={this.reset}>重置</Button>
                    <span>更多搜索<Switch defaultChecked={false} onChange={this.searchSwitch} /></span>
                    <div className="search-more" style={this.state.searchMore ? { display: 'none' } : {}}>
                        <p>
                            <span>注册时间：</span>
                            <RangePicker value={[moment(this.state.searchTime[0]),moment(this.state.searchTime[1])]}
                                onChange={(data, dataString) => { this.setState({searchTime : dataString}); }} />
                        </p>
                    </div>
                </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>
                <OrgDesc data={this.state.RowData} showDesc={this.state.showDesc} closeDesc={this.closeDesc} />
            </div >
        );
    }
});

export default UserList;