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 './content.less';
import { getTime } from '../../tools'


const OrderList = React.createClass({
    loadData (pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/user/consultorder/list",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                number: this.state.searchNumber,
                toNumber: this.state.searchToNumber,
                id: this.state.searchId,
                paymentId: this.state.searchPaymentId,
                state: this.state.searchState
            },
            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,
                            price: thisdata.price,
                            orderState: thisdata.orderState,
                            orderTime: thisdata.orderTime,
                            orderTimeFormatted: thisdata.orderTimeFormatted,
                            userId: thisdata.userId,
                            toUserId: thisdata.toUserId,
                            nickname: thisdata.nickname,
                            number: thisdata.number,
                            toNickname: thisdata.toUsername || thisdata.toNickname,
                            toNumber: thisdata.toNumber,
                            paymentTime: thisdata.paymentTime,
                            paymentTimeFormatted: thisdata.paymentTimeFormatted,
                            paymentValue: thisdata.paymentValue,
                            paymentType: thisdata.paymentType,
                            paymentId: thisdata.paymentId,
                        });
                    };
                    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,
            visible: false,
            deleteId: null,
            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: 'id',
                    key: 'id',
                }, {
                    title: '专家编号',
                    dataIndex: 'toNumber',
                    key: 'toNumber',
                }, {
                    title: '专家名称',
                    dataIndex: 'toNickname',
                    key: 'toNickname'
                }, {
                    title: '订单状态',
                    dataIndex: 'orderState',
                    key: 'orderState',
                    render: (text, record) => {
                        if (text == 1) {
                            return '已支付'
                        } else if (text == 9) {
                            return '已取消'
                        } else if (text == 2) {
                            return '支付中'
                        } else {
                            let now = new Date();
                            let endTime = new Date(record.orderTime + 24 * 60 * 60 * 1000 * 15)
                            if (now > endTime) {
                                return '已失效'
                            } else {
                                return '未支付'
                            }
                        }
                    }
                }, {
                    title: '生成时间',
                    dataIndex: 'orderTimeFormatted',
                    key: 'orderTimeFormatted',
                }, {
                    title: '支付金额',
                    dataIndex: 'price',
                    key: 'price',
                    render: (index, record) => {
                        if (record.paymentValue && record.paymentValue != 0) {
                            return record.paymentValue
                        } else {
                            return index
                        }
                    }
                }, {
                    title: '支付时间',
                    dataIndex: 'paymentTimeFormatted',
                    key: 'paymentTimeFormatted',
                }, {
                    title: '支付流水',
                    dataIndex: 'paymentId',
                    key: 'paymentId'
                }, {
                    title: '操作',
                    dataIndex: 'action',
                    key: 'action',
                    render: (text, record) => {
                        let now = new Date();
                        let endTime = new Date(record.orderTime + 24 * 60 * 60 * 1000 * 15)
                        if (record.orderState == 0 && now < endTime) {
                            return <div>
                                <Button type="primary" onClick={() => { }}>支付</Button>
                                <Button style={{ marginLeft: '10px' }} type="primary"
                                    onClick={() => { this.setState({ visible: true, deleteId: record.id }) }} >取消订单</Button>
                            </div>
                        } else if (record.orderState == 0) {
                            return <div><Button type="primary"
                                onClick={() => { this.setState({ visible: true, deleteId: record.id }) }}>取消订单</Button></div>
                        }
                    }
                }
            ],
            dataSource: []
        };
    },
    delRow () {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/user/consultorder/del",
            data: {
                id: this.state.deleteId
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success('删除成功！');
            } else {
                message.warning(data.error[0].message);
            };
            this.setState({
                deleteId: null,
                visible: false,
                loading: false,
            });
            this.loadData();
        }.bind(this));
    },
    componentWillMount () {
        this.loadData();
    },
    search () {
        this.loadData();
    },
    reset () {
        this.state.searchNumber = undefined;
        this.state.searchToNumber = undefined;
        this.state.searchId = undefined;
        this.state.searchPaymentId = undefined;
        this.state.searchState = undefined;
        this.state.searchTime = [];
        this.loadData();
    },
    render () {
        const { RangePicker } = DatePicker;
        const Option = Select.Option;
        return (
            <div className="collection" >
                <div className="content-title">
                    <span>咨询订单列表</span>
                </div>
                <div className="user-search">
                    <Input placeholder="会员编号" value={this.state.searchNumber}
                        onChange={(e) => { this.setState({ searchNumber: e.target.value }); }} />
                    <Input placeholder="专家编号" value={this.state.searchToNumber}
                        onChange={(e) => { this.setState({ searchToNumber: e.target.value }); }} />
                    <Input placeholder="订单编号" value={this.state.searchId}
                        onChange={(e) => { this.setState({ searchId: e.target.value }); }} />
                    <Select placeholder="订单状态" value={this.state.searchState} style={{ width: 120 }}
                        onChange={(e) => { this.setState({ searchState: e }) }}>
                        <Option value="0">未付款</Option>
                        <Option value="1">已付款</Option>
                        <Option value="9">已取消</Option>
                        <Option value="10">已失效</Option>
                    </Select>
                    <Input placeholder="支付流水" value={this.state.searchPaymentId}
                        onChange={(e) => { this.setState({ searchPaymentId: e.target.value }); }} />
                    <div className="search-div" style={{ display: 'none' }}>
                        <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>
                <Modal title="取消确认" width={600} className="activity-modal"
                    visible={this.state.visible}
                    maskClosable={false}
                    footer={null}
                    onCancel={() => { this.setState({ visible: false }) }} >
                    <Spin spinning={this.state.loading}>
                        <div className="clearfix">
                            <div>
                                <span>是否确定取消这个订单？取消后后订单不可恢复！</span>
                            </div>
                            <div className="modal-box" >
                                <Button type="primary" onClick={this.delRow}>确认</Button>
                                <Button type="ghost" onClick={() => { this.setState({ visible: false }) }}>取消</Button>
                            </div>
                        </div>
                    </Spin>
                </Modal>
            </div >
        );
    }
});

export default OrderList;