<?php
namespace nur\v\bs3\fo;

use nur\b\params\Tparametrable;
use nur\b\ValueException;
use nur\v\v;

class ControlSubmit extends ControlVisual {
  use Tparametrable;

  /** @var string type de champ input */
  const INPUT_TYPE = null;
  const AUTOID = false;

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "submit" => ["?content", null, "libellé du bouton"],
    "type" => ["?string", "button", "type de submit: button, link"],
    "href" => "?string", # pour le type link
    "target" => "?string", # pour le type link
    "class" => ["?array", "btn-default"],
    #XXX ajouter type pour contrĂ´ler le type de bouton (default, primary, etc.)
  ];

  /** @var array|string */
  protected $ppSubmit;

  protected $tag, $tagAttrs;

  function pp_setType(?string $type="button"): void {
    switch ($type) {
    case "link":
    case "a":
      $this->tag = "a";
      $this->tagAttrs = null;
      break;
    case "button":
    case "submit":
    case "b":
      $this->tag = "button";
      $this->tagAttrs = ["type" => "submit"];
      break;
    default:
      throw ValueException::invalid_value($type, "submit type");
    }
  }

  /** @var ?string */
  protected $ppHref;

  /** @var ?string */
  protected $ppTarget;

  function getLayout(array $control): array {
    return $this->getFgsLayout($control, true);
  }

  function getControl(): array {
    $control = v::tag($this->tag, [
      $this->tagAttrs,
      "id" => $this->ppId,
      "name" => $this->ppName,
      "value" => $this->ppValue,
      "href" => $this->ppHref,
      "target" => $this->ppTarget,
      "class" => ["btn", $this->ppClass],
      "style" => $this->ppStyle,
      $this->ppAttrs,
      q($this->ppSubmit),
    ]);
    if ($this->ppNaked) return $control;
    else return $this->getLayout($control);
  }
}