{{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/group"><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-horizontal">
                            <div class="box-body">
                                <div class="form-group">
                                    <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="">批量开启</a>
                                    <a class="btn btn-sm btn-default margin" onclick="">批量关闭</a>
                                </div>
                            </div>
                        </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>
                                <th>操作</th>
                            </tr>
                            </thead>
                        </table>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
        </div>
    </section>
</div>
{{include "com/footer.html"}}

<div class="modal fade" id="modal-modify-user" 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="user-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="user-login" placeholder="用户账号" readonly="readonly">
                                </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="user-name" 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="user-contract" placeholder="电话/邮箱">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">登陆密码</label>
                                <div class="col-sm-5">
                                    <input type="password" id="user-pwd" readonly="readonly" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">服务时间</label>
                                <div class="col-sm-8">
                                    <input type="text" id="stratDate" class="col-sm-2 form-control" readonly="readonly" 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>

<script>
    menuActive("group");
    let gid = {{ .T.gid }}
    let selectIndex = [];               //选中的编号
    let keyTableChecked = false;        //关键词表格数据是否有选中
    let keyTableAllChecked = true;      //表单数据是否被全选中

    $(function () {
        ttable = $('#dataTable').dataTable({
            "paging": true,
            "lengthChange": false,
            "searching": false,
            "ordering": false,
            "info": true,
            "autoWidth": false,
            "serverSide": true,
            "ajax": {
                "url": "/front/group/user",
                "type": "post",
                "data": {"gid": gid}
            },
            "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_login", width: "9%"},
                {"data": "s_name", width: "9%"},
                {"data": "i_role", width:"9%", render: function (val) {
                        if (val === "1") {
                            return "管理员"
                        }else if (val === "2") {
                            return "质检员"
                        }else {
                            return "普通用户"
                        }
                    }},
                {"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: "10%" },
                {"data": "i_createtime",width:"9%", render: function (val) {
                        var dt = new Date()
                        dt.setTime(parseInt(val) * 1000);
                        return dt.format("yyyy-MM-dd")
                    }},
                {"data": "i_updatetime",width:"9%", render: function (val) {
                        var dt = new Date()
                        dt.setTime(parseInt(val) * 1000);
                        return dt.format("yyyy-MM-dd")
                    }},
                {"data": "_id", width:"11%",render: function (val, a, row, pos) {
                        tmp = '<div>' +
                            '<a class="btn btn-sm btn-primary" onclick="modifyInfo(\''+pos.row+'\')">设置</a>&nbsp;&nbsp;'+
                            '<a class="btn btn-sm btn-danger" onclick="del(\''+val+'\')">删除</a>'+
                            '</div>';
                        return tmp
                    }}
            ]
        });
    });

    function setupState(id, v) {
        console.log(id, v)
        showConfirm("确定修改当前状态？", function() {
            $.ajax({
                url: "/front/group/user/state",
                type: 'POST',
                data: {"id": id, "state": v},
                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($(this).parents("tr").index());
            })
        } 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($(this).parents("tr").index());
            }else {
                tag = false
            }
        });
        if (tag) {
            keyTableAllChecked = true;
            $('#allCheckbox').prop("checked", true);
        } else {
            keyTableAllChecked = false;
            $('#allCheckbox').prop("checked", false);
        }
    }

    function modifyInfo(v) {
        let tmp = ttable.fnGetData()[v]
        $("#user-id").val(tmp["_id"])
        $("#user-login").val(tmp["s_login"])
        $("#user-pwd").val(tmp["s_password"])
        $("#user-name").val(tmp["s_name"])
        $("#user-contract").val(tmp["s_phone"])
        $('#modal-modify-user').modal("show")

    }

    function modifySave() {
        let id = $("#user-id").val()
        let name = $("#user-name").val()
        let phone = $("#user-contract").val()
        if (name !== "" || phone !== "") {
            $.ajax({
                url: "/front/group/user/modify",
                type: 'POST',
                data: {"id": id, "s_name": name, "s_phone": phone},
                success: function (r) {
                    if (r.rep) {
                        $('#modal-modify-user').modal('hide')
                    } else {
                        showTip("保存失败");
                    }
                }
            })
        }
    }

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

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

</script>
