$(function(){
    layui.use('form', function(){
        layui.form.render();
    })

})
function appendSelect(type,dataStr,form,clean){
    var data = 'type='+type;
    if(dataStr){
        data = data+'&'+dataStr;
    }
    if(type == 'default'){
        $(".customer_value_box").each(function(){
            $(this).remove();
        });
        $('#order_sort').val(9999);
    }
    
    $.ajax({
        type: "POST",
        url: "getCustomerConditionData",
        data: data,
        dataType: 'json',
        success: function (ret) {
            if(ret.error > 0){
                return false;
            }
            var html = '';
            for(var i in ret){
                html += '<option value="'+ret[i].value+'" title="'+ret[i].title+'">'+ret[i].text+'</option>';
            }
            if(clean == 1){
                $(".customer_value_box").each(function(){
                    $(this).remove();
                });
            }
            html = '<div class="layui-inline w-90 customer_value_box"><select class="w-90" lay-search name="customer_value" lay-filter="customer_value">'+html+'</select></div>';
            $("#condition-wrap").append(html);
            form.render('select');
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var ret = XMLHttpRequest.responseJSON
            layer.alert('操作失败,请重试');
        }
    })
}

layui.use('form', function() {
    var form = layui.form;
    form.on('radio(test-radio)', function (data) {
        //点击选择客户类型时，触发，获取可供选择的类型值
        $("#morechannel").empty();
        if(data.value == 'customer_region' || data.value == 'add_organize'){
            $('#img').attr('src','/img/mr_menu05.png');
        }else{
            $('#img').attr('src','');
        }
        appendSelect(data.value,'',form,1);
    });
    $('#img').click(function(){
        var selLen=$("#condition-wrap .customer_value_box");
        var selNum= 0,selStr='',selName;
        for(var i=selLen.length-1;i>=0;i--) {
            if(selNum==0){
                selNum = selLen.eq(i).find('select option:selected').val();
                selName = selLen.eq(i).find('select option:selected').html();
                selStr = selLen.eq(i).find('select option:selected').attr("title");
            }
        }
        if(selStr == 'provincelist'){
            selStr = 'province';
        }else if(selStr == 'citylist'){
            selStr = 'city';
        }else if(selStr == 'staffshoplist'){
            selStr = 'shop';
        }else if(selStr == 'staffdepartmentlist'){
            selStr = 'department';
        }else if(selStr == 'stafflist'){
            selStr = 'staff';
        }else{
            selStr = 'region';
        }
        //console.log(selNum,selName,selStr);
        var moreregion = '<div class="filter-wrap"><span class="filter-text type-val" value="'+selStr+'-'+selNum+'-'+selName+'">'+selName+'</span><span class="cus-close">×</span></div>';
        $("#morechannel").append(moreregion);
    })
    form.on('select(customer_value)', function (data) {
        var type = data.elem[data.elem.selectedIndex].title;
        $(data.othis).parent().nextAll().remove('div');
        if(data.value == 0 /* && data.elem[data.elem.selectedIndex].title != 'staffdepartmentlist'*/){
            //选部门时，就算部门为0，也要取一下数据，主要是取店下员工数据
            return false;
        }
        if(type == 'channelshoplist'){
            appendSelect('channelshoplist','shop_code=' + data.value,form);
        }else if(type == 'staffshoplist'){
            appendSelect('staffshoplist','shop_code=' + data.value,form);
        }else if (type == 'staffdepartmentlist'){
            appendSelect('staffdepartmentlist','department_id=' + data.value,form);
        }else if (type == 'region'){
            appendSelect('region','region=' + data.value,form);
        }else if (type == 'provincelist'){
            appendSelect('province','province_id=' + data.value,form);
        }else{
            var morechannel;
            if (type == 'channellist') {
                var  channelname = data.elem[data.elem.selectedIndex].innerHTML;
                var  channelid = data.elem[data.elem.selectedIndex].value;
                morechannel = '<div class="filter-wrap"><span class="filter-text type-val" value="'+channelid+'">'+channelname+'</span><span class="cus-close">×</span></div>';
                $("#morechannel").append(morechannel);
            }
            appendSelect(type,'',form);
        }
    })
    // 删除选中渠道
    $('#morechannel').on("click",".cus-close",function () {
        $(this).parent().remove();
    })

    form.on('radio(allocationtype)', function (data) {
        $('#content').css('display','block');
    })
    form.on('radio(allocationtype_none)', function (data) {
        $('#content').css('display','none');
    })
    form.on('radio(def2)', function (data) {
        $('#def').css('display','block');
    })
    form.on('radio(def1)', function (data) {
        $('#def').css('display','none');
    })
    form.on('radio(qd_radio)', function (data) {
        $("#d_dpg").html('');
        $(".set_group").html('');
        var shop_code = $('#gs_shop_code').val();
        $.ajax({
            type: "POST",
            url: "get"+data.value,
            data: {channelclass: data.value,shop_code:shop_code},
            dataType: 'json',
            success: function (ret) {
                // console.log(ret)
                var a = JSON.parse(ret);
                if(data.value == 'department_name'){
                    var crmItem = '<select class="w-90" lay-search id="d_dpg">';
                    for (var i = 0; i < a.length; i++) {
                        crmItem += '<option value="' + a[i]['id'] + '">' + a[i]['department_name'] + '</option>';
                    }
                    crmItem += '</select>';
                    $("#type").html(crmItem);
                }else if(data.value == 'position_name'){
                    var crmItem = '<select class="w-90" lay-search id="d_dpg">';
                    for (var i = 0; i < a.length; i++) {
                        crmItem += '<option value="' + a[i]['id'] + '">' + a[i]['position_name'] + '</option>';
                    }
                    crmItem += '</select>';
                    $("#type").html(crmItem);
                }else if(data.value == 'group_name') {
                    var crmItem = '<select class="w-90" lay-search id="d_dpg">';
                    for (var i = 0; i < a.length; i++) {
                        crmItem += '<option value="' + a[i]['id'] + '">' + a[i]['group_name'] + '</option>';
                        $("input[name=group_id]").val(a[i]['id']);
                    }
                    crmItem += '</select>';
                    $("#type").html(crmItem);
                }else if(data.value == 'staff_name') {
                    var inp = '<div class="layui-inline"><label class="layui-form-label" style="width:115px;padding:5px 0;">双击添加删除人员：</label><div class="layui-input-block" style=""><input type="text" id="search" class="layui-input w-120" style="float: left;margin-right: 5px" value=""><button class="layui-btn" style="float: left" type="button" id="query">查询</button></div><div class="layui-input-block" style="margin-top:10px;overflow: hidden"><div class="store-wrap"><ul class="store-list all-list-contain">';
                    for (var i = 0; i < a.length; i++) {
                        inp += '<li id="'+a[i]["id"]+'">'+a[i]["staffname"]+'</li>';
                    }
                    inp += '</ul> <div class="store-tit" style="font-size: 14px">可添加</div> </div> <span class="layui-icon set-qh">&#xe65b;</span><div class="store-wrap"><ul class="store-list recive-list-contain"></ul> <div class="store-tit" style="font-size: 14px">已添加</div> </div> </div> </div> ';
                    $("#type").html('');
                    $(".set_group").html(inp);
                }
                form.render('select');
                // }

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var ret = XMLHttpRequest.responseJSON
                layer.alert('操作失败');
            }
        })
    });
})
var listStr=function(name){
    var str="";
    var staffname="";
    for(var i=0;i<$(name).length;i++){
        var sid=$(name).eq(i).attr("id");
        var sname=$(name).eq(i).html();
        if(i==$(name).length-1){
            str+=sid;
            staffname+=sname;
        }else{
            str+=sid+',';
            staffname+=sname+',';
        }
    }
    return str+'-'+staffname;
};
$('#add-box').on('click','#query',function(){
    var search = $('#search').val();
    $.ajax({
        type: "POST",
        url: "searchStaff",
        data: {key_words:search},
        dataType: 'json',
        success: function (ret) {
            if(ret.code == 200){
                var data = ret.data;
                var inp = '';
                for (var i = 0; i < data.length; i++) {
                    inp += '<li id="'+data[i]["id"]+'">'+data[i]["staffname"]+'</li>';
                }
                $(".all-list-contain").html(inp);
            }
        }
    })
})

//双击添加和移除事件
$("#add-box").on("dblclick",".all-list-contain li",function(){
    var removeDom=$(this).remove();
    $(".recive-list-contain").append(removeDom);
});
$("#add-box").on("dblclick",".recive-list-contain li",function(){
    var removeDom=$(this).remove();
    $(".all-list-contain").append(removeDom);
});
$('#btn').click(function(){
    table.reload('idTest', {
        url: 'FPListJSON'
        ,where: {department_name:$('#department_name').val()} //设定异步数据接口的额外参数
        //,height: 300
    });
})