@extends('Templates/templateAdmin')

@section('Main-content')
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>{{ $DatasProject->company_project_description }}</h1>
                    <input type="hidden" value="{{ $DatasProject->company_projects_id }}" id="receiveNumberProject">
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="{{ route('home') }}">Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="./preparadas.html">Em Execucao</a></li>
                        <li class="breadcrumb-item active">{{ $DatasProject->company_project_description }}</li>
                    </ol>
                </div><!-- /.col -->
            </div>
        </div><!-- /.container-fluid -->
    </section>
    <!-- /.content-header -->


    <!-- Main content -->
    <section class="content">
        <div class="container-fluid">
            <div class="row justify-content-center">
                <div class="col-md-12">

                    <!-- Card box criar instalção -->
                    <form>
                        <div class="row">
                            <div class="col-sm-6" id="BotaoDetalhesObra">
                                <a href="#" type="button" class="btn btn-block bg-gradient-primary btn-lg">
                                    Detalhes da Obra</a>
                            </div>
                            <div class="col-sm-6" id="BotaoArticulado">
                                <a href="#" type="button"
                                    class="btn btn-block bg-gradient-primary btn-lg">Articulado</a>
                            </div>
                            <div class="col-sm-6" id="BotaoPostosDeTrabalho">
                                <a href="#" type="button" class="btn btn-block bg-gradient-primary btn-lg">Postos de
                                    Trabalho</a>
                            </div>
                        </div>
                        <br><br>

                        <div class="card card-primary" id="CardDetalhesObra">
                            <div class="card-header">
                                <h3 class="card-title">Detalhes da Obra</h3>
                                <div class="card-tools">
                                </div>
                                <!-- /.card-tools -->
                            </div>
                            <!-- /.card-header -->

                            <div class="card-body p-0">
                                <table class="table table-striped text-center">
                                    <tbody>
                                        <tr>
                                            <td>Descrição da obra:</td>
                                            <td>{{ $DatasProject->company_project_description }}</td>
                                        </tr>
                                        <tr>
                                            <td>N.ºobra ISPT:</td>
                                            <td>{{ $DatasProject->project_ispt_number }}</td>
                                        </tr>
                                        <tr>
                                            <td>Resp.ISPT:</td>
                                            <td>{{ $DatasProject->project_ispt_responsible }}</td>
                                        </tr>
                                        <tr>
                                            <td>Cliente :</td>
                                            <td>Cliente1</td>
                                        </tr>
                                        <tr>
                                            <td>N.ºobra Cliente :</td>
                                            <td>{{ $DatasProject->project_company_number }}</td>
                                        </tr>
                                        <tr>
                                            <td>Resp.Cliente:</td>
                                            <td>{{ $DatasProject->project_company_responsible }}</td>
                                        </tr>
                                        <tr>
                                            <td>Obra Iniciada em :</td>
                                            <td>{{ $DatasProject->date_started }}</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <!-- /.card-body -->

                            <div class="card-footer">
                                <div class="float-right">

                                    {{-- <a href="#" type="button" class="btn btn-primary">Editar</a> --}}
                                    {{-- <h1>test</h1>
                                    <ul id="myList"> --}}
                                    <!-- Itens da lista serão gerados dinamicamente aqui -->
                                    {{-- </ul> --}}
                                </div>
                            </div>
                        </div>
                        <!-- /.card -->
                    </form>
                    <!-- /.Card box criar instalção -->

                    <!-- Card box criar equipamentos -->
                    <form>
                        <!-- Articulado -->
                        <div class="card card-primary" id="CardArticuladoObra">
                            <div class="card-header">
                                <h3 class="card-title">Articulado</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">
                                <!-- Table articulado de obra -->
                                <div class="card">
                                    <div class="card-header">
                                        <h3 class="card-title">Equipamentos da obra</h3>
                                    </div>
                                    <!-- /.card-header -->
                                    <div class="card-body">
                                        <div class="table-responsive">
                                            <div class="row text-center">

                                                <div class="form-group col-sm-3">
                                                    <label>Tipo de Equipamento </label>
                                                    <select id="tipo_valvulasList" name="equipmentTypeId"
                                                        class="form-control">
                                                        <option value='#' selected>Mostrar Todos</option>
                                                        @foreach ($equipmentsTypes as $equipmentsType)
                                                            <option value="{{ $equipmentsType->equipment_type_id }}">
                                                                {{ $equipmentsType->equipment_type_name }}</option>
                                                        @endforeach
                                                    </select>
                                                </div>

                                                <div class="col-sm-3">
                                                    <div class="form-group">
                                                        <label>Âmbitos </label>
                                                        <select class="form-control" name="EquipmentAmbit"
                                                            id="AmbitsEquipments_list" required>
                                                            <option value="#" hidden>Mostrar Todos</option>
                                                        </select>
                                                    </div>
                                                </div>

                                                <div class="col-sm-3">
                                                    <div class="form-group">
                                                        <label>Inspeção</label>
                                                        <select id="inspecValvula" class="form-control">
                                                            <option value="#">Mostrar Todos</option>
                                                            <option value="Sim">Sim </option>
                                                            <option value="Nao">Nao </option>
                                                        </select>
                                                    </div>
                                                </div>
                                            </div>
                                            <br>

                                            <table id="TableExecutionProjectEquipments"
                                                class="table table-bordered table-striped">
                                                <thead>
                                                    <tr>
                                                        <th>Tag</th>
                                                        <th>Tipo Equipamento</th>
                                                        <th>Âmbito</th>
                                                        <th>Tarefas</th>
                                                        <th>Tarefa Atual</th>
                                                        <th>Data Entrada</th>
                                                        <th>Data Conclusao</th>
                                                        <th>Inspecao</th>
                                                        <th>Detalhes</th>
                                                    </tr>
                                                </thead>
                                            </table>
                                            <!-- /. Table-->
                                        </div>
                                        <!--/table obra-->
                                    </div>
                                    <!-- /.card-body -->

                                </div>
                                <!-- ./card -->
                            </div>
                            {{-- card-body --}}

                            <!-- TEM DE TER FOOTER  -->
                        </div>
                        <!--/.CardArticuladoObra -->
                    </form>
                    <!-- /.card -->
                    <!-- /.Card box criar equipamentos -->

                    <form>
                        <div class="card card-primary" id="CardPostosDeTrabalhoObra">
                            <div class="card-header">
                                <h3 class="card-title">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">

                                <!-- Criar tarefa -->
                                <div class="card card-primary collapsed-card">

                                    <!-- /.card-header -->
                                    <div class="card-body">
                                        <div class="card ">
                                            <div class="form-group col-md-12">
                                                <div class="card">
                                                    <div class="form-group">
                                                        <label>Selecione o Número de Postos Pretendidos :
                                                        </label>
                                                        <input class="form-control" type="number" id="numberPosts">
                                                    </div>
                                                </div>
                                                <!-- <p id="receiveNumberPosts"></p> -->
                                                <table class="table table-bordered table-striped">
                                                    <thead>
                                                        <tr>
                                                            <th>Postos de Trabalho</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody id="receiveNumberPosts">
                                                        <!-- Vai receber o Número de Oficinas -->
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- /.card-body -->

                                </div>
                                <!--/Criar tarefa-->
                                <div class="card">
                                    <div class="card-header">
                                        <h3 class="card-title">Listas de Postos de Obra</h3>
                                    </div>
                                    <div class="card-body">
                                        <div class="table-responsive">
                                            <table id="TableExecutionProjectWorkstation"
                                                class="table table-bordered table-striped">
                                                <thead>
                                                    <tr>
                                                        <th>Postos de Trabalho</th>
                                                        <th>Nome Posto</th>
                                                        <th>Tarefas Elementares</th>
                                                        <th>Tarefas Complementares</th>
                                                    </tr>
                                                </thead>
                                            </table>
                                        </div>
                                        <!--/articulado de obra-->
                                    </div>
                                    <!-- /.card-body -->
                                </div>
                                <!-- modal Remover -->
                                <!-- /.card -->
                            </div>
                            <!-- /.card-body -->
                    </form>
                </div>
            </div>
        </div>


    </section>
    <!-- ./content -->

    {{-- Modal-showProgressEquipment --}}
    <div class="modal fade" id="modal-showProgressEquipment" tabindex="-1" role="dialog"
        aria-labelledby="ModalTransferForArticulated" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="ModalTransferForArticulatedLabel">Progresso Equipamento</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">

                    <h1 id="equipmentId"></h1>
                    <h2 id="task_codes"></h2>

                    <section id="employer-post-new-job">
                        <div class="row">
                            <div class="container">
                                <div class="row">
                                    <div class="col-xs-10 col-xs-offset-1" id="container">
                                        <div class="res-steps-container">
                                            <div class="res-steps res-step-one active" data-class=".res-form-one">
                                                <div class="res-step-bar">1</div>
                                                <div class="res-progress-bar"></div>
                                                <div class="res-progress-title">Add Title & Description 1</div>
                                            </div>
                                            <div class="res-steps res-step-two" data-class=".res-form-two">
                                                <div class="res-step-bar">2</div>
                                                <div class="res-progress-bar"></div>
                                                <div class="res-progress-title">Add Title & Description 2</div>
                                            </div>
                                            <div class="res-steps res-step-three" data-class=".res-form-three">
                                                <div class="res-step-bar">3</div>
                                                <div class="res-progress-bar"></div>
                                                <div class="res-progress-title">Add Title & Description 3</div>
                                            </div>
                                            <div class="res-steps res-step-four" data-class=".res-form-four">
                                                <div class="res-step-bar">4</div>
                                                <div class="res-progress-bar"></div>
                                                <div class="res-progress-title">Add Title & Description 4</div>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                    <br>
                    <!-- Registro de Trabalho da Válvula -->
                    <div class="card card-primary collapsed-card">
                        <div class="card-header">
                            <h3 class="card-title">Registros</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">
                            <h4 class="text-center">Historico de registro :</h4>
                            <div class="row">
                                <div class="col-sm-6">Work Entry:</div>
                                <div class="col-sm-6"></div>
                            </div>
                            <br>

                            <div id="containerEquipmentComment"></div>

                            <br>

                            <div class="card card-success" id="CardShowCommetsEquipment">
                                <div class="card-header">
                                    <h4>Comments</h3>
                                </div>
                                <br>
                                <div id="containerShowCommetsEquipment"></div>
                            </div>
                        </div>
                        <!-- /.card-body -->

                    </div>
                    <!-- /Registro de Trabalho da Válvula -->
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
                </div>
            </div>
        </div>



    </div>
    {{-- ./Modal-showProgressEquipment --}}
@endsection

@section('scriptsTemplateAdmin')
    <script type="text/javascript">
        $(document).ready(function() {

            $("#CardShowCommetsEquipment").hide();

            $("#ShowCommetsEquipment").click(function() {
                $("#CardShowCommetsEquipment").toggle();
            });

            //Cards das div : Destricao - Articulado - Postos de Trabalho

            $("#CardArticuladoObra").hide();
            $("#CardPostosDeTrabalhoObra").hide();



            //Botoes  : Destricao - Articulado - Postos de Trabalho
            $('#BotaoDetalhesObra').hide();
            // $('#BotaoArticulado').hide();
            // $('BotaoPostosDeTrabalho').hide();

            $('#BotaoArticulado').on('click', function() {
                // Mostra o Card do Articulado e o Botao de detalhes 
                $('#CardArticuladoObra').show();
                $("#BotaoDetalhesObra").show();
                $('#BotaoPostosDeTrabalho').show();

                $('#BotaoArticulado').hide();
                $("#CardPostosDeTrabalhoObra").hide();
                $("#CardDetalhesObra").hide();
            });

            $('#BotaoDetalhesObra').on('click', function() {
                // Mostra o Card do Articulado e o Botao de detalhes 
                $('#CardDetalhesObra').show();
                $("#BotaoArticulado").show();
                $('#BotaoPostosDeTrabalho').show();

                $('#BotaoDetalhesObra').hide();
                $("#CardPostosDeTrabalhoObra").hide();
                $("#CardArticuladoObra").hide();
            });

            $('#BotaoPostosDeTrabalho').on('click', function() {
                // Mostra o Card do Articulado e o Botao de detalhes 
                $('#CardPostosDeTrabalhoObra').show();
                $("#BotaoArticulado").show();
                $("#BotaoDetalhesObra").show();

                $('#BotaoPostosDeTrabalho').hide();
                $("#CardDetalhesObra").hide();
                $("#CardArticuladoObra").hide();
            });
        })
    </script>

    <script type="Text/javascript">
        $(document).ready(function() {
                                                                    $('#TableExecutionProjectWorkstation').DataTable({
                                                                        autoWidth: false,
                                                                        processing: true,
                                                                        serverSide: true,
                                                                        ajax: {
                                                                            url: '/api/receiveWorkstationProject/' + $('#receiveNumberProject').val()
                                                                        },
                                                                        columns: [{
                                                                                data: 'name_workstations',
                                                                                name: 'name_workstations'
                                                                            },
                                                                            {
                                                                                data: 'nomenclature_workstation',
                                                                                name: 'nomenclature_workstation'
                                                                            },
                                                                            {
                                                                                data: 'workstations_Association_Tasks',
                                                                                name: 'workstations_Association_Tasks'
                                                                            }
                                                                            
                                                                        ] 
                                                                    })
                                                                })
                                                                </script>



    <script>
        $(document).ready(function() {
            $('#TableExecutionProjectEquipments').DataTable({
                autoWidth: false,
                processing: true,
                serverSide: true,
                ajax: {
                    url: '/api/receiveEquipmentsProject/' + $('#receiveNumberProject').val(),
                    data: function(d) {
                        d.equipment_type_id = $('#tipo_valvulasList').val();
                        d.ambits_id = $('#AmbitsEquipments_list').val();
                    },
                },

                columns: [{
                        data: 'equipment_tag',
                        name: 'equipment_tag'
                    },
                    {
                        data: 'equipment_type',
                        name: 'equipment_type'
                    },
                    {
                        data: 'Ambits',
                        name: 'Ambits'
                    },
                    {
                        data: 'order_tasks',
                        name: 'order_tasks'
                    },
                    {
                        data: 'current_task',
                        name: 'current_task'
                    },
                    {
                        data: 'Inspec',
                        name: 'Inspec'
                    },
                    // {
                    //     data: 'entry_date',
                    //     name: 'entry_date'
                    // },
                    {
                        data: 'equipment_id',
                        name: 'equipment_id',
                        render: function(data, type, row) {
                            return '<a href="#" data-toggle="modal" data-target="#modal-showProgressEquipment" data-equipment-id="' +
                                data + '"><i class="fa-solid fa-eye text-secondary"></i> </a>';
                        }
                    }
                ],
                rowId: 'equipment_id'
            });
            // Scripts para que com base no recebido , ele atualiza a pagina automaticamente
            $('#tipo_valvulasList').on('change', function() {
                // Atualiza a tabela quando o valor selecionado no select de tipo de válvulas for alterado
                $('#TableExecutionProjectEquipments').DataTable().ajax.reload();
            });
            $('#AmbitsEquipments_list').on('change', function() {
                // Atualiza a tabela quando o valor selecionado no select de tipo de válvulas for alterado
                $('#TableExecutionProjectEquipments').DataTable().ajax.reload();
            });

            $(document).on('click', '[data-toggle="modal"]', function() {
                var equipmentId = $(this).data('equipment-id');
                $('#equipmentId').text(equipmentId);
            });
        });
    </script>

    <script>
        $(document).ready(function() {
            $('#tipo_valvulasList').on('change', function() {

                var equipmentTypeID = $(this).val();
                if (equipmentTypeID) {
                    $.ajax({
                        url: '/api/ambits/prepared' + equipmentTypeID,
                        type: 'GET',
                        success: function(data) {
                            $('#AmbitsEquipments_list').empty(); // Limpar o select de ambits
                            $('#AmbitsEquipments_list').append(
                                '<option value="#" selected>Mostrar Todos</option>'
                            ); // Opção para mostrar todos
                            $('#AmbitsEquipments_list').append(
                                '<option value="" hidden>Selecionar Tipo de Âmbito...</option>'
                            );
                            $.each(data, function(key, value) {
                                $('#AmbitsEquipments_list').append('<option value="' +
                                    value.ambits_id + '">' + value
                                    .ambits_description + '</option>');
                            });
                        }
                    });
                } else {
                    $('#AmbitsEquipments_list')
                        .empty(); // Limpar o select de ambits se não há tipo de equipamento selecionado
                    $('#AmbitsEquipments_list').append(
                        '<option value="#" hidden>Mostrar Todos</option>');
                }
            });
        });
    </script>

    <script>
        $('#modal-showProgressEquipment').on('shown.bs.modal', function(event) {
            var button = $(event.relatedTarget); // Botão que acionou o modal
            var equipmentId = button.data(
                'equipment-id'); // Extrai o ID do equipamento dos atributos de dados do botão

            $.ajax({
                url: '/ReceiveEquipmentIdForShowModal/' + equipmentId,
                type: 'GET',
                success: function(data) {
                    // Aqui, você pode preencher os campos da modal com os dados retornados
                    $('#equipmentId').text(data.id);

                    var task_codes = data.task_codes;
                    createListItems(task_codes);
                    // console.log(data.receiveCommentsEquipment);

                    // Limpa o container que irá receber os novos elementos
                    $('#containerEquipmentComment').empty();

                    // Percorre os equipamentos recebidos
                    data.receveControlEquipment.forEach(function(equipment) {
                        // Cria o HTML para cada equipamento
                        var html = `
                            <div class="row">
                                <div class="col-sm-6">Workstation ${equipment.id_workstations}:</div>
                                <div class="col-sm-6">${equipment.entry_date}</div>
                            </div>
                            <div class="row">
                                <div class="col-sm-6">Elementary task(s):</div>
                                <div class="col-sm-6">${equipment.elemental_tasks_id}</div>
                            </div>
                            <div class="row">
                                <div class="col-sm-6">Exit Workstation ${equipment.id_workstations}:</div>
                                <div class="col-sm-6">${equipment.departure_date}</div>
                                <div class="col-sm-6"></div>
                                <div class="col-sm-6">
                                    <button class="btn btn-outline-primary" id="ShowCommetsEquipment" data-workstation-id='${equipment.id_workstations}' data-equipment-id='${equipment.equipment_id}'>Comments (0)</button>
                                </div>
                            </div>`;
                        
                        // Adiciona o HTML ao container
                        $('#containerEquipmentComment').append(html);
                    });

                    data.receiveCommentsEquipment.forEach(function(equipmentComment) {
                        var html = ` <div class="row">
                                        <div class="col-sm-6">Cometarios :</div>
                                        <div class="col-sm-6">${equipmentComment.comments}</div>
                                    </div>
                                    <div class="row">
                                        <div class="col-sm-6">Data :</div>
                                        <div class="col-sm-6">${equipmentComment.data}</div>
                                    </div>`;
                        // Adiciona o HTML ao container
                        $('#containerShowCommetsEquipment').append(html);
                    });
                }
            });
        });
        $('#modal-showProgressEquipment').on('hide.bs.modal', function(event) {
            $('#containerEquipmentComment').empty();
            $('#containerShowCommetsEquipment').empty();
        });
    </script>

    <script>
        function createListItems(task_codes) {
            // Limpar quaisquer itens de lista anteriores
            $('#myList').empty();

            // Criar e inserir itens de lista dinamicamente
            for (var i = 0; i < task_codes.length; i++) {
                var listItem = $('<li>', {
                    text: task_codes[i]
                });
                $('#myList').append(listItem);
            }
        }
    </script>

    <script>
        $(document).ready(function() {
            var steps = ['.res-step-one', '.res-step-two', '.res-step-three', '.res-step-four'];
            var i = 1;
            $(".res-step-form .res-btn-orange").click(function() {
                var getClass = $(this).attr('data-class');
                $(".res-steps").removeClass('active');
                $(steps[i]).addClass('active');
                i++;
                if (getClass != ".res-form-four") {
                    $(getClass).animate({
                        left: '-150%'
                    }, 500, function() {
                        $(getClass).css('left', '150%');
                    });
                    $(getClass).next().animate({
                        left: '0%'
                    }, 500, function() {
                        $(this).css('display', 'block');
                    });
                }
            });

            /* step back */
            $(".res-step-form .res-btn-gray").click(function() {
                var getClass = $(this).attr('data-class');
                $(".res-steps").removeClass('active');
                i--;
                $(steps[i - 1]).addClass('active');
                $(getClass).prev().css('left', '-150%')
                $(getClass).animate({
                    left: '150%'
                }, 500);
                $(getClass).prev().animate({
                    left: '0%'
                }, 500)
            });

            /* click from top bar steps */
            $('.res-step-one').click(function() {
                if (!$(this).hasClass('active')) {
                    $(".res-steps").removeClass('active');
                    i = 0;
                    $(steps[i]).addClass('active');
                    i++;
                    $('.res-form-one').css('left', '-150%');
                    $('.res-form-two, .res-form-three, .res-form-four').animate({
                        left: '150%'
                    }, 500);
                    $('.res-form-one').animate({
                        left: '0%'
                    }, 500);
                }
            });

            $('.res-step-two').click(function() {
                if (!$(this).hasClass('active')) {
                    $(".res-steps").removeClass('active');
                    i = 1;
                    $(steps[i]).addClass('active');
                    i++;
                    $('.res-form-two').css('left', '-150%');
                    $('.res-form-one, .res-form-three, .res-form-four').animate({
                        left: '150%'
                    }, 500);
                    $('.res-form-two').animate({
                        left: '0%'
                    }, 500);
                }
            });

            $('.res-step-three').click(function() {
                if (!$(this).hasClass('active')) {
                    $(".res-steps").removeClass('active');
                    i = 2;
                    $(steps[i]).addClass('active');
                    i++;
                    $('.res-form-three').css('left', '-150%');
                    $('.res-form-one, .res-form-two, .res-form-four').animate({
                        left: '150%'
                    }, 500);
                    $('.res-form-three').animate({
                        left: '0%'
                    }, 500);
                }
            });

            $('.res-step-four').click(function() {
                if (!$(this).hasClass('active')) {
                    $(".res-steps").removeClass('active');
                    i = 3;
                    $(steps[i]).addClass('active');
                    i++;
                    $('.res-form-four').css('left', '-150%');
                    $('.res-form-one, .res-form-two, .res-form-four').animate({
                        left: '150%'
                    }, 500);
                    $('.res-form-four').animate({
                        left: '0%'
                    }, 500);
                }
            });
        });
    </script>
@endsection
