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 { getAchievementCategory, getMaturity, getInnovation } from '../../tools.js';


const AchievementDetail = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            tags: [],
            technicalPictureUrl: [],
            maturityPictureUrl: [],
        };
    },
    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/achievementDetail',
            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(",") : [],
                    technicalPictureUrl: thisData.technicalPictureUrl ? splitUrl(thisData.technicalPictureUrl, ',', globalConfig.avatarHost + '/upload') : [],
                    maturityPictureUrl: thisData.maturityPictureUrl ? splitUrl(thisData.maturityPictureUrl, ',', 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/achievementInterest',
            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/achievementCancelInterest',
            data: {
                id: this.state.data.achievementInterestId
            },
            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.maturityPictureUrl = [];
                this.state.technicalPictureUrl = [];
            };
        };
    },
    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>;
                                        case "2":
                                            return <span>项目</span>;
                                    }
                                })()}
                            </Col>
                            <Col span={4}>类型</Col>
                            <Col span={8}>{getAchievementCategory(theData.category)}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>应用领域</Col>
                            <Col span={8}>{getIndustryCategory(theData.fieldA, theData.fieldB)}</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.summary}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>成果简介</Col>
                            <Col span={20}>{theData.introduction}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>技术图片</Col>
                            <Col span={20}>
                                <div className="clearfix">
                                    <Upload
                                        listType="picture-card"
                                        fileList={this.state.technicalPictureUrl}
                                        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={8}>{getMaturity(theData.maturity)}</Col>
                            <Col span={4}>创新度</Col>
                            <Col span={8}>{getInnovation(theData.innovation)}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>成熟度证明材料(图片)</Col>
                            <Col span={20}>
                                <Upload className="demandDetailShow-upload"
                                    listType="picture-card"
                                    fileList={this.state.maturityPictureUrl}
                                    onPreview={(file) => {
                                        this.setState({
                                            previewImage: file.url || file.thumbUrl,
                                            previewVisible: true,
                                        });
                                    }} >
                                </Upload>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={4}>成熟度证明材料(文本)</Col>
                            <Col span={20}>
                                {theData.maturityTextFileUrl ?
                                    <span className="download-file">
                                        <a onClick={() => { window.open(globalConfig.context + '/api/admin/achievement/download?id=' + this.props.data.id + '&sign=achievement_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.maturityVideoUrl}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>成果所有人名称</Col>
                            <Col span={8}>{theData.username || theData.unitName}</Col>
                            <Col span={4}>合作方式</Col>
                            <Col span={8}>
                                {(() => {
                                    switch (theData.cooperationMode) {
                                        case "0":
                                            return <span>技术转让</span>;
                                        case "1":
                                            return <span>授权生产</span>;
                                    }
                                })()}
                            </Col>
                        </Row>
                        <Row>
                            <Col span={4}>转让方式</Col>
                            <Col span={8}>
                                {(() => {
                                    switch (theData.transferMode) {
                                        case "0":
                                            return <span>完全转让</span>;
                                        case "1":
                                            return <span>许可转让</span>;
                                        case "2":
                                            return <span>技术入股</span>;
                                    }
                                })()}
                            </Col>
                            <Col span={4}>转让价格</Col>
                            <Col span={8}>
                                {(() => {
                                    switch (theData.bargainingMode) {
                                        case "0":
                                            return <span>面议</span>;
                                        case "1":
                                            return <span>{theData.transferPrice} 万元</span>;
                                    }
                                })()}
                            </Col>
                        </Row>
                        <Row>
                            <Col span={4}>技术场景</Col>
                            <Col span={20}>{theData.technicalScene}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>技术突破</Col>
                            <Col span={20}>{theData.breakthrough}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>专利情况</Col>
                            <Col span={20}>{theData.patentCase}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>获奖情况</Col>
                            <Col span={20}>{theData.awards}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>技术团队情况</Col>
                            <Col span={20}>{theData.teamDes}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>技术参数</Col>
                            <Col span={20}>{theData.parameter}</Col>
                        </Row>
                        <Row>
                            <Col span={4}>技术方案</Col>
                            <Col span={20}>
                                {theData.techPlanUrl ?
                                    <span className="download-file">
                                        <a onClick={() => { window.open(globalConfig.context + '/api/admin/achievement/download?id=' + this.props.data.id + '&sign=achievement_tech_plan') }}>技术方案</a>
                                    </span>
                                    : <span><Icon type="exclamation-circle" style={{ color: '#ffbf00', marginRight: '6px' }} />未上传！</span>}
                            </Col>
                        </Row>
                        <Row>
                            <Col span={4}>商业计划书</Col>
                            <Col span={20}>
                                {theData.businessPlanUrl ?
                                    <span className="download-file">
                                        <a onClick={() => { window.open(globalConfig.context + '/api/admin/achievement/download?id=' + this.props.data.id + '&sign=achievement_business_plan') }}>商业计划书</a>
                                    </span>
                                    : <span><Icon type="exclamation-circle" style={{ color: '#ffbf00', marginRight: '6px' }} />未上传！</span>}
                            </Col>
                        </Row>
                        <div className="interest clearfix">
                            {theData.achievementInterestId ? <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 AchievementDetail;