<?php

namespace App\Models\Configuracion;

use App\Entities\Configuracion\ImposicionEsquemaEntity;
use CodeIgniter\Model;

class ImposicionEsquemaModel extends Model
{
    protected $table            = 'imposicion_esquemas';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = ImposicionEsquemaEntity::class;
    protected $useSoftDeletes   = true;
    protected $protectFields    = true;
    protected $allowedFields    = [
        "name",
        "rows",
        "columns",
        "orientacion",
        "rotativa",
        "cosido",
        "svg_schema"
    ];

    protected bool $allowEmptyInserts = false;
    protected bool $updateOnlyChanged = true;

    protected array $casts = [];
    protected array $castHandlers = [];

    // Dates
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    // Validation
    protected $validationRules      = [
        "name" => [
            "label" => "Imposiciones.esquema.name",
            "rules" => "required|alpha_numeric_punct",
        ],
        "rows" => [
            "label" => "Imposiciones.esquema.rows",
            "rules" => "required|integer"
        ],
        "columns" => [
            "label" => "Imposiciones.esquema.columns",
            "rules" => "required|integer"
        ],
        "orientacion" => [
            "label" => "Imposiciones.esquema.orientacion",
            "rules" => "required|in_list[V,H]"
        ],

    ];
    protected $validationMessages   = [
        "name" => [
            "required" => "Validation.required",
        ],
        "rows" => [
            "required" => "Validation.required",
            "integer" => "Validation.integer",
        ],
        "columns" => [
            "required" => "Validation.required",
            "integer" => "Validation.integer",
        ],
        "orientacion" => [
            "required" => "Validation.required",
            "in_list" => "Validation.in_liust",
        ],
    ];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert   = [];
    protected $afterInsert    = [];
    protected $beforeUpdate   = [];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];

    public function querySelect(?string $q)
    {
        $query = $this->builder()->select([
            "id",
            "name",
            "svg_schema as description"
        ])->where('deleted_at', null);
        if ($q) {
            $query->orLike("name", $q);
        }
        return $query
            ->orderBy('id', 'ASC')
            ->get()->getResultArray();
    }
    public function queryDatatable()
    {
        return $this->builder()
            ->select([
                "id",
                "name",
                "svg_schema"

            ])->where('deleted_at', null);
    }
    public static function datatable_buttons(int $id)
    {
        $btn = "";
        if (auth()->user()->inGroup("admin")) {
            $btn .= "<a type='button' href='/imposiciones/esquema/edit/{$id}' data-id='{$id}'><i class='ti ti-eye ti-sm'></i></a>";
            $btn .= "<a type='button'><i class='ti ti-trash ti-sm imposicion-esquema-delete' data-id='{$id}'></i></a>";
        }
        return $btn;
    }
}
