<?php

namespace GriffinForms\Admin\Html\FormControls;

class inputCheckBox extends \GriffinForms\Admin\Html\FormControls\Format
{

    protected $options;

    public function checkboxOptions($options)
    {
        if (is_array($options)) {
            $this->options = $options;
        } else {
            $this->options = array();
        }
    }
    
    public function html()
    {
        if (!is_array($this->value)) {
            $this->value = array();
        }
        
        echo '<div ' . wp_kses_post($this->css_id) . '>';
        foreach ($this->options as $option) {
            
            $html_id = 'griffinforms-' . $this->purgeString($option) . '-' . strtolower($this->meta) . '"';
            $checked = '';
            
            if (in_array($option, $this->value)) {
                $checked = ' checked="checked"';
            }
            
            $class = !empty($this->data['app_context']) ? 'form-check-input gf-app-control-checkbox' : 'form-control';
            echo '<input type="checkbox" class="' . esc_attr($class) . '" id="' . esc_attr($html_id) . '" value="' . esc_attr($option) . '"' . esc_attr($checked) . '>';
            echo '<label class="me-3" for="' . esc_attr($html_id) . '">' . esc_html($this->data['lang']->getText($option, 'label')) . '</label>';
        }
        
        if (count($this->options) > 3) {
            $this->existingValues();
            $this->quickOptions();
        }
    }
    
    private function existingValues()
    {
        echo '<input id="griffinforms-' . esc_attr($this->purgeString($this->name)) . '-existingval" type="hidden" value="' . esc_attr(implode(',', $this->value)) . '">';
        echo '</div>';
    }
    
    protected function quickOptions()
    {
        echo '<div class="mt-1">';
        echo '<button type="button" id="griffinforms-' . esc_attr($this->name)  . '-checkall" class="btn btn-link btn-sm">' . esc_html($this->data['lang']->getText('check_all', 'label')) . '</button>';
        echo '<button type="button" id="griffinforms-' . esc_attr($this->name)  . '-uncheckall" class="btn btn-link btn-sm">' . esc_html($this->data['lang']->getText('uncheck_all', 'label')) . '</button>';
        echo '<button type="button" id="griffinforms-' . esc_attr($this->name)  . '-restorecheck" class="btn btn-link btn-sm">' . esc_html($this->data['lang']->getText('restore_check', 'label')) . '</button>';
        echo '</div>';
    }
    
    public function fieldJs()
    {
        $container_id = 'griffinforms-' . $this->purgeString($this->name) . '-' . strtolower($this->meta);
    
        $js = '
        jQuery(document).ready(function($) {
        
            $("#griffinforms-' . esc_js($this->name) . '-checkall").click(function(){
                $("#' . esc_js($container_id) . '").children("input").prop("checked", true);
            });
            
            $("#griffinforms-' . esc_js($this->name) . '-uncheckall").click(function(){
                $("#' . esc_js($container_id) . '").children("input").prop("checked", false);
            });
            
            $("#griffinforms-' . esc_js($this->name) . '-restorecheck").click(function(){
                let existingVal =  $("#griffinforms-' . esc_js($this->purgeString($this->name)) . '-existingval").val();
                existingVal = existingVal.split(",");
                $("#' . esc_js($container_id) . '").children("input").each(function(){
                    if (existingVal.includes($(this).val())) {
                        $(this).prop("checked", true);
                    } else {
                        $(this).prop("checked", false);
                    }
                });
            });
        
        });';
    
        // Use wp_add_inline_script to append the JS
        wp_add_inline_script('griffinforms-global-js', $js);
    }
}
