/**
 * Created by hzd on 2017/10/23.
 */

/**
 * 移除图片
 */
function removePic(_self,deleteurl){
    $(_self).parent().remove();
    if(deleteurl){
        $.post(deleteurl,{path:$(_self).parent().find('img').attr('src')});
    }
}

/**
 * 图片上传
 */
function imgfiveup (object){
    /*
        <button class="btn-flat btn btn-success btn-sm jqfivefile" type="button">
            上传图片<span id="progress"></span>
            <input id="imgUpload" type="file" accept="image/*" data-url="{:U('File/uploadPicture')}" />
        </button>
     */
    this.target = object.target?object.target:"#imgUpload", //上传按钮id
    this.progress = object.progress?object.progress:"#progress",//进度条id
    this.imgbox = object.imgbox?object.imgbox:"#imgsbox",//头像图片id
    this.multi = object.multi?object.multi:false; //多图/单图
    this.name = object.name?object.name:'img'; //表单name
    this.deleteurl = object.deleteurl?object.deleteurl:''; //表单name
    if(this.multi){
        this.name = this.name+'[]';
        $(this.target).attr('multiple','multiple');
    }else{
        $(this.target).removeAttr('multiple');
    }
    that = this;
    this.upload = function(){
        $(that.target).fileupload({
            dataType: 'json',
            done: function (e, data) {
                if (data.result && data.result.status == 1) {
                    /*
                       <div id="imgsbox" class="imgsbox">
                         <div class="imgbox">
                            <div class="removePic" onclick="removePic(this)">×</div>
                            <input type="hidden" name="site_video_img" value="/Uploads/Picture/2017-10-23/59ed852565a94.png">
                            <img src="/Uploads/Picture/2017-10-23/59ed852565a94.png">
                         </div>
                        </div>
                    */
                    var img = '<div class="imgbox">';
                    img += '<div class="removePic" onclick="removePic(this,\''+that.deleteurl+'\')">×</div>';
                    img += '<input type="hidden" name="' + that.name + '" value="' + data.result.path + '">';
                    img += '<img src="' + data.result.path + '">';
                    img += '</div>';
                    if(that.multi){
                        $(that.imgbox).append(img);
                    }else{
                        $(that.imgbox).html(img);
                    }
                } else {
                    layer.msg(data.result.msg);
                }
            },
            progressall: function (e, data) {
                var progressnum = parseInt(data.loaded / data.total * 100);
                if (progressnum == 100) {
                    $(that.progress).text('');
                } else {
                    $(that.progress).text(progressnum + "%");
                }
            },
            error: function(e,data){
                layer.msg('上传请求失败');
            }
        });
    }
}