<div class="modal fade" id="modal-ViewOfices-{{ $workstation->id_workstations }}">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header bg-primary">
                <h4 class="modal-title">{{ $workstation->name_workstations }}</h4>
                <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('workstationsAssociationTasks') }}" method="get">
                    {{-- Necessario passar essas 2 variaveis, para poder verificar quais as tarefas que foram ou nao selecionadas  --}}
                    <input type="hidden" name="receiveElementalTasks"
                        value="{{ json_encode($receiveElementalTasks) }}">
                    <input type="hidden" name="receiveAllFurtherTasks" value="{{ $receiveAllFurtherTasks }}">
                    <div class="row">
                        <p>Nome Posto de Trabalho : </p>
                        <input class="form-control col-sm-6" type="text" name="nameWorkstation"
                            value="{{ $workstation->nomenclature_workstation }}">
                        <input type="hidden" name="idWorkStation" value="{{ $workstation->id_workstations }}">
                    </div>
                    <br>
                    <section class="content">
                        <div class="container-fluid">
                            <div class="row">
                                @if (count($receiveElementalTasks['geral']) > 0)
                                    <div class="col-sm-6">
                                        <div class="card card-success">
                                            <div class="card-header">
                                                <h3 class="card-title">Tarefas Gerais</h3>
                                                <div class="card-tools">
                                                    <button type="button" class="btn btn-tool"
                                                        data-card-widget="collapse"><i class="fas fa-minus"></i>
                                                    </button>
                                                </div>
                                            </div>
                                            <div class="card-body">
                                                @foreach ($receiveElementalTasks['geral'] as $taskId => $generalTasks)
                                                    <p>{{ $generalTasks['code'] }} - {{ $generalTasks['description'] }}
                                                        <input type="hidden" name="generalTasks[{{ $taskId }}]"
                                                            value="off">
                                                        <input type="checkbox" value="on"
                                                            name="generalTasks[{{ $taskId }}]">
                                                    </p>
                                                @endforeach
                                            </div>
                                        </div>
                                    </div>
                                @endif

                                <div class="col-sm-6">
                                    <div class="row">
                                        @if (count($receiveElementalTasks['3']) > 0)
                                            <div class="col-sm-12">
                                                <div class="card card-primary collapsed-card">
                                                    <div class="card-header">
                                                        <h3 class="card-title">
                                                            Tarefas PSV</h3>
                                                        <div class="card-tools">
                                                            <button type="button" class="btn btn-tool"
                                                                data-card-widget="collapse">
                                                                <i class="fas fa-plus"></i>
                                                            </button>
                                                        </div>
                                                    </div>
                                                    <div class="card-body">
                                                        @foreach ($receiveElementalTasks['3'] as $taskId => $generalTasks)
                                                            <p>{{ $generalTasks['code'] }} -
                                                                {{ $generalTasks['description'] }}
                                                                <input type="hidden" name="3[{{ $taskId }}]"
                                                                    value="off">
                                                                <input type="checkbox" value="on"
                                                                    name="3[{{ $taskId }}]">
                                                            </p>
                                                        @endforeach
                                                    </div> {{-- ./card-body  --}}
                                                </div>
                                            </div> {{-- ./col-sm-12 --}}
                                        @endif

                                        @if (count($receiveElementalTasks['2']) > 0)
                                            <div class="col-sm-12">
                                                <div class="card card-primary collapsed-card">
                                                    <div class="card-header">
                                                        <h3 class="card-title">
                                                            Tarefas ISV</h3>
                                                        <div class="card-tools">
                                                            <button type="button" class="btn btn-tool"
                                                                data-card-widget="collapse">
                                                                <i class="fas fa-plus"></i>
                                                            </button>
                                                        </div>
                                                    </div>
                                                    <div class="card-body">
                                                        @foreach ($receiveElementalTasks['2'] as $taskId => $generalTasks)
                                                            <p>{{ $generalTasks['code'] }} -
                                                                {{ $generalTasks['description'] }}
                                                                <input type="hidden" name="2[{{ $taskId }}]"
                                                                    value="off">
                                                                <input type="checkbox" value="on"
                                                                    name="2[{{ $taskId }}]">
                                                            </p>
                                                        @endforeach
                                                    </div> {{-- ./card-body  --}}
                                                </div>
                                            </div>
                                        @endif

                                        @if (count($receiveElementalTasks['1']) > 0)
                                            <div class="col-sm-12">
                                                <div class="card card-primary collapsed-card">
                                                    <div class="card-header">
                                                        <h3 class="card-title">
                                                            Tarefas CV</h3>
                                                        <div class="card-tools">
                                                            <button type="button" class="btn btn-tool"
                                                                data-card-widget="collapse">
                                                                <i class="fas fa-plus"></i>
                                                            </button>
                                                        </div>
                                                    </div>
                                                    <div class="card-body">
                                                        @foreach ($receiveElementalTasks['1'] as $taskId => $generalTasks)
                                                            <p>{{ $generalTasks['code'] }} -
                                                                {{ $generalTasks['description'] }}
                                                                <input type="hidden" name="1[{{ $taskId }}]"
                                                                    value="off">
                                                                <input type="checkbox" value="on"
                                                                    name="1[{{ $taskId }}]">
                                                            </p>
                                                        @endforeach
                                                    </div> {{-- ./card-body  --}}
                                                </div>
                                            </div>
                                        @endif

                                        @if (count($receiveAllFurtherTasks) > 0)
                                            <div class="col-sm-12">
                                                <div class="card card-info collapsed-card">
                                                    <div class="card-header">
                                                        <h3 class="card-title">
                                                            Tarefas Complementares</h3>
                                                        <div class="card-tools">
                                                            <button type="button" class="btn btn-tool"
                                                                data-card-widget="collapse">
                                                                <i class="fas fa-plus"></i>
                                                            </button>
                                                        </div>
                                                    </div>
                                                    <div class="card-body">
                                                        @foreach ($receiveAllFurtherTasks as $furtherTasks)
                                                            <p>{{ $furtherTasks->further_tasks_name }} -
                                                                {{ $furtherTasks->further_tasks_description }}
                                                                <input type="hidden"
                                                                    name="FurtherTasks[{{ $furtherTasks->further_tasks_id }}]"
                                                                    value="off">
                                                                <input type="checkbox" value="on"
                                                                    name="FurtherTasks[{{ $furtherTasks->further_tasks_id }}]">
                                                            </p>
                                                        @endforeach
                                                    </div> {{-- ./card-body  --}}
                                                </div>
                                            </div>
                                        @endif

                                        {{-- @dump($receiveAllFurtherTasks) --}}


                                    </div>{{-- ./row --}}

                                </div> {{-- ./col-sm-6 --}}
                            </div> {{-- ./row --}}

                        </div>
                    </section>

            </div>
            {{-- Modal-body --}}
            <div class="modal-footer justify-content-between">
                <input type="submit" class="btn btn-success" value="Guardar">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
            </div>
            </form>
        </div>
    </div>
</div>

{{-- Scrip para assim que entrar na modal, ele verifica quais as tarefas ja associadas a essa Ws e mostra nas checkbox como checked --}}
<script>
   $(document).ready(function() {
    $('.open-modal').click(function(e) {
        e.preventDefault();

        var WorkstationId = $(this).data('workstation-id');

        $.ajax({
            url: '/api/receveTasksWorkstationPlanning/' + WorkstationId,
            method: 'GET',
            success: function(data) {
                console.log(data.workstationsAssociationTasks);

                data.workstationsAssociationTasks.forEach(function(item) {
                    // Seleciona o checkbox com nome "3[taskId]" e marca como selecionado
                    $('input[name="1[' + item.elemental_tasks_id + ']"]').prop('checked', true);
                    $('input[name="2[' + item.elemental_tasks_id + ']"]').prop('checked', true);
                    $('input[name="3[' + item.elemental_tasks_id + ']"]').prop('checked', true);
                    $('input[name="generalTasks[' + item.elemental_tasks_id + ']"]').prop('checked', true);
                    $('input[name="FurtherTasks[' + item.further_tasks_id + ']"]').prop('checked', true);

                });
            }
        });
    });
});

</script>
