@extends('layouts.dashboard')

@section('content')
    <section class="container all-container">
        <div class="header-titulo">
            Aplicacion
        </div>
        <nav>
            <div class="nav nav-tabs" id="nav-tab" role="tablist">
                <a class="nav-item nav-link active" id="nav-home-tab" data-toggle="tab" href="#nav-home" role="tab" aria-controls="nav-home" aria-selected="true">Aplicacion</a>
                <a class="nav-item nav-link" id="nav-profile-tab" data-toggle="tab" href="#nav-profile" role="tab" aria-controls="nav-profile" aria-selected="false">Usuarios de la aplicacion</a>
            </div>
        </nav>
        <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-agregar-user mx-auto" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <div class="title-buscador-container">
                            <div class="tabla-buscador">
                                <div class="barra-container">
                                    <select class="form-control pagination-option" url="{{URL::to('NotAppUser')}}" id="filtroUsuarios" onchange="filtrarUsuarios(this.value,{{$aplicacion->id}})">
                                        <option value="cliente">Clientes</option>
                                        <option value="desarrollador">Desarrolladores</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <button type="button" class="btn btn-light" id="save-user">
                            <i class="fas fa-save"></i>
                        </button>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form action="{{ route('asignarUsuario',[$aplicacion->id]) }}" method="post"  id="form-datos">
                            {{ csrf_field() }}
                            <div class="container">
                                <div class="row" id="users-container">
                                    @if ( $usersByRole == '[]')
                                    <small>No hay mas usuarios disponibles para esta aplicacion</small>
                                    @endif
                                    @foreach($usersByRole as $userByRole)
                                        <div class="col-md-3 list-group-item list-group-item-action flex-column pointer user-select">
                                            <div class="w-100">
                                                <h5 class="mb-1 size-1m">{{$userByRole->name}} .{{substr(ucfirst($userByRole->surname), 0, 1)}}</h5>
                                                <small class="text-muted user-card">{{$userByRole->email}}</small>
                                                <input class="envio" type="hidden" name="user" value="{{$userByRole->id}}">
                                            </div>
                                        </div>
                                    @endforeach
                                </div>
                                    <input type="hidden" id="asignarUsuario" value="{{ route('asignarUsuario')}}">
                                    <input type="hidden" id="token" value="{{ csrf_token() }}" >
                                    <input id="app_name" class="envio" type="hidden" name="app" value="{{$aplicacion->id}}">
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <!-- <nav aria-label="Page navigation example">
                            <ul class="pagination paginador">
                                <li class="page-item"><a class="page-link" href="#">Previous</a></li>
                                <li class="page-item"><a class="page-link" href="#">1</a></li>
                                <li class="page-item"><a class="page-link" href="#">2</a></li>
                                <li class="page-item"><a class="page-link" href="#">3</a></li>
                                <li class="page-item"><a class="page-link" href="#">Next</a></li>
                            </ul>
                        </nav> -->
                    </div>
                </div>
            </div>
        </div>
        <div class="tab-content" id="nav-tabContent">
            <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
                <div class="card mb-3">
                    <div class="card-header titulo-eliminar">
                        <div class="title">
                        </div>
                        <div class="eliminar-container">
                            <div class="btn-group mr-2">
                                <button class="btn btn-sm btn-outline-secondary" onclick="editar()" data-toggle="tooltip" data-placement="bottom" title="Editar"><i class="fas fa-edit"></i></button>
                                <button class="btn btn-sm btn-outline-secondary" onclick="eliminarApp('{{ route("eliminar-aplicacion",[$aplicacion->id]) }}','{{ route('aplicaciones') }}')" data-toggle="tooltip" data-placement="bottom" title="Eliminar"><i class="far fa-trash-alt" ></i></button>
                            </div>
                        </div>
                    </div>
                    <div class="card-body padding-10">
                        <form action="{{ route('actualizarAplicacion',[$aplicacion->id]) }}" method="post"  id="form-datos">
                            {{ csrf_field() }}
                            @if ($errors->get('estado'))
                                <div class="alert alert-danger text-center">
                                    Error!.<br> Asegurese de llenar los datos correctamente
                                </div>
                            @endif
                            <div class="form-group row">
                                <label for="inputEmail3" class="col-sm-2 col-form-label">Nombre</label>
                                <div class="col-sm-10">
                                    <input type="text" id="nombre" value="{{$aplicacion->nombre}}" name="nombre"  class="form-control" placeholder="Nombre" disabled required>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label for="inputPassword3" class="col-sm-2 col-form-label">Codigo</label>
                                <div class="col-sm-10">
                                <input type="text" id="codigo" value="{{$aplicacion->codigo}}" name="codigo"  class="form-control" placeholder="codigo" disabled required>
                                </div>
                            </div>
                            <div class="form-group row">
                                <div class="col-sm-2">Tipo</div>
                                <div class="col-sm-10 col-md-2">
                                    <span class="form-control span-edit-show">{{$aplicacion->tipo}}</span>
                                    <select class="form-control span-edit-hidden" name="tipo" disabled required>
                                        <option value="proyecto">Proyecto</option>
                                        <option value="sistema">Sistema</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group row">
                                <div class="col-sm-2">Estado</div>
                                <div class="col-sm-10 col-md-2">
                                    <span class="form-control span-edit-show">{{$aplicacion->estado}}</span>
                                    <select class="form-control span-edit-hidden" name="estado" disabled required>
                                        <option value="activo">Activo</option>
                                        <option value="inactivo">Inactivo</option>
                                    </select>
                                </div>
                            </div>
                            <fieldset class="form-group">
                                <div class="row">
                                    <legend class="col-form-label col-sm-2 pt-0">Descripcion</legend>
                                    <div class="col-sm-10">
                                    <textarea  style="resize: none;" rows="5" id="textarea" name="descripcion" class="form-control" disabled required>{{$aplicacion->descripcion}}</textarea>
                                    </div>
                                </div>
                            </fieldset>
                            <div class="form-group buttons-edit-container row">
                                <div class="col-sm-10">
                                    <button type="button" onclick="editar()" id="button-editar" class="btn btn-dark">Cancelar</button>
                                    <button type="submit" class="btn btn-light" id="button-guardar">Guardar</button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="card-footer small text-muted">
                        Updated {{$aplicacion->updated_at}}
                    </div>
                </div>
                
                <div class="card mb-3">
                    <div class="card-header">
                    <i class="fas fa-ruler"></i> Modulos de la aplicacion
                    </div>
                    <div class="card-body">
                        <div class="container">
                            <div class="row">
                                @if ( $modulos == '[]')
                                <small>Esta aplicacion no tiene modulos</small>
                                @endif
                                @foreach($modulos as $modulo)
                                    <div class="col-md-4 list-group-item list-group-item-action flex-column pointer">
                                        <i onclick="destroyModule('{{ route('eliminarModulo',[$modulo->id]) }}')" class="fas fa-backspace float-right deleteIcon"></i>
                                        <div onclick="window.location='{{ route("modulo",[$modulo->id]) }}'" class="d-flex w-100 justify-content-between">
                                            <h5 class="mb-1">{{$modulo->nombre}}</h5>
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">
            
                <div class="card mb-3">
                    <div class="card-header titulo-eliminar">
                        <div class="title">
                        </div>
                        <div class="eliminar-container">
                            <button class="btn btn-sm btn-outline-secondary" data-toggle="modal" data-target="#exampleModal" title="Agregar usuario"><i class="fas fa-user-plus"></i></button>
                            
                        </div>
                    </div>
                    <div class="card-body">
                        <legend class="col-form-label col-sm-4 pt-0"><h5> Clientes: </h5></legend>
                        <div class="container">
                            <div class="row">
                                @if ( $clientes == '[]')
                                <small class="text-muted">Esta aplicacion no tiene clientes</small>
                                @endif
                                @foreach($clientes as $cliente)
                                    <div class="col-md-4 list-group-item list-group-item-action flex-column pointer">
                                        <i onclick="EliminarUsuarioApp('{{ route('EliminarUsuarioApp',[$cliente->id]) }}')" class="fas fa-backspace float-right deleteIcon"></i>
                                        <div onclick="window.location='{{ route("user",[$cliente->user_id]) }}'" class="d-flex w-100 justify-content-between">
                                            <h5 class="mb-1 size-1m">{{$cliente->name}} .{{substr(ucfirst($cliente->surname), 0, 1)}}</h5>
                                            <small class="text-muted user-card">{{$cliente->email}}</small>
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                        </div><hr>
                        <legend class="col-form-label col-sm-4 pt-0 mt-4"><h5> Desarrolladores: </h5></legend>
                        <div class="container">
                            <div class="row">
                                @if ( $desarrolladores == '[]')
                                <small class="text-muted">Esta aplicacion no tiene desarrolladores</small>
                                @endif
                                @foreach($desarrolladores as $desarrollador)
                                    <div class="col-md-4 list-group-item list-group-item-action flex-column pointer">
                                        <i onclick="EliminarUsuarioApp('{{ route('EliminarUsuarioApp',[$desarrollador->id]) }}')" class="fas fa-backspace float-right deleteIcon"></i>
                                        <div onclick="window.location='{{ route("user",[$desarrollador->user_id]) }}'" class="d-flex w-100 justify-content-between">
                                            <h5 class="mb-1 size-1m">{{$desarrollador->name}} .{{substr(ucfirst($desarrollador->surname), 0, 1)}}</h5>
                                            <small class="text-muted user-card">{{$desarrollador->email}}</small>
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <style>        
        #mainNav.navbar-dark .navbar-collapse .navbar-sidenav>.nav-item>.aplicacionesLink {color:#c1d61e;}
    </style>
	<script src="{{ asset('js/sweetalert.min.js') }}"></script>
	<link  media="all" rel="stylesheet" type="text/css" href="{{ asset('css/general.css') }}">
	<script src="{{ asset('js/aplicacion.js') }}"></script>
@endsection