<?php include_once(mvc::$cfg['PATH_TPL'].'public/headBase.php');?>
<style>
    .layui-table td,
    .layui-table th{
        text-align: center;
    }
</style>
<div class="layui-tab-content" style="padding: 10px;width: 1035px;">
    <!-- <div style="margin-bottom: 5px;">
        <button class="layui-btn layui-btn-xs" onclick="checkAll()">全选/全不选</button>
    </div> -->
	<form  method="post" action="" id="saveMemberRule"  lay-filter="component-form-group">
        <table  class="layui-table">
            <thead>
                <tr>
                    <th style="width: 20%">功能权限</th>
                    <?php if(!empty($memberLevelArr)){ ?>
                        <?php foreach ($memberLevelArr as $key => $level) { ?>
                            <th>
                                <input type="checkbox" id="check<?php echo $key;?>" class="checkAll" data-melid="<?php echo $level['mel_id'];?>" onclick="checkAll(<?php echo $level['mel_id']; ?>,this)" />   
                                <label for="check<?php echo $key;?>"> <?php echo $level['mel_name'];?></label>
                            </th>
                        <?php } ?>
                    <?php } ?>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($memberRuleArr)) { ?>
                    <?php foreach ($memberRuleArr as $rk => $rule) { ?>
                        <input type="hidden" name="ruleArr[<?php echo $rule['smr_code'];?>][]" value="">
                        <tr> 
                            <td><?php echo $rule['smr_name'];?></td>
                            <?php foreach ($memberLevelArr as $lk => $level) { ?>
                                <td>
                                    <input type="checkbox" <?php if($oldRuleArr && !in_array($level['mel_id'],$oldRuleArr[$rule['smr_code']]?$oldRuleArr[$rule['smr_code']]:[])){  echo ""; }else{ echo "checked";} ?>  name="ruleArr[<?php echo $rule['smr_code'];?>][]"  value="<?php echo $level['mel_id'];?>"/>
                                </td>
                            <?php } ?>
                        </tr>
                    <?php } ?>
                <?php } ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="<?php echo count($memberLevelArr)+1;?>" style="text-align:center;">
                        <button type="button" onclick="saveMemberRule();return false;"   class="layui-btn layui-btn-xs">保存</button>     
                    </td>
                    
                </tr>
                
            </tfoot>
        </table>
    </form>
</div>
<?php include_once(mvc::$cfg['PATH_TPL'].'public/footBase.php');?>
<script>
    var ruleCount = <?php echo $ruleCount;?>;
    function saveMemberRule(){

        let json = $("#saveMemberRule").serialize();
        $.ajax({
            url:SELF_URL+'super/setMemberRule?isAjax=1',
            type: "post",
            dataType: "json",
            data:json,
            success:function(result){
                if(AJAX.checkData(result)){
                    layer.alert('保存成功',function(){
                        location.reload();
                    });  
                }
            },
            error:function(){

            }
        })
    }
  
    function checkAll(member_id,thisObj) {
        //先全选再取消全选
        var obj = $("input[value='"+member_id+"']");
        if($("input[value='"+member_id+"']:checked").length != obj.length){
            $(thisObj).prop('checked',true);
            obj.prop('checked',true);
        }else{
            $(thisObj).prop('checked',false);
            obj.prop('checked',false);
        }
    }
	//按钮的状态
    // $(".checkAll").each(function(){
    //     var member_id =  $(this).data('melid');
    //     if( $("input[value='"+member_id+"']:checked").length == ruleCount){
    //        $(this).prop("checked",true); 
    //     }else{
    //         $(this).prop("checked",false);  
    //     }
    // });
    
</script>
