<?php

namespace App\Models\Libskill;

use App\Models\BaseModel;

class LibModel extends BaseModel
{
	protected $table            = 'libss';
    protected $primaryKey       = 'id';
    protected $allowedFields    = ['judul', 'uraian', 'penulis', 'tag', 'cover', 'berkas', 'id_user', 'wkt_rek'];

    public function get_all()
    {
        $db = \Config\Database::connect();
        $builder = $db->table($this->table);
        $builder->select('*');
        $builder->orderBy($this->primaryKey, 'DESC');
        return $builder->get()->getResult();
    }

    public function cek_data($judul, $penulis)
    {
        $db = \Config\Database::connect();
        $builder = $db->table($this->table);
        $builder->where('judul', $judul);
        $builder->where('penulis', $penulis);
        return $builder->countAllResults();
    }

    public function get_data_with_pagination($limit = 12, $offset = 0, $search = '')
    {
        $db = \Config\Database::connect();
        $builder = $db->table($this->table);
        $builder->select('*');
        
        if (!empty($search)) {
            $builder->groupStart();
            $builder->like('judul', $search);
            $builder->orLike('penulis', $search);
            $builder->orLike('uraian', $search);
            $builder->groupEnd();
        }
        
        $builder->orderBy($this->primaryKey, 'DESC');
        $builder->limit($limit, $offset);
        
        return $builder->get()->getResult();
    }

    public function count_all_data($search = '')
    {
        $db = \Config\Database::connect();
        $builder = $db->table($this->table);
        
        if (!empty($search)) {
            $builder->groupStart();
            $builder->like('judul', $search);
            $builder->orLike('penulis', $search);
            $builder->orLike('uraian', $search);
            $builder->groupEnd();
        }
        
        return $builder->countAllResults();
    }
}