<?php

declare(strict_types=1);

$form = $form ?? [];
$sections = $sections ?? [];

ob_start();

$formId = (int) (
    $form['id']
    ?? 0
);

$formStatus = (string) (
    $form['status']
    ?? 'draft'
);
?>

<style>
.form-designer-page {
    --designer-border: #e5eaf0;
    --designer-muted: #718096;
    --designer-bg: #f5f7fa;
}

.designer-topbar {
    background: #ffffff;
    border: 1px solid var(--designer-border);
    border-radius: 16px;
    padding: 18px 20px;
    margin-bottom: 20px;
}

.designer-code {
    display: inline-flex;
    align-items: center;
    padding: 5px 9px;
    border-radius: 7px;
    background: #f1f5f9;
    font-family: monospace;
    font-size: 12px;
    font-weight: 700;
}

.designer-status {
    display: inline-flex;
    align-items: center;
    padding: 5px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
}

.designer-status-draft {
    background: rgba(255, 193, 7, 0.16);
    color: #8a6700;
}

.designer-status-published {
    background: rgba(25, 135, 84, 0.12);
    color: #198754;
}

.designer-status-archived {
    background: #edf1f5;
    color: #667085;
}

.designer-panel {
    background: #ffffff;
    border: 1px solid var(--designer-border);
    border-radius: 16px;
    overflow: hidden;
}

.designer-panel-header {
    padding: 16px 18px;
    border-bottom: 1px solid var(--designer-border);
}

.designer-panel-title {
    font-size: 14px;
    font-weight: 700;
    margin: 0;
}

.designer-panel-subtitle {
    font-size: 12px;
    color: var(--designer-muted);
    margin-top: 3px;
}

.toolbox-body {
    padding: 14px;
}

.toolbox-group-title {
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.7px;
    color: #8a96a6;
    text-transform: uppercase;
    margin: 8px 4px 10px;
}

.toolbox-item {
    width: 100%;
    min-height: 44px;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    margin-bottom: 8px;
    border: 1px solid #e5eaf0;
    border-radius: 10px;
    background: #ffffff;
    color: #344054;
    font-size: 13px;
    text-align: left;
    cursor: pointer;
    transition:
        transform 0.15s ease,
        border-color 0.15s ease,
        box-shadow 0.15s ease;
}

.toolbox-item:hover {
    transform: translateY(-1px);
    border-color: #b8c8df;
    box-shadow: 0 6px 18px rgba(20, 35, 60, 0.06);
}

.toolbox-item i {
    width: 26px;
    height: 26px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 7px;
    background: rgba(13, 110, 253, 0.08);
    color: var(--bs-primary);
}

.canvas-shell {
    background: var(--designer-bg);
    min-height: 680px;
    padding: 22px;
}

.form-canvas {
    max-width: 850px;
    margin: 0 auto;
}

.canvas-form-header {
    background: #ffffff;
    border: 1px solid var(--designer-border);
    border-radius: 14px;
    padding: 22px;
    margin-bottom: 16px;
}

.canvas-section {
    background: #ffffff;
    border: 1px solid var(--designer-border);
    border-radius: 14px;
    margin-bottom: 16px;
    overflow: hidden;
}

.canvas-section-header {
    padding: 16px 18px;
    border-bottom: 1px solid #edf1f5;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 15px;
}

.section-order {
    width: 30px;
    height: 30px;
    flex: 0 0 30px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    background: #f1f5f9;
    font-size: 12px;
    font-weight: 700;
}

.section-title {
    font-size: 15px;
    font-weight: 700;
    color: #1f2937;
}

.section-description {
    font-size: 12px;
    color: var(--designer-muted);
    margin-top: 3px;
}

.section-empty {
    padding: 28px 18px;
    text-align: center;
    color: #98a2b3;
    font-size: 13px;
}

.empty-canvas {
    background: #ffffff;
    border: 2px dashed #d8e0e9;
    border-radius: 14px;
    padding: 55px 25px;
    text-align: center;
}

.empty-canvas-icon {
    width: 64px;
    height: 64px;
    margin: 0 auto 14px;
    border-radius: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(13, 110, 253, 0.08);
    color: var(--bs-primary);
    font-size: 28px;
}

.properties-empty {
    padding: 35px 20px;
    text-align: center;
    color: #98a2b3;
}

.properties-empty i {
    display: block;
    font-size: 30px;
    margin-bottom: 10px;
}

.section-action-group {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.section-action-group .btn {
    width: 32px;
    height: 32px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
}

.designer-column-sticky {
    position: sticky;
    top: 20px;
}

.modal-content {
    border: 0;
    border-radius: 16px;
}

.modal-header,
.modal-footer {
    border-color: #edf1f5;
}

@media (max-width: 1199.98px) {
    .designer-column-sticky {
        position: static;
    }
}
</style>


<div class="container-fluid py-4 form-designer-page">

    <div class="designer-topbar">

        <div class="row align-items-center g-3">

            <div class="col-lg-8">

                <div class="d-flex align-items-center gap-3">

                    <a
                        href="/public/dynamic-forms"
                        class="btn btn-light">

                        <i class="bi bi-arrow-left"></i>

                    </a>

                    <div>

                        <div class="d-flex align-items-center gap-2 mb-1">

                            <span class="designer-code">

                                <?= htmlspecialchars(
                                    (string) (
                                        $form['form_code']
                                        ?? ''
                                    ),
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>

                            </span>

                            <span class="designer-status designer-status-<?= htmlspecialchars(
                                $formStatus,
                                ENT_QUOTES,
                                'UTF-8'
                            ) ?>">

                                <?= ucfirst(
                                    htmlspecialchars(
                                        $formStatus,
                                        ENT_QUOTES,
                                        'UTF-8'
                                    )
                                ) ?>

                            </span>

                        </div>

                        <h3 class="mb-1">

                            <?= htmlspecialchars(
                                (string) (
                                    $form['form_name']
                                    ?? 'Form Designer'
                                ),
                                ENT_QUOTES,
                                'UTF-8'
                            ) ?>

                        </h3>

                        <div class="text-muted small">
                            Build sections and fields for this form.
                        </div>

                    </div>

                </div>

            </div>


            <div class="col-lg-4 text-lg-end">

                <button
                    type="button"
                    class="btn btn-primary"
                    data-bs-toggle="modal"
                    data-bs-target="#addSectionModal">

                    <i class="bi bi-plus-lg me-1"></i>
                    Add Section

                </button>

            </div>

        </div>

    </div>


    <?php if (!empty($sectionCreated)): ?>

        <div class="alert alert-success alert-dismissible fade show">
            Section created successfully.

            <button
                type="button"
                class="btn-close"
                data-bs-dismiss="alert">
            </button>
        </div>

    <?php endif; ?>


    <?php if (!empty($sectionUpdated)): ?>

        <div class="alert alert-success alert-dismissible fade show">
            Section updated successfully.

            <button
                type="button"
                class="btn-close"
                data-bs-dismiss="alert">
            </button>
        </div>

    <?php endif; ?>


    <?php if (!empty($error)): ?>

        <div class="alert alert-danger alert-dismissible fade show">

            <?= htmlspecialchars(
                (string) $error,
                ENT_QUOTES,
                'UTF-8'
            ) ?>

            <button
                type="button"
                class="btn-close"
                data-bs-dismiss="alert">
            </button>

        </div>

    <?php endif; ?>


    <div class="row g-4">

        <!-- FIELD TOOLBOX -->

        <div class="col-xl-2 col-lg-3">

            <div class="designer-column-sticky">

                <div class="designer-panel">

                    <div class="designer-panel-header">

                        <h6 class="designer-panel-title">
                            Field Toolbox
                        </h6>

                        <div class="designer-panel-subtitle">
                            Field insertion will be connected next.
                        </div>

                    </div>


                    <div class="toolbox-body">

                        <div class="toolbox-group-title">
                            Basic Fields
                        </div>

                        <button type="button" class="toolbox-item">
                            <i class="bi bi-input-cursor-text"></i>
                            Text
                        </button>

                        <button type="button" class="toolbox-item">
                            <i class="bi bi-textarea-t"></i>
                            Textarea
                        </button>

                        <button type="button" class="toolbox-item">
                            <i class="bi bi-123"></i>
                            Number
                        </button>

                        <button type="button" class="toolbox-item">
                            <i class="bi bi-calendar3"></i>
                            Date
                        </button>

                        <button type="button" class="toolbox-item">
                            <i class="bi bi-clock"></i>
                            Time
                        </button>


                        <div class="toolbox-group-title mt-4">
                            Choice Fields
                        </div>

                        <button type="button" class="toolbox-item">
                            <i class="bi bi-menu-button-wide"></i>
                            Select
                        </button>

                        <button type="button" class="toolbox-item">
                            <i class="bi bi-record-circle"></i>
                            Radio
                        </button>

                        <button type="button" class="toolbox-item">
                            <i class="bi bi-check2-square"></i>
                            Checkbox
                        </button>


                        <div class="toolbox-group-title mt-4">
                            Advanced
                        </div>

                        <button type="button" class="toolbox-item">
                            <i class="bi bi-envelope"></i>
                            Email
                        </button>

                        <button type="button" class="toolbox-item">
                            <i class="bi bi-telephone"></i>
                            Phone
                        </button>

                        <button type="button" class="toolbox-item">
                            <i class="bi bi-paperclip"></i>
                            File Upload
                        </button>

                    </div>

                </div>

            </div>

        </div>


        <!-- FORM CANVAS -->

        <div class="col-xl-7 col-lg-9">

            <div class="designer-panel">

                <div class="designer-panel-header">

                    <h6 class="designer-panel-title">
                        Form Canvas
                    </h6>

                    <div class="designer-panel-subtitle">
                        Form sections appear here in display order.
                    </div>

                </div>


                <div class="canvas-shell">

                    <div class="form-canvas">

                        <div class="canvas-form-header">

                            <h4 class="mb-2">

                                <?= htmlspecialchars(
                                    (string) (
                                        $form['form_name']
                                        ?? ''
                                    ),
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>

                            </h4>

                            <?php if (
                                !empty(
                                    $form['description']
                                )
                            ): ?>

                                <div class="text-muted">

                                    <?= htmlspecialchars(
                                        (string) $form['description'],
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>

                                </div>

                            <?php endif; ?>

                        </div>


                        <?php if (empty($sections)): ?>

                            <div class="empty-canvas">

                                <div class="empty-canvas-icon">
                                    <i class="bi bi-layout-text-window-reverse"></i>
                                </div>

                                <h5>
                                    Start with a section
                                </h5>

                                <p class="text-muted">
                                    Sections organize related fields
                                    inside your dynamic form.
                                </p>

                                <button
                                    type="button"
                                    class="btn btn-primary"
                                    data-bs-toggle="modal"
                                    data-bs-target="#addSectionModal">

                                    <i class="bi bi-plus-lg me-1"></i>
                                    Add First Section

                                </button>

                            </div>

                        <?php else: ?>

                            <?php foreach (
                                $sections as $index => $section
                            ): ?>

                                <div class="canvas-section">

                                    <div class="canvas-section-header">

                                        <div class="d-flex gap-3">

                                            <div class="section-order">
                                                <?= $index + 1 ?>
                                            </div>

                                            <div>

                                                <div class="section-title">

                                                    <?= htmlspecialchars(
                                                        (string) (
                                                            $section[
                                                                'section_title'
                                                            ]
                                                            ?? ''
                                                        ),
                                                        ENT_QUOTES,
                                                        'UTF-8'
                                                    ) ?>

                                                </div>

                                                <?php if (
                                                    !empty(
                                                        $section[
                                                            'section_description'
                                                        ]
                                                    )
                                                ): ?>

                                                    <div class="section-description">

                                                        <?= htmlspecialchars(
                                                            (string) $section[
                                                                'section_description'
                                                            ],
                                                            ENT_QUOTES,
                                                            'UTF-8'
                                                        ) ?>

                                                    </div>

                                                <?php endif; ?>

                                            </div>

                                        </div>


                                        <div class="section-action-group">

                                            <button
                                                type="button"
                                                class="btn btn-outline-warning edit-section-btn"
                                                title="Edit Section"
                                                data-bs-toggle="modal"
                                                data-bs-target="#editSectionModal"
                                                data-id="<?= (int) $section['id'] ?>"
                                                data-title="<?= htmlspecialchars(
                                                    (string) (
                                                        $section['section_title']
                                                        ?? ''
                                                    ),
                                                    ENT_QUOTES,
                                                    'UTF-8'
                                                ) ?>"
                                                data-description="<?= htmlspecialchars(
                                                    (string) (
                                                        $section['section_description']
                                                        ?? ''
                                                    ),
                                                    ENT_QUOTES,
                                                    'UTF-8'
                                                ) ?>">

                                                <i class="bi bi-pencil"></i>

                                            </button>

                                        </div>

                                    </div>


                                    <div class="section-empty">

                                        <i class="bi bi-plus-circle me-1"></i>

                                        No fields in this section yet.

                                    </div>

                                </div>

                            <?php endforeach; ?>

                        <?php endif; ?>

                    </div>

                </div>

            </div>

        </div>


        <!-- PROPERTIES PANEL -->

        <div class="col-xl-3">

            <div class="designer-column-sticky">

                <div class="designer-panel">

                    <div class="designer-panel-header">

                        <h6 class="designer-panel-title">
                            Properties
                        </h6>

                        <div class="designer-panel-subtitle">
                            Configure the selected field.
                        </div>

                    </div>


                    <div class="properties-empty">

                        <i class="bi bi-sliders"></i>

                        Select a field from the canvas
                        to edit its properties.

                    </div>

                </div>

            </div>

        </div>

    </div>

</div>


<!-- ADD SECTION MODAL -->

<div
    class="modal fade"
    id="addSectionModal"
    tabindex="-1"
    aria-hidden="true">

    <div class="modal-dialog modal-lg modal-dialog-centered">

        <div class="modal-content">

            <form
                method="POST"
                action="/public/dynamic-forms/sections/create">

                <input
                    type="hidden"
                    name="form_id"
                    value="<?= $formId ?>">


                <div class="modal-header">

                    <div>
                        <h5 class="modal-title">
                            Add Form Section
                        </h5>

                        <div class="small text-muted">
                            Group related form fields into a section.
                        </div>
                    </div>

                    <button
                        type="button"
                        class="btn-close"
                        data-bs-dismiss="modal">
                    </button>

                </div>


                <div class="modal-body">

                    <div class="mb-3">

                        <label class="form-label">
                            Section Title
                            <span class="text-danger">*</span>
                        </label>

                        <input
                            type="text"
                            name="section_title"
                            class="form-control"
                            placeholder="Personal Information"
                            required>

                    </div>


                    <div>

                        <label class="form-label">
                            Description
                        </label>

                        <textarea
                            name="section_description"
                            class="form-control"
                            rows="4"
                            placeholder="Optional section instructions or description."></textarea>

                    </div>

                </div>


                <div class="modal-footer">

                    <button
                        type="button"
                        class="btn btn-light"
                        data-bs-dismiss="modal">

                        Cancel

                    </button>

                    <button
                        type="submit"
                        class="btn btn-primary">

                        <i class="bi bi-plus-lg me-1"></i>
                        Add Section

                    </button>

                </div>

            </form>

        </div>

    </div>

</div>


<!-- EDIT SECTION MODAL -->

<div
    class="modal fade"
    id="editSectionModal"
    tabindex="-1"
    aria-hidden="true">

    <div class="modal-dialog modal-lg modal-dialog-centered">

        <div class="modal-content">

            <form
                method="POST"
                action="/public/dynamic-forms/sections/update">

                <input
                    type="hidden"
                    name="id"
                    id="editSectionId">

                <input
                    type="hidden"
                    name="form_id"
                    value="<?= $formId ?>">


                <div class="modal-header">

                    <div>
                        <h5 class="modal-title">
                            Edit Form Section
                        </h5>

                        <div class="small text-muted">
                            Update section information.
                        </div>
                    </div>

                    <button
                        type="button"
                        class="btn-close"
                        data-bs-dismiss="modal">
                    </button>

                </div>


                <div class="modal-body">

                    <div class="mb-3">

                        <label class="form-label">
                            Section Title
                        </label>

                        <input
                            type="text"
                            name="section_title"
                            id="editSectionTitle"
                            class="form-control"
                            required>

                    </div>


                    <div>

                        <label class="form-label">
                            Description
                        </label>

                        <textarea
                            name="section_description"
                            id="editSectionDescription"
                            class="form-control"
                            rows="4"></textarea>

                    </div>

                </div>


                <div class="modal-footer">

                    <button
                        type="button"
                        class="btn btn-light"
                        data-bs-dismiss="modal">

                        Cancel

                    </button>

                    <button
                        type="submit"
                        class="btn btn-primary">

                        <i class="bi bi-save me-1"></i>
                        Update Section

                    </button>

                </div>

            </form>

        </div>

    </div>

</div>


<script>
document.addEventListener(
    'DOMContentLoaded',
    function () {

        const editSectionButtons =
            document.querySelectorAll(
                '.edit-section-btn'
            );

        editSectionButtons.forEach(
            function (button) {

                button.addEventListener(
                    'click',
                    function () {

                        document.getElementById(
                            'editSectionId'
                        ).value =
                            this.dataset.id || '';

                        document.getElementById(
                            'editSectionTitle'
                        ).value =
                            this.dataset.title || '';

                        document.getElementById(
                            'editSectionDescription'
                        ).value =
                            this.dataset.description || '';
                    }
                );

            }
        );

    }
);
</script>


<?php

$content = ob_get_clean();

$title = 'Form Designer';

include APP_PATH .
    '/Modules/Administration/Views/layouts/admin.php';