'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.default = undefined;

var _icon = require('jdcloudui/lib/icon');

var _icon2 = _interopRequireDefault(_icon);

var _upload = require('jdcloudui/lib/upload');

var _upload2 = _interopRequireDefault(_upload);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _message2 = require('jdcloudui/lib/message');

var _message3 = _interopRequireDefault(_message2);

require('jdcloudui/lib/icon/style');

require('jdcloudui/lib/upload/style');

require('jdcloudui/lib/message/style');

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _Dupload = require('./style/3Dupload.less');

var _Dupload2 = _interopRequireDefault(_Dupload);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var uploadFlag = true;

function beforeUpload(file) {
    var is3DTypes = true;
    var fileName = file.name;
    var threeDTypes = ['zip'];
    if (threeDTypes && threeDTypes.length > 0) {
        var fileTypes = fileName.split('.');
        var type = fileTypes[fileTypes.length - 1];
        type = type.toUpperCase();
        threeDTypes = threeDTypes.map(function (types, index) {
            return types.toUpperCase();
        });
        if (threeDTypes.indexOf(type) === -1) {
            //文件类型不在用户指定的范围内
            is3DTypes = false;
        }
    }
    if (!is3DTypes) {
        _message3.default.error('允许的模型格式有zip');
    }
    return is3DTypes;
};

var Upload3D = function (_Component) {
    (0, _inherits3.default)(Upload3D, _Component);

    function Upload3D(props, context) {
        (0, _classCallCheck3.default)(this, Upload3D);

        var _this = (0, _possibleConstructorReturn3.default)(this, (Upload3D.__proto__ || (0, _getPrototypeOf2.default)(Upload3D)).call(this, props, context));

        _this.handleChange = function (info) {
            console.log('info------------------', info);
            if (info.file.status === 'uploading') {
                if (uploadFlag) {
                    _message3.default.loading('正在上传中，请稍等....', 1.5);
                }
                uploadFlag = false;
            }
            if (info.file.status === 'done') {
                uploadFlag = true;
                //注意：这里是实现成功 接受返回值 图片的url
                if (info.file.response.code == 0) {
                    // console.log('data:'+typeof info.file.response.data)
                    var urls = info.file.response.data;
                    urls && _this.item3DInfos.push({
                        url: urls,
                        alt: info.file.name,
                        type: 5
                    });
                    //将数据传给父组件
                    _this.props.onCallBackData(_this.item3DInfos, 5);
                } else {
                    _message3.default.error(info.file.response.msg);
                }
            } else if (info.file.status === 'error') {
                uploadFlag = true;
                var msg = info.file.response && info.file.response.msg ? info.file.response.msg : "上传失败";
                _message3.default.error(msg);
            }
        };

        _this.handleReChange = function (info) {
            if (info.file.status === 'uploading') {
                if (uploadFlag) {
                    _message3.default.loading('正在上传中，请稍等....', 1.5);
                }
                uploadFlag = false;
            }
            if (info.file.status === 'done') {
                uploadFlag = true;
                //重新上传成功 接受返回图片的url
                if (+info.file.response.code === 0) {
                    var url = info.file.response.data;
                    var repeatItem = {
                        url: url,
                        alt: info.file.name,
                        type: 5
                    };
                    _this.item3DInfos.splice(_this.state.index, 1, repeatItem);
                    //将数据传给父组件
                    _this.props.onCallBackData(_this.item3DInfos, 5);
                } else {
                    _message3.default.error(info.file.response.msg);
                }
            } else if (info.file.status === 'error') {
                uploadFlag = true;
                var msg = info.file.response && info.file.response.msg ? info.file.response.msg : "上传失败";
                _message3.default.error(msg);
            }
        };

        _this.handleCancel = function (index) {
            console.log('aa');
            _this.item3DInfos = [];
            //将数据传给父组件
            _this.props.onCallBackData(_this.item3DInfos, 5);
        };

        _this.handleReUpload = function (index) {
            _this.state.index = index;
            _this.setState({
                index: _this.state.index
            });
        };

        _this.loop = function (data) {
            return data.map(function (item, index) {
                return item.url ? _react2.default.createElement(
                    'div',
                    { className: _Dupload2.default.avatarcontainer, key: index },
                    _react2.default.createElement(
                        'div',
                        { className: _Dupload2.default.avatarwrapper },
                        _react2.default.createElement(
                            'div',
                            { className: _this.uploadName },
                            _react2.default.createElement(
                                _upload2.default,
                                {
                                    className: _Dupload2.default.lUpload,
                                    name: 'file',
                                    showUploadList: false,
                                    action: '/proxy/base/upload/uploadOtherFile?type=2',
                                    beforeUpload: beforeUpload,
                                    onChange: _this.handleReChange
                                },
                                _react2.default.createElement(
                                    'span',
                                    { className: _Dupload2.default.reupload, onClick: function onClick() {
                                            return _this.handleReUpload(index);
                                        } },
                                    '重新上传'
                                )
                            ),
                            _react2.default.createElement('span', { className: _Dupload2.default.gip }),
                            _react2.default.createElement(
                                'span',
                                { className: _Dupload2.default.deletespan, onClick: function onClick() {
                                        return _this.handleCancel(index);
                                    } },
                                '删除'
                            )
                        ),
                        _react2.default.createElement(
                            'span',
                            { className: _Dupload2.default.modelImg },
                            _react2.default.createElement('img', { src: 'http://b2b-v2-pre.oss.cn-north-1.jcloudcs.com/846a5de98cf4eb37671378b3f37ccfb620190329194000428.png' })
                        )
                    ),
                    _react2.default.createElement(
                        'p',
                        { style: { textAlign: 'center' } },
                        _react2.default.createElement(
                            'a',
                            { href: item.url },
                            item.alt
                        )
                    )
                ) : null;
            });
        };

        _this.state = {
            index: null
        };
        _this.loop = _this.loop.bind(_this);
        _this.uploadName = _Dupload2.default.avatardelete;
        return _this;
    }

    (0, _createClass3.default)(Upload3D, [{
        key: 'componentDidMount',
        value: function componentDidMount() {}
    }, {
        key: 'addEventHandler',
        value: function addEventHandler(target, type, func) {
            if (target.addEventListener) {
                //监听IE9，谷歌和火狐
                target.addEventListener(type, func, false);
            } else if (target.attachEvent) {
                target.attachEvent("on" + type, func);
            } else {
                target["on" + type] = func;
            }
        }
        //上传

        //重新上传

        //删除

        //重新上传时的下标

        //循环的到插入的每张照片

    }, {
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(nextProps) {
            // console.log("nextProps====",nextProps);
        }
    }, {
        key: 'componentWillMount',
        value: function componentWillMount() {
            var browser = '';
            var b_version = '';
            var version = [];
            var trim_Version = '';
            if (navigator) {
                browser = navigator.appName;
                b_version = navigator.appVersion;
                if (b_version) {
                    version = b_version.split(";");
                    if (version.length > 1) {
                        trim_Version = version[1].replace(/[ ]/g, "");
                    } else {}
                } else {}
            } else {}
            if (browser && b_version && version && trim_Version) {
                console.log(browser, b_version, version, trim_Version, 'browser, b_version, version, trim_Version');
                if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE6.0") {
                    console.log("IE 6.0");
                } else if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE7.0") {
                    console.log("IE 7.0");
                } else if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE8.0") {
                    console.log("IE 8.0");
                } else if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE9.0") {
                    //alert("IE 9.0");
                    this.uploadName = _Dupload2.default.avatardelete2;
                }
            }
        }
    }, {
        key: 'render',
        value: function render() {
            this.item3DInfos = this.props.item3DInfos ? this.props.item3DInfos : [];
            var uploadButton = _react2.default.createElement(
                'div',
                { className: _Dupload2.default.avatarcontainer },
                _react2.default.createElement(
                    'div',
                    { className: _Dupload2.default.avatarwrapperadd },
                    _react2.default.createElement(
                        _upload2.default,
                        {
                            className: _Dupload2.default.avataruploader,
                            name: 'file',
                            showUploadList: false,
                            action: '/proxy/base/upload/uploadOtherFile?type=2',
                            beforeUpload: beforeUpload,
                            onChange: this.handleChange,
                            data: { platformId: 2 },
                            multiple: true
                        },
                        _react2.default.createElement(
                            'div',
                            { className: _Dupload2.default.avataruploadertrigger },
                            _react2.default.createElement(_icon2.default, { type: 'profile', size: 'large', className: _Dupload2.default.plusicon }),
                            _react2.default.createElement(
                                'span',
                                { className: _Dupload2.default.plus1 },
                                '\u6DFB\u52A03D\u6A21\u578B'
                            )
                        )
                    )
                ),
                _react2.default.createElement(
                    'p',
                    { style: { textAlign: 'center' } },
                    _react2.default.createElement(
                        'a',
                        { href: '//ar.jd.com/arpages/market.html' },
                        '\u53D1\u5E03\u5EFA\u6A21\u9700\u6C42'
                    )
                )
            );
            return _react2.default.createElement(
                'div',
                { className: _Dupload2.default.avatarfl },
                this.item3DInfos !== [] && this.item3DInfos.length > 0 && this.loop(this.item3DInfos),
                this.item3DInfos.length >= this.props.num ? null : uploadButton
            );
        }
    }]);
    return Upload3D;
}(_react.Component);

exports.default = Upload3D;
module.exports = exports['default'];
//# sourceMappingURL=Upload3D.js.map