<div class="content_wrapper">
    <style>
        .btn-name {
            white-space: nowrap;
        }
    </style>
    <div class="container-fluid">
        <div id="msg-tip"></div>
        <aside style="display: none">
            <header><i class="fa fa-fw fa-file"></i>分组列表</header>
            <section>
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-sm-12">
                            <?php
                            foreach ($nameArr as $key => $vl) { ?>
                                <a style="margin:5px;" class="btn btn-primary" href="#config/userGroupList/<?php echo $key;?>" role="button"><?php echo $vl;?></a>
                            <?php } ?>
                        </div>
                    </div>
                </div>
            </section>
        </aside>
        <div class="row">
            <div class="col-sm-12">
                <aside>
                    <header><i class="fa fa-fw fa-file"></i>分组配置</header>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="btn-group" style="margin-bottom:20px;">
                                        <?php if($cid!=5){?>
                                        <a class="btn btn-success" href="/#config/userGroup2/<?php echo $cid;?>" role="button">新增<?php echo $name;?>分组</a>
                                        <?php }else{
                                            echo '<button class="btn btn-success" id="getSSGroup">拉取数数最新分组(不会自动拉取，数数添加分组后点击)</button>';
                                            echo '<button class="btn btn-success" id="changeUserGroup">手动更新用户所在分组(耗时可能较长)</button>';
                                            echo '<button class="btn btn-danger" id="hideUserGroup" onclick="showStatus(-1,0)">全部隐藏</button>';
                                        }?>
                                    </div>
                                    <div class="table-responsive">
                                        <table id="example" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                                            <thead>
                                            <tr>
                                                <th>分组ID</th>
                                                <th>分组名</th>
                                                <th>分组备注</th>
                                                <th>分组排序</th>
                                                <th>更新方式</th>
                                                <th>现有人数</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php
                                            if(is_array($list)  && !empty($list)){
                                                foreach ($list as $key => $vl) { ?>
                                                    <tr>
                                                        <td><?php echo $vl['groupId'];?></td>
                                                        <td><?php echo $vl['name'];?></td>
                                                        <td><textarea readonly="readonly" class="form-control show" rows="5"><?php echo $vl['remark'];?></textarea></td>
                                                        <td><?php echo $vl['sort']; ?></td>
                                                        <td><?php if($vl['update'] == 1){ echo '手动更新'; }else{ echo '自动更新'; } ;?></td>
                                                        <td><?php echo $vl['count']; ?></td>
                                                        <td>
                                                            <?php if($cid!=5){?>
                                                            <div>
                                                                <a href="#config/userGroup2/<?php echo $vl['cid'];?>/<?php echo $vl['id'];?>" class="btn btn-primary active" role="button">编辑</a>
                                                                <button class="btn btn-danger delete" onclick="del(<?php echo $vl['id'];?>)" role="button">删除</button>
                                                            </div>
                                                            <?php if($cid == 4){ ?>
                                                                <div style="margin-top: 1rem">
                                                                    <?php if(empty($vl['signIn'])){ ?>
                                                                        <a href="#config/userGroupSignIn/<?php echo $vl['id'];?>" class="btn btn-success" role="button">新增签到配置</a>
                                                                    <?php }else{ ?>
                                                                        <a href="#config/userGroupSignIn/<?php echo $vl['id'];?>" class="btn btn-success" role="button">编辑签到配置</a>
                                                                    <?php } ?>
                                                                </div>
                                                                <?php if(!empty($vl['signIn'])){ ?>
                                                                    <div style="margin-top: 1rem">
                                                                        <button class="btn btn-danger delete" onclick="delSignIn(<?php echo $vl['id'];?>)" role="button">删除签到配置</button>
                                                                    </div>
                                                                <?php } ?>
                                                            <?php } ?>
                                                            <div style="margin-top: 1rem">
                                                                <a href="#statistics/userGroupSta/<?php echo $vl['id'];?>" class="btn btn-success" role="button">分组统计数据</a>
                                                            </div>
                                                            <?php }else{ ?>
                                                                <div style="margin-top: 1rem">
                                                                    <button  class="btn btn-<?php echo $vl['showStatus']==1?"danger":"success";?>  btn-<?=$vl['id']?>" data-status="<?php echo $vl['showStatus']==1?0:1;?>" onclick="showStatus(<?php echo $vl['id'];?>,<?php echo $vl['showStatus']==1?0:1;?>)" role="button"><?php echo $vl['showStatus']==1?"隐藏":"显示";?></button>
                                                                </div>
                                                                <div style="margin-top: 1rem">
                                                                    <input data-id="<?=$vl['groupId']?>" class="updateStatusSwitch" <?php echo $vl['updateStatus'] == 1 ? 'checked' : '';?>  type="checkbox"  data-on-text="更新" data-off-text="不更新"/>
                                                                </div>
                                                            <?php } ?>
                                                        </td>
                                                    </tr>
                                                <?php }
                                            } else{?>
                                                <tr>
                                                    <td colspan="10">暂无数据</td>
                                                </tr>
                                            <?php } ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
    </div>

</div>

<script>
    function del(id) {
        var msg = "您真的确定要删除吗？\n\n请确认！";
        if (confirm(msg)==true){
            $.post(rootPath + 'config/delUserGroup',{
                id:id,
            },function (data) {
                window.location.reload();
            });
        }
    }

    function delSignIn(id) {
        var msg = "您真的确定要删除该分组的签到配置吗？\n\n请确认！";
        if (confirm(msg)==true){
            $.post(rootPath + 'config/delUserGroupSignIn',{
                id:id,
            },function (data) {
                window.location.reload();
            });
        }
    }

    $("#getSSGroup").click(function () {
        $.post(rootPath + 'config/getSSGroupData',{},function (data) {
            console.log(data);
            window.location.reload();
        });
    })

    //显示隐藏分组
    function showStatus(id, status){
        var status = $('.btn-'+id).data('status')
        console.log(status)
        $.post(rootPath + 'config/userGroupShow/'+id+'/'+status,{},function (data) {
            console.log(data);
            if(data.data.status == 1){
                $('.btn-'+id).addClass('btn-danger')
                $('.btn-'+id).removeClass('btn-success')
                $('.btn-'+id).text('隐藏')
                $('.btn-'+id).data('status', 0)
            }else{
                $('.btn-'+id).addClass('btn-success')
                $('.btn-'+id).removeClass('btn-danger')
                $('.btn-'+id).text('显示')
                $('.btn-'+id).data('status', 1)
            }
            // window.location.reload();
        },"json");
    }


    $('.updateStatusSwitch').bootstrapSwitch({
    });

    $('.updateStatusSwitch').on('switchChange.bootstrapSwitch', function (event,state) {
        var status = state ? 1 : 0;
        updateStatus(event.currentTarget.dataset.id, status)
    });
    function updateStatus(id, status){
        showLoading()
        $.post(rootPath + 'config/userGroupUpdate/'+id+'/'+status,{},function (data) {
            hideLoading()
            console.log(data);
            // window.location.reload();
        },"json");
    }

</script>