<div class="content-wrapper">
    <?php
    $form = $this->form;
    $form->prepare();

    $form->get('adminGroupName')->setAttributes([
        'placeholder' => $this->translate('管理员组名称')
    ]);

    ?>
    <?php echo $this->form()->openTag($form); ?>
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right">
            <a href="<?php echo $this->url('admin-group', ['action'=> 'adminGroupList']);?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回管理员组列表'); ?></a>
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('保存管理员组'); ?></button>
            <?php echo (isset($group) ? $this->HelpUrl('adminGroupEdit') : $this->HelpUrl('adminGroupAdd')); ?>
        </h1>
    </section>

    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><?php echo ((isset($group) && $group->getAdminGroupId()) ? $this->translate('编辑管理员组') : $this->translate('添加管理员组')); ?></h3>
                    </div>

                    <div class="box-body">
                        <?php echo $this->formElementErrors($form->get('dberp_csrf'), ['class'=>'error-message']); ?>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('管理员组'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-6">
                                <?php echo $this->formElement($form->get('adminGroupName')); ?>
                                <?php echo $this->formElementErrors($form->get('adminGroupName'), ['class'=>'error-message']); ?>
                            </div>
                        </div>

                        <?php if(!isset($this->group) || (isset($this->group) && $this->group->getAdminGroupId() != 1)) { ?>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('权限设置'); ?></label>
                            <div class="col-sm-6">
                                <label class="checkbox-inline">
                                    <input type="checkbox" name="allSelect" id="allSelect" value="1" class="minimal"><?php echo $this->translate('全选'); ?>
                                </label>
                            </div>
                        </div>

                        <?php
                        if($this->group && !empty($this->group->getAdminGroupPurview())) $groupPermission = explode(',', $this->group->getAdminGroupPurview());
                        foreach ($this->permissionArray as $key => $permission) { ?>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><?php echo $this->translate($permission['name']); ?></label>
                                <div class="col-sm-6">
                                    <label class="checkbox-inline">
                                        <input type="checkbox" value="1" id="<?php echo $key?>" class="minimal erp-group"><?php echo $this->translate($permission['name']); ?><?php echo $this->translate('全选'); ?>
                                    </label>
                                </div>
                            </div>
                            <?php foreach ($permission['controllers'] as $controllerKey => $controllerValue) {
                                $controllerStr = str_replace('\\', '_', $controllerKey);
                                ?>
                            <div class="form-group">
                                <div class="col-sm-2 control-label"><?php echo $this->translate($controllerValue['name']); ?> <input type="checkbox" value="1" class="minimal <?php echo $key?> erp-group-one" id="<?php echo $controllerStr?>"></div>
                                <div class="col-sm-10">
                                    <?php foreach ($controllerValue['action'] as $value) {
                                        $pName = $controllerStr . '_' . $value;
                                        ?>
                                        <label class="checkbox-inline">
                                            <input type="checkbox" name="adminGroupPermission[]" class="minimal <?php echo $key?> <?php echo $controllerStr?>" <?php echo (isset($groupPermission) && in_array($pName, $groupPermission)) ? 'checked' : ''?> value="<?php echo $pName?>"><?php echo $this->translate($controllerValue['actionNames'][$value]); ?>
                                        </label>
                                    <?php } ?>
                                </div>
                            </div>
                            <?php } ?>
                        <?php } ?>
                        <?php } ?>
                    </div>

                    <div>
                        <div class="box-footer">
                            <?php echo $this->formElement($form->get('dberp_csrf')); ?>
                            <button type="submit" class="btn btn-primary col-md-offset-2"><i class="fa fa-check"></i> <?php echo $this->translate('保存管理员组'); ?></button>
                        </div>
                    </div>

                </div>
            </div>
        </div>

    </section>
<?php echo $this->form()->closeTag(); ?>

</div>
<script type="text/javascript">
    $(document).ready(function() {
        $("#<?php echo $form->getName(); ?>").validate({
            rules: {
                adminGroupName: {
                    required: true
                }
            },
            messages: {
                adminGroupName: {
                    required: "<?php echo $this->translate('管理员组不能为空！'); ?>"
                }
            }
        });
    });

    $("#allSelect").click(function () {
        if(!this.checked) {
            $("input[name='adminGroupPermission[]']").prop("checked", false);
            $(".erp-group,.erp-group-one").prop("checked", false);
        } else {
            $("input[name='adminGroupPermission[]']").prop("checked", true);
            $(".erp-group,.erp-group-one").prop("checked", true);
        }
    });

    $(".erp-group").click(function () {
        if(!this.checked) {
            $("."+this.id).prop("checked", false);
        } else {
            $("."+this.id).prop("checked", true);
        }
    });

    $(".erp-group-one").click(function () {
        if(!this.checked) {
            $("."+this.id).prop("checked", false);
        } else {
            $("."+this.id).prop("checked", true);
        }
    });
</script>
