/**
 * 人人站CMS
 * ============================================================================
 * 版权所有 2015-2030 山东康程信息科技有限公司，并保留所有权利。
 * 网站地址: http://www.rrzcms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 */
(function ($) {

    var imgSpace = function (callback, multiple = false) {
        var $this = $(this), callback = callback || $this.data('callback') || function (url) {
                $('[lay-upvalue]', $this).val(url);
                $('[lay-uphref]', $this).attr('href', url);
                $('[lay-upsrc]', $this).attr('src', url);
            },
            url = window._global.root + '/Index/Imgspace' + (multiple ? '?multiple=1' : '');

        $.loading.show();
        $.get(url, function (rs) {
            $.loading.hide();
            $.confirm({
                type: 1,
                title: false,
                area: ['850px'],
                msg: rs,
                success: function (layero, index) {
                    window.layui && layui.render && layui.render();
                    $('button[lay-upload]', layero).data('callback', function (res) {
                        res && res.msg && $.showMsg(res.code, res.msg);
                        if (res && res.url) {
                            callback.call($this, res.url, res.data ? res.data : []);
                            $this.trigger('change');
                            $('.layui-layer-close', layero).trigger('click');
                        }
                        return false;
                    });
                    multiple && $('.layui-upload-file', layero).attr('multiple', 'multiple');
                },
                yes: function (layero, index) {
                    var slist = $('.img-list', layero).data('slist') || [];
                    if (!slist.length) {
                        $.msg('请先选择图片或上传图片！');
                        return false;
                    }
                    callback.call($this, slist[0], slist);
                    $this.trigger('change');
                }
            });
        });
    };
    window.ImgSpace = imgSpace;

    $('body').on('click', '.imgspace [lay-up]', function () {
        var $p = $(this).closest('.imgspace');
        imgSpace.call($p);
        return false;
    }).on('click', '.imgspace[lay-up]', function () {
        imgSpace.call(this);
        return false;
    });

})(window.jQuery);