<?php

namespace App\Models;

use App\Models\AdmiAcceso;
use App\Models\GenericModel;

class SubModulo extends GenericModel
{
    protected $table            = 'sub_modulo';
    protected $primaryKey       = 'sumo_id';
    protected $allowedFields    = ['sumo_id','sumo_descripcion','sumo_enlace','sumo_orden','sumo_estado','modu_id'];
    
    function getDataTable($columns,$order = null){
        if($order == null){
            $order = $this->primaryKey . " DESC";
        }
        return $this->asObject()->select($columns)
            ->join('modulo AS modulo', 'sub_modulo.modu_id = modulo.modu_id','inner')
            ->orderBy($order)
            ->where($this->getColumnaEstado(),true);
    }
    public function getSubModulosByUsuarioModulo($userId,$moduId,$modulo,$subModulo){
        $lsSubModulos = $this->asObject()->select("sub_modulo.sumo_id,sumo_descripcion,sumo_enlace,sumo_orden,sumo_estado,sub_modulo.modu_id")
            ->join('modulo AS m', 'm.modu_id = sub_modulo.modu_id','inner')
            ->join('detalle_perfil AS dp', 'dp.sumo_id = sub_modulo.sumo_id','inner')
            ->join('perfil AS p', 'dp.perf_id = p.perf_id','inner')
            ->join('permiso AS per', 'p.perf_id = per.perf_id','inner')
            ->where('per.usua_id',$userId)
            ->where('m.modu_id',$moduId)
            ->where('sub_modulo.sumo_estado',true)
            ->where('dp.depe_estado',true)
            ->where('p.perf_estado',true)
            ->where('per.perm_estado',true)
            ->orderBy('sub_modulo.sumo_orden')->distinct()->get()->getResultObject();

        for ($i = 0 ; $i < count($lsSubModulos); $i++){
            if($lsSubModulos[$i]->sumo_enlace == $subModulo){
                $lsSubModulos[$i]->temp_activo = true;
            }else{
                $lsSubModulos[$i]->temp_activo = false;
            }
            $lsSubModulos[$i]->temp_enlace = '/' . $modulo . '/' . $lsSubModulos[$i]->sumo_enlace;
        }

        return $lsSubModulos;
    }
}
