{extend name="Public/base" /}
{block name="css"}
<link rel="stylesheet" href="__PUBLIC__/css/set.css?v={$Think.const.APP_VERSION}">
<style>
.shouye .layui-form-switch {
    height: 18px;
    margin-top: -1px;
}
</style>
{/block}
{block name="js"}
<script src="__PUBLIC__/js/jquery-1.9.0.min.js?v={$Think.const.APP_VERSION}" type="text/jscript"></script>
<script>
    $(function(){
        // $(".nul_bli").eq(6).find(".navb").addClass("active");
        // $(".navb.active").parent().find(".nul_s .nul_sli").eq(1).find(".navs").addClass("active");
    })
</script>
{/block}
{block name="main"}
<div class="if_wrap">
    <div class="cus-con">
        <div class="set_tit"><span>客资自动分配规则设定</span></div>
        <div class="set-top layui-form" style="min-width: 1130px;">
            <button class="layui-btn fp-btn" data-type="add" style="float:left;">新增规则</button>

            <div class="mkz_title">自动分配：</div>
            <div class="set_che">
                <input type="checkbox" lay-filter="auto_fp" {if $switch eq 1}checked{/if} id="auto_fp" lay-skin="switch" lay-text="ON|OFF" value="1">
            </div>
            <div class="mkz_title">自动接收：</div>
            <div class="set_che">
                <input type="checkbox" lay-filter="auto_jr" {if $auto_jr eq 1}checked{/if} id="auto_jr" lay-skin="switch" lay-text="ON|OFF" value="1">
            </div>
            <div class="mkz_title">开始结束时间</div>
            <input type="text" id="fp_time" name="fp_time" class="layui-input" value="{$fp_time}" readonly style="width:127px;float: left;">
            <div class="mkz_title">接收过期时间：</div>
            <input class="layui-input" value="{$timeout}" id="timeout" placeholder="" onkeyup="value=value.replace(/[\D]/g,'')" type="text" maxlength="4" style="width:60px;float: left;">
            <div class="mkz_title">秒</div>
            <div class="mkz_title">离线是否分配：</div>
            <div class="layui-input-block w-60" style="float: left;margin-left: 0px;">
                <select name="" id="not_online_is_fp">
                    <option value="0"{if $not_online_is_fp eq false}selected{/if}>否</option>
                    <option value="1"{if $not_online_is_fp eq 1}selected{/if}>是</option>
                </select>
            </div>
            <button class="layui-btn" id="set_fp_time" style="margin-left: 10px;">确定</button>
        </div>
        <div class="set-top">
            <table class="layui-table" lay-data="{ url:'/crm/setting/FPListJSON.html',id:'idTest'}" lay-filter="demo">
                <thead>
                <tr>
                    <th lay-data="{width:100,field:'id',align:'center',templet: '#titleTpl'}">序号</th>
                    <th lay-data="{width:120,field:'score',align:'center', toolbar: '#barDemo'}">操作</th>
                    <th lay-data="{width:150,field:'customer_condition',align:'center'}">分配维度</th>
                    <!--<th lay-data="{width:200,field:'customer_value',align:'center'}">维度值</th>-->
                    <th lay-data="{width:100,align:'center', toolbar: '#enabledModal'}">当前状态</th>
                    <th lay-data="{width:100,field:'customer_value',align:'center'}">目标地域</th>
                    <th lay-data="{width:100,field:'department_name',align:'center'}">目标部门</th>
                    <th lay-data="{width:100,field:'position_name',align:'center'}">目标岗位</th>
                    <th lay-data="{width:100,field:'group_name',align:'center'}">目标小组</th>
                    <th lay-data="{width:300,field:'staff_name',align:'center'}">目标员工</th>
                    <th lay-data="{width:100,field:'allocationtype',align:'center', toolbar: '#allocationtype'}">分配方式</th>
                    <th lay-data="{width:100,field:'allocationtype_value',align:'center'}">业绩</th>
                    <th lay-data="{width:200,field:'active_time',align:'center'}">业绩统计区间</th>

                    <th lay-data="{width:100,field:'order_sort',align:'center'}">优先级</th>


                </tr>
                </thead>
            </table>
        </div>
        <script type="text/html" id="titleTpl">
            {{d.LAY_TABLE_INDEX+1}}
        </script>
    </div>
</div>
<script type="text/html" id="condition">
    {{#  if(d.customer_condition == 'default'){ }}
    默认
    {{#  } else if(d.customer_condition == 'add_organize') { }}
    客资提报人
    {{#  } else if(d.customer_condition == 'customer_source') { }}
    客资来源
    {{#  } else if(d.customer_condition == 'customer_area') { }}
    客资分区
    {{#  } else if(d.customer_condition == 'customer_region') { }}
    客资地域
    {{#  } }}
</script>
<script type="text/html" id="allocationtype">
    {{#  if(d.allocationtype == 1){ }}
    业绩
    {{#  } else { }}
    平均
    {{#  } }}
</script>
<script type="text/html" id="enabledModal">
    {{#  if(d.isopen == 1){ }}
    <!--input type="checkbox" checked=""  name="isopen" lay-skin="switch" lay-text="ON|OFF" lay-event="qswitch"-->
    <span  title="已开启">已开启</span>
    {{#  } else { }}
    <!--input type="checkbox" name="isopen" lay-skin="switch" lay-text="ON|OFF" lay-event="qswitch"-->
    <span title="已关闭">已关闭</span>
    {{#  } }}
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-mini" lay-event="edit">编辑</a>
    {{#  if(d.customer_condition == 'default'){ }}
    &nbsp;
    {{#  } else { }}
    <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del">删除</a>
    {{#  } }}
    <!--a class="layui-btn layui-btn-mini layui-btn-warm" lay-event="up" title="向上">↑</a-->
    <!--a class="layui-btn layui-btn-mini layui-btn-normal" lay-event="down" title="向下">↓</a-->
</script>
{/block}
<script>
    {block name="script"}
    $(function(){
        layui.use('form', function(){
            layui.form.render();
        })
        //日历
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            var a = laydate.render({
                elem: '#fp_time'
                ,type: 'time'
                ,range: true
                ,done: function(value, date, endDate) {
                    if(date.hours >= endDate.hours){
                        layer.alert('开始时间不能大于等于结束时间');
                        return false;
                    }
                }
            });
        });


    })


    layui.use('form', function() {
        var form = layui.form;
        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');
        })


    })
    /*
     $('#btn').click(function(){
     table.reload('idTest', {
     url: 'FPListJSON'
     ,where: {department_name:$('#department_name').val()} //设定异步数据接口的额外参数
     //,height: 300
     });
     })*/
    layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element'], function(){
        var laydate = layui.laydate //日期
                ,laypage = layui.laypage //分页
        layer = layui.layer //弹层
                ,table = layui.table //表格
                ,carousel = layui.carousel //轮播
                ,upload = layui.upload //上传
                ,element = layui.element; //元素操作

        //监听工具条
        table.on('tool(demo)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'qswitch') {
                layer.msg('查看操作');
            } else if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    var id = obj.data.id;
                    //向服务端发送删除指令
                    $.ajax({
                        type: "POST",
                        url: "{:Url('crm/Setting/delFPSet')}",
                        data: {id:id},
                        dataType:'json',
                        success: function(ret){
                            if(ret){
                                layer.alert('分配规则删除成功');
                            }else{
                                layer.alert('分配规则删除失败');
                            }
                        },
                        error:function(XMLHttpRequest, textStatus, errorThrown){
                            var ret = XMLHttpRequest.responseJSON
                            layer.alert('操作失败');
                        }
                    })
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);
                    //向服务端发送删除指令
                });
            } else if (layEvent === 'edit') {



                load_page(
                        '/crm/Setting/fpIframe.html?id='+obj.data.id,
                        '修改分配规则',
                        function(){
                            windowFunc(obj.data.id);
                        },['保存','取消'],485,660
                );


            }else if(layEvent === 'up'){
                layer.confirm('确定上移吗', function (index) {
                    var id = obj.data.id;
                    var order_sort = obj.data.order_sort;
                    //向服务端发送删除指令
                    $.ajax({
                        type: "POST",
                        url: "{:Url('crm/Setting/upOrderSort')}",
                        data: {id:id,order_sort:order_sort},
                        dataType:'json',
                        success: function(ret){
                            if(ret.code == 200){
                                layer.alert(ret.msg);
                                table.reload('idTest', {
                                });
                            }else{
                                layer.alert(ret.msg);
                            }
                        },
                        error:function(XMLHttpRequest, textStatus, errorThrown){
                            var ret = XMLHttpRequest.responseJSON
                            layer.alert('操作失败');
                        }
                    })
                });
            }else if(layEvent === 'down'){
                layer.confirm('确定下移吗', function (index) {
                    var id = obj.data.id;
                    var order_sort = obj.data.order_sort;
                    //向服务端发送删除指令
                    $.ajax({
                        type: "POST",
                        url: "{:Url('crm/Setting/downOrderSort')}",
                        data: {id:id,order_sort:order_sort},
                        dataType:'json',
                        success: function(ret){
                            if(ret.code == 200){
                                layer.alert(ret.msg);
                                table.reload('idTest', {
                                });
                            }else{
                                layer.alert(ret.msg);
                            }
                        },
                        error:function(XMLHttpRequest, textStatus, errorThrown){
                            var ret = XMLHttpRequest.responseJSON
                            layer.alert('操作失败');
                        }
                    })
                });
            }
        });
        //添加规则事件
        $(".fp-btn").click(function(){
            load_page('/crm/Setting/fpIframe.html','添加分配规则',function(){
                windowFunc();
            },['保存','取消'],500,660)
        });

        layui.use('form', function(){
            var form = layui.form;
            form.on('switch(auto_fp)', function(data){
                // var fp_time = $(this).parent().next().next('input[name=fp_time]').val();
                // alert(fp_time);return;
                var fp_time = $('#fp_time').val();
                if(!fp_time || fp_time == null){
                    layer.alert('开始结束时间设置不正确');
                    return false;
                }
                var va = $('#timeout').val();
                if((va < 30) || (va > 9999) || (va == null)){
                    layer.alert('接收过期时间应设置在30--9999之间');
                    return false;
                }
                $.ajax({
                    type: "POST",
                    url: "auto_fp",
                    data: {},
                    dataType: "json",
                    success: function (ret) {
                        if(ret.error == 1){
                            layer.alert(ret.msg);
                            // return false;
                            //location.reload();
                        }else{
                            layer.alert(ret.msg);
                        }
                    }
                })
            });
            form.on('switch(auto_jr)', function(data){
                // var fp_time = $(this).parent().next().next('input[name=fp_time]').val();
                // alert(fp_time);return;
                $.ajax({
                    type: "POST",
                    url: "auto_jr",
                    data: {},
                    dataType: "json",
                    success: function (ret) {
                        if(ret.error == 1){
                            layer.alert(ret.msg);
                            // return false;
                            //location.reload();
                        }else{
                            layer.alert(ret.msg);
                        }
                    }
                })
            });
        });
        $('#set_fp_time').click(function(){
            var fp_time = $('#fp_time').val();
            if(!fp_time){
                layer.alert('请选择客资开始结束时间');
                return false;
            }
            var va = $('#timeout').val();
            if(va < 30){
                layer.alert('接收过期时间不能小于30秒');
                return false;
            }
            if(va > 9999){
                layer.alert('接收过期时间不能大于9999');
                return false;
            }
            var not_online_is_fp = $("#not_online_is_fp").val();
            $.ajax({
                type: "POST",
                url: "setFPTime",
                data: {fp_time:fp_time,timeout:va,not_online_is_fp:not_online_is_fp},
                dataType: "json",
                success: function (ret) {
                    if(ret.code == 200){
                        layer.alert("操作成功");
//                        location.reload();
                    }else{
                        layer.alert(ret.msg);
                    }
                }
            })
        })
        $('#default').click(function () {
            var id = $('#id').val();
            var dpg = $('input[name=dpg]:checked').val();
            var d_dpg = $('#d_dpg option:selected').val();
            var name = $('#d_dpg option:selected').html();
            var allocationtype = $('input[name=allocationtype]:checked').val();
            var allocationtype_value = $('input[name=all_value]:checked').val();
            var active_time = $('input[name=active_time2]:checked').val();
            var order_sort = $('input[name=order_sort]').val();
            if(allocationtype == 1 && (!allocationtype_value || !active_time)){
                layer.alert("按业绩分配时，业绩内容和统计区间不能为空");
                return false;
            }
            var data = "id=" + id + "&dpg=" + dpg + "&d_dpg=" + d_dpg + "&name=" + name + "&allocationtype=" + allocationtype + "&allocationtype_value=" + allocationtype_value + "&active_time=" + active_time + "&order_sort=" + order_sort;
            // alert(data);
            // return;
            $.ajax({
                type: "POST",
                url: "addDefaultFPSet",
                data: data,
                dataType: "json",
                success: function (ret) {
                    if (ret) {
                        layer.alert("操作成功");
                        // location.reload();
                    } else {
                        layer.alert("操作失败");
                    }
                }
            })
        })

    });
    {/block}
</script>
