@extends('Dashboard.layout')
@section('conteudo')


<div class="pcoded-content">
    <!-- [ breadcrumb ] start -->
    <div class="page-header">
        <div class="page-block">
            <div class="row align-items-center">
                <div class="col-md-12">
                    <div class="page-header-title">
                        <h5 class="m-b-10">Transportes</h5>
                    </div>
                    <ul class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index.html"><i class="feather icon-home"></i></a></li>
                        <li class="breadcrumb-item"><a href="#!">Transportes</a></li>
                        <li class="breadcrumb-item"><a href="#!">Listagem</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <div class="row">

        <div class="col-xl-12">

            @if($errors->any())

                <div class="sufee-alert alert with-close alert-danger alert-dismissible fade show">
                    <span class="badge badge-pill badge-danger">Erro</span>
                    <ul class="errors">
                        @foreach($errors->all() as $erro)
                            <li> {{ $erro }}</li>
                        @endforeach
                    </ul>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
        @endif

            @if(session()->has('sms'))
                <div class="sufee-alert alert with-close alert-success alert-dismissible fade show">
                    <span class="badge badge-pill badge-success">Successo</span>
                    {{ session()->get('sms') }}
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            @endif


            <div class="card">
                <div class="card-header">
                    <h5>Listagem</h5>
               </div>
                <div class="card-body table-border-style">
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Nome da Turma</th>
                                    <th>Ano Lectivo</th>
                                    <th>Cuidadora</th>
                                    <th>Auxiliar</th>
                                    <th>Capacidade</th>
                                    <th>Acção</th>

                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($transporte as $trans)

                                    <tr>
                                        <td>{{$trans->marca}} </td>
                                        <td>{{$trans->modelo}}</td>
                                        <td>{{$trans->matricula}}</td>
                                        <td>{{$trans->capacidade}}</td>
                                        <td>{{$trans->cor}}</td>


                                        <td>



                                            @can('editar_transporte')
                                                <a href="" data-toggle="modal" data-target="#editar_turma"

                                                onclick="editar_transporte({{$trans->id}}, '{{$trans->marca}}', '{{$trans->modelo}}', '{{$trans->capacidade}}', '{{$trans->matricula}}', '{{$trans->cor}}')"
                                                >
                                                    <i class="fa-solid fa-pen-to-square" style="color: blue;"></i>
                                                </a>
                                            @endcan

                                            @can('eliminar_transporte')
                                                <a href="" onclick="retornaid_nome({{$trans->id}}, '{{$trans->marca}}')"  data-toggle="modal" data-target="#Eliminar_director">
                                                    <i class="fa-solid fa-trash" style="color: red;"></i>
                                                </a>

                                            @endcan



                                        </td>
                                    </tr>
                                @endforeach

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

    </div>

</div>


<div id="editar_turma" class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title h4" id="myLargeModalLabel">Editar Dados do Transporte</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>

            <div class="modal-body">
                <div  id="sms_erro" class="sufee-alert alert with-close alert-danger alert-dismissible fade show">
                    <span class="badge badge-pill badge-danger">Erro</span>
                   Preencha todos os campos obrigatórios.

                </div>

                <div class="modal-body">
                    <form id="formulario_editar" action="{{route('editar.trasnporte')}}" method="post" enctype="multipart/form-data" >
                       @csrf
                       <div class="form-row">

                        <div class="form-group col-md-6">
                            <label for="inputCity">Marca <span style="color: red">*</span></label>
                            <input type="text" id="marca" name="marca" class="form-control" >
                            <input type="text" id="id" name="id" hidden class="form-control" >

                        </div>
                        <div class="form-group col-md-6">
                            <label for="inputCity">Modelo <span style="color: red">*</span></label>
                            <input type="text"  id="modelo" name="modelo" class="form-control" >

                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group col-md-4">
                            <label for="inputZip">Matrícula <span style="color: red">*</span></label>
                            <input    type="text" id="matricula" name="matricula" class="form-control" >
                        </div>
                        <div class="form-group col-md-4">
                            <label for="inputZip">Cor <span style="color: red">*</span></label>
                            <input  id="cor" name="cor" class="form-control" >
                        </div>
                        <div class="form-group col-md-4">
                            <label for="inputZip">Capacidade <span style="color: red">*</span></label>
                            <input type="number" min="1" id="capacidade" name="capacidade" class="form-control" >
                        </div>

                    </div>

                        <div class="modal-footer">
                            <button type="button" class="btn  btn-secondary" data-dismiss="modal">Fechar</button>
                            <button onclick="verificarRegisto()"  class="btn  btn-primary">Confirmar</button>
                        </div>
                    </form>
                </div>

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

<div id="Eliminar_director" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="exampleModalLiveLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLiveLabel">Eliminar Transporte</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body text-center">
                <h6 class="mb-0">Deseja realmente eliminar este Transporte <label class="badge badge-light-warning" id="dir_marca"></label> ?</h6>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn  btn-secondary" data-dismiss="modal">Não</button>
                <form action="{{route('eliminar.transporte')}}" method="post" enctype="multipart/form-data" >
                    @csrf
                    <input type="text" id="id_trasporte" name="id_trasporte" hidden>
                    <button type="submit" class="btn  btn-primary">Sim</button>
                </form>

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


<script>
    document.getElementById('sms_erro').style.display = 'none';
    function editar_transporte(id, marca,modelo,capacidade, matricula, cor){

        document.getElementById("id").value = id;
        document.getElementById("marca").value = marca;
        document.getElementById("modelo").value  = modelo;
        document.getElementById("matricula").value = matricula;
        document.getElementById("cor").value = cor;
        document.getElementById("capacidade").value = capacidade;
    }


    function verificarRegisto(){
        event.preventDefault();
        var marca = document.getElementById("marca").value;
        var modelo = document.getElementById("modelo").value ;
        var  matricula = document.getElementById("matricula").value;
        var cor = document.getElementById("cor").value;
        var capacidade = document.getElementById("capacidade").value;

        if(capacidade == 0 || marca ==="" || modelo==="" || cor === "" || matricula ===""){
            document.getElementById('sms_erro').style.display = 'block';
        }else{
            document.getElementById('sms_erro').style.display = 'none';
            document.getElementById('formulario_editar').submit();
        }
    }

    function buscar_data(){

        var data = document.getElementById('ano_letivo').value;

        const ano =  parseInt(data.split('-')[0], 10);
        var ano_letivo = ano+1;
        document.getElementById('ano_letivo2').value = ano_letivo;

    }


    function adicionar_professor(id) {
        document.getElementById('sms_erro').style.display = 'none';
        document.getElementById('id_turma').value = id;
    }

    function retornaid_nome(id,nome){
        $('#id_trasporte').val(id);
        document.getElementById("dir_marca").innerHTML = nome;
    }

</script>


@endsection
