<?php
class Level {
    private $id;
    private $name;
    private $type;
    private $description;

    private $level;

    public function loadFromId($id) {
        global $wpdb;

        $sql = "
            SELECT IDLevel, name, type, description, level FROM cass_level WHERE IDLevel = %d;
        ";
        $result = $wpdb->get_row($wpdb->prepare($sql, $id));

        if ($result) {
            $this->setId($result->IDMaterial);
            $this->setName($result->name);
            $this->setType($result->type);
            $this->setDescription($result->description);
            $this->setLevel($result->level);
            return true;
        } else {
            return false;
        }
    }

    //Update
    public function update() {
        global $wpdb;

        $sql = "
            UPDATE cass_level
            SET name = %s, type=%s, description = %s, level = %d
            WHERE IDLevel = %d;
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getName(),$this->getType(), $this->getDescription(), $this->getLevel(),  $this->getId()));


         $table = 'cass_level';
    }

    //Delete
    public function delete() {
        global $wpdb;

        $sql = "
            DELETE FROM cass_level
            WHERE IDLevel = %d;
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getId()));
    }

    //Insert
    public function create() {
        global $wpdb;

        $sql = "
        INSERT INTO cass_level(name, type, description, level)
        VALUES(%s,%s,%s,%d);
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getName(),$this->getType(), $this->getDescription(), $this->getLevel()));
    }

    public function sqlWPTable() {
        global $wpdb;

        $sql = "
            SELECT IDLevel, name, type, description, level 
            FROM cass_level ORDER BY type ASC;
             ";
     
        return $wpdb->get_results(
            $sql,
            ARRAY_A
        );
    }

    public static function selectAll() {
        global $wpdb;

        $sql = "
            SELECT IDLevel, name, type, description, level 
            FROM cass_level ORDER BY type ASC;
             ";
     
        return $wpdb->get_results($sql);
    }

    // Getter and setters
    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getType() {
        return $this->type;
    }

    public function getDescription() {
        return $this->description;
    }

    public function getLevel() {
        return $this->level;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setType($type) {
        $this->type = $type;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function setLevel($level) {
        $this->level = $level;
    }
}
