import React from 'react';
import { Icon, Modal, message, Spin, Button, Row, Col, Upload, Tooltip, Tag } from 'antd';
import '../portal.less';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import { getIndustryCategory } from '../../DicIndustryList.js';
import { getDemandType } from '../../tools.js';


const DemandDetail = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            tags: [],
            pictureUrl: [],
        };
    },
    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));
    },
    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));;
    },
    componentWillReceiveProps(nextProps) {
        if (!this.props.showDesc && nextProps.showDesc) {
            if (nextProps.data.id) {
                this.loadData(nextProps.data.id);
            } else {
                this.state.data = {};
                this.state.tags = [];
                this.state.pictureUrl = [];
            };
        };
    },
    render() {
        const theData = this.state.data || {};
        if (this.props.data) {
            return (
                <Spin spinning={this.state.loading} className="portal-desc">
                    <Modal maskClosable={false}
                        visible={this.props.showDesc}
                        onCancel={this.handleCancel}
                        title="需求详情"
                        width='1000px'
                        footer=''
                        className="portal-desc-content">
                        <Row>
                            <Col span={4}>编号</Col>
                            <Col span={8}>{theData.serialNumber}</Col>
                            <Col span={4}>名称</Col>
                            <Col span={8}>{theData.name}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>数据类别</Col>
                            <Col span={8}>
                                {(() => {
                                    switch (theData.dataCategory) {
                                        case "0":
                                            return <span>个人需求</span>;
                                        case "1":
                                            return <span>单位需求</span>;
                                    }
                                })()}
                            </Col>
                            <Col span={4}>需求类型</Col>
                            <Col span={8}>{getDemandType(theData.demandType)}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>应用领域</Col>
                            <Col span={8}>{getIndustryCategory(theData.industryCategoryA, theData.industryCategoryB)}</Col>
                            <Col span={4}>有效期限</Col>
                            <Col span={8}>{theData.releaseDateFormattedDate}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>关键词</Col>
                            <Col span={20}>
                                {this.state.tags.map((tag) => {
                                    return <Tooltip key={tag} title={tag}>
                                        <Tag key={tag}>{tag}</Tag>
                                    </Tooltip>;
                                })}
                            </Col>
                        </Row>
                        <Row>
                            <Col span={4}>问题说明</Col>
                            <Col span={20}>{theData.problemDes}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>技术指标要求</Col>
                            <Col span={20}>{theData.technicalRequirements}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>需求相关文件(图片)</Col>
                            <Col span={20}>
                                <div className="clearfix">
                                    <Upload
                                        listType="picture-card"
                                        fileList={this.state.pictureUrl}
                                        onPreview={(file) => {
                                            this.setState({
                                                previewImage: file.url || file.thumbUrl,
                                                previewVisible: true,
                                            });
                                        }} >
                                    </Upload>
                                    <Modal maskClosable={false} footer={null}
                                        visible={this.state.previewVisible}
                                        onCancel={() => { this.setState({ previewVisible: false }) }}>
                                        <img alt="" style={{ width: '100%' }} src={this.state.previewImage || ''} />
                                    </Modal>
                                </div>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={4}>需求相关文件(文本)</Col>
                            <Col span={20}>
                                {theData.textFileUrl ?
                                    <span className="download-file">
                                        <a onClick={() => { window.open(globalConfig.context + '/api/user/demand/download?id=' + this.props.data.id + '&sign=demand_maturity_text_file') }}>需求文件(文本文件)</a>
                                    </span>
                                    : <span><Icon type="exclamation-circle" style={{ color: '#ffbf00', marginRight: '6px' }} />未上传！</span>}
                            </Col>
                        </Row>
                        <Row>
                            <Col span={4}>需求文件(视频地址)</Col>
                            <Col span={20}>{theData.videoUrl}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>雇佣方名称</Col>
                            <Col span={8}>{theData.username || theData.unitName}</Col>
                            <Col span={4}>固定周期</Col>
                            <Col span={8}>{theData.fixedCycle}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>固定人数</Col>
                            <Col span={8}>{theData.peopleNumber}</Col>
                            <Col span={4}>预算价格</Col>
                            <Col span={8}>{theData.budgetCost}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>固定方案</Col>
                            <Col span={20}>{theData.fixedScheme}</Col>
                        </Row>
                        <div className="interest clearfix">
                            {theData.demandInterestId ? <a onClick={this.cancelInterestClick}><Icon type="heart" /> 不感兴趣</a>
                                : <a onClick={this.interestClick}><Icon type="heart-o" /> 感兴趣</a>}
                        </div>
                    </Modal>
                </Spin>
            );
        } else {
            return <div></div>
        }
    },
});

export default DemandDetail;