/**
 * create by robot
 * date Tue Sep 10 15:03:55 CST 2019
 */

var pageIndex = 1;

var type = '${admin}'
$(document).ready(function () {
    //调用查询
    pageIndex = StringUtil.getQueryString('pageIndex');
    if (pageIndex == undefined || pageIndex == null) {
        pageIndex = 1;
    }
    // queryMerchantStore(pageIndex);
    //绑定hover操作
    $(document).on('mouseenter', '.listOperaters', function () {
        $(this).children(".ops_itemDiv").show();
        var width = 15;
        $(this).children('div').find('li').each(function () {
            //获取li宽度
            width += $(this).width();
        });
        $(this).children('div').width(width);
    });
    $(document).on('mouseleave', '.listOperaters', function () {
        $(this).children(".ops_itemDiv").hide();
    });

    //日历控件
    layui.use(['layer', 'laydate'], function () {
        var layer = layui.layer;
        laydate = layui.laydate;
        laydate.render({
            elem: '#date1' //指定元素
        });
        laydate.render({
            elem: '#date2' //指定元素
        });
    });

    //行选中事件
    $("#allCheck").find("span").click(function () {
        $(this).toggleClass('checked');
        if ($(this).hasClass('checked')) {
            //所有列选中
            $(".checker").find("span").each(function () {
                $(this).addClass("checked");
            })
        } else {
            //所有列取消选中
            $(".checker").find("span").each(function () {
                $(this).removeClass("checked");
            })
        }
    });

    //单个选中
    $(document).on('click', '.checkeBox span', function () {
        $(this).toggleClass('checked');
        //设置头部事件
        var length = $('.checkeBox span').length;
        var count = 0;
        $(".checkeBox").find("span").each(function () {
            if ($(this).hasClass("checked")) {
                count++;
            }
        });
        //全选
        if (count == length) {
            $("#allCheck").find("span").addClass('checked');
        } else {
            $("#allCheck").find("span").removeClass('checked');
        }
    });
    //左右div高度相等
    var left_content_height = $(".right-content").height();
    $(".left-content").height(left_content_height + 35);
    var sub_left_height = $(".res-menu").height(left_content_height - 60);
});

//初始化新增
function initAddMerchantStore() {
    window.location = ctx + '/merchantStore/queryMerchantStoreDetails?pageIndex=' + pageIndex;
}

//初始化详情
function showMerchantStoreParticulars(id) {
    window.location = ctx + '/merchantStore/merchantStorePath?id=' + id;
}

//快付状态审核
function updateChannelStatus(id,status,userId) {
    var index = layer.confirm('确定对用户ID为【'+userId+'】开通快付吗？', {icon: 3, title:'提示'}, function(index){
        $.ajax({
            url: ctx + '/merchantStore/updateChannelStatus',
            data: {"csId":id,"status":status},
            type: 'POST',
            dataType: 'json',
            success: function (res) {
                layer.close(index);
                let icon = 2;
                if (res.code == 200) {
                    icon = 1;
                    reload();
                }
                layer.msg(res.message,{icon:icon});
            }
        });
    });
}

//初始化修改
function showMerchantStoreDetails(id) {
    window.location = ctx + '/merchantStore/queryMerchantStoreDetails?id=' + id + '&pageIndex=' + pageIndex;
}

/**
 * 跳转进件详情
 * @returns
 */
function showMerchantInfo(userId) {
    window.location = ctx + "/merchantBankCard/queryMerchantInfo?userId=" + userId;
}

//初始化收款码
var dkIndex = 0;
var dkId = 0;
var dkStatus = 0;
function showMerchantStoreReceipt(data,payEcdemicSwitch) {
    let channelVal = data.channel;
    let channelText = data.channel == 4 ? "银盛" : data.channel == 3 ? "瑞银信" : "随行付";
    var url = ctx + '/merchantStore/showMerchantStoreReceipt?userId=' + data.userId + "&channel="+channelVal;
    var load = layer.load(1);
    dkId = data.userId;
    dkStatus = payEcdemicSwitch;
    $.get(url, function (res) {
        layer.close(load);
        if (res.code == 1) {
            var qrCodeUrl = res.url;
            dkIndex = layer.open({
                type: 1,
                title: res.storeName+'('+channelText+')',
                skin: 'layui-layer-molv',
                shade: 0.3,
                content: '<div id="qrcode" style="width:250px; height:250px;"></div><div class="allopatry-btn"><button class="layui-btn layui-btn-sm allopatry" data-t="open" data-id="'+data.userId+'" data-value="'+payEcdemicSwitch+'">开启</button><button class="layui-btn layui-btn-sm layui-btn-normal allopatry" data-t="close" data-id="'+data.userId+'" data-value="'+payEcdemicSwitch+'">关闭</button></div>',//这里content是一个普通的String
                success: function () {
                    $("#qrcode").qrcode({text:qrCodeUrl,width:250,height:250});
                }
            });
        } else {
            layer.msg(res.msg, {icon: 2});
        }
    })
}
//手动进件
function manualMerchantIncom(userId) {
    layer.confirm('确认对用户ID为【' + userId + '】发起进件?', {icon: 3, title: '提示', skin: 'layui-layer-molv'}, function (index) {
        var load = layer.load(1);
        var url = ctx + '/merchantStore/manualMerchantIncom/' + userId;
        $.get(url, function (res) {
            layer.close(load);
            layer.close(index);
            if (res.code == 1) {
                layer.msg(res.msg, {icon: 1});
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        })
    });
}

//手动进件
function batchMerchantIncom() {
    layer.confirm('确认批量发起进件?', {icon: 3, title: '提示', skin: 'layui-layer-molv'}, function (index) {
        var load = layer.load(1);
        var url = ctx + '/merchantStore/batchMerchantIncom';
        $.get(url, function (res) {
            layer.close(load);
            layer.close(index);
            if (res.code == 1) {
                layer.msg(res.msg, {icon: 1});
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        })
    });
}

//批量删除
function deleteBatchMerchantStore() {
    $(".checkeBox").find("span").each(function () {
        if ($(this).hasClass("checked")) {
            var id = $(this).data('id');
            deleteMerchantStore(id, false);
            layer.msg('删除成功!');
        }
    })
}

//点击放大图片
function enlargePicture(billPicUrl) {
    //alert("即将弹出图片");
    var data = '';
    data += '<img src="' + billPicUrl + '"  class="img" onerror="this.src=\'https://www.shouyinbei.net/resources/images/error.jpg\'" >';
    $('#picture').append(data);
    $('#pictureWindow').show();
}

function hidePictureWindow() {
    $('#picture').empty();
    $('#pictureWindow').hide();
}


//头部禁用
function disableMerchantStore() {
    var count = 0;
    var checkStatus = table.checkStatus('merchantStoreList');
    checkStatus.data;
    count = checkStatus.data.length;
    console.log(checkStatus.data);

    if (count == 0) {
        layer.msg('请选中一行记录!');
        return;
    }
    if (count > 1) {
        layer.msg('请单行记录!');
        return;
    }

    $.ajax({
        url: ctx + '/merchantStore/queryStatus',
        data: {
            id: checkStatus.data[0].id
        },
        type: 'POST',
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 1) {
                var resultValue = res.resultValue;
                if (resultValue == 2) {
                    url = ctx + '/merchantStore/merchantStoreForbidden?resultValue=' + resultValue + '&id=' + checkStatus.data[0].userId;
                    layer.open({
                        type: 2,
                        title: '开通所选中的交易状态',
                        shade: 0.5,
                        maxmin: true,
                        area: ['550px', '300px'],
                        content: url
                    });
                } else if (resultValue == 1) {
                    url = ctx + '/merchantStore/merchantStoreForbidden?resultValue=' + resultValue + '&id=' + checkStatus.data[0].userId;
                    layer.open({
                        type: 2,
                        title: '禁用所选中的交易状态',
                        shade: 0.5,
                        maxmin: true,
                        area: ['550px', '300px'],
                        content: url
                    });
                } else layer.msg(" 请选择您需要开通或禁用的交易状态！");
            }
        }
    });
}

//电子围栏
function payEcdemicSwitchMerchantStore() {
    var count = 0;
    var checkStatus = table.checkStatus('merchantStoreList');
    checkStatus.data;
    count = checkStatus.data.length;
    if (count == 0) {
        layer.msg('请选中一行记录!');
        return;
    }
    if (count > 1) {
        layer.msg('请单行记录!');
        return;
    }

    $.ajax({
        url: ctx + '/merchantStore/merchantStorePayEcdemicSwitch',
        data: {
            id: checkStatus.data[0].id,
        },
        type: 'POST',
        dataType: 'json',
        success: function (res) {
            // layer.close(index);
            if (res.resultCode == 1) {
                var payEcdemicSwitch = res.payEcdemicSwitch;
                if (payEcdemicSwitch == 1) {
                    url = ctx + '/merchantStore/merchantStoreThoroughfare?payEcdemicSwitch=' + payEcdemicSwitch + '&id=' + checkStatus.data[0].userId;
                    layer.open({
                        type: 2,
                        title: '关闭所选中的异地支付通道',
                        shade: 0.5,
                        maxmin: true,
                        area: ['550px', '300px'],
                        content: url
                    });
                } else if (payEcdemicSwitch == 0) {
                    url = ctx + '/merchantStore/merchantStoreThoroughfare?payEcdemicSwitch=' + payEcdemicSwitch + '&id=' + checkStatus.data[0].userId;
                    layer.open({
                        type: 2,
                        title: '开启所选中的异地支付通道',
                        shade: 0.5,
                        maxmin: true,
                        area: ['550px', '300px'],
                        content: url
                    });
                }
            }
        }
    });
}

//操作营销商户
function isMarket() {
    var count;
    var checkStatus = table.checkStatus('merchantStoreList');
    checkStatus.data;
    count = checkStatus.data.length;

    if (count == 0) {
        layer.msg('请选中一行记录！');
        return;
    }

    if (count > 1) {
        layer.msg('禁止操作多行');
        return;
    }

    var data = checkStatus.data[0].isMarket == 0? 1: 0;
    var index = layer.confirm("是否确认" + (checkStatus.data[0].isMarket == 0 ? '<span style="color: green;">开通</span>' : '<span style="color: red;">关闭</span>') + "所选商户为营销商户？",
        {title: "开通/关闭营销商户"},
        function () {
        var idArr = [];
        checkStatus.data.forEach(function(item) {
            var id = item.id;
            idArr.push(id);
        });

        $.ajax({
            url: ctx + '/merchantStore/operatMarket',
            data: {ids: idArr, isMarker: data},
            type: 'POST',
            dataType: 'json',
            success: function (res) {
                layer.close(index);
                if (res.resultCode == 1) {
                    table.reload('merchantStoreList');
                }
            }
        });
    })
}

function dateFtt(fmt, date) { //author: meizz
    var o = {
        "M+": date.getMonth() + 1,                 //月份
        "d+": date.getDate(),                    //日
        "h+": date.getHours(),                   //小时
        "m+": date.getMinutes(),                 //分
        "s+": date.getSeconds(),                 //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}