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

use nur\A;
use nur\b\params\Tparametrable;
use nur\data\types\Metadata;
use nur\v\js;

class CDatatable extends CTable {
  use Tparametrable;

  const CSS = [
    "nur-v-bs3/datatables/datatables.css",
    "nur-v-bs3/datatables.css",
  ];
  const JS = "nur-v-bs3/datatables/datatables.js";

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "dtclass" => ["string", "datatable", "classe des tables mises en forme par Datatable"],
    "dtconfig" => ["?array", ["order" => []], "configuration de l'objet"],
    "dtauto" => ["?bool", "configurer la table pour que sa taille soit automatique"],
  ];

  protected $ppDtclass;

  protected $ppDtconfig;

  protected $ppDtauto;

  protected function afterSetParametrableParams(array $modifiedKeys, ?Metadata $md=null): void {
    parent::afterSetParametrableParams($modifiedKeys, $md);
    if (self::was_parametrable_param_modified($modifiedKeys, "dtauto")) {
      A::merge($this->ppDtconfig, [
        "dom" => "<'row dtcontrols'<'col-sm-12 dt-autodiv'lfi>><'row'<'col-sm-12'tr>><'row'<'col-sm-12 dt-autodiv'p>>",
        "bAutoWidth" => false,
      ]);
      A::append($this->ppTableClass, "table-auto");
    }
  }

  protected function getTableClass(): array {
    return [$this->ppTableClass, $this->ppDtclass];
  }

  const HAVE_JQUERY = true;

  function printJquery(): void {
    ?>
<script type="text/javascript">
jQuery.noConflict()(function($) {
  $(<?=js::qclass($this->ppDtclass)?>).DataTable(<?=js::qo($this->ppDtconfig)?>);
});
</script>
<?php
  }
}
