<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" %>

<script>
    let upload;

    let auditLoading = 0, merchBaseInfoLoading = 0, merchLicenseLoading = 0, settlementLoading = 0;

    layui.use(['upload'], function() {
        upload = layui.upload;
    });

    var xhrOnProgress = function (fun) {
        xhrOnProgress.onprogress = fun; //绑定监听
        //使用闭包实现监听绑定
        return function () {
            //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
            var xhr = $.ajaxSettings.xhr();
            //判断监听函数是否为函数
            if (typeof xhrOnProgress.onprogress !== 'function') {
                return xhr;
            }
            //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
            if (xhrOnProgress.onprogress && xhr.upload) {
                xhr.upload.onprogress = xhrOnProgress.onprogress;
            }
            return xhr;
        }
    }
    var data = {};
    var inputUpload;
    var imgUpload;

    function baseSet() {
        // 因为上传功能只能初始化一次，所以这里等所有页面加载完成再进行初始化
        if (auditLoading == 0) {
            return;
        }

        if (merchBaseInfoLoading == 0) {
            return;
        }

        if (merchLicenseLoading == 0) {
            return;
        }

        if (settlementLoading == 0) {
            return;
        }


        upload.render({
            elem: '.upload', // 文件选择
            url: '${ctx}/upload/ossUploadFile',
            exts: 'jpg|png|gif|bmp|jpeg',
            method: 'POST',
            auto: true,
            before: function () {
                var dom = $(this.item);
                inputUpload = dom.find("input");
                imgUpload = dom.find("img");

                openIndex = layer.open({
                    type: 1,
                    title: '上传进度',
                    closeBtn: 1, //不显示关闭按钮
                    area: ['300px', '130px'],
                    shadeClose: false, //开启遮罩关闭
                    content: $("#uploadLoadingDiv").html(),
                    offset: '100px'
                });
            }
            , xhr: xhrOnProgress
            , progress: function (value) {//上传进度回调 value进度值
                element.progress('js_upload_progress', value + '%');//设置页面进度条
            }
            , done: function (res) {
                layer.close(openIndex);
                if (res.code == 200) {
                    layer.msg("上传成功！")
                    inputUpload.val(res.url);
                    imgUpload.attr("src", res.url);
                } else {
                    layer.msg("上传失败！");
                }
            }
            , error: function (res) {
                layer.close(openIndex);
                layer.msg(res.msg);
            }
        });

        /**
         * 屏蔽点击事件
         */
        $('.cover').unbind()
        $('.cover').click(function (e) {
            e.stopPropagation()
        })

        /**
         * 重置
         */
        $('.upload_icon').unbind()
        $('.upload_icon').click(function (e) {
            e.stopPropagation();
            console.log("upload icon click")
            $(this).parents('.upload').click()
        })
    }

    // 省市区同步方法
    let funcDistrict = function (data) {
        var areaCode = data.value;
        var domId = $(data.elem).attr("data-dom");
        $.ajax({
            url: ctx + '/promoter/getDowntownByCode/' + areaCode,
            method: 'get',
            dataType: 'json',
            success: function (res) {
                if (res.code == 200) {
                    var dom = $("#" + domId);
                    var html = "<option value=''>请选择</option>";
                    dom.empty();
                    if (res.areaCityList.length > 0) {
                        $.each(res.areaCityList, function (k, v) {
                            html += "<option value='" + v.areaCode + "'>" + v.areaName + "</option>";
                        })
                        dom.append(html);
                    }
                    form.render('select');
                } else {
                    layer.msg(res.message, {
                        icon: 5
                    });
                }
            }
        })
    };

    // 省市区同步方法
    let funcDistrictByName = function (data) {
        var areaName = data.value;
        var domId = $(data.elem).attr("data-dom");
        $.ajax({
            url: ctx + '/promoter/getDowntownByName/area/' + areaName,
            method: 'get',
            dataType: 'json',
            success: function (res) {
                if (res.code == 200) {
                    var dom = $("#" + domId);
                    var html = "<option value=''>请选择</option>";
                    dom.empty();
                    if (res.areaCityList.length > 0) {
                        $.each(res.areaCityList, function (k, v) {
                            html += "<option value='" + v.areaName + "'>" + v.areaName + "</option>";
                        })
                        dom.append(html);
                    }
                    form.render('select');
                } else {
                    layer.msg(res.message, {
                        icon: 5
                    });
                }
            }
        })
    };

    // 省市区同步方法
    let funcCityByName = function (data) {
        var areaName = data.value;
        var domId = $(data.elem).attr("data-dom");
        $.ajax({
            url: ctx + '/promoter/getDowntownByName/city/' + areaName,
            method: 'get',
            dataType: 'json',
            success: function (res) {
                if (res.code == 200) {
                    var dom = $("#" + domId);
                    var html = "<option value=''>请选择</option>";
                    dom.empty();
                    if (res.areaCityList.length > 0) {
                        $.each(res.areaCityList, function (k, v) {
                            html += "<option value='" + v.areaName + "'>" + v.areaName + "</option>";
                        })
                        dom.append(html);
                    }
                    form.render('select');
                } else {
                    layer.msg(res.message, {
                        icon: 5
                    });
                }
            }
        })
    };

    let funcBranchByBank = function (data) {
        var bankCityId = $(data.elem).attr('bank-city');
        var bankCity = $('#' + bankCityId).val();
        var bankName = $(data.elem).children("option:selected").val();
        var domId = $(data.elem).attr("data-dom");
        $.ajax({
            url: ctx + '/promoter/getBranchList',
            method: 'get',
            data: {
                "bankName": bankName,
                "cityName": bankCity,
            },
            success: function (res) {
                if (res.code == 200) {
                    var dom = $("#" + domId);
                    var html = "<option value=''>请选择</option>";
                    dom.empty();
                    if (res.branchList.length > 0) {
                        $.each(res.branchList, function (k, v) {
                            html += "<option value='" + v.cnapsCode + "'>" + v.cnapsName + "</option>";
                        })
                        dom.append(html);
                    }
                    form.render('select');
                } else {
                    layer.msg(res.message, {
                        icon: 5
                    });
                }
            }
        })
    }

    function magnify(obj) {

        var imgUrl = $('#' + obj).attr("src");
        if (imgUrl) {
            var img = new Image();
            img.src = imgUrl;
            //弹出层
            parent.layer.photos({
                photos: {"data": [{"src": imgUrl}]}
            });
        }
    }

</script>