import React from 'react';
import { Icon, Modal, message, Spin, Button, Row, Col, Upload, Tooltip, Tag, Input, Table } from 'antd';
import '../portal.less';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import { getIndustryCategory } from '../../DicIndustryList.js';
import { splitUrl, getDemandType, beforeUploadFile, getOrderStatus } from '../../tools.js';
const DemandDetail = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            buttonLoading: false,
            dataSource: [],
            orderVisible: false,
            fileList: false,
            tags: [],
            pictureUrl: [],
            columns: [
                {
                    title: '状态',
                    dataIndex: 'status',
                    key: 'status',
                    render: (text) => { return getOrderStatus(text) }
                }, {
                    title: '处理时间',
                    dataIndex: 'recordTimeFormattedDate',
                    key: 'recordTimeFormattedDate',
                }, {
                    title: '备注',
                    dataIndex: 'comment',
                    key: 'comment',
                }
            ]
        };
    },
    handleCancel(e) {
        this.props.closeDesc(false);
    },
    loadData(id) {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/user/portal/demandDetail',
            data: {
                id: id
            },
            success: function (data) {
                let thisData = data.data;
                if (!thisData) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                    thisData = {};
                };
                this.setState({
                    data: thisData,
                    tags: thisData.keyword ? thisData.keyword.split(",") : [],
                    pictureUrl: thisData.pictureUrl ? splitUrl(thisData.pictureUrl, ',', globalConfig.avatarHost + '/upload') : []
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    loadLogsData(id) {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/user/portal/order/demandOrderLog',
            data: {
                demandOrderId: id
            },
            success: function (data) {
                let thisData = data.data, theArr = [];
                if (!thisData) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                } else {
                    thisData.map(function (item, i) {
                        theArr.push({
                            key: i,
                            recordTime: item.recordTime,
                            recordTimeFormattedDate: item.recordTimeFormattedDate,
                            status: item.status,
                            comment: item.comment
                        })
                    });
                };
                this.setState({
                    dataSource: theArr
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    interestClick() {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/user/portal/demandInterest',
            data: {
                id: this.props.data.id
            },
            success: function (data) {
                if (data.error && data.error.length) {
                    message.warning(data.error[0].message);
                } else {
                    this.loadData(this.props.data.id);
                };
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));;
    },
    cancelInterestClick() {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/user/portal/demandCancelInterest',
            data: {
                id: this.state.data.demandInterestId
            },
            success: function (data) {
                if (data.error && data.error.length) {
                    message.warning(data.error[0].message);
                } else {
                    this.loadData(this.props.data.id);
                };
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));;
    },
    handleSubmit() {
        this.setState({
            buttonLoading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/user/portal/order/orderDemand',
            data: {
                demandId: this.state.data.id,
                enclosureUrl: this.state.enclosureUrl,
                comment: this.state.comment
            },
            success: function (data) {
                if (data.error && data.error.length) {
                    message.warning(data.error[0].message);
                } else { message.success("提交成功!") };
            }.bind(this),
        }).always(function () {
            this.loadData(this.state.data.id);
            this.setState({
                orderVisible: false,
                buttonLoading: false
            });
        }.bind(this));
    },
    cancelSubmit() {
        this.setState({
            buttonLoading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/user/portal/order/demandShutdown',
            data: {
                id: this.state.data.demandOrderId
            },
            success: function (data) {
                if (data.error && data.error.length) {
                    message.warning(data.error[0].message);
                } else { message.success("撤销成功!") };
            }.bind(this),
        }).always(function () {
            this.loadData(this.state.data.id);
            this.setState({
                buttonLoading: false
            });
        }.bind(this));
    },
    componentWillReceiveProps(nextProps) {
        if (!this.props.showDesc && nextProps.showDesc) {
            if (nextProps.data.id) {
                this.loadData(nextProps.data.id);
                if (nextProps.data.orderId) {
                    this.loadLogsData(nextProps.data.orderId)
                }
            } else {
                this.state.data = {};
                this.state.tags = [];
                this.state.pictureUrl = [];
            };
        };
    },
    render() {
        const theData = this.state.data || {};
        if (this.props.data) {
            return (
                 状态流转记录: