<?php
namespace model;

use model\database;


class books {
    
    public static function get_book_by_id(int $bid) {
        return (new database())->table('books')->where(['bid' => $bid])->select();
    }
    
    public static function del_book_by_id(int $bid) {
        return (new database())->table('books')->where(['bid' => $bid])->delete();
    }
    
    public static function add_book($name, $author, $publish, $isbn, $cover, $cid, $lang) {
        return (new database())->table('books')->insert([
            'name' => $name,
            'author' => $author,
            'publish' => $publish,
            'isbn' => $isbn,
            'cover' => $cover,
            'cid' => (int)$cid,
            'lang' => $lang
        ]);
    }
    
    public static function update_book(int $bid, $name, $author, $publish, $isbn, $cover, $cid, $lang) {
        return (new database())->table('books')->where(['bid' => $bid])->update([
            'name' => $name,
            'author' => $author,
            'publish' => $publish,
            'isbn' => $isbn,
            'cover' => $cover,
            'cid' => (int)$cid,
            'lang' => $lang
        ]);
    }
    
    public static function update_book_category(int $cid, $category) {
        return (new database())->table('books')->where(['cid' => $cid])->update(['cid' => (int)$category]);
    }
    
    public static function list_books(int $page = 1) {
        $books = (new database())->table('books')->page(20, $page);
        return isset($books[0]['bid']) ? $books : [$books];
    }
    
    public static function find_books($word, int $page = 1) {
        $books = (new database())->table('books')->like('name', $word)->page(20, $page);
        return isset($books[0]['bid']) ? $books : [$books];
    }
    
    public static function count_books() {
        return (new database())->table('books')->count();
    }
    
    public static function count_find_books($word) {
        return (new database())->table('books')->like('name', $word)->count();
    }
    
}
