@extends('layouts.index')
@section('contenido')
<div class="content">
	<div class="card">
		<div class="card-header header-elements-inline">
			<h5 class="card-title">NUEVA ASIGNACIÓN DE TERRITORIO</h5>
			<div class="header-elements">
				<div class="list-icons">
				    <a class="list-icons-item" data-action="collapse"></a>
				</div>
			</div>
		</div>

		<div class="card-body">
			<form action="{{ route('territorio.actualizar',$planTrabajo->id) }}" method="POST">
                @csrf
                <fieldset>
                    <div class="collapse show">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="">APM</label>
                                    <select class="form-control" name="id_apm">
                                        @foreach ($apms as $apm)
                                            <option {{ $planTrabajo->id_apm == $apm['id'] ? "selected" : "" }} value="{{$apm['id']}}">{{$apm['primer_nombre']}} {{$apm['segundo_nombre']}} {{$apm['primer_apellido']}} {{$apm['segundo_apellido']}}</option>
                                        @endforeach
                                    </select>
                                </div>
                                <input type="hidden" id="id_gira" name="id_gira" value="{{$giraPlan->id}}">
                                <div class="form-group">
                                    <label for="">País</label>
                                    <input type="text" class="form-control" id="pais" value="{{$giraPlan->region->pais->descripcion_pais}}" name="pais" placeholder="" readonly>
                                </div>
                            </div>
                            <div class="col-md-6">

                                <div class="form-group">
                                    <label for="">Región</label>
                                    <input type="text" class="form-control" id="region" value="{{$giraPlan->region->descripcion_region}}" name="region" placeholder="" readonly>
                                </div>
                                <div class="form-group">
                                    <label for="">Gira</label>
                                    <input type="text" class="form-control" id="gira" value="{{$giraPlan->descripcion_gira}}" name="gira" placeholder="" readonly>
                                </div>
                                <a class="btn btn-success"  data-toggle="modal" data-target="#tablaCodigoArea">Buscar <i class="icon-search4 ml-2"></i></a>
                            </div>
                        </div>
                    </div>
                </fieldset>

				<div class="text-right">
					<button type="submit" class="btn btn-primary">Guardar asignación<i class="icon-floppy-disk ml-2"></i></button>
				</div>
            </form>

		</div>
	</div>

</div>

<div class="modal fade" id="tablaCodigoArea" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-xl">
        <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">
                <table id="tablaJS" class="table datatable-basic">
                    <thead>
                        <tr>
                            <th>id</th>
                            <th>País</th>
                            <th>Región</th>
                            <th>Gira</th>
                            <th class="text-center">Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($giras as $gira)
                            <tr>
                                <td class="id_gira">{{$gira->id}}</td>
                                <td class="pais">{{$gira->region->pais->descripcion_pais}}</td>
                                <td class="region">{{$gira->region->descripcion_region}}</td>
                                <td class="gira">{{$gira->descripcion_gira}}</td>
                                <td><button onclick="guardarGira(this)" class='btn btn-success seleccionarCodigoArea'>Seleccionar</button></td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection

@section('javascript')
<script>
    function guardarGira(elemento){
        const padre=elemento.parentElement.parentElement;
        const id_gira=padre.getElementsByClassName('id_gira')[0].innerText;
        const pais=padre.getElementsByClassName('pais')[0].innerText;
        const region=padre.getElementsByClassName('region')[0].innerText;
        const gira=padre.getElementsByClassName('gira')[0].innerText;

        document.getElementById('pais').value=pais;
        document.getElementById('region').value=region;
        document.getElementById('gira').value=gira;
        document.getElementById('id_gira').value=id_gira;

        $('#tablaCodigoArea').modal('hide');
        
    }
</script>
@endsection
