'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function request(url, data) {
    var method = arguments.length <= 2 || arguments[2] === undefined ? 'GET' : arguments[2];

    return new Promise(function (resolve, reject) {
        $.ajax(url, {
            type: method,
            dataType: 'json',
            data: data,
            success: function success(res) {
                if (res && res.status == 1) {
                    resolve(res.result);
                } else {
                    reject({
                        code: res && res.code || 0,
                        message: res && res.msg || res.message || '系统错误,请稍后再试!'
                    });
                }
            },
            error: function error(xhr, status, text) {
                console.log(xhr, status, text);
                reject({
                    code: xhr && xhr.status || status,
                    message: text
                });
            }
        });
    });
}

function formatMoney() {
    var money = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0];

    var ret = void 0;
    if (isNaN(money)) {
        ret = '0';
    } else if (money >= 1E10) {
        ret = money / 1E10 + '亿';
    } else if (money >= 1E6) {
        ret = money / 1E6 + '万';
    } else {
        ret = money / 1E2;
    }
    return ret;
}

function deserialize() {
    var str = arguments.length <= 0 || arguments[0] === undefined ? location.search : arguments[0];

    var query = {};
    (str + '').replace(/^\?/g, '').split('&').forEach(function (item) {
        var tmp = (item + '').split('=');
        if (tmp.length) {
            query[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp[1]);
        }
    });
    return query;
}

function serialize(obj, prefix) {
    var str = [];
    Object.keys(obj).map(function (p) {
        var v = obj[p];
        if (p && typeof v !== 'undefined') {
            var k = prefix ? prefix + "." + p : p;
            if ((typeof v === 'undefined' ? 'undefined' : _typeof(v)) == 'object') {
                v = serialize(v, k);
                if (v) {
                    str.push(v);
                }
            } else {
                str.push(encodeURIComponent(k) + "=" + encodeURIComponent(v));
            }
        }
    });
    return str.length ? str.join("&") : '';
}

var Tabs = function () {
    function Tabs(options) {
        _classCallCheck(this, Tabs);

        this.options = _extends({ activeClass: 'active' }, options);
    }

    _createClass(Tabs, [{
        key: 'initialize',
        value: function initialize() {
            this.container = $(this.options.container || 'body').empty();
            this.render();
            return this;
        }
    }, {
        key: 'findHeader',
        value: function findHeader(index) {
            return this.element.find('.tabs-headers-item:eq(' + index + ')');
        }
    }, {
        key: 'findPanel',
        value: function findPanel(index) {
            return this.element.find('.tabs-panels-item:eq(' + index + ')');
        }
    }, {
        key: 'selectIndex',
        value: function selectIndex(index) {

            var header = this.findHeader(this.activeIndex);
            var panel = this.findPanel(this.activeIndex);

            if (typeof index === 'number' && index !== this.activeIndex) {
                var _options = this.options;
                var activeClass = _options.activeClass;
                var initializePanel = _options.initializePanel;

                header.removeClass(activeClass);
                panel.removeClass(activeClass);
                this.activeIndex = index;
                header = this.findHeader(index).addClass(activeClass);
                panel = this.findPanel(index).addClass(activeClass);
                if (!panel.attr('initialized')) {
                    if (typeof initializePanel === 'function') {
                        initializePanel.call(this, index, header, panel);
                    }
                    panel.attr('initialized', true);
                }
            }
            return {
                header: header,
                panel: panel
            };
        }
    }, {
        key: 'render',
        value: function render() {
            var headers = [];
            var panels = [];
            var self = this;
            this.options.headers.forEach(function (item, index) {
                headers.push('<a class="tabs-headers-item" data-index="' + index + '" href="javascript:void(\'' + item + '\')">' + item + '</a>');
                panels.push('<div class="tabs-panels-item" data-index="' + index + '"></div>');
            });
            var html = '<div class="tabs">\n                            <header class="tabs-header">\n                                <div class="clear">\n                                   ' + headers.join('') + '\n                                </div>\n                            </header>\n                            <section>\n                                ' + panels.join('') + '\n                            </section>\n                        </div>';
            this.element = $(html).appendTo(this.container);

            this.selectIndex(this.options.defaultIndex || 0);

            this.element.on('click', '.tabs-headers-item', function (e) {
                e.preventDefault();
                var tabHeader = $(this);
                var index = tabHeader.data('index');
                if (index != self.activeIndex) {
                    self.selectIndex(index);
                }
            });
            return this;
        }
    }]);

    return Tabs;
}();

//# sourceMappingURL=utils.js.map