<!DOCTYPE html>
<html>
<head>
    <base href="#springUrl('/assets/adminlte/')"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>工作台</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="plugins/font-awesome/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="plugins/ionicons/css/ionicons.min.css">

    <!-- DataTables -->
    <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">

    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
    <!-- iCheck -->
    <link rel="stylesheet" href="plugins/iCheck/flat/blue.css">
    <!-- Morris chart -->
    <link rel="stylesheet" href="plugins/morris/morris.css">
    <!-- jvectormap -->
    <link rel="stylesheet" href="plugins/jvectormap/jquery-jvectormap-1.2.2.css">
    <!-- Date Picker -->
    <link rel="stylesheet" href="plugins/datepicker/datepicker3.css">
    <!-- Daterange picker -->
    <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker-bs3.css">
    <link rel="stylesheet"
          href="plugins/bootstrap-fileinput/bootstrap-fileinput.css">
    <link rel="stylesheet"
          href="plugins/bootstrap-fileinput/fileinput.min.css">
    <!-- ztree css -->
    <link rel="stylesheet"
          href="plugins/ztree/css/metroStyle/metroStyle.css" type="text/css"/>
    <!-- bootstrap wysihtml5 - text editor -->
    <link rel="stylesheet" href="plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body class="hold-transition skin-blue sidebar-mini">


<div class="wrapper">
    <div class="content-wrapper" style="margin-left:0;">
        <div id="importExcelDiv"></div>
        <section class="content-header">
            <h1>
                人员管理
                <small>人员信息</small>
            </h1>
        </section>


        <div id="myModal" class="modal fade" data-backdrop="static">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">导入日志</h4>
                    </div>
                    <!-- dialog body -->
                    <div  class="modal-body" style="height: 200px;overflow: scroll">
                        <div id="logText">${text}</div>
                    </div>
                    <!-- dialog buttons -->
                </div>
            </div>
        </div>

        <!-- 设置峰值弹出窗 -->
        <div id="peakButtom" class="modal fade">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">设置峰值</h4>
                    </div>

                    <!-- dialog body -->
                    <div class="modal-body">
                        <input type="number" class="form-control select2" id="peakVal" name="peakVal" oninput="if(value<=0)value=0" onchange="updateCount(this)" placeholder="请输入一个正整数"></input>
                        <input type="hidden" id="peakId">
                    </div>
                    <div style="text-align: center;padding-bottom: 20px;">
                        <button style="width: 100px;" type="button" class="btn btn-primary" onclick="submitPeakVal()">确定</button>
                        <button style="width: 100px;" type="button"  class="btn btn-default" data-dismiss="modal" aria-label="Close">取消</button>
                    </div>
                    <!-- dialog buttons -->
                </div>
            </div>
        </div>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-2" style="display:none">
                    <div class="box box-primary">
                        <ul id="orgTree" class="ztree"></ul>
                    </div>
                </div>
                <div class="col-xs-12">
                    <div class="row">
                        <div class="col-xs-12">
                            <form id="seachTableForm" method="get" action="#springUrl('/a/qyuser/exportExcel')" onkeydown="if(event.keyCode==13){return false;}">
                                <div class="box box-primary">
                                    <div class="box-header with-border">
                                        <h3 class="box-title">查询窗口</h3>
                                        <div class="box-tools pull-right">
                                            <button class="search btn btn-primary" type="button">
                                                搜索
                                            </button>
                                            <button type="submit" class="btn btn-primary">导出</button>
                                            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i
                                                    class="fa fa-minus"></i>
                                            </button>
                                        </div>
                                    </div>

                                    <div class="box-body">
                                        <div class="form-group col-md-4">
                                            <label class="control-label">人员名称</label>
                                            <input type="text" class="form-control required"
                                                   name="name" placeholder="人员名称">
                                        </div>
                                        <div class="form-group col-md-4">
                                            <label class="control-label">手机号</label>
                                            <input type="text" class="form-control required"
                                                   name="mobile" placeholder="手机号">
                                        </div>
                                        <div class="form-group col-md-4">
                                            <label class="control-label">部门</label>
                                            <input type="text" class="form-control required"
                                                   name="description" placeholder="部门">
                                        </div>
                                        <div class="" style="display: none">
                                            <input type="text" class="form-control required"
                                                   name="id" id="orgid" placeholder="组织Id">
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    <input name="bindProId" id="bindProId" style="display:none">
                    <input name="bindProName" id="bindProName" style="display:none">

                    <div class="row">
                        <div class="col-xs-12">
                            <div class="box box-primary">
                                <div class="box-header with-border">
                                    <h3 class="box-title"></h3>
                                    <div class="box-tools pull-right">

                                        #if($shiro.hasPermission("sys:qyuser:edit"))
                                        <!--<a onclick="syncWx();" class="btn btn-success">同步微信</a>-->
                                        <a href="#springUrl('/a/qyuser/form')" class="btn btn-primary">新增</a>
                                        <a onclick="importExcel();" class="btn btn-primary">导入</a>
                                        #end
                                    </div>
                                </div><!-- /.box-header -->


                                <div class="box-body">
                                    <table id="example2" class="table table-bordered table-striped">
                                        <thead>
                                        <tr>
                                            <td hidden="true">Id</td>
                                            <th>二维码</th>
                                            <th>登录账号</th>
                                            <th>姓名</th>
                                            <th>手机号</th>
                                            <th>邮箱</th>
                                            <th>职位</th>
                                            <th>峰值</th>
                                            <th>操作</th>
                                        </tr>
                                        </thead>
                                        <tbody id="tablebody">
                                        </tbody>

                                    </table>
                                </div><!-- /.box-body -->
                            </div><!-- /.box -->
                        </div><!-- /.col -->
                    </div><!-- /.row -->
                </div>
            </div>
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->


    <!-- Add the sidebar's background. This div must be placed
         immediately after the control sidebar -->
    <div class="control-sidebar-bg"></div>
</div><!-- ./wrapper -->
<!-- jQuery 2.1.4 -->
<script src="plugins/jQuery/jQuery-2.1.4.min.js"></script>
<!-- Bootstrap 3.3.5 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="plugins/input-mask/jquery.inputmask.js"></script>
<script src="plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
<script src="plugins/input-mask/jquery.inputmask.extensions.js"></script>
<!-- DataTables -->
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables/extensions/i18n/lanauage_ch.js"></script>
<script src="plugins/datatables/dataTables.bootstrap.min.js"></script>
<!-- SlimScroll -->
<script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
<script src="plugins/datepicker/bootstrap-datepicker.js"></script>
<script src="plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"></script>
<!-- FastClick -->
<script src="plugins/fastclick/fastclick.min.js"></script>
<!-- fileinput -->
<script src="plugins/bootstrap-fileinput/bootstrap-fileinput.js"></script>
<script src="plugins/bootstrap-fileinput/fileinput.min.js"></script>
<script src="plugins/bootstrap-fileinput/zh2.js"></script>
<!-- ztree -->
<script src="plugins/ztree/js/jquery.ztree.all.min.js"
        type="text/javascript"></script>
<!-- AdminLTE App -->
<script src="plugins/bootstrap-fileinput/bootstrap-fileinput.js"></script>
<script src="plugins/bootstrap-fileinput/fileinput.min.js"></script>
<script src="plugins/bootstrap-fileinput/zh2.js"></script>
<script src="dist/js/app.min.js"></script>
<script type="text/javascript"
        src="plugins/jquery-validation/js/jquery.validate.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script>
    var csrfheader = {name:'_csrf_header',value:'${_csrf.headerName}'};
    var csrftoken  = {name:'_csrf',value:'${_csrf.token}'};
</script>
<script src="common/js/cfapp.js"></script>
<script src="dist/js/demo.js"></script>
<script>

    var myDataTable = null;

    function formatDates(now) {
        var now = new Date(now);
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = now.getSeconds();
        return year + "-" + month + "-" + date + "   " + hour + ":"
            + minute + ":" + second;
    }

    if($("#logText").html()!=null && $("#logText").html()!="" && $("#logText").html()!="${text}"){
        $("#myModal").modal('show');
    }

    function seachTable() {
        var sSource = "#springUrl('/a/qyuser/listDataNoAuth')";
        var aoData = {
            iDisplayStart: 1,
            iDosplayLength: 10
        }
        var retrieveData = function (sSource, aoData, fnCallback) {

            $("#seachTableForm input").each(function () {
                var params = {
                    name: $(this).attr("name"),
                    value: $(this).val()
                };
                aoData.push(params);
            })

            $.ajax({
                "type": "GET",
                "url": sSource,
                "dataType": "json",
                "data": aoData, //以json格式传递
                "success": fnCallback
            });
        };

        myDataTable = $('#example2').DataTable({
//            scrollX: true,
//            "sScrollY" : 400,
//            sScrollX: "100%",
//            bScrollCollapse: false,
//            sScrollXInner: "150%",
//            scrollCollapse: true,
            "lengthChange": false,
            "searching": false,
            "ordering": false,
            "bFiltered": false,
            "bStateSave": true, // save datatable state(pagination, sort, etc) in cookie.
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": sSource,
            "fnServerData": retrieveData,
            "pagingType": "full_numbers",
            "aoColumns": [{
                "mData": "id"
            }, {
                "mData": "qrcode"
            },{
                "mData": "loginName"
            },{
                "mData": "name"
            },{
                "mData": "mobile"
            }, {
                "mData": "email"
            },{
                "mData": "userType"
            },{
                "mData": "peakVal"
            },/* {
                "mData": "storeName"
            },{
                "mData": "description"
            },{
                "mData": "fanssCount"
            }, */{
                "mData": "id"
            }],
            "aoColumnDefs": [{ // set default column settings
                'visible': false,
                'targets': [0]
            },{
                "aTargets": [1],
                "mData": "qrcode",
                "mRender": function (a, b, c, d) {//a表示对应mData的值，c表示当前记录行对象
                    if (a) {
                        return "<img src='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + a + "' style='width: 80px;height: 80px;'>";
                    }
                    return "<font color='red'>未绑定</font>";
                }
            },{
                "aTargets": [6],
                "mData": "userType",
                "mRender": function (a, b, c, d) {//a表示对应mData的值，c表示当前记录行对象
                    let postion = null;
                    switch (a) {
                        case "0":
                            postion = "其他";
                            break;
                        case "1":
                            postion = "药师";
                            break;
                        case "2":
                            postion = "客服";
                            break;
                        case "3":
                            postion = "医生";
                            break;
                        case "4":
                            postion = "订单员";
                            break;
                    }
                    return "<font color='blue'>" +postion + "</font>";
                }
            },{
                "aTargets": [8],
                "mData": "id",
                "mRender": function (a, b, c, d) {//a表示id对应的值，c表示当前记录行对象
                    var html = '#if($shiro.hasPermission("sys:qyuser:edit"))';//
                    var html = '<div class="btn-group">\n' +
                        '<button type="button" class="btn btn-success btn-flat">操作</button>\n' +
                        '<button type="button" class="btn btn-success btn-flat dropdown-toggle" data-toggle="dropdown">\n' +
                        '<span class="caret"></span>\n' +
                        '<span class="sr-only">Toggle Dropdown</span>\n' +
                        '</button>\n' +
                        '<ul class="dropdown-menu" role="menu">\n';
                    html += "<li><a href=#springUrl('/a/qyuser/form?id=" + a +"') >修改</a></li>";
                    if (c.userType == '0' || c.userType == '1' || c.userType == '2') {
                        html += "<li><a href='javascript:bindQrCode(" + a + ", "+ c.userType +")' >绑定二维码</a></li>";
                        if (c.qrcode != null)
                            html += "<li><a href='javascript:unBindQrCode(" + a + ", "+ c.userType +")' >取消绑定二维码</a></li>";
                        html += "<li><a href='javascript:peakVal(" + a + ")' data-target='#foo'>设置患者分配峰值</a></li>";
                    }
                    html += '<li><a href="javascript:removeData(' + a + ')" >删除</a></li>#end';
                    html+= '</ul></div>';
                    return html;
                }
            }]
        });
    }

    jQuery(document).ready(function () {
        seachTable();
        $('.datepicker').datepicker({
            show: true,
            format: 'yyyy-mm-dd',
            autoclose: true,
            language: 'zh-CN',
            todayBtn: 'linked',
            clearBtn: 'linked'

        });
        $('.search').click(function () {
            $("#example2").dataTable().fnClearTable();
        });
        //initOrgTree();
    });

    function unBindQrCode(id, type) {
        var unBindQrcodeUri = "#springUrl('/a/qyuser/unBindQrcode?type="+type+"')";
        Cfapp.confirm({
            message: "确定要解除二维码绑定吗",
            btnoktext: "确定",
            btncanceltext: "取消",
            success: function () {
                $.ajax({
                    "type": "post",
                    "url": unBindQrcodeUri,
                    "dataType": "json",
                    "data": {userId: id,_csrf_header:csrfheader.value,_csrf:csrftoken.value}, //以json格式传递
                    "success": function (data) {
                        if (data.errorNo == 0) {
                            Cfapp.alert({
                                message: "解除绑定成功",
                                btntext: "确定",
                                success: function () {
                                    $("#example2").dataTable().fnClearTable();
                                }
                            })
                        } else {
                            Cfapp.alert({
                                message: "解除绑定失败",
                                btntext: "确定",
                                success: function () {
                                }
                            })
                        }
                    }
                });
            },
            cancel: function () {
                $(".modal-backdrop").fadeOut();

            }
        })
    }

    function peakVal(id) {
        $("#peakButtom").modal('show');
        $("#peakId").val(id);
    }

    function submitPeakVal() {
        var setPeakValUrl = "#springUrl('/a/qyuser/setPeakVal')";
        Cfapp.confirm({
            message: "确认提交峰值？",
            btnoktext: "确定",
            btncanceltext: "取消",
            success: function () {
                $.ajax({
                    "type": "post",
                    "url": setPeakValUrl,
                    "dataType": "json",
                    "data": {id: $("#peakId").val(), peakVal: $("#peakVal").val(), _csrf_header:csrfheader.value, _csrf:csrftoken.value}, //以json格式传递
                    "success": function (data) {
                        if (data.errorNo == 0) {
                            Cfapp.alert({
                                message: "设置成功",
                                btntext: "确定",
                                success: function () {
                                    location.href = "#springUrl('/a/qyuser/list')";
                                }
                            })
                        } else {
                            Cfapp.alert({
                                message: "设置失败",
                                btntext: "确定",
                                success: function () {
                                }
                            })
                        }
                    }
                });
            },
            cancel: function () {
                $("#peakButtom").modal('hide');
            }
        })
    }


    //绑定门店二维码
    function bindQrCode(id, type) {
        var qrCodeUri = "#springUrl('/a/wxQrcode/listData?type="+type+"&isempty=1')";
        var bindQrcodeUri = "#springUrl('/a/qyuser/bindQrcode')";
        Cfapp.f7({
            dataUrl: qrCodeUri,  //导入的action Url
            checkType: 'single', // 多选为multi
            title: "绑定二维码",
            btnoktext: "确定",
            btncanceltext: "取消",
            columns: ['选择', '描述', '场景值', '创建时间'],
            isSearch: '1',
            aoColumns: [{
                "mData": "id"
            }, {
                "mData": "description"
            }, {
                "mData": "sceneId"
            }, {
                "mData": "createTime"
            }],
            aoColumnDefs: [{
                "aTargets": [0],
                "mData": "id",
                "mRender": function (a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
                    return '<input type="radio" id="' + a + '" name="sel" data-value="' + a + '" data-name="' + c.name + '">';
                }
            },
                {
                    "aTargets": [3],
                    "mData": "createTime",
                    "mRender": function (a, b, c, d) {//a表示createtime对应的值，c表示当前记录行对象
                        return formatDates(a, "yyyy-MM-dd HH:mm:ss");
                    }
                }
            ],
            success: function () {
                var qrcodeId = $('#bindProId').val();
                if (qrcodeId && qrcodeId !== '') {
                    $.ajax({
                        "type": "POST",
                        "url": bindQrcodeUri,
                        "dataType": "json",
                        "data": {id:  id, wxQrcodeId: qrcodeId,_csrf_header:csrfheader.value,_csrf:csrftoken.value}, //以json格式传递
                        "success": function (data) {
                            if (data.errorNo == '0') {
                                Cfapp.alert({
                                    message: "绑定二维码成功",
                                    btntext: "确定",
                                    success: function () {
                                        myDataTable.draw(false);
                                    }
                                })
                            } else {
                                Cfapp.alert({
                                    message: "绑定二维码失败",
                                    btntext: "确定",
                                    success: function () {
                                    }
                                })
                            }
                        }
                    });
                }
            },
            cancel: function () {

            },
            displayFields: 'bindProName',  //显示的字段
            storeFields: 'bindProId' // 储存的字段
        })
    }

    var orgTreeUri = "#springUrl('/a/orgunit/treeDataNoAuth')";

    function initOrgTree() {
        var orgTree = $('#orgTree');

        var getOrgTreeData = function () {
            $.ajax({
                "type": "GET",
                "url": orgTreeUri,
                "dataType": "json",
                "success": buildOrgTree
            });
        };
        var buildOrgTree = function (data) {
            var setting = {
                data: {simpleData: {enable: true}}, callback: {
                    beforeClick: function (id, node) {
                        var orgid = node.id;
                        if (orgid == 0) {
                            $('#orgid').val('');
                        } else {
                            $('#orgid').val(orgid);
                        }
                        $("#example2").dataTable().fnClearTable();
                    }
                }
            };
            var zNodes = data.aaData;
            tree = $.fn.zTree.init(orgTree, setting, zNodes);

            tree.expandAll(true);
            tree.expandNode(tree.getNodesByFilter(function (node) {
                return node.level == 0;
            }, true), true, false, false);
        };
        getOrgTreeData();
    }

    Cfapp.init();

    /**
     * 除去门店绑定关系，包含已绑定的角色
     */
    function unBindStore(id) {
        Cfapp.confirm({
            message: "确定要去除门店绑定关系吗",
            btnoktext: "确定",
            btncanceltext: "取消",
            success: function () {
                $.ajax({
                    type: "POST",
                    url: "#springUrl('/a/qyuser/unBindStore')",
                    data: {id: id,_csrf_header:csrfheader.value,_csrf:csrftoken.value},
                    dataType: "json",
                    success: function (data) {
                        if (data.errorNo == 0) {
                            Cfapp.alert({
                                message: "去除门店绑定关系成功",
                                btntext: "确定",
                                success: function () {
                                    myDataTable.draw(false);
                                }
                            });
                        }
                    },
                    error: function () {
                    }
                })
            },
            cancel: function () {
                $(".modal-backdrop").fadeOut();
            }
        });
    }

    function removeData(data) {
        Cfapp.confirm({
            message: "确定要删除吗",
            btnoktext: "确定",
            btncanceltext: "取消",
            success: function () {
                $.ajax({
                    type: "POST",
                    url: "#springUrl('/a/qyuser/delete')",
                    data: {id: data,_csrf_header:csrfheader.value,_csrf:csrftoken.value},
                    dataType: "json",
                    success: function (data) {
                        if (data.errorNo == 0) {
                            Cfapp.alert({
                                message: "删除成功",
                                btntext: "确定",
                                success: function () {
                                    myDataTable.draw(false);
                                }
                            });
                        }else{
                            Cfapp.alert({
                                message: "删除失败",
                                btntext: "确定",
                                success: function () {

                                }
                            });
                        }
                    },
                    error: function () {
                    }
                })
            },
            cancel: function () {
                $(".modal-backdrop").fadeOut();
            }
        });
    }

    function importExcel() {
        var templateExcelUrl = "#springUrl('/a/qyuser/templateExcel')";
        var importExcelUrl = "#springUrl('/a/qyuser/importExcel')?_csrf_header=" + csrfheader.value + "&_csrf=" + csrftoken.value;

        Cfapp.importExcel({
            title: '人员导入',
            importurl: importExcelUrl,
            templateurl: templateExcelUrl,
            cancel: function () {

            },
            success: function () {

            }
        });
    }

    function syncWx() {
        var warningMsg = Cfapp.warning({
            content: '正在同步中，请稍后!',  //
            title: "提示"
        });
        var syncUrl = "#springUrl('/a/qyuser/sync')";
        $.ajax({
            "type": "GET",
            "url": syncUrl,
            "dataType": "json",
            "success": function (retData) {
                warningMsg.destory();
                var message = '<label>成功创建人员<font color="green">' + retData.successCreateCount + '</font>条</label><br>';
                message += '<label>成功更新人员<font color="green">' + retData.successUpdateCount + '</font>条</label><br>';
                message += '<label>同步失败<font color="red">' + retData.failureUpdateCount + '</font>条</label><br>';
                Cfapp.alert({
                    title: "同步成功",
                    message: message,
                    btntext: "确定",
                    success: function () {
                        myDataTable.draw(false);
                    }
                });
            }
        });
    }


</script>

</body>
</html>
