@extends('Templates/templateAdmin')

@section('Main-content')
    @if (session('success'))
        <div class="alert alert-success" role="alert" id="alert-message-success" style="transition: opacity 1s;">
            {{ session('success') }}
        </div>
        <script>
            setTimeout(function() {
                $('#alert-message-success').fadeOut('slow', function() {
                    $(this).remove();
                });
            }, 5000); // A mensagem desaparecerá após 5 segundos
        </script>
    @endif

    {{-- @if (session('error'))
        <div class="alert alert-danger" role="alert" id="alert-message-danger" style="transition: opacity 1s;">
            {!! session('error') !!}
        </div>
    @endif --}}
    @if (session('errors'))
        <div class="alert alert-danger" role="alert" id="alert-message-danger" style="transition: opacity 1s;">
            @foreach (session('errors') as $error)
                {!! $error !!}
                <br>
            @endforeach
        </div>
    @endif


    <section class="content-header">
        <div class="container-fluid">

            <!-- Progressbar -->
            <ul id="progressbar" class="nav nav-pills d-flex flex-sm-row justify-content-center">
                <li class="@if ($step == 3) active @endif "></li>
                {{-- Project details --}}
                <li class="@if ($step == 3) active @endif "></li>
                {{-- Articulated --}}
                <li class="@if ($step == 3) active @endif "></li>
                {{-- Workstation --}}
            </ul>

        </div>
    </section>
    {{-- ./content-header --}}
    <fieldset class="content">
        <div class="container-fluid">

            <div class="card card-primary">
                <div class="card-header">
                    <h3 class="card-title">Postos de Trabalho</h3>
                </div>
                <!-- ./Card-header -->
                <div class="card-body">
                    <!-- Criar tarefa -->
                    <div class="card card-success collapsed-card">
                        <div class="card-header">
                            <h3 class="card-title">Criar Postos de Trabalho</h3>

                            <div class="card-tools">
                                <button type="button" class="btn btn-tool" data-card-widget="collapse"><i
                                        class="fas fa-plus"></i>
                                </button>
                            </div>
                            <!-- /.card-tools -->
                        </div>
                        <!-- /.card-header -->
                        <div class="card-body">
                            <div class="card ">
                                <div class="form-group col-md-12">
                                    <form action="{{ route('createWorkStations') }}" method="post">
                                        @csrf
                                        {{-- <input type="hidden" name="numberProject" value="{{ $numberProject }}">
                                            <div class="row">
                                                <div class="form-group">
                                                    <label>Selecione o Número de Postos Pretendidos :
                                                    </label>
                                                    <input class="form-control" name="numberWorkstations" type="number"
                                                        id="numberPosts">
                                                </div>
                                                <input class="btn btn-success" type="submit" value="Guardar">
                                            </div> --}}
                                            <input type="hidden" name="numberProject" value="{{ $numberProject }}">
                                        <div style="display: flex; justify-content: space-between;">
                                            <div style="display: flex; align-items: center;">
                                                <label for="inputName" style="margin-right: 10px;"> Quantidade de Postos Pretendidos :</label>
                                                <input class="form-control" name="numberWorkstations" type="number"
                                                        id="numberPosts">
                                            </div>
                                            <div>
                                                <button class="btn btn-success" type="submit" value="Guardar" type="button" style="align-self: flex-end;">Button</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                        <!-- /.card-body -->

                    </div>
                    <!--/Criar tarefa-->

                    <!-- Card Posto de Trabalho -->
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Postos da Obra </h3>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <form action="{{ route('AddNomenclatureWorkstation') }}" method="GET">
                                    <table id="workstationTable" class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>Posto de Trabalho</th>
                                                <th>Nome Posto de Trabalho</th>
                                                <th>Detalhes</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach ($listWorkstations as $listWorkstation)
                                                <tr>
                                                    <td>{{ $listWorkstation->name_workstations }}</td>
                                                    <td class="text-center">
                                                        @if ($listWorkstation->nomenclature_workstation == null)
                                                            <input type="text" class="form-control"
                                                                name="nameWorkstations[{{ $listWorkstation->name_workstations }}]"
                                                                value="">
                                                        @else
                                                            <input type="text" class="form-control"
                                                                name="nameWorkstations[{{ $listWorkstation->name_workstations }}]"
                                                                value="{{ $listWorkstation->nomenclature_workstation }}"
                                                                readonly>
                                                        @endif
                                                    </td>
                                                    <td class="text-center d-flex justify-content-around ">
                                                        <a href='#' data-toggle='modal' class="open-modal"
                                                            data-workstation-id="{{ $listWorkstation->id_workstations }}"
                                                            data-target='#modal-ViewOfices-{{ $listWorkstation->id_workstations }}'>
                                                            <i class='fa-solid fa-edit text-primary'></i>
                                                        </a>
                                                        <a href="#" data-toggle="modal"
                                                            data-target="#modal-remover-{{ $listWorkstation->id_workstations }}">
                                                            <i class="fa-solid fa-trash-alt text-danger"></i>
                                                        </a>
                                                    </td>
                                                </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                    <input class="btn btn-primary" type="submit" value="Guardar">
                                </form>
                            </div>
                            <!--/articulado de obra-->
                        </div>
                        <!-- /.card-body -->
                    </div>
                    <!-- ./Card Posto de Trabalho -->
                </div>
                {{-- ./card-body --}}
            </div>
            <!-- ./Card card-primary -->
            <a href="{{ route('test2', ['id' => $numberProject]) }}"
                class="btn btn-primary previous float-left">Anterior</a>

            <form action="{{ route('finishCreatingProject', ['numberProject' => $numberProject]) }}">
                @csrf
                <input type="submit" class="btn btn-success previous float-right" value="Concluir">
            </form>

        </div>
        {{-- ./container-fluid --}}
    </fieldset>
    {{-- ./content --}}
    @foreach ($listWorkstations as $listWorkstation)
        <div class="modal fade" id="modal-ViewOfices-{{ $listWorkstation->id_workstations }}">
            <div class="modal-dialog modal-xl">
                <div class="modal-content">
                    <div class="modal-header bg-primary">
                        <h4 class="modal-title">{{ $listWorkstation->name_workstations }}</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">x</span>
                        </button>
                    </div>
                    <div class="modal-body">

                        <form action="{{ route('workstationsAssociationTasks') }}" method="get">
                            <div class="row">
                                <p>Nome Posto de Trabalho : </p>
                                <input class="form-control col-sm-6" type="text" name="nameWorkstation"
                                    value="{{ $listWorkstation->nomenclature_workstation }}">
                                <input type="hidden" name="idWorkStation"
                                    value="{{ $listWorkstation->id_workstations }}">
                            </div>
                            <br>
                            <section class="content">
                                <div class="container-fluid">

                                    <div class="row ">
                                        <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">
                                                    <!-- Tabela de Checkbox -->
                                                    <div>

                                                        <p>TE1 - Desmontar da linha
                                                            <input type="hidden" name="generalTasks[1]" value="off">
                                                            <input type="checkbox" class="checkboxChoseTasksOficesPSV"
                                                                value="on" name="generalTasks[1]">
                                                        </p>
                                                        <p>TE2 - Descontaminar
                                                            <input type="hidden" name="generalTasks[2]" value="off">
                                                            <input type="checkbox" class="checkboxChoseTasksOficesPSV"
                                                                value="on" name="generalTasks[2]">
                                                        </p>
                                                        <p>TE5 - Limpeza e lavagem dos componentes
                                                            <input type="hidden" name="generalTasks[8]" value="off">
                                                            <input type="checkbox" class="checkboxChoseTasksOficesPSV"
                                                                value="on" name="generalTasks[8]">
                                                        </p>
                                                        <p>TE9 - Pintura
                                                            <input type="hidden" name="generalTasks[14]" value="off">
                                                            <input type="checkbox" class="checkboxChoseTasksOficesPSV"
                                                                value="on" name="generalTasks[14]">
                                                        </p>
                                                        <p>TE11 - Inspeção Final
                                                            <input type="hidden" name="generalTasks[17]" value="off">
                                                            <input type="checkbox" class="checkboxChoseTasksOficesPSV"
                                                                value="on" name="generalTasks[17]">
                                                        </p>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="col-sm-6">
                                            <div class="row">

                                                @if ($equipments->firstWhere('equipment_type_id', 3))
                                                    <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">
                                                                <p>TE3 - Pré-teste
                                                                    <input type="hidden" name="PsvTasks[3]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="PsvTasks[3]">
                                                                </p>
                                                                <p>TE4 - Abertura da válvula, análise e controle dos
                                                                    componentes
                                                                    <input type="hidden" name="PsvTasks[5]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="PsvTasks[5]">
                                                                </p>

                                                                <p>TE6 - Retificação e lapidação
                                                                    <input type="hidden" name="PsvTasks[9]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="PsvTasks[9]">
                                                                </p>
                                                                <p>TE7 - Fecho da válvula e substituição de componentes
                                                                    <input type="hidden" name="PsvTasks[10]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="PsvTasks[10]">
                                                                </p>
                                                                <p>TE8 - Calibrar e certificar
                                                                    <input type="hidden" name="PsvTasks[13]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="PsvTasks[13]">
                                                                </p>
                                                                <p>TE10 - Montagem na linha
                                                                    <input type="hidden" name="PsvTasks[15]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="PsvTasks[15]">
                                                                </p>
                                                            </div>
                                                        </div>
                                                    </div>
                                                @endif

                                                @if ($equipments->firstWhere('equipment_type_id', 1))
                                                    <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">
                                                                <p>TE12 - Inspeção visual
                                                                    <input type="hidden" name="CvTasks[18]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="CvTasks[18]">
                                                                </p>
                                                                <p>TE3(2) - Pré-teste
                                                                    <input type="hidden" name="CvTasks[4]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="CvTasks[4]">
                                                                </p>
                                                                <p>TE4(2) - Abertura da válvula, análise e controle dos
                                                                    componentes
                                                                    <input type="hidden" name="CvTasks[6]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="CvTasks[6]">
                                                                </p>
                                                                <p>TE7(2) - Fecho da válvula e substituição de componentes
                                                                    <input type="hidden" name="CvTasks[11]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="CvTasks[11]">
                                                                </p>
                                                                <p>TE13 - Teste final
                                                                    <input type="hidden" name="CvTasks[19]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="CvTasks[19]">
                                                                </p>
                                                                <p>TE10(2) - Montagem na linha
                                                                    <input type="hidden" name="CvTasks[16]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="CvTasks[16]">
                                                                </p>
                                                            </div>
                                                        </div>
                                                    </div>
                                                @endif

                                                {{-- Tarefas ISV --}}
                                                @if ($equipments->firstWhere('equipment_type_id', 2))
                                                    <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">
                                                                <p>TE4(3) - Abertura da válvula, análise e controle dos
                                                                    componentes

                                                                    <input type="hidden" name="IsvTasks[7]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="IsvTasks[7]">
                                                                </p>
                                                                <p>TE7(3) - Fecho da válvula e substituição de componentes
                                                                    <input type="hidden" name="IsvTasks[12]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="IsvTasks[12]">
                                                                </p>
                                                                <p>TE14 - Ensaio
                                                                    <input type="hidden" name="IsvTasks[20]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="IsvTasks[20]">
                                                                </p>
                                                                <p>TE10 - Montagem na linha
                                                                    <input type="hidden" name="IsvTasks[23]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="IsvTasks[23]">
                                                                </p>
                                                                <p>TE15 - Empancar
                                                                    <input type="hidden" name="IsvTasks[21]"
                                                                        value="off">
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="on"
                                                                        name="IsvTasks[21]">
                                                                </p>
                                                            </div>
                                                        </div>
                                                    </div>
                                                @endif

                                                @if ($futherTasks->count() > 0)
                                                    <div class="col-sm-12">
                                                        <div class="card card-primary 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">
                                                                <p>TC1 - Pintar de Verde
                                                                    <input type="checkbox"
                                                                        class="checkboxChoseTasksOficesPSV" value="16"
                                                                        name="CvTasks[TE10(2)]">
                                                                </p>
                                                            </div>
                                                        </div>
                                                    </div>
                                                @endif

                                            </div>
                                        </div>

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

                            <div class="modal-footer justify-content-between">
                                <!-- Vai ficar o Footer -->
                                <input type="submit" class="btn btn-success" value="Guardar">
                        </form>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- modal-dialog -->
        </div>
        <!-- /.modal remover-->

        </div>
        <!-- /.Modal, Postos de Trabalho -->

        {{-- modal-remover --}}
        <div class="modal fade" id="modal-remover-{{ $listWorkstation->id_workstations }}">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header bg-primary">
                        <h4 class="modal-title">Remover Posto {{ $listWorkstation->name_workstations }} </h4>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">x</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form action="{{ route('deleteWorkstation', $listWorkstation->name_workstations) }}"
                            method="POST">
                            @csrf
                            @method('DELETE')
                            <p>Deseja realmente remover o Posto de Trabalho : {{ $listWorkstation->name_workstations }}?
                            </p>
                            <input class="btn btn-danger" type="submit" value="Remover">
                        </form>

                    </div>
                </div>
            </div>
        </div>
        {{-- ./modal-remover --}}
    @endforeach
@endsection

@section('scriptsTemplateAdmin')
    <script>
        $(document).ready(function() {
            $('.open-modal').click(function(e) {
                e.preventDefault();

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

                // Limpa todos os checkboxes ao abrir a modal
                $('input[name^="generalTasks"]').prop('checked', false);
                $('input[name^="PsvTasks"]').prop('checked', false);
                $('input[name^="CvTasks"]').prop('checked', false);

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

                        data.workstationsAssociationTasks.forEach(function(item) {
                            // Aqui estamos assumindo que os checkboxes estão nomeados exatamente como "generalTasks[elemental_tasks_id]",
                            // "PsvTasks[elemental_tasks_id]", e "CvTasks[elemental_tasks_id]". Se eles não estiverem, você precisará ajustar isto.
                            $('input[name="generalTasks[' + item.elemental_tasks_id +
                                ']"]').prop('checked', true);
                            $('input[name="PsvTasks[' + item.elemental_tasks_id + ']"]')
                                .prop('checked', true);
                            $('input[name="CvTasks[' + item.elemental_tasks_id + ']"]')
                                .prop('checked', true);
                            $('input[name="IsvTasks[' + item.elemental_tasks_id + ']"]')
                                .prop('checked', true);

                        });
                    }
                })

            });
        });
    </script>

    <script>
        $(function() {
            $("#workstationTable").DataTable({
                "responsive": true,
                "lengthChange": false,
                "autoWidth": false,
                "buttons": ["copy", "csv", "excel", "pdf", "print", "colvis"]
            }).buttons().container().appendTo('#workstationTable_wrapper .col-md-6:eq(0)');
        });
    </script>
@endsection
