<?php
    use App\Models\LicitacoesProcedimentos\Processo;

    $tipoProcessoSelecionado = old('tipo_processo', $registro->tipo_processo ?? '');
    $cdTipoSelecionado = (string) old('cd_tipo', $registro->cd_tipo ?? '');
    $tiposLicitacaoJson = json_encode(
        collect(Processo::TIPOS_LICITACAO)->mapWithKeys(fn (string $label, int $codigo) => [$codigo => __($label)])->all(),
        JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE
    );
    $tiposProcedimentoJson = json_encode(
        collect(Processo::TIPOS_PROCEDIMENTO)->mapWithKeys(fn (string $label, int $codigo) => [$codigo => __($label)])->all(),
        JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE
    );
?>

<?php echo csrf_field(); ?>

<?php if(isset($registro)): ?>
    <?php echo method_field('put'); ?>
<?php endif; ?>

<?php $__currentLoopData = ['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $filterKey): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
    <?php if(request()->filled($filterKey)): ?>
        <input type="hidden" name="<?php echo e($filterKey); ?>" value="<?php echo e(request($filterKey)); ?>">
    <?php endif; ?>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

<div class="card border-0 shadow rounded-4">
    <div class="card-body p-4">

        <h5 class="mb-4 fw-semibold">
            <i class="fa-solid fa-file-lines me-2"></i>
            <?php echo e(__('Dados do Processo')); ?>

        </h5>

        <div class="row g-4">

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary" for="lp-tipo-processo"><?php echo e(__('Tipo de processo:')); ?></label>
                <select class="form-select shadow-sm" name="tipo_processo" id="lp-tipo-processo" required>
                    <option value=""><?php echo e(__('Selecione')); ?></option>
                    <option value="licitacao" <?php if($tipoProcessoSelecionado === 'licitacao'): echo 'selected'; endif; ?>><?php echo e(__('Licitação')); ?></option>
                    <option value="procedimento" <?php if($tipoProcessoSelecionado === 'procedimento'): echo 'selected'; endif; ?>><?php echo e(__('Procedimento')); ?></option>
                </select>
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary" for="lp-cd-tipo"><?php echo e(__('Tipo:')); ?></label>
                <select class="form-select shadow-sm" name="cd_tipo" id="lp-cd-tipo" required
                    data-tipos-licitacao="<?php echo e($tiposLicitacaoJson); ?>"
                    data-tipos-procedimento="<?php echo e($tiposProcedimentoJson); ?>"
                    data-cd-tipo-selecionado="<?php echo e($cdTipoSelecionado); ?>"
                    data-selecione-label="<?php echo e(__('Selecione')); ?>">
                    <option value=""><?php echo e(__('Selecione')); ?></option>
                    <?php
                        $tiposAtuais = $tipoProcessoSelecionado === 'procedimento'
                            ? Processo::TIPOS_PROCEDIMENTO
                            : ($tipoProcessoSelecionado === 'licitacao' ? Processo::TIPOS_LICITACAO : []);
                    ?>
                    <?php $__currentLoopData = $tiposAtuais; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $codigo => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($codigo); ?>" <?php if($cdTipoSelecionado === (string) $codigo): echo 'selected'; endif; ?>><?php echo e(__($label)); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary"><?php echo e(__('Número da Licitação:')); ?></label>
                <input type="text" maxlength="30" name="nr_licitacao" class="form-control shadow-sm" value="<?php echo e(old('nr_licitacao', $registro->nr_licitacao ?? '')); ?>" required>
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary"><?php echo e(__('Nº do processo:')); ?></label>
                <input type="text" maxlength="30" name="nr_processo" class="form-control shadow-sm" value="<?php echo e(old('nr_processo', $registro->nr_processo ?? '')); ?>">
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary"><?php echo e(__('Nº do Memorando:')); ?></label>
                <input type="text" maxlength="30" name="nr_memorando" class="form-control shadow-sm" value="<?php echo e(old('nr_memorando', $registro->nr_memorando ?? '')); ?>">
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary" for="lp-id-setor">
                    <?php echo e(__('Setor:')); ?>

                </label>
                <?php
                    $idSetorSelecionado = (string) old('id_setor', $registro->id_setor ?? '');
                    $setoresLista = collect($secretarias ?? []);
                    $setoresPorTipo = [
                        'SECRETARIA' => $setoresLista->where('type', 'SECRETARIA'),
                        'SECRETARIA_EXECUTIVA' => $setoresLista->where('type', 'SECRETARIA_EXECUTIVA'),
                        'AUTARQUIA_FUNDACAO' => $setoresLista->whereIn('type', ['AUTARQUIA', 'FUNDACAO']),
                    ];
                    $labelsGrupo = [
                        'SECRETARIA' => __('Secretarias'),
                        'SECRETARIA_EXECUTIVA' => __('Secretarias Executivas'),
                        'AUTARQUIA_FUNDACAO' => __('Autarquias e Fundações'),
                    ];
                    $idsDisponiveis = $setoresLista->pluck('id')->map(fn ($id) => (string) $id);
                    $opcaoLegada = ($idSetorSelecionado !== '' && !$idsDisponiveis->contains($idSetorSelecionado))
                        ? Processo::getSetorOptionById($idSetorSelecionado)
                        : null;
                ?>
                <select class="form-select shadow-sm" name="id_setor" id="lp-id-setor" required>
                    <option value=""><?php echo e(__('Selecione')); ?></option>
                    <?php if($opcaoLegada): ?>
                        <option value="<?php echo e($opcaoLegada['value']); ?>" <?php if($idSetorSelecionado === (string) $opcaoLegada['value']): echo 'selected'; endif; ?>>
                            <?php echo e(__($opcaoLegada['label'])); ?>

                        </option>
                    <?php endif; ?>
                    <?php $__currentLoopData = $labelsGrupo; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $grupoKey => $grupoLabel): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php $itensGrupo = $setoresPorTipo[$grupoKey] ?? collect(); ?>
                        <?php if($itensGrupo->isNotEmpty()): ?>
                            <optgroup label="<?php echo e($grupoLabel); ?>">
                                <?php $__currentLoopData = $itensGrupo; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $setor): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <?php
                                        $setorLabel = $setor->name;
                                        if ($setor->abbreviation) {
                                            $setorLabel .= ' (' . $setor->abbreviation . ')';
                                        }
                                        if (($setor->status ?? '') !== 'ATIVADO') {
                                            $setorLabel .= ' — ' . __('Inativo');
                                        }
                                    ?>
                                    <option value="<?php echo e($setor->id); ?>" <?php if($idSetorSelecionado === (string) $setor->id): echo 'selected'; endif; ?>>
                                        <?php echo e($setorLabel); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </optgroup>
                        <?php endif; ?>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary"><?php echo e(__('Data:')); ?></label>
                <input type="date" name="dt_data" class="form-control shadow-sm" value="<?php echo e(old('dt_data', isset($registro->dt_data) ? $registro->dt_data->format('Y-m-d') : '')); ?>" required>
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary"><?php echo e(__('Hora:')); ?></label>
                <input type="time" name="dt_hora" class="form-control shadow-sm rounded-3" value="<?php echo e(old('dt_hora', $registro->dt_hora ?? '')); ?>">
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary"><?php echo e(__('Data de encerramento:')); ?></label>
                <input type="date" name="dt_encerramento" class="form-control shadow-sm" value="<?php echo e(old('dt_encerramento', isset($registro->dt_encerramento) ? $registro->dt_encerramento->format('Y-m-d') : '')); ?>">
            </div>

            <div class="col-md-3">
                <label class="form-label fw-semibold text-secondary"><?php echo e(__('Status:')); ?></label>
                <select class="form-select shadow-sm" name="cd_status" required>
                    <option value=""><?php echo e(__('Selecione')); ?></option>

                    <option value="1" <?php if((string) old('cd_status', $registro->cd_status ?? '') === '1'): echo 'selected'; endif; ?>><?php echo e(__('Em Andamento / Licitado')); ?></option>
                    <option value="2" <?php if((string) old('cd_status', $registro->cd_status ?? '') === '2'): echo 'selected'; endif; ?>><?php echo e(__('Cancelado')); ?></option>
                    <option value="3" <?php if((string) old('cd_status', $registro->cd_status ?? '') === '3'): echo 'selected'; endif; ?>><?php echo e(__('Suspenso')); ?></option>
                    <option value="4" <?php if((string) old('cd_status', $registro->cd_status ?? '') === '4'): echo 'selected'; endif; ?>><?php echo e(__('Aguardando')); ?></option>
                    <option value="5" <?php if((string) old('cd_status', $registro->cd_status ?? '') === '5'): echo 'selected'; endif; ?>><?php echo e(__('Não Homologado(a)')); ?></option>
                    <option value="6" <?php if((string) old('cd_status', $registro->cd_status ?? '') === '6'): echo 'selected'; endif; ?>><?php echo e(__('Homologado(a)')); ?></option>
                    <option value="7" <?php if((string) old('cd_status', $registro->cd_status ?? '') === '7'): echo 'selected'; endif; ?>><?php echo e(__('Fracassado')); ?></option>
                </select>
            </div>

            <div class="col-12">
                <label class="form-label fw-semibold text-secondary"><?php echo e(__('Objeto:')); ?></label>
                <textarea name="ds_objeto" rows="4" class="form-control shadow-sm" required><?php echo e(old('ds_objeto', $registro->ds_objeto ?? '')); ?></textarea>
            </div>

        </div>

        <div class="d-flex justify-content-end mt-4">
            <button class="btn btn-success px-4 py-2 shadow-sm">
                <i class="fa-solid fa-check me-2"></i>
                <?php echo e(__('Salvar')); ?>

            </button>
        </div>

    </div>
</div>


<?php /**PATH /var/www/html/portal/resources/views/admin/licitacoesProcedimentos/_form.blade.php ENDPATH**/ ?>