<?php

namespace GriffinForms\Admin\Html\Pages\Single;

class Form extends \GriffinForms\Admin\Html\Pages\Single\Format
{   
    protected $type = 'form';

    protected function useSectionNavigation()
    {
        return true;
    }

    protected function getSections()
    {
        return array(
            array(
                'id' => 'general',
                'label' => $this->lang->getText('general'),
                'callback' => 'renderGeneralSection',
            ),
            array(
                'id' => 'category',
                'label' => $this->lang->getText('category'),
                'callback' => 'renderCategorySection',
            ),
            array(
                'id' => 'browser-behavior',
                'label' => $this->lang->getText('browser_behavior'),
                'callback' => 'renderBrowserBehaviorSection',
            ),
            array(
                'id' => 'pre-processing',
                'label' => $this->lang->getText('pre_processing'),
                'callback' => 'renderPreProcessingSection',
            ),
            array(
                'id' => 'submission-edit',
                'label' => $this->lang->getText('submission_edit'),
                'callback' => 'renderSubmissionEditSection',
            ),
            array(
                'id' => 'compliance',
                'label' => $this->lang->getText('compliance'),
                'callback' => 'renderComplianceSection',
            ),
            array(
                'id' => 'post-processing',
                'label' => $this->lang->getText('post_processing'),
                'callback' => 'renderPostProcessingSection',
            ),
        );
    }
    
    public function itemOptions()
    {
        $this->category();
        $this->browserBehavior();
        $this->preProcessing();
        $this->submissionEditOptions();
        $this->complianceSettings();
        $this->postProcessing();
    }

    protected function topNav()
    {
        if ($this->action === 'edit') {
            echo '<div class="d-flex justify-content-between">';
            $this->backToFormsBtn();
            $this->formLayoutBtn();
            echo '</div>';
        }
    }
    
    private function backToFormsBtn()
    {
        $this->buttons->bsBtn([
            'class' => 'gf-back-to-forms-btn gf-btn-light me-2',
            'label' => $this->lang->getText('Back to forms'),
            'link'  => admin_url('admin.php?page=griffinforms-forms'),
            'icon'  => 'list'
        ]);
    }

    private function formLayoutBtn()
    {
        $this->buttons->bsBtn([
            'class' => 'gf-form-layout-btn gf-btn-light me-2',
            'label' => $this->lang->getText('Form layout'),
            'link'  => admin_url('admin.php?page=griffinforms-app&form_id=' . $this->item_id),
            'icon'  => 'form-builder'
        ]);
    }
    
    protected function category()
    {
        echo '<div class="container border-bottom griffinforms-item-options w-75 mt-3 pt-3">';
        $this->optionsHeader('category');
        $this->getOption('folder');
        echo '</div>'; 
    }
    
    protected function postProcessing()
    {
        echo '<div class="container border-bottom griffinforms-item-options w-75 mt-3 pt-3">';
        $this->optionsHeader('post_processing');
        $this->getOption('user_action');
        $this->getOption('autoresponder');
        $this->getOption('alert_emails');
        $this->getOption('save_entries');
        echo '</div>';
        
    }    

    protected function formOptions()
    {
        echo '<div class="container border-bottom griffinforms-item-options w-75 mt-3 pt-3">';
        $this->optionsHeader('form_options');
        $this->getOption('progress_bar');
        echo '</div>';
    }

    protected function complianceSettings()
    {
        echo '<div class="container border-bottom griffinforms-item-options w-75 mt-3 pt-3">';
        $this->optionsHeader('compliance');
        $this->complianceWarnings();
        $this->getOption('compliance_flags');
        echo '<div class="griffinforms-hidden-option" id="griffinforms-compliance-custom-options-wrap">';
        $this->getOption('compliance_mode');
        echo '<div class="griffinforms-hidden-option" id="griffinforms-complianceprofile-wrap">';
        $this->getOption('compliance_profile');
        echo '</div>';
        echo '<div class="griffinforms-hidden-option" id="griffinforms-complianceflags-custom-wrap">';
        echo '<div class="row mb-3">';
        echo '<div class="col-md-3 ps-3"></div>';
        echo '<div class="col-md-9">';
        $flags = new \GriffinForms\Admin\Html\FormControls\Form\ComplianceFlags('compliance_flags', $this->item->getProp('compliance_flags'));
        $flags->renderFlagsOnly();
        echo '</div>';
        echo '</div>';
        echo '</div>';
        echo '</div>';
        echo '</div>';
    }

    protected function complianceWarnings()
    {
        if ($this->item_id <= 0) {
            return;
        }

        $checks = new \GriffinForms\Includes\Checks\Form();
        $issues = $checks->run($this->item_id);
        if (!empty($issues)) {
            $issues = array_values(array_filter($issues, function ($issue) {
                if (!is_array($issue)) {
                    return false;
                }
                $code = isset($issue['code']) ? (string) $issue['code'] : '';
                return $code !== '' && strpos($code, 'compliance_') === 0;
            }));
        }
        if (empty($issues)) {
            return;
        }

        echo '<div class="gf-notice gf-notice-warning">';
        echo '<div>';
        echo '<strong>' . esc_html__('Compliance warnings', 'griffinforms-form-builder') . '</strong>';
        echo '<ul class="mb-0 mt-2">';
        foreach ($issues as $issue) {
            if (!is_array($issue) || empty($issue['message'])) {
                continue;
            }
            echo '<li>' . esc_html($issue['message']) . '</li>';
        }
        echo '</ul>';
        echo '</div>';
        echo '</div>';
    }
    
    protected function preProcessing()
    {
        echo '<div class="container border-bottom griffinforms-item-options w-75 mt-3 pt-3">';
        $this->optionsHeader('pre_processing');
        $this->getOption('limit_entries');
        $this->getOption('availability_window');
        $this->getOption('access_level');
        echo '</div>';
        
    }

    protected function submissionEditOptions()
    {
        echo '<div class="container border-bottom griffinforms-item-options w-75 mt-3 pt-3">';
        $this->optionsHeader('submission_edit');
        $this->getOption('submission_edit');
        echo '</div>';
    }

    public function nameOptions()
    {
        echo '<div class="container border-bottom griffinforms-item-options w-75 mt-3 pt-3">';
        $this->optionsHeader('general');
        $this->getOption('name');
        $this->getOption('heading');
        $this->getOption('description');
        $this->getOption('lifetime_submission_count');
        echo '</div>';
    }

    protected function renderGeneralSection()
    {
        $this->getOption('name');
        $this->getOption('heading');
        $this->getOption('description');
        $this->getOption('lifetime_submission_count');
    }

    protected function renderCategorySection()
    {
        $this->getOption('folder');
    }

    protected function renderBrowserBehaviorSection()
    {
        $this->getOption('autofill');
    }

    protected function renderPreProcessingSection()
    {
        $this->getOption('limit_entries');
        $this->getOption('availability_window');
        $this->getOption('access_level');
        $this->getOption('progress_bar');
    }

    protected function renderSubmissionEditSection()
    {
        $this->getOption('submission_edit');
    }

    protected function renderComplianceSection()
    {
        $this->complianceWarnings();
        $this->getOption('compliance_flags');
        echo '<div class="griffinforms-hidden-option" id="griffinforms-compliance-custom-options-wrap">';
        $this->getOption('compliance_mode');
        echo '<div class="griffinforms-hidden-option" id="griffinforms-complianceprofile-wrap">';
        $this->getOption('compliance_profile');
        echo '</div>';
        echo '<div class="griffinforms-hidden-option" id="griffinforms-complianceflags-custom-wrap">';
        echo '<div class="row mb-3">';
        echo '<div class="col-md-3 ps-3"></div>';
        echo '<div class="col-md-9">';
        $flags = new \GriffinForms\Admin\Html\FormControls\Form\ComplianceFlags('compliance_flags', $this->item->getProp('compliance_flags'));
        $flags->renderFlagsOnly();
        echo '</div>';
        echo '</div>';
        echo '</div>';
        echo '</div>';
    }

    protected function renderPostProcessingSection()
    {
        $this->getOption('user_action');
        $this->getOption('autoresponder');
        $this->getOption('alert_emails');
        $this->getOption('save_entries');
    }
    
    protected function browserBehavior()
    {
        echo '<div class="container border-bottom griffinforms-item-options w-75 mt-3 pt-3">';
        $this->optionsHeader('browser_behavior');
        $this->getOption('autofill');
        echo '</div>';        
    }

    protected function submissionEdit()
    {
        $data = array(
            'form_id' => $this->item_id,
        );
        $control = new \GriffinForms\Admin\Html\FormControls\Form\SubmissionEdit('submission_edit', $this->item->getProp('submission_edit'), $data);
        $control->html();
    }
    
    protected function successMessage()
    {
        $data = array(
            'item_type' => 'message',
            'allow_create' => 1,
            'choices' => array('none' => 'none')
        );
        $field = $this->selectObject($data);
        $field->html();
    }    
    
    protected function autoresponder()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\Form\AutoResponder($this->prop, $this->value);
        $field->html();
    }    
    
    protected function userAction()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\Form\UserAction($this->prop, $this->value);
        $field->html();
    }    
    
    protected function saveEntries()
    {
        $options = $this->choices->getChoices('save_entries');
        $field = new \GriffinForms\Admin\Html\FormControls\SelectField($this->prop, $this->value);
        $field->selectOptions($options);
        $field->html();
    }    
    
    protected function alertEmails()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\Form\EntryAlerts('entry_alerts', $this->value);
        $field->html();
    }
    
    protected function limitEntries()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\Form\LimitEntries($this->prop, $this->value);
        $field->html();
    }

    protected function availabilityWindow()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\Form\AvailabilityWindow($this->prop, $this->value);
        $field->html();
    }

    protected function progressBar()
    {
        $page_count = 0;
        $formpages = $this->item_id ? $this->sql->getItemValue($this->item_id, 'formpages', 'form') : '';
        $formpages = maybe_unserialize($formpages);
        if (is_array($formpages)) {
            $page_count = count($formpages);
        }

        if ($page_count <= 1) {
            echo '<div class="gf-notice gf-notice-info small mb-0">' . esc_html__('Progress bar is only available for multi-step forms.', 'griffinforms-form-builder') . '</div>';
            return;
        }

        $field = new \GriffinForms\Admin\Html\FormControls\Form\ProgressBar($this->prop, $this->value);
        $field->html();
    }

    protected function lifetimeSubmissionCount()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\Form\LifetimeSubmissionCount($this->prop, $this->value, ['item_id' => $this->item_id]);
        $field->html();
    }
    
    protected function accessLevel()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\Form\AccessLevel($this->prop, $this->value);
        $field->html();
    }
    
    protected function autofill()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\ToggleCheckbox($this->prop, $this->value);
        $field->html();
    }

    protected function compliancemode()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\ToggleCheckbox($this->prop, $this->value);
        $field->html();
    }

    protected function complianceprofile()
    {
        $options = $this->choices->getChoices('compliance_profile');
        $field = new \GriffinForms\Admin\Html\FormControls\SelectField($this->prop, $this->value);
        $field->selectOptions($options);
        $field->html();
    }

    protected function complianceflags()
    {
        $field = new \GriffinForms\Admin\Html\FormControls\Form\ComplianceFlags($this->prop, $this->value);
        $field->html();
    }
}
