<?php
    $leg = $legislacao ?? null;
?>

<div class="row g-3">

    
    <div class="col-12">
        <div class="card border-0 shadow-sm rounded-3">
            <div class="card-header bg-white border-0 py-3">
                <h5 class="mb-0 fw-bold text-dark">
                    <i class="fa-solid fa-scale-balanced me-2 text-primary"></i>
                    <?php echo e(__('Dados da legislação')); ?>

                </h5>
            </div>

            <div class="card-body pt-0">
                <div class="row g-3">

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Tipo de legislação')); ?>

                        </label>

                        <select name="id_tipo_legislacao" class="form-select rounded-3" required>
                            <option value=""><?php echo e(__('Selecione')); ?></option>

                            <?php $__currentLoopData = $tipos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tipo): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($tipo->id_tipo_legislacao); ?>"
                                    <?php if((int) old('id_tipo_legislacao', $leg->id_tipo_legislacao ?? 0) === (int) $tipo->id_tipo_legislacao): echo 'selected'; endif; ?>>
                                    <?php echo e($tipo->ds_tipo_legislacao); ?>

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

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Nº da legislação')); ?>

                        </label>

                        <input
                            type="text"
                            name="nr_legislacao"
                            class="form-control rounded-3"
                            required
                            maxlength="60"
                            value="<?php echo e(old('nr_legislacao', $leg->nr_legislacao ?? '')); ?>">
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Data de assinatura')); ?>

                        </label>

                        <input
                            type="date"
                            name="dt_assinatura"
                            class="form-control rounded-3"
                            value="<?php echo e(old('dt_assinatura', $leg && $leg->dt_assinatura ? $leg->dt_assinatura->format('Y-m-d') : '')); ?>">
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Data de publicação')); ?>

                        </label>

                        <input
                            type="date"
                            name="dt_publicacao"
                            id="legislacao_dt_publicacao"
                            class="form-control rounded-3"
                            value="<?php echo e(old('dt_publicacao', $leg && $leg->dt_publicacao ? $leg->dt_publicacao->format('Y-m-d') : '')); ?>">
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Autor da lei / cargo')); ?>

                        </label>

                        <select name="id_autor" class="form-select rounded-3">
                            <option value=""><?php echo e(__('Selecione')); ?></option>

                            <?php $__currentLoopData = $autores; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $autor): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($autor->id_autor); ?>"
                                    <?php if((int) old('id_autor', $leg->id_autor ?? 0) === (int) $autor->id_autor): echo 'selected'; endif; ?>>
                                    <?php echo e($autor->autor_com_cargo); ?>

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

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Sancionado por (prefeito/prefeito em exercício)')); ?>

                        </label>

                        <select name="id_autor_sancao" class="form-select rounded-3">
                            <option value=""><?php echo e(__('Selecione')); ?></option>

                            <?php $__currentLoopData = $autoresSancao; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $autor): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($autor->id_autor); ?>"
                                    <?php if((int) old('id_autor_sancao', $leg->id_autor_sancao ?? 0) === (int) $autor->id_autor): echo 'selected'; endif; ?>>
                                    <?php echo e($autor->autor_com_cargo); ?>

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

                    <div class="col-12">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Ementa / objeto')); ?>

                        </label>

                        <textarea
                            name="ds_ementa"
                            class="form-control rounded-3"
                            rows="4"
                            required><?php echo e(old('ds_ementa', $leg->ds_ementa ?? '')); ?></textarea>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Nº do BO de publicação')); ?>

                        </label>

                        <?php
                            $boSelId = old('id_boletim_oficial', $leg->id_boletim_oficial ?? '');
                            $boSelLabel = $leg && $leg->boletim ? $leg->boletim->nr_bo : '';
                        ?>

                        <select
                            name="id_boletim_oficial"
                            id="legislacao_id_boletim"
                            class="form-select rounded-3 js-legislacao-boletim-api"
                            data-api-url="<?php echo e(route('api.boletinsOficiais')); ?>"
                            data-placeholder="<?php echo e(__('Selecione')); ?>"
                            data-selected-id="<?php echo e($boSelId); ?>"
                            data-selected-label="<?php echo e(e($boSelLabel)); ?>">

                            <option value=""><?php echo e(__('Selecione')); ?></option>
                        </select>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Data do BO')); ?>

                        </label>

                        <input
                            type="text"
                            id="legislacao_dt_bo_display"
                            class="form-control rounded-3 bg-light"
                            readonly
                            value="<?php echo e($leg && $leg->boletim && $leg->boletim->dt_publicacao ? $leg->boletim->dt_publicacao->format('d/m/Y') : ''); ?>">
                    </div>

                    <div class="col-md-3 d-flex align-items-end">
                        <a
                            href="<?php echo e($leg && $leg->boletim?->public_bo_pdf_url ? $leg->boletim->public_bo_pdf_url : '#'); ?>"
                            target="_blank"
                            rel="noopener noreferrer"
                            id="legislacao_link_bo"
                            class="btn btn-outline-primary rounded-3 w-100 <?php if(!($leg && $leg->boletim?->public_bo_pdf_url)): ?> disabled <?php endif; ?>">

                            <i class="fa-solid fa-file-pdf me-2"></i>
                            <?php echo e(__('Link do BO')); ?>

                        </a>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Status')); ?>

                        </label>

                        <select name="cd_status" class="form-select rounded-3" required>
                            <option value="1" <?php if((int) old('cd_status', $leg->cd_status ?? 1) === 1): echo 'selected'; endif; ?>>
                                <?php echo e(__('Aguardando revisão')); ?>

                            </option>

                            <option value="2" <?php if((int) old('cd_status', $leg->cd_status ?? 1) === 2): echo 'selected'; endif; ?>>
                                <?php echo e(__('Pendente')); ?>

                            </option>

                            <option value="3" <?php if((int) old('cd_status', $leg->cd_status ?? 1) === 3): echo 'selected'; endif; ?>>
                                <?php echo e(__('Publicado')); ?>

                            </option>
                        </select>
                    </div>

                    <?php if($leg && (int) $leg->cd_status === 3): ?>
                        <div class="col-md-6 d-flex align-items-end">
                            <a
                                href="<?php echo e(route('paginas.legislacao.detalhe', $leg->id_legislacao)); ?>"
                                target="_blank"
                                rel="noopener noreferrer"
                                class="btn btn-outline-dark rounded-3 w-100">

                                <i class="fa-solid fa-globe me-2"></i>
                                <?php echo e(__('Visualizar no portal')); ?>

                            </a>
                        </div>
                    <?php endif; ?>

                </div>
            </div>
        </div>
    </div>

    
    <div class="col-12">
        <div class="card border-0 shadow-sm rounded-3">
            <div class="card-header bg-white border-0 py-3">
                <h5 class="mb-0 fw-bold">
                    <i class="fa-solid fa-file-pdf me-2 text-danger"></i>
                    <?php echo e(__('Documento em PDF')); ?>

                </h5>
            </div>

            <div class="card-body pt-0">
                <div class="row g-3 align-items-end">

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Enviar novo PDF')); ?>

                        </label>

                        <input
                            type="file"
                            name="pdf_arquivo"
                            class="form-control rounded-3"
                            accept=".pdf,application/pdf">
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Arquivo atual')); ?>

                        </label>

                        <input
                            type="text"
                            name="nm_arquivo_anexo"
                            class="form-control rounded-3 bg-light"
                            readonly
                            value="<?php echo e(old('nm_arquivo_anexo', $leg->nm_arquivo_anexo ?? '')); ?>">
                    </div>

                    <?php if($leg && $leg->public_pdf_url): ?>
                        <div class="col-12">
                            <a
                                href="<?php echo e($leg->public_pdf_url); ?>"
                                target="_blank"
                                rel="noopener noreferrer"
                                class="btn btn-outline-danger rounded-3">

                                <i class="fa-solid fa-file-pdf me-2"></i>
                                <?php echo e(__('Abrir PDF cadastrado')); ?>

                            </a>
                        </div>
                    <?php endif; ?>

                </div>
            </div>
        </div>
    </div>

    
    <div class="col-lg-6">
        <div class="card border-0 shadow-sm rounded-3 h-100">
            <div class="card-header bg-white border-0 py-3 d-flex align-items-center justify-content-between">
                <h5 class="mb-0 fw-bold">
                    <i class="fa-solid fa-tags me-2 text-info"></i>
                    <?php echo e(__('Categorias relacionadas')); ?>

                </h5>
                <?php
                    $selectedCategorias = old('categorias', isset($leg) ? $leg->categorias->pluck('id_categoria')->toArray() : []);
                    $totalSelecionadas = count(array_filter(array_map('intval', $selectedCategorias)));
                ?>
                <?php if($totalSelecionadas > 0): ?>
                    <span class="badge bg-info text-white fs-6 rounded-pill" id="legislacao_categorias_badge">
                        <?php echo e($totalSelecionadas); ?>

                    </span>
                <?php else: ?>
                    <span class="badge bg-secondary text-white fs-6 rounded-pill" id="legislacao_categorias_badge">0</span>
                <?php endif; ?>
            </div>

            <div class="card-body pt-2">
                <div class="legislacao-categorias-ts-host">
                    <select
                        id="legislacao_categorias_select"
                        name="categorias[]"
                        multiple
                        autocomplete="off"
                        class="form-select"
                        data-placeholder="<?php echo e(__('Buscar categoria…')); ?>"
                        aria-describedby="legislacao_categorias_ajuda">

                        <?php $__currentLoopData = $categorias; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $categoria): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <?php $cid = (int) $categoria->id_categoria; ?>
                            <option value="<?php echo e($cid); ?>" <?php if(in_array($cid, array_map('intval', $selectedCategorias), true)): echo 'selected'; endif; ?>>
                                <?php echo e($categoria->nm_categoria); ?>

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

                <div id="legislacao_categorias_ajuda" class="form-text mt-2">
                    <i class="fa-solid fa-circle-info me-1 text-info"></i>
                    <?php echo e(__('Clique para abrir a lista. Selecione quantas categorias precisar.')); ?>

                </div>
            </div>
        </div>
    </div>

    
    <div class="col-lg-6">
        <div class="card border-0 shadow-sm rounded-3 h-100">
            <div class="card-header bg-white border-0 py-3">
                <h5 class="mb-0 fw-bold">
                    <i class="fa-solid fa-align-left me-2 text-success"></i>
                    <?php echo e(__('Texto compilado')); ?>

                </h5>
            </div>

            <div class="card-body pt-0">
                <textarea
                    name="ds_legislacao_sem_html"
                    class="form-control rounded-3 h-100"
                    rows="10"
                    required><?php echo e(old('ds_legislacao_sem_html', $leg->ds_legislacao_sem_html ?? '')); ?></textarea>
            </div>
        </div>
    </div>

    
    <div class="col-12">
        <div class="card border-0 shadow-sm rounded-3">
            <div class="card-header bg-white border-0 py-3">
                <h5 class="mb-0 fw-bold">
                    <i class="fa-solid fa-link me-2 text-info"></i>
                    <?php echo e(__('Relacionamentos de legislações')); ?>

                </h5>
            </div>

            <div class="card-body pt-0">

                <div class="row g-3">

                    <div class="col-md-3">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Ação')); ?>

                        </label>

                        <select id="legislacao_sel_acao" class="form-select rounded-3">
                            <option value=""><?php echo e(__('Selecione')); ?></option>
                            <option value="1"><?php echo e(__('Altera')); ?></option>
                            <option value="2"><?php echo e(__('Revoga')); ?></option>
                            <option value="3"><?php echo e(__('Anula')); ?></option>
                            <option value="4"><?php echo e(__('Promulga')); ?></option>
                        </select>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold">
                            <?php echo e(__('Legislação')); ?>

                        </label>

                        <select id="legislacao_sel_alvo" class="form-select rounded-3">
                            <option value=""><?php echo e(__('Selecione')); ?></option>

                            <?php $__currentLoopData = $legislacaoVinculos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $opt): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php if(!$leg || (int) $opt->id_legislacao !== (int) $leg->id_legislacao): ?>
                                    <option value="<?php echo e($opt->id_legislacao); ?>"
                                        data-mes="<?php echo e($opt->dt_publicacao ? $opt->dt_publicacao->translatedFormat('d \d\e F \d\e Y') : ''); ?>">

                                        <?php echo e(($opt->tipo->ds_tipo_legislacao ?? '') . ' ' . $opt->nr_legislacao); ?>

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

                    <div class="col-md-3 d-flex align-items-end">
                        <button
                            type="button"
                            class="btn btn-primary rounded-3 w-100"
                            id="legislacao_btn_vincular">

                            <i class="fa-solid fa-plus me-2"></i>
                            <?php echo e(__('Vincular')); ?>

                        </button>
                    </div>

                </div>

                <div id="legislacao_acoes_list" class="mt-4"></div>

                <input type="hidden" name="acoes_json" id="legislacao_acoes_json" value="">

                <table class="d-none" aria-hidden="true">
                    <tbody id="legislacao_acoes_semente">
                        <?php $__currentLoopData = ($leg?->acoes ?? collect()); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $acao): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <tr
                                data-cd="<?php echo e($acao->cd_acao); ?>"
                                data-alvo="<?php echo e($acao->id_legislacao_sofre_acao); ?>"
                                class="acao-semente"></tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>
                </table>

            </div>
        </div>
    </div>

    
    <div class="col-12">
        <div class="d-flex justify-content-end">
            <button class="btn btn-success px-5 rounded-3 shadow-sm" type="submit">
                <i class="fa-solid fa-floppy-disk me-2"></i>
                <?php echo e(__('Salvar legislação')); ?>

            </button>
        </div>
    </div>

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