@extends('layouts.master')

@section('content')
<!-- Main content -->
<section class="content">
    <!-- Main row -->
    <div class="row">
        <div class="col-md-12">
            <section class="panel tasks-widget">
                <header class="panel-heading" style="height:40px;">
                    <div class="pull-left">
                        {{$title}}
                    </div>
                    <div>
                        <button class="btn btn-success btn-xs pull-right" data-toggle="modal" data-target=".modal">Add user group</button>
                    </div>
                </header>
                <div class="table-responsive project-list">

                @if(Session::has('msg'))
                    {{Session::get('msg')}}
                @endif
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th colspan="2">
                                    Group Name
                                </th>
                                <th>
                                    User
                                </th>
                                <th>
                                    Designation
                                </th>
                                <th>
                                    Action
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                        @if (session('data'))
                            <div class="alert alert-success">
                                {{ session('data') }}
                            </div>
                        @endif
                        @if(!empty($all))
                            <?php $count=1; $count_color=1; ?>
                            @foreach($all as $info)
                                <?php 
                                    $chk = App\Models\Group::with('one_rlation')->where('depend_on',$info->id)->get();

                                    $count_color++;

                                    if($count_color%2==0){
                                        $sty = "#118314";
                                    }
                                    else{
                                        $sty = "#0861BE";
                                    }
                                ?>

                                @if($chk->count() > 0)
                                    @foreach($chk as $val)
                                    <tr>
                                        <td>
                                            {{$count++}}
                                        </td>
                                        <td style="color:<?php echo $sty; ?>">
                                            {{$info->group_name}}
                                        </td>
                                        <td>
                                            {{$val->one_rlation->name}}
                                        </td>
                                        <td>
                                            {{$val->designation}}
                                        </td>
                                        <td>
                                            <a href="" style="color: #7dcc93;">
                                                <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
                                            </a>
                                            &nbsp;  &nbsp;
                                            <a onclick="return confirm ('want to delete?')" href="{{url("admin/remove_group_user/$val->id")}}" style="color:#FD4B39;">
                                                <i class="fa fa-times" aria-hidden="true"></i>
                                            </a>
                                        </td>
                                    </tr> 
                                    @endforeach 
                                @else
                                <tr>
                                    <td>
                                        {{$count++}}
                                    </td>
                                    <td style="color:<?php echo $sty;?>">
                                        {{$info->group_name}}
                                    </td>
                                    <td>
                                        
                                    </td>
                                    <td>
                                        
                                    </td>
                                    <td>
                                        <a href="" style="color: #7dcc93;">
                                            <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
                                        </a>
                                        &nbsp;  &nbsp;
                                        <a onclick="return confirm ('want to delete?')" href="{{url("admin/remove_group_user/$info->id")}}" style="color:#FD4B39;">
                                            <i class="fa fa-times" aria-hidden="true"></i>
                                        </a>
                                    </td>
                                </tr> 
                                @endif 
                            @endforeach
                        @endif   
                            
                        </tbody>
                    </table>
                    <hr>
                    </hr>
                </div>
                <div class="panel-body">
                    <div class=" add-task-row">
                    </div>
                    <div class="table-foot">
                        <ul class="pagination pagination-sm no-margin ">
                            <li>
                                <a href="#">
                                    «
                                </a>
                            </li>
                            <li>
                                <a href="#">
                                    1
                                </a>
                            </li>
                            <li>
                                <a href="#">
                                    2
                                </a>
                            </li>
                            <li>
                                <a href="#">
                                    3
                                </a>
                            </li>
                            <li>
                                <a href="#">
                                    »
                                </a>
                            </li>
                        </ul>
                    </div>
                </div>
            </section>
        </div>
    </div>
</section>
<!-- row end -->
<!-- /.content -->


<!-- Modal Add User -->
<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="gridSystemModalLabel">
  <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>
        <h4 class="modal-title" id="gridSystemModalLabel">Add Group/User Under Group</h4>
      </div>
      <div class="modal-body">

        <form action="{{url('admin/add_group_user')}}" method="POST">

            {{ csrf_field() }}
            <div class="form-group">
                <label for="exampleInputEmail1">What you want to insert</label>
                <select name="type" class="form-control s-type" name="">
                    <option value="0">Select type</option>
                    <option value="1">Group</option>
                    <option value="2">User Under Group</option>
                </select>
            </div>
            <div class="form-group in-group">
                <label for="exampleInputEmail1">Group name</label>
                <input type="text" name="group_name" class="form-control" id="" placeholder="Group name" >
            </div>
            <div class="form-group in-g-user">
                <label for="exampleInputEmail1">Select Group</label>
                <select class="form-control" name="group_id">
                    <option value="0">Select Group</option>
                    @foreach($all_group as $ag)
                        <option value="{{$ag->id}}">{{$ag->group_name}}</option>
                    @endforeach
                </select>
            </div>
            <div class="form-group in-g-user">
                <label for="exampleInputEmail1">Select User</label>
                <select class="form-control" name="user_id">
                    <option value="0">Select User</option>
                    @foreach($all_user as $au)
                        <option value="{{$au->id}}">{{$au->name}}</option>
                    @endforeach
                </select>
            </div>
            <div class="form-group in-group in-g-user">
                <label for="exampleInputEmail1">Designation</label>
                <input type="text" name="designation" class="form-control" id="" placeholder="Designation" >
            </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Submit</button>
      </div>
      </form>  
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
@endsection


@section('script')
<script type="text/javascript">
    
    jQuery(document).ready(function($)
    {
        $('.in-group').hide();
        $('.in-g-user').hide();

        $('.chk-active').click(function(event)
        {
            var id = $(this).val();
            var stat;
            
            if($(this).is(':checked'))
            {
                stat = 1;
            }
            else
            {
                stat = 0;
            }

            $.ajax({
                url: '{{url('admin/user_active')}}',
                type: 'POST',
                data: {id: id,stat: stat},
            })
            .done(function(data){
                alert(data);
            });
            
        });

        $('.s-type').change(function(event){
            
            var val = $(this).val();

            if(val == 1){
                $('.in-group').show();
                $('.in-g-user').hide();
            }
            else if(val == 2){
                $('.in-group').hide();
                $('.in-g-user').show();
            }
            else{
                $('.in-group').hide();
                $('.in-g-user').hide();
            }
        });
    });
</script>
@endsection
