{extend name="Public/base" /}
{block name="css"}
<link rel="stylesheet" href="__PUBLIC__/item/css/layui.css?v={$Think.const.APP_VERSION}">
<link rel="stylesheet" href="__PUBLIC__/css/customer.css?v={$Think.const.APP_VERSION}">
<link rel="stylesheet" href="__PUBLIC__/css/set.css?v={$Think.const.APP_VERSION}">
<style>
    .layui-table-page {
        height: 26px;
        margin-top: -14px;
    }
</style>
{/block}
{block name="js"}
<script>
    $(function(){
        // $(".nul_bli").eq(4).find(".navb").addClass("active");
//        $(".navb.active").parent().find(".nul_s .nul_sli").eq(18).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">
            <div class="layui-inline">
                {auth code='menu1512637269'}
                <div class="layui-input-inline w-150">
                    <select id="department_id" lay-filter="department_id" lay-verify="required" lay-search="">
                        <option value="">请选择部门</option>
                        {volist name="department" id="nel"}
                        <option value="{$nel.id}">{$nel.department_name}</option>
                        {/volist}
                    </select>
                </div>
                <div class="layui-input-inline w-200" >
                    <select id="position_code" lay-filter="position_code" lay-verify="required" lay-search="">
                        <option value="">请选择岗位</option>
                        {volist name="position" id="nel"}
                        <option value="{$nel.position_code}">{$nel.position_name}</option>
                        {/volist}
                    </select>
                </div>
                <div class="layui-input-inline w-200" >
                    <select id="staff_id" lay-filter="staff_id" lay-verify="required" lay-search="">
                        <option value="">请选择人员</option>
                        {volist name="staff" id="nel"}
                        <option value="{$nel.id}">{$nel.staffname}</option>
                        {/volist}
                    </select>
                </div>
                <div class="layui-input-inline w-200" >
                    <input type="text" class="layui-input" id="staff_name" placeholder="请输入员工名称">
                </div>
                <button class="layui-btn" id="btn">查询</button>
                {/auth}
            </div>
        </div>
        <table class="layui-table" lay-data="{height:740, url:'/crm/setting/staffStatusJSON.html',id:'idTest'}"  lay-filter="demo">
            <thead>
            <tr>
                <th lay-data="{width:100,field:'id',sort: true,align:'center',templet: '#titleTpl'}">序号</th>
                <th lay-data="{width:80,field:'staff_name',align:'center'}">人员</th>
                <th lay-data="{width:150,field:'department_name',align:'center'}">部门</th>
                <th lay-data="{width:150,field:'main_position_name',align:'center'}">岗位</th>
                <th lay-data="{width:80,field:'status',align:'center',toolbar:'#status'}">在线状态</th>
                <th lay-data="{width:150,field:'score',align:'center', toolbar: '#barDemo'}">操作</th>
            </tr>
            </thead>
        </table>
    </div>
</div>
<script type="text/html" id="titleTpl">
    {{d.LAY_TABLE_INDEX+1}}
</script>
<script type="text/html" id="barDemo">
    {{#  if(d.status == 1){ }}
    <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="busy">忙碌</a>
    <a class="layui-btn layui-btn-warm layui-btn-mini" lay-event="Invisible">隐身</a>
    {{#  } else if(d.status == 2) { }}
    <a class="layui-btn layui-btn-mini" lay-event="online">在线</a>
    <a class="layui-btn layui-btn-warm layui-btn-mini" lay-event="Invisible">隐身</a>
    {{#  } else if(d.status == 3) { }}
    <a class="layui-btn layui-btn-mini" lay-event="online">在线</a>
    <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="busy">忙碌</a>
    {{#  } else  { }}

    {{#  } }}
</script>
<script type="text/html" id="status">
    {{#  if(d.status == 1){ }}
    <a class="layui-btn layui-btn-mini">在线</a>
    {{#  } else if(d.status == 2) { }}
    <a class="layui-btn layui-btn-danger layui-btn-mini">忙碌</a>
    {{#  } else if(d.status == 3) { }}
    <a class="layui-btn layui-btn-warm layui-btn-mini">隐身</a>
    {{#  } else  { }}
    离线
    {{#  } }}
</script>
<script>

    $('#btn').click(function(){
        table.reload('idTest', {
            url: 'staffStatusJSON'
            ,where: {department_id:$('#department_id').val(),position_code:$('#position_code').val(),staff_id:$('#staff_id').val(),staff_name:$('#staff_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 === 'detail') {
                layer.msg('查看操作');
            } else if (layEvent === 'online') {
                $.ajax({
                    type: "POST",
                    url: "{:Url('crm/Customer/update_staff_status')}",
                    data: {staff_id:obj.data.id,status:1},
                    dataType:'json',
                    success: function(ret){
                        if(ret){
                            layer.alert('操作成功');
                            table.reload('idTest', {
                            });
                        }else{
                            layer.alert(ret.msg);
                        }
                    },
                    error:function(XMLHttpRequest, textStatus, errorThrown){
                        var ret = XMLHttpRequest.responseJSON
                        layer.alert('操作失败');
                    }
                })
            } else if (layEvent === 'busy') {
                $.ajax({
                    type: "POST",
                    url: "{:Url('crm/Customer/update_staff_status')}",
                    data: {staff_id:obj.data.id,status:2},
                    dataType:'json',
                    success: function(ret){
                        if(ret){
                            layer.alert('操作成功');
                            table.reload('idTest', {
                            });
                        }else{
                            layer.alert(ret.msg);
                        }
                    },
                    error:function(XMLHttpRequest, textStatus, errorThrown){
                        var ret = XMLHttpRequest.responseJSON
                        layer.alert('操作失败');
                    }
                })
            } else if (layEvent === 'Invisible') { //隐身
                $.ajax({
                    type: "POST",
                    url: "{:Url('crm/Customer/update_staff_status')}",
                    data: {staff_id:obj.data.id,status:3},
                    dataType:'json',
                    success: function(ret){
                        if(ret){
                            layer.alert('操作成功');
                            table.reload('idTest', {
                            });
                        }else{
                            layer.alert(ret.msg);
                        }
                    },
                    error:function(XMLHttpRequest, textStatus, errorThrown){
                        var ret = XMLHttpRequest.responseJSON
                        layer.alert('操作失败');
                    }
                })
            }
        });

        layui.use('form', function() {
            var form = layui.form;
            form.on('select(department_id)', function(data){
                var department_id = $('#department_id').val();
                $.ajax({
                    type: "POST",
                    url: "getPositionByDepId",
                    data: {department_id:department_id},
                    dataType: 'json',
                    success: function (ret) {
                        var replace = '<option value="">请选择岗位</option>';
                        for (var i = 0; i < ret.length; i++) {
                            replace += '<option value="' + ret[i]['position_code'] + '">' + ret[i]['position_name'] + '</option>';
                        }
                        $("#position_code").html(replace);
                        layui.form.render();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        var ret = XMLHttpRequest.responseJSON
                        layer.alert('操作失败');
                    }
                })
            })
            form.on('select(position_code)', function(data){
                var position_code = $('#position_code').val();
                $.ajax({
                    type: "POST",
                    url: "getPositionStaffs",
                    data: {main_position_code:position_code},
                    dataType: 'json',
                    success: function (ret) {
                        var replace = '<option value="">请选择人员</option>';
                        for (var i = 0; i < ret.length; i++) {
                            replace += '<option value="' + ret[i]['id'] + '">' + ret[i]['staffname'] + '</option>';
                        }
                        $("#staff_id").html(replace);
                        layui.form.render();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        var ret = XMLHttpRequest.responseJSON
                        layer.alert('操作失败');
                    }
                })
            })
        })

        //分页
        laypage.render({
            elem: 'pageDemo' //分页容器的id
            ,count: 100 //总页数
            ,skin: '#1E9FFF' //自定义选中色值
//            ,skip: true //开启跳页
            ,jump: function(obj, first){
                if(!first){
                    layer.msg('第'+ obj.curr +'页');
                }
            }
        });
    })
</script>
{/block}