<?php

namespace GriffinForms\Admin\Js\Local;

abstract class format extends \GriffinForms\Admin\Format
{   
    protected $meta;
    protected $lang;
    protected $data;
    protected $choices;
    
    abstract protected function localJs();
    
    protected function setChoices()
    {
        $this->setHelper('choices');
    }
    
    public function js()
    {
        $js = '';
        $js .= 'jQuery(document).ready(function($) {' . PHP_EOL;
        $js .= $this->globalVars() . PHP_EOL;
        $js .= $this->setMessage() . PHP_EOL;
        $js .= $this->notUpdated() . PHP_EOL;
        $js .= $this->updated() . PHP_EOL;
        $js .= $this->snackBar() . PHP_EOL;
        $js .= $this->localJs() . PHP_EOL;
        $js .= '});' . PHP_EOL;
        
        return $js;
    }
    
    protected function refererJs()
    {
        if(isset($_GET['return']) and $_GET['return'] == 1) {
            return 'location.replace("' . esc_url_raw($this->data['referer']) . '");';
        }
    }
    
    protected function globalVars()
    {
        return 'var griffinformsErrors, fieldOptions;
                
                griffinformsErrors = false;
                fieldOptions = 0;
        
                function showItemError(errorText, inputId = "") {
                    griffinformsErrors = true;
                    
                    var errorHtml = "<li>" + errorText + " " + "<a class=\"\" href=#" + inputId + ">' . esc_js($this->lang->getText('view_error')) . '</a></li>";
                    $("#" + inputId).addClass("border-danger");
                    $("#griffinforms-item-errors-list").append(errorHtml);
                }
                
                function resetItemErrors() {
                    $("#griffinforms-item-errors-wrap").hide();
                    $("#griffinforms-item-errors-list").html("");
                    $(".griffinforms-formcontrol-inline-error").hide();
                    $(".border-danger").removeClass("border-danger");
                    griffinformsErrors = false; 
                }
        ';
    }   
    
    protected function setMessage()
    {
        return 'function griffinformsSetMessage(message = "Starting") {
                    $(".griffinforms-spinner-text").text(message);
                }';
    }
    
    protected function notUpdated()
    {
        return 'function griffinformsNotUpdated(){
                $(".toast-title").html("' . esc_js($this->lang->getText('Notice')) . '");
                $(".toast-body").html("' . esc_js($this->lang->getText('nothing to update')) . '");
                $(".toast").toast("show");
        }';
    }    
    
    protected function updated()
    {
        return 'function griffinformsUpdated(){
                $(".toast-title").html("' . esc_js($this->lang->getText('Done!')) . '");
                $(".toast-body").html("' . esc_js($this->lang->getText('item_updated')) . '");
                $(".toast").toast("show");
        }';
    }
    
    protected function snackBar()
    {
        return 'function griffinformsSnackBar(text = "", timeText = ""){
            var html = "<div>" + text + "</div>";
            if (timeText !== "") {
                html += "<div style=\'font-size: 80%; opacity: 0.7;\' class=\'mt-1\'>" + timeText + "</div>";
            }
            $(".toast-body").html(html);
            $("#griffinforms-snackbar").toast("show");
        }';
    }
    
    public function __construct($data)
    {
        $this->data = $data;
        $this->setAll();
    }
}