<?php

namespace GriffinForms\Admin\App\Html\Modals\FormOptions;

use GriffinForms\Admin\App\FormControls\Factory;

trait Markup
{
    protected function renderTabStrip()
    {
        $tabs = $this->getTabs();
        echo '<nav class="gf-formoptions-sidenav" aria-label="' . esc_attr__('Form options navigation', 'griffinforms-form-builder') . '">';
        echo '<span class="gf-formoptions-tab-indicator" aria-hidden="true"></span>';
        foreach ($tabs as $i => $tab) {
            $active   = $i === 0 ? ' gf-formoptions-tab-btn--active' : '';
            $selected = $i === 0 ? 'true' : 'false';
            echo '<button class="gf-formoptions-tab-btn' . esc_attr($active) . '" id="gf-formoptions-tab-' . esc_attr($tab['id']) . '"';
            echo ' data-bs-target="#gf-formoptions-panel-' . esc_attr($tab['id']) . '"';
            echo ' type="button" role="tab"';
            echo ' aria-controls="gf-formoptions-panel-' . esc_attr($tab['id']) . '" aria-selected="' . esc_attr($selected) . '">';
            echo '<span class="gf-formoptions-tab-btn-label">' . esc_html($tab['label']) . '</span>';
            echo '<span class="gf-formoptions-tab-error-dot d-none" aria-hidden="true"></span>';
            echo '<svg class="gf-formoptions-tab-chevron" viewBox="0 0 16 16" fill="none" aria-hidden="true"><path d="M6 4l4 4-4 4" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>';
            echo '</button>';
        }
        echo '</nav>';
    }

    protected function renderTabPanels()
    {
        $form    = $this->utility->getForm();
        $form_id = $this->utility->getFormId();
        $tabs    = $this->getTabs();

        echo '<div class="gf-formoptions-content" id="gf-formoptions-tab-content">';
        foreach ($tabs as $i => $tab) {
            $active_class = $i === 0 ? ' gf-formoptions-panel--active' : '';
            echo '<div class="gf-formoptions-panel' . esc_attr($active_class) . '" id="gf-formoptions-panel-' . esc_attr($tab['id']) . '"';
            echo ' role="tabpanel" aria-labelledby="gf-formoptions-tab-' . esc_attr($tab['id']) . '">';
            echo '<div class="gf-formoptions-panel-body">';
            if (method_exists($this, $tab['callback'])) {
                $this->{$tab['callback']}($form, $form_id);
            }
            echo '</div>';
            echo '</div>';
        }
        echo '</div>';
    }

    protected function getTabs(): array
    {
        return [
            ['id' => 'general',      'label' => __('General',      'griffinforms-form-builder'), 'callback' => 'renderGeneralTab'],
            ['id' => 'behavior',     'label' => __('Behavior',     'griffinforms-form-builder'), 'callback' => 'renderBehaviorTab'],
            ['id' => 'availability', 'label' => __('Availability', 'griffinforms-form-builder'), 'callback' => 'renderAvailabilityTab'],
            ['id' => 'submission',   'label' => __('Submission',   'griffinforms-form-builder'), 'callback' => 'renderSubmissionTab'],
            ['id' => 'compliance',   'label' => __('Compliance',   'griffinforms-form-builder'), 'callback' => 'renderComplianceTab'],
        ];
    }

    protected function renderGeneralTab($form, $form_id)
    {
        $name        = (string) ($form ? $form->getProp('name')        : '');
        $heading     = (string) ($form ? $form->getProp('heading')     : '');
        $description = (string) ($form ? $form->getProp('description') : '');

        $this->renderOptionRow('name', __('Form Name', 'griffinforms-form-builder'), true, function() use ($name) {
            echo Factory::create('text', 'name', $name, 'formoptions', ['show_label' => false])->render();
            echo '<div class="gf-formoptions-field-error text-danger small mt-1 d-none" data-gf-error-for="name"></div>';
        });

        $this->renderOptionRow('heading', __('Heading', 'griffinforms-form-builder'), false, function() use ($heading) {
            echo Factory::create('text', 'heading', $heading, 'formoptions', ['show_label' => false])->render();
        });

        $this->renderOptionRow('description', __('Description', 'griffinforms-form-builder'), false, function() use ($description) {
            echo Factory::create('textarea', 'description', $description, 'formoptions', ['show_label' => false, 'rows' => 3])->render();
        });
    }

    protected function renderCategoryTab($form, $form_id)
    {
        $folder_id   = $form ? $form->getProp('folder') : 0;
        $folder_name = '';
        if ($folder_id) {
            $sql = \GriffinForms\Admin\Sql\App::getInstance();
            $folder_name = $sql->getItemValue($folder_id, 'name', 'folder');
        }

        $edit_url = admin_url('admin.php?page=griffinforms-form&id=' . absint($form_id));

        $this->renderOptionRow('folder', __('Folder', 'griffinforms-form-builder'), false, function() use ($folder_name, $edit_url) {
            if ($folder_name) {
                echo '<span class="gf-formoptions-folder-name fw-medium">' . esc_html($folder_name) . '</span>';
            } else {
                echo '<span class="text-muted small">' . esc_html__('No folder assigned', 'griffinforms-form-builder') . '</span>';
            }
            echo '<div class="mt-1">';
            echo '<a href="' . esc_url($edit_url) . '" target="_blank" rel="noopener noreferrer" class="small">';
            echo esc_html__('Change folder on settings page', 'griffinforms-form-builder');
            echo '</a>';
            echo '</div>';
        });
    }

    protected function renderBehaviorTab($form, $form_id)
    {
        $autofill    = $form ? $form->getProp('autofill')     : 0;
        $save        = $form ? $form->getProp('save_entries') : 1;
        $user_action = $form ? $form->getProp('user_action')  : null;
        $progress    = $form ? $form->getProp('progress_bar') : null;

        $choices = \GriffinForms\Admin\Choices\Form::getInstance();

        $this->renderOptionRow('autofill', __('Browser Autofill', 'griffinforms-form-builder'), false, function() use ($autofill) {
            echo Factory::create('checkbox', 'autofill', $autofill, 'formoptions', [
                'show_label'     => false,
                'checkbox_label' => __('Enable browser autofill for this form', 'griffinforms-form-builder'),
            ])->render();
        });

        $this->renderOptionRow('save_entries', __('Save Entries', 'griffinforms-form-builder'), false, function() use ($save, $choices) {
            echo Factory::create('select', 'save_entries', (string) $save, 'formoptions', [
                'show_label' => false,
                'options'    => $choices->getChoices('save_entries'),
            ])->render();
        });

        $this->renderOptionRow('user_action', __('Post-Submission Action', 'griffinforms-form-builder'), false, function() use ($user_action) {
            $field = new \GriffinForms\Admin\Html\FormControls\Form\UserAction('user_action', $user_action, ['app_context' => true]);
            $field->html();
            echo '<div class="gf-formoptions-field-error text-danger small mt-1 d-none" data-gf-error-for="user_action"></div>';
        });

        $this->renderOptionRow('progress_bar', __('Progress Bar', 'griffinforms-form-builder'), false, function() use ($progress, $form_id) {
            $field = new \GriffinForms\Admin\Html\FormControls\Form\ProgressBar('progress_bar', $progress, ['form_id' => $form_id, 'app_context' => true]);
            $field->html();
        });
    }

    protected function renderAvailabilityTab($form, $form_id)
    {
        $availability = $form ? $form->getProp('availability_window') : null;
        $limit        = $form ? $form->getProp('limit_entries')       : null;
        $access       = $form ? $form->getProp('access_level')        : null;

        $this->renderOptionRow('availability_window', __('Availability Window', 'griffinforms-form-builder'), false, function() use ($availability) {
            $field = new \GriffinForms\Admin\Html\FormControls\Form\AvailabilityWindow('availability_window', $availability, ['app_context' => true]);
            $field->html();
            echo '<div class="gf-formoptions-field-error text-danger small mt-1 d-none" data-gf-error-for="availability_window"></div>';
        });

        $this->renderOptionRow('limit_entries', __('Limit Entries', 'griffinforms-form-builder'), false, function() use ($limit) {
            $field = new \GriffinForms\Admin\Html\FormControls\Form\LimitEntries('limit_entries', $limit, ['app_context' => true]);
            $field->html();
            echo '<div class="gf-formoptions-field-error text-danger small mt-1 d-none" data-gf-error-for="limit_entries"></div>';
        });

        $this->renderOptionRow('access_level', __('Access Level', 'griffinforms-form-builder'), false, function() use ($access) {
            $field = new \GriffinForms\Admin\Html\FormControls\Form\AccessLevel('access_level', $access, ['app_context' => true]);
            $field->html();
        });
    }

    protected function renderSubmissionTab($form, $form_id)
    {
        $sub_edit = $form ? $form->getProp('submission_edit')           : null;
        $count    = $form ? $form->getProp('lifetime_submission_count') : 0;

        $this->renderOptionRow('submission_edit', __('Submission Editing', 'griffinforms-form-builder'), false, function() use ($sub_edit, $form_id) {
            $field = new \GriffinForms\Admin\Html\FormControls\Form\SubmissionEdit('submission_edit', $sub_edit, ['form_id' => $form_id, 'app_context' => true]);
            $field->html();
            echo '<div class="gf-formoptions-field-error text-danger small mt-1 d-none" data-gf-error-for="submission_edit"></div>';
        });

        $this->renderOptionRow('lifetime_submission_count', __('Submission Count', 'griffinforms-form-builder'), false, function() use ($count, $form_id) {
            $field = new \GriffinForms\Admin\Html\FormControls\Form\LifetimeSubmissionCount('lifetime_submission_count', $count, ['item_id' => $form_id]);
            $field->html();
        });
    }

    protected function renderComplianceTab($form, $form_id)
    {
        $c_flags   = $form ? $form->getProp('compliance_flags')   : null;
        $c_mode    = $form ? $form->getProp('compliance_mode')    : 0;
        $c_profile = $form ? $form->getProp('compliance_profile') : 'standard';
        $choices   = \GriffinForms\Admin\Choices\Form::getInstance();

        $this->renderOptionRow('compliance_flags', __('Data Privacy Mode', 'griffinforms-form-builder'), false, function() use ($c_flags) {
            $field = new \GriffinForms\Admin\Html\FormControls\Form\ComplianceFlags('compliance_flags', $c_flags, ['app_context' => true]);
            $field->html();
        });

        echo '<div class="griffinforms-hidden-option" id="griffinforms-compliance-custom-options-wrap">';

        $this->renderOptionRow('compliance_mode', __('Compliance Mode', 'griffinforms-form-builder'), false, function() use ($c_mode) {
            echo Factory::create('checkbox', 'compliance_mode', $c_mode, 'formoptions', [
                'show_label'     => false,
                'checkbox_label' => __('Enable strict compliance mode', 'griffinforms-form-builder'),
            ])->render();
        });

        echo '<div class="griffinforms-hidden-option" id="griffinforms-complianceprofile-wrap">';
        $this->renderOptionRow('compliance_profile', __('Compliance Profile', 'griffinforms-form-builder'), false, function() use ($c_profile, $choices) {
            echo Factory::create('select', 'compliance_profile', (string) $c_profile, 'formoptions', [
                'show_label' => false,
                'options'    => $choices->getChoices('compliance_profile'),
            ])->render();
        });
        echo '</div>';

        echo '<div class="griffinforms-hidden-option" id="griffinforms-complianceflags-custom-wrap">';
        $this->renderOptionRow('compliance_flags_detail', __('Custom Flags', 'griffinforms-form-builder'), false, function() use ($c_flags) {
            $field = new \GriffinForms\Admin\Html\FormControls\Form\ComplianceFlags('compliance_flags', $c_flags, ['app_context' => true]);
            $field->renderFlagsOnly();
        });
        echo '</div>';

        echo '</div>';
    }

    protected function renderOptionRow(string $id, string $label, bool $required, callable $control)
    {
        $req_mark = $required ? ' <span class="text-danger">*</span>' : '';
        echo '<div class="row mb-3 gf-formoptions-row" id="gf-formoptions-' . esc_attr($id) . '-row">';
        echo '<div class="col-md-3 ps-0">';
        echo '<label class="form-label fw-medium small">' . esc_html($label) . wp_kses_post($req_mark) . '</label>';
        echo '</div>';
        echo '<div class="col-md-9">';
        $control();
        echo '</div>';
        echo '</div>';
    }
}
