{{include "com/inc.html"}}
<!-- Main Header -->
{{include "com/header.html"}}
<!-- Left side column. 权限菜单 -->
{{include "com/menu.html"}}
<div class="content-wrapper">
    <section class="content-header">
        <h1>
            <small></small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="/front/user"><i class="fa fa-dashboard"></i> 用户组管理</a></li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-body">

                        <div class="form-group" style="margin-left: 20px">
                            <a class="btn btn-sm btn-success margin" href="/front/group/new"><i class="fa fa-fw fa-plus fa-lg"></i>新建用户组</a>
                            <a class="btn btn-sm btn-default margin" onclick="bulkSetup(true)">批量开启</a>
                            <a class="btn btn-sm btn-default margin" onclick="bulkSetup(false)">批量关闭</a>
                        </div>
                        <div id="status-div" class="col-xs-1" style="width: auto;float: right">
                            <select class="form-control selectpicker" id="pushModelSelect">
                                <option value=-1>全部</option>
                                <option value=0>开启</option>
                                <option value=1>关闭</option>
                            </select>
                        </div>
                        <table id="dataTable" class="table table-bordered table-hover">
                            <thead>
                            <tr>
                                <th><input type="checkbox" onclick="allSelect(this)" id="allCheckbox"></th>
                                <th>用户组名称</th>
                                <th>负责人</th>
                                <th>创建时间</th>
                                <th>更新时间</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                        </table>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
        </div>
    </section>
</div>


<div class="modal fade" id="modal-modify-group" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" style="width: 30%">
        <div class="modal-content">
            <div class="modal-header">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <div class="edit-info">
                        <span class="glyphicon glyphicon-alert" aria-hidden="true"></span>
                        <span class="h4">修改用户信息</span>
                    </div>
                    <div class="edit-form">
                        <hr>
                        <form class="form-horizontal">
                            <div class="form-group margin-bottom">
                                <input type="text" class="form-control" id="group-id" placeholder="用户账号" style="display: none">
                                <label class="col-sm-3 control-label">用户组名称</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="group-name" placeholder="用户组名称" readonly>
                                </div>
                            </div>
                            <div class="form-group margin-bottom">
                                <label class="col-sm-3 control-label"><span style="color:red;">* </span>公司名称</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="group-entname" placeholder="公司名称">
                                </div>
                            </div>
                            <div class="form-group margin-bottom">
                                <label class="col-sm-3 control-label">负责人</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="group-person" placeholder="负责人">
                                </div>
                            </div>
                            <div class="form-group margin-bottom">
                                <label class="col-sm-3 control-label">联系方式</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="group-contract" placeholder="电话/邮箱">
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <input type="button" onclick="modifySave()" class="btn btn-primary saveBtn" value="保存">
                <input type="button" onclick="cancelModel()" class="btn btn-default" value="取消">
            </div>
        </div>
    </div><!-- /.modal -->
</div>

{{include "com/footer.html"}}
<script>
    menuActive("group");

    let selectIndex = [];               //选中的编号
    let keyTableChecked = false;        //关键词表格数据是否有选中
    let keyTableAllChecked = true;      //表单数据是否被全选中

    $(function () {
        ttable = $('#dataTable').dataTable({
            "paging": true,
            "lengthChange": false,
            "searching": true,
            "ordering": false,
            "info": true,
            "autoWidth": false,
            "serverSide": true,
            "ajax": {
                "url": "/front/group",
                "type": "post",
                "data": {}
            },
            "language": {
                "url": "/dist/js/dataTables.chinese.lang"
            },
            "fnDrawCallback": function () {
                $("ul.pagination").prepend("&nbsp;&nbsp;&nbsp;转到第 <input type='text' id='changePage'   style='width:20px;'> 页    <a type='text' href='javascript:void(0);' id='dataTable-btn' style='text-align:center'>GO</a>");
                $('#dataTable-btn').click(function (e) {
                    var redirectpage = 0
                    if ($("#changePage").val() && $("#changePage").val() > 0) {
                        var redirectpage = $("#changePage").val() - 1;
                    }
                    ttable.page(redirectpage).draw(false);
                });
                // this.api().column(0).nodes().each(function(cell, i) {
                //     cell.innerHTML = i + 1;
                // });
            },
            "columns": [
                    {"data": "", width: "1%", render: function() {
                        return `<input type="checkbox" name="ckb-keyid" onclick="singleSelect(this)" style="text-align: center">`
                    }},
                {"data": "s_name", width: "10%"},
                {"data": "s_personname", width: "5%"},
                {"data": "i_createtime",width:"5%", render: function (val) {
                        var dt = new Date()
                        dt.setTime(parseInt(val) * 1000);
                        return dt.format("yyyy-MM-dd")
                    }},
                {"data": "i_updatetime", width:"5%", render: function (val) {
                  var dt = new Date()
                  dt.setTime(parseInt(val) * 1000);
                  return dt.format("yyyy-MM-dd")
                }},
                {"data": "i_state", render: function (val, a, row) {
                        tmp = ""
                        if(val){
                            tmp="<a href='#' title='停用' onclick='setupState(\""+row._id+"\",false)'><i class='fa fa-fw fa-circle text-green'></i></a>已启用"
                        }else{
                            tmp="<a href='#' title='启用' onclick='setupState(\""+row._id+"\",true)'><i class='fa fa-fw fa-circle text-red'></i></a>未启用"
                        }
                        return tmp
                    }, width: "5%" },
                {"data": "_id", width:"11%",render: function (val, a, row, pos) {
                      tmp = '<div>' +
                          '<a class="btn btn-sm btn-primary" onclick="editGroup(\''+pos.row+'\')">编辑</a>&nbsp;&nbsp;'+
                          '<a class="btn btn-sm btn-primary" href="/front/group/user?id='+val+'&s_sourceinfo='+row.s_sourceinfo+'">设置</a>&nbsp;&nbsp;'+
                          '<a class="btn btn-sm btn-danger" onclick="del(\''+val+'\')">删除</a>'+
                          '</div>';
                      return tmp
                }}
            ],
            "initComplete": function () {
                $("#dataTable_filter").append($('#status-div'))
            }
        });

    });

    function setupState(id, v) {
        console.log(id, v)
        showConfirm("确定修改当前状态？", function() {
            $.ajax({
                url: "/front/group/state",
                type: 'POST',
                data: {"id": id, "state": v},
                success: function (r) {
                    if (r.rep) {
                        ttable.ajax.reload();
                    } else {
                        showTip("状态修改失败");
                    }
                }
            })
        })
    }

    function del(id) {
        showConfirm("确定删除该用户组？", function() {
            $.ajax({
                url: "/front/group/del",
                type: 'POST',
                data: {"id": id},
                success: function (r) {
                    if (r.rep) {
                        ttable.ajax.reload();
                    } else {
                        showTip("状态修改失败");
                    }
                }
            })
        })
    }

    //表格全选
    function allSelect(obj) {
        if ($(obj).is(':checked')) {
            selectIndex.length = 0;
            keyTableChecked = true;
            keyTableAllChecked = true;
            $('input[name="ckb-keyid"]').each(function () {
                this.checked = true;
                selectIndex.push(ttable.fnGetData()[$(this).parents("tr").index()]["_id"]);
            })
        } else {
            selectIndex.length = 0;
            keyTableChecked = false;
            keyTableAllChecked = false;
            $('input[name="ckb-keyid"]').each(function () {
                this.checked = false;
            })
        }
    }

    //表格单个选中
    function singleSelect(obj) {
        let tag = true;
        selectIndex.length = 0;
        $('input[name="ckb-keyid"]').each(function () {
            if (this.checked) {
                keyTableChecked = true;
                selectIndex.push(ttable.fnGetData()[$(this).parents("tr").index()]["_id"]);
            }else {
                tag = false
            }
        });
        if (tag) {
            keyTableAllChecked = true;
            $('#allCheckbox').prop("checked", true);
        } else {
            keyTableAllChecked = false;
            $('#allCheckbox').prop("checked", false);
        }
    }

    function bulkSetup(val) {
        showConfirm("确定修改全部状态吗？", function (){
            $.ajax({
                url: "/front/group/bulks_setup",
                type: 'POST',
                data: {"ids": selectIndex.toString(), "state": val},
                success: function (r) {
                    if (r.rep) {
                        $("#allCheckbox").prop("checked", false)
                        ttable.api().ajax.reload()
                    } else {
                        showTip("状态修改失败");
                    }
                }
            })
        })
    }

    function editGroup(v){
        let tmp = ttable.fnGetData()[v]
        $("#group-id").val(tmp["_id"])
        $("#group-name").val(tmp["s_name"])
        $("#group-entname").val(tmp["s_entname"])
        $("#group-person").val(tmp["s_personname"])
        $("#group-contract").val(tmp["s_personcontact"])
        $('#modal-modify-group').modal("show")
    }

    function cancelModel() {
        $('#modal-modify-group').modal('hide')
    }

    function modifySave() {
        let id = $("#group-id").val()
        let entname = $("#group-entname").val().trim()
        let personname = $("#group-person").val().trim()
        let contract = $("#group-contract").val().trim()
        if (entname !== "") {
            $.ajax({
                url: "/front/group/group/modify",
                type: 'POST',
                data: {"id": id, "s_entname": entname, "s_personname": personname, "s_personcontact": contract},
                success: function (r) {
                    if (r.rep) {
                        $('#modal-modify-group').modal('hide')
                        ttable.api().ajax.reload()
                    } else {
                        showTip("保存失败");
                    }
                }
            })
        }else {
            showMsg("公司名称不能为空")
        }
    }

    function editPro() {
        $.ajax({
            url: "/front/group/edit",
            type: "POST",
            data: {"gid": ""},
            success: function (r) {

            }
        })
    }

</script>
