@extends('Public.index')
@section('admin')
<!-- 新增 -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body">
                <form class="form-horizontal">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-3 control-label"><span style="color:red">*</span> 账号</label>
                            <div class="col-sm-12">
                            <input type="text" class="form-control username" placeholder="账号">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputPassword3" class="col-sm-3 control-label"><span style="color:red">*</span> 密码</label>
                            <div class="col-sm-12">
                            <input type="password" class="form-control password">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="name" class="col-sm-2 control-label"><span style="color:red">*</span> 角色</label>
                                <div class="col-sm-12">
                                    <select class="form-control" id="rule">
                                        @foreach($rule as $key => $val)
                                        <option value="{{ $val -> id }}"> {{ $val -> rule_name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                        </div>
                </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary" onclick="create()">添加</button>
      </div>
    </div>
  </div>
</div>
<!-- 修改 -->
<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModal2Label">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body">
                <form class="form-horizontal">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-3 control-label"><span style="color:red">*</span> 当前角色：<span id="now_rule">**</span> </label>
                        </div>
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-3 control-label"><span style="color:red">*</span> 账号</label>
                            <div class="col-sm-12">
                            <input type="hidden" class="id2" value="">
                            <input type="text" class="form-control username2" placeholder="账户名">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputPassword3" class="col-sm-3 control-label"><span style="color:red">*</span> 密码</label>
                            <div class="col-sm-12">
                            <input type="password" class="form-control password2" placeholder="密码不改留空">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="name" class="col-sm-6 control-label"><span style="color:red">*</span> 更改角色</label>
                                <div class="col-sm-12">
                                    <select class="form-control" id="rule2">
                                        @foreach($rule as $key => $val)
                                        <option value="{{ $val -> id }}"> {{ $val -> rule_name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                        </div>
                </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary" onclick="update()">修改</button>
      </div>
    </div>
  </div>
</div>
<!-- 莫泰结束 -->

<div class="content-wrapper" style="min-height: 810px;">
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h4>账号列表</h4>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="/admin/index"> 首页</a></li>
              <li class="breadcrumb-item active"> 权限管理</li>
              <li class="breadcrumb-item active"> 账号列表</li>
            </ol>
          </div>
        </div>
      </div>
    </section>
    <div class="col-xs-3">
        <button class="btn btn-primary add" data-toggle="modal" data-target="#myModal"  style="margin-left:17px;margin-bottom:15px"> 添加账号</button>
    </div>

    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-12">

            <div class="card">
              <div class="card-body p-0">
                <table class="table table-condensed">
                  <tbody>
               
                  <tr style="text-align:center">
                    <th>账号</th>
                    <th>角色</th>
                    <th>状态</th>
                    <th style="width: 100px">操作</th>
                  </tr>
                  @foreach($admin as $key => $val)
                  <tr style="text-align:center">
                    <input type="hidden" value="{{ $val -> id }}">
                    <td>{{ $val -> username }}</td>
                    <td>{{ DB::table('rule') -> where('id',$val -> rule) -> value('rule_name') }}</td>
                    <td>
                        @if($val -> status == 1)
                        <a href="javascript:void(0)" onclick="status({{$val -> id}})"><span class="badge bg-success">正常</span></a>
                        @else
                        <a href="javascript:void(0)" onclick="status({{$val -> id}})"><span class="badge bg-danger">冻结</span></a>
                        @endif
                    </td>
                    <td>
                        <a href="javascript:void(0)" onclick="edit(this)"><i class="fa fa-edit" style="font-size: 16px;" title="编辑"></i></a>
                        <a href="javascript:void(0)" onclick="del({{$val -> id}})"><i class="fa fa-trash" style="font-size: 16px;" title="删除"></i></a>
                    </td>
                  </tr>
                  @endforeach
                </tbody></table>
              </div>
            </div>
          </div>
        </div>
    </section>
  </div>
  <input type="hidden" id="userid">
<script src="/admin/plugins/jquery/jquery.min.js"></script>
<script src="/admin/layer-v3.1.1/layer/layer.js"></script>
<script>
    function create()
      {
          var username = $('.username').val();
          var password = $('.password').val();
          var rule = $('#rule').val();
          if(!username || !password || !rule) {
              layer.msg('参数不全');
              return false;
          }
          $.get('/admin/admin/create',{username:username,password:password,rule:rule},function(res){
              if(res.code == '1000'){
                  layer.msg(res.msg);
                  window.location.reload();
              }else{
                  layer.msg(res.msg);
              }
          })
      }
   function del(id)
    {
        if(!id) {
            layer.msg('参数不全');
            return false;
        }
        layer.confirm('确认删除该账号吗？删除后不可恢复', {
            btn: ['确认','取消'] //按钮
            }, function(){
                $.get('/admin/admin/delete',{id:id},function(res){
                    if(res.code == '1000'){
                        layer.msg(res.msg);
                        window.location.reload();
                    }else{
                        layer.msg(res.msg);
                    }
                })
        });
    }

    function status(id)
    {
        if(!id){
            layer.alert('参数错误');
            return false;
        }
        layer.confirm('确认操作吗？', {
            btn: ['确认','取消'] //按钮
            }, function(){
                $.get('/admin/admin/status',{id:id},function(res){
                    if(res.code == '1000'){
                        layer.msg(res.msg);
                        window.location.reload();
                    }else{
                        layer.msg(res.msg);
                    }
                })
        });  
    }

    function edit(obj)
    {
        var username = $(obj).parent().prev().prev().prev().html();
        var rule = $(obj).parent().prev().prev().html();
        var id = $(obj).parent().prev().prev().prev().prev().val()
        $('#userid').val(id);
        $('.username2').val(username);
        $('#now_rule').html(rule);
        $('#myModal2').modal();
    }
    function update()
    {
        var username = $('.username2').val();
        var password = $('.password2').val();
        var rule = $('#rule2').val();
        var id = $('#userid').val();
        $.get('/admin/admin/update',{id:id,username:username,password:password,rule:rule},function(res){
            if(res.code == '1000'){
                layer.msg(res.msg);
                window.location.reload();
            }else{
                layer.alert(res.msg);
            }
        }) 
    }

</script>
@endsection
@section('title','账号列表')
@section('access','menu-open')
@section('access2','active')
@section('access-list','active')