@extends('admin.master')

@section('top')
    <div class="top-breadcrumb pull-left">
        <h5>编辑角色</h5>
        <a class="btn btn-xs btn-default goback" onclick="javascript:history.go(-1);">
            <i class="iconfont icon-back-copy"></i><span>返回</span>
        </a>
    </div>
@endsection

@section('content')
{!! Form::model($role, ['route' => ['admin.role.update', $role->id], 'method'=>'put', 'class'=>'form-horizontal form-detail']) !!}
<div class="tab-content">
	<div role="tabpanel" class="tab-pane active" id="home">
		    <div class="form-group form-item-required">
            <label class="col-sm-2 control-label label-width mt-5" style="width: 90px;">名称：</label>
            <div class="col-sm-5">
                {!! Form::text('name', null, ['class'=>'form-control']) !!}
            </div>
        </div>
        <div class="form-group form-item-required">
            <label class="col-sm-2 control-label label-width mt-5" style="width: 90px;">别名：</label>
            <div class="col-sm-5">
              @if(auth()->user()->isMaster() || $role->slug != 'admin')
                {!! Form::text('slug', null, ['class'=>'form-control']) !!}
              @else
                <input class="form-control" name="slug" type="text" value="{{ $role->slug }}" readonly>
              @endif
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label label-width mt-5" style="width: 90px;">描述：</label>
            <div class="col-sm-5">
                {!! Form::text('description', null, ['class'=>'form-control']) !!}
            </div>
        </div>
        @if(!$menus->isEmpty())
            @foreach ($menus as $value)
            <table class="table table-hover menu-content">
                <thead>
                    <tr>
                        <th width="60" class="text-center">
                            <div class="pretty p-default p-smooth">
                                <input name="menu_ids[]" type="checkbox" class="menu-cate" value="{{ $value['id'] }}"
                                @if($role->menu_ids)
                                    @if(in_array($value->id, $role->menu_ids->toArray()))
                                        checked
                                    @endif
                                @endif
                                >
                                <div class="state p-success"><label></label></div>
                            </div>
                        </th>
                        <th>{{ $value->name }}</th>
                    </tr>
                </thead>
                <tbody>
                    @if(!$value->children->isEmpty())
                        @foreach ($value->children as $val)
                        <tr>
                            <td align="center">
                                <div class="pretty p-default p-smooth">
                                    <input class="menu-box two-box parent{{ $val['id'] }}" name="menu_ids[]" type="checkbox" value="{{ $val['id'] }}" data-cls=".two-box{{ $val['id'] }}"
                                    @if($role->menu_ids)
                                        @if(in_array($val->id, $role->menu_ids->toArray()))
                                            checked
                                        @endif
                                    @endif
                                    >
                                    <div class="state p-success"><label></label></div>
                                </div>
                            </td>
                            <td>&nbsp;&nbsp;&nbsp;&nbsp;∟ {{ $val->name }}</td>
                        </tr>
                            @if(!$val->children->isEmpty())
                                @foreach ($val->children as $v)
                                <tr>
                                    <td align="center">
                                        <div class="pretty p-default p-smooth">
                                            <input class="menu-box three-box two-box{{ $val['id'] }}" name="menu_ids[]" data-cls=".two-box{{ $val['id'] }}" data-parent=".parent{{ $val['id'] }}" type="checkbox" value="{{ $v['id'] }}"
                                            @if($role->menu_ids)
                                                @if(in_array($v->id, $role->menu_ids->toArray()))
                                                    checked
                                                @endif
                                            @endif
                                            >
                                            <div class="state p-success"><label></label></div>
                                        </div>
                                    </td>
                                    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;∟ {{ $v->name }}</td>
                                </tr>
                                @endforeach
                            @endif
                        @endforeach
                    @endif
                </tbody>
            </table>
            @endforeach
            <tr>
                <td class="text-left" colspan="9">
                    <div class="ml-10 pl-5">
                        {!! Form::submit('提交', ['class'=>'btn btn-primary']) !!}
                    </div>
                </td>
            </tr>
        @endif
    </div>
</div>
{!! Form::close() !!}
@endsection

@section('foot')
<script>
$(function(){
    var firstChecked = function(that) {
        var firstAll = that.parents('.menu-content').find('.menu-box');
        var num = 0;
        for (var i = 0; i < firstAll.length; i++) {
            if (!firstAll[i].checked) {
                num += 1;
            }
        }
        var isTrue = num == firstAll.length ? false : true;
        that.parents('.menu-content').find('.menu-cate')[0].checked = isTrue;
    }
    
    $('.menu-cate').on('click', function() {
        var all = $(this).parents('.menu-content').find('.menu-box');
        for (var i = 0; i < all.length; i++) {
            all[i].checked = $(this)[0].checked;
        }
    });

    $('.two-box').on('click', function() {
        var all = $(this).parents('.menu-content').find($(this).data('cls'));
        for (var i = 0; i < all.length; i++) {
            all[i].checked = $(this)[0].checked;
        }
        firstChecked($(this));
    });

    $('.three-box').on('click', function() {
        var all = $(this).parents('.menu-content').find($(this).data('cls'));
        var num = 0;
        for (var i = 0; i < all.length; i++) {
            if (!all[i].checked) {
                num += 1;
            }
        }
        var isTrue = num == all.length ? false : true;
        $(this).parents('.menu-content').find($(this).data('parent'))[0].checked = isTrue;
        firstChecked($(this));
    });
})
</script>
@endsection