<?php

namespace App\Models\Configuracion;

use App\Entities\Configuracion\Imposicion;
use App\Models\BaseModel;

class ImposicionModel extends BaseModel
{
    protected $table = "lg_imposiciones";

    /**
     * Whether primary key uses auto increment.
     *
     * @var bool
     */
    protected $useAutoIncrement = true;

    const SORTABLE = [
        1 => "t1.id",
        2 => "t1.ancho",
        3 => "t1.alto",
        4 => "t1.unidades",
        5 => "t1.orientacion",
        6 => "t1.maquina",
        7 => "t1.etiqueta",
    ];

    protected $allowedFields = ["ancho", "alto", "unidades", "orientacion", "maquina", "etiqueta","imposicion_esquema_id"];
    protected $returnType = Imposicion::class;

    public static $labelField = "ancho";

    protected $validationRules = [
        "alto" => [
            "label" => "Imposiciones.alto",
            "rules" => "required|integer",
        ],
        "ancho" => [
            "label" => "Imposiciones.ancho",
            "rules" => "required|integer",
        ],
        "etiqueta" => [
            "label" => "Imposiciones.etiqueta",
            "rules" => "trim|max_length[100]",
        ],
        "maquina" => [
            "label" => "Imposiciones.maquina",
            "rules" => "trim|max_length[100]",
        ],
        "orientacion" => [
            "label" => "Imposiciones.orientacion",
            "rules" => "permit_empty|in_list[H,V]",
        ],
        "unidades" => [
            "label" => "Imposiciones.unidades",
            "rules" => "integer|permit_empty",
        ],
        "imposicion_esquema_id" => [
            "label" => "Imposiciones.imposicion_esquema",
            "rules" => "integer|permit_empty",
        ],
    ];

    protected $validationMessages = [
        "alto" => [
            "integer" => "Imposiciones.validation.alto.integer",
            "required" => "Imposiciones.validation.alto.required",
        ],
        "ancho" => [
            "integer" => "Imposiciones.validation.ancho.integer",
            "required" => "Imposiciones.validation.ancho.required",
        ],
        "etiqueta" => [
            "max_length" => "Imposiciones.validation.etiqueta.max_length",
        ],
        "maquina" => [
            "max_length" => "Imposiciones.validation.maquina.max_length",
        ],
        "orientacion" => [
            "in_list" => "Imposiciones.validation.orientacion.in_list",
        ],
        "unidades" => [
            "integer" => "Imposiciones.validation.unidades.integer",
        ],
        "imposicion_esquema_id" => [
            "integer" => "Imposiciones.validation.unidades.integer",
        ],
    ];

    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResource(string $search = "")
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.ancho AS ancho, t1.alto AS alto, t1.unidades AS unidades, t1.orientacion AS orientacion, t1.maquina AS maquina, t1.etiqueta AS etiqueta"
            );

        return empty($search)
            ? $builder
            : $builder
            ->groupStart()
            ->like("t1.id", $search)
            ->orlike("t1.ancho", $search)
            ->orLike("t1.alto", $search)
            ->orLike("t1.unidades", $search)
            ->orLike("t1.orientacion", $search)
            ->orLike("t1.maquina", $search)
            ->orLike("t1.etiqueta", $search)
            ->orlike("t1.id", $search)
            ->orLike("t1.ancho", $search)
            ->orLike("t1.alto", $search)
            ->orLike("t1.unidades", $search)
            ->orLike("t1.orientacion", $search)
            ->orLike("t1.maquina", $search)
            ->orLike("t1.etiqueta", $search)
            ->groupEnd();
    }

    public function querySelect(?string $q)
    {
        $query = $this->builder()->select([
            "id",
            "CONCAT(lg_imposiciones.ancho,'x',lg_imposiciones.alto,'_',COALESCE(lg_imposiciones.unidades,'NULL'),'_',COALESCE(lg_imposiciones.orientacion,'NULL')) as name",
            "COALESCE(lg_imposiciones.etiqueta,'" . lang("Produccion.imposicion_no_label") . "') as description"
        ]);
        if ($q) {
            $query->orLike("CONCAT(lg_imposiciones.ancho,'x',lg_imposiciones.alto)", $q);
            $query->orLike("lg_imposiciones.etiqueta", $q);
        }
        return $query
            ->orderBy('id', 'ASC')
            ->get()->getResultArray();
    }
    public function queryDatatable()
    {
        return $this->builder()
            ->select([
                "lg_imposiciones.id",
                "lg_imposiciones.ancho",
                "lg_imposiciones.alto",
                "lg_imposiciones.unidades",
                "lg_imposiciones.maquina",
                "lg_imposiciones.orientacion",
                "lg_imposiciones.etiqueta",
                "imposicion_esquemas.id as esquemaId",
                "imposicion_esquemas.name as esquemaName"
            ])
            ->join("imposicion_esquemas","imposicion_esquemas.id = lg_imposiciones.imposicion_esquema_id","left")
            ->where('lg_imposiciones.deleted_at', null);
    }
    public static function datatable_buttons(int $id)
    {
        $btn = "";
        if(auth()->user()->inGroup("admin")){
            $btn.="<a type='button' href='/imposiciones/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-delete' data-id='{$id}'></i></a>";
        }
        return $btn;
    }
}
