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

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

class ControlFile extends ControlVisualInput {
  use Tparametrable;

  const INPUT_TYPE = "file";

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "accept" => ["?string", ".pdf,image/*", "types MIME acceptés"],
    "btn_class" => ["?string", "btn-default", "Classe du bouton"],
    "btn_style" => ["?string", null, "Style du bouton"],
    "btn_label" => ["content", null, "Texte du bouton"],
    "multiple" => ["?bool", null, "est-il possible de sélectionner plusieurs fichiers?"],
  ];

  function __construct(Form $form, ?array $params=null) {
    parent::__construct($form, $params);
    $this->ppPlaceholder = null;
  }

  function pp_setAccept(?string $accept) {
    $this->ppAttrs["accept"] = $accept;
  }

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

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

  /** @var ?bool */
  protected $ppMultiple;

  function buildControl(): array {
    $name = $this->ppName;
    $value = $this->ppValue;
    $multiple = $this->ppMultiple;
    $defaultLabel = $multiple ? "Sélectionner des fichiers" : "Sélectionner un fichier";
    $btnLabel = $this->ppBtnLabel ?? $defaultLabel;
    return v::tag("label", [
      "class" => ["btn btn-file", $this->ppBtnClass],
      v::tag1("input", [
        "type" => static::INPUT_TYPE,
        "id" => $this->ppId,
        "name" => $name,
        "value" => $value,
        "readonly" => $this->ppReadonly? "readonly": false,
        "disabled" => $this->ppDisabled? "disabled": false,
        "required" => $this->ppRequired? "required": false,
        "multiple" => $multiple ? "multiple": false,
        "style" => "display: inline; visibility: hidden; width: 0px; height: 0px; vertical-align: middle;",
        $this->ppAttrs,
      ]),
      v::span([
        "class" => ["file-label", $this->ppClass],
        "style" => $this->ppStyle,
        $btnLabel,
      ]),
    ]);
  }
}
