<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

use DB;
use App\Models\superadmin;
class superadmin extends Model
{
    use HasFactory;
    
     public static function houseajexadminuser($draw, $searchValue, $columnSortOrder, $columnName, $columnIndex, $filltersearchValue, $rowcount, $length)
{
 
    $user_id = session('SUPERADMIN_ID');
    
    $start = $draw;
    if (!is_numeric($start)) {
        $start = 0;
    }
    if (!is_numeric($length) || $length <= 0) {
        $length = 10; 
    }

    $canViewGlobal = permission($user_id, '4', 'action_globle_view');
    if ($user_id == '1') {
        $query = DB::table('superadmins')->orderBy('id', 'DESC');
    }
    elseif($canViewGlobal){
          $query = DB::table('superadmins')->orderBy('id', 'DESC');
    }
    else {
        $query = DB::table('superadmins')->where('id', $user_id)->orderBy('id', 'DESC');
    }


    if ($searchValue) {
        $query->where(function ($q) use ($searchValue) {
            $q->where('username', 'like', '%' . $searchValue . '%')
              ->orWhere('mobile', 'like', '%' . $searchValue . '%')
               ->orWhere('firstname', 'like', '%' . $searchValue . '%')
              ->orWhere('email', 'like', '%' . $searchValue . '%');
        });
    }

    // Debugging SQL query (optional)
    // $sql = $query->toSql();
    // $bindings = $query->getBindings();

    if ($rowcount == 1) {
        
        $query->skip($start)->take($length);
        return $query->get();
    } else {
        return $query->count();
    }
}
}
