import React from 'react';
import { Spin, Button, Tabs, Table } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js'
import $ from 'jquery/src/ajax';
import DemandDesc from '@/portal/search/demandDesc';
import AchievementDesc from '@/portal/search/achievementDesc';
import { getOrderStatus } from '@/tools';

import './content.less';
import SpinContainer from "../../SpinContainer";

const TabPane = Tabs.TabPane;

const Content = React.createClass({
    achievementLoadData (pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/user/portal/order/achievementOrderList",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.achievementPagination.pageSize
            },
            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,
                            orderId: thisdata.id,
                            serialNumber: thisdata.serialNumber,
                            id: thisdata.achievementId,
                            createTime: thisdata.createTime,
                            status: thisdata.status,
                            name: thisdata.name,
                            achievementSerialNumber: thisdata.achievementSerialNumber,
                            deletedSign: thisdata.deletedSign,
                            auditStatus: thisdata.auditStatus,
                            createTimeFormattedDate: thisdata.createTimeFormattedDate,
                            paymentId: thisdata.paymentId,
                            paymentTime: thisdata.paymentTime,
                            paymentTimeFormattedDate: thisdata.paymentTimeFormattedDate,
                            paymentType: thisdata.paymentType,
                            paymentValue: thisdata.paymentValue,
                            transferPrice: thisdata.transferPrice
                        });
                    };
                }
                this.state.achievementPagination.current = data.data.pageNo;
                this.state.achievementPagination.total = data.data.totalCount;
                this.setState({
                    achievementDataSource: theArr,
                    achievementPagination: this.state.achievementPagination
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    demandLoadData (pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/user/portal/order/demandOrderList",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.demandPagination.pageSize
            },
            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,
                            orderId: thisdata.id,
                            name: thisdata.name,
                            demandSerialNumber: thisdata.demandSerialNumber,
                            id: thisdata.demandId,
                            createTime: thisdata.createTime,
                            createTimeFormattedDate: thisdata.createTimeFormattedDate,
                            serialNumber: thisdata.serialNumber,
                            status: thisdata.status,
                            auditStatus: thisdata.auditStatus,
                            deletedSign: thisdata.deletedSign,
                            intentionMoney: thisdata.intentionMoney,  //意向金
                            contractMoney: thisdata.contractMoney,  //合同金
                            commission: thisdata.commission,  //佣金
                            serviceMoney: thisdata.serviceMoney  //服务金
                        });
                    };
                }
                this.state.demandPagination.current = data.data.pageNo;
                this.state.demandPagination.total = data.data.totalCount;
                this.setState({
                    demandDataSource: theArr,
                    demandPagination: this.state.demandPagination
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState () {
        return {
            loading: false,
            demandShowDesc: false,
            achievementShowDesc: false,
            achievementPagination: {
                defaultCurrent: 1,
                defaultPageSize: 10,
                showQuickJumper: true,
                pageSize: 10,
                onChange: function (page) {
                    this.achievementLoadData(page);
                }.bind(this),
                showTotal: function (total) {
                    return '共' + total + '条数据';
                }
            },
            achievementColumns: [
                {
                    title: '编号',
                    dataIndex: 'serialNumber',
                    key: 'serialNumber',
                }, {
                    title: '成果名称',
                    dataIndex: 'name',
                    key: 'name',
                }, {
                    title: '订单状态',
                    dataIndex: 'auditStatus',
                    key: 'auditStatus',
                    render: (text, record) => {
                        if (record.deletedSign == 1) {
                            return "已删除"
                        } else if (record.auditStatus != 3) {
                            return "已下架"
                        } else {
                            return "正常"
                        }
                    }
                }, {
                    title: '流程状态',
                    dataIndex: 'status',
                    key: 'status',
                    render: (text, record) => { return getOrderStatus(text); }
                }, {
                    title: '接单时间',
                    dataIndex: 'createTimeFormattedDate',
                    key: 'createTimeFormattedDate',
                }, {
                    title: '订单金额',
                    dataIndex: 'paymentValue',
                    key: 'paymentValue',
                }, {
                    title: '支付时间',
                    dataIndex: 'paymentTimeFormattedDate',
                    key: 'paymentTimeFormattedDate',
                }, {
                    title: '支付流水',
                    dataIndex: 'paymentId',
                    key: 'paymentId',
                }
            ],
            achievementDataSource: [],
            demandPagination: {
                defaultCurrent: 1,
                defaultPageSize: 10,
                showQuickJumper: true,
                pageSize: 10,
                onChange: function (page) {
                    this.demandLoadData(page);
                }.bind(this),
                showTotal: function (total) {
                    return '共' + total + '条数据';
                }
            },
            demandColumns: [
                {
                    title: '编号',
                    dataIndex: 'serialNumber',
                    key: 'serialNumber',
                }, {
                    title: '需求名称',
                    dataIndex: 'name',
                    key: 'name',
                }, {
                    title: '订单状态',
                    dataIndex: 'auditStatus',
                    key: 'auditStatus',
                    render: (text, record) => {
                        if (record.deletedSign == 1) {
                            return "已删除"
                        } else if (record.auditStatus != 3) {
                            return "已下架"
                        } else {
                            return "正常"
                        }
                    }
                }, {
                    title: '流程状态',
                    dataIndex: 'status',
                    key: 'status',
                    render: (text, record) => { return getOrderStatus(text); }
                }, {
                    title: '意向金',
                    dataIndex: 'intentionMoney',
                    key: 'intentionMoney',
                }, {
                    title: '合同金',
                    dataIndex: 'contractMoney',
                    key: 'contractMoney',
                }, {
                    title: '佣金',
                    dataIndex: 'commission',
                    key: 'commission',
                }, {
                    title: '服务金',
                    dataIndex: 'serviceMoney',
                    key: 'serviceMoney',
                }, {
                    title: '接单时间',
                    dataIndex: 'createTimeFormattedDate',
                    key: 'createTimeFormattedDate',
                }
            ],
            demandDataSource: [],
        };
    },
    componentWillMount () {
        this.achievementLoadData();
        this.demandLoadData();
    },
    tabChange (key) {

    },
    demandRowClick (record, index) {
        this.state.demandRowData = record;
        if (record.deletedSign != 1 && record.auditStatus == 3) {
            this.setState({
                demandShowDesc: true
            });
        };
    },
    achievementRowClick (record, index) {
        this.state.achievementRowData = record;
        if (record.deletedSign != 1 && record.auditStatus == 3) {
            this.setState({
                achievementShowDesc: true
            });
        };
    },
    demandCloseDesc (e) {
        this.setState({
            demandShowDesc: e
        });
        this.demandLoadData();
    },
    achievementCloseDesc (e) {
        this.setState({
            achievementShowDesc: e
        });
        this.achievementLoadData();
    },
    render () {
        return (
            <div className="collection">
                <SpinContainer spinning={this.state.loading}>
                    <Tabs defaultActiveKey="1" onChange={this.tabChange}>
                        <TabPane tab="我的成果订单" key="1">
                            <Table columns={this.state.achievementColumns}
                                dataSource={this.state.achievementDataSource}
                                pagination={this.state.achievementPagination}
                                onRowClick={this.achievementRowClick} />
                        </TabPane>
                        <TabPane tab="我的需求订单" key="2">
                            <Table columns={this.state.demandColumns}
                                dataSource={this.state.demandDataSource}
                                pagination={this.state.demandPagination}
                                onRowClick={this.demandRowClick} />
                        </TabPane>
                    </Tabs>
                </SpinContainer>
                <DemandDesc data={this.state.demandRowData}
                    showDesc={this.state.demandShowDesc}
                    closeDesc={this.demandCloseDesc} />
                <AchievementDesc data={this.state.achievementRowData}
                    showDesc={this.state.achievementShowDesc}
                    closeDesc={this.achievementCloseDesc} />
            </div>
        )
    }
});

export default Content;
