<?php
class Role{

    private $table = "T_Role";
    private $itemPerPageAdmin = 10;


    public function get_data($crud, $page){
        $result = 0;

        //get total data
        $text_total = "SELECT role_id, role_title, role_create_date, COUNT(ata_id) AS total_access FROM $this->table LEFT JOIN AT_Access ON role_id = ata_ref_role GROUP BY role_id";
        $query_total = $crud->conn()->query($text_total);
        $total_data  = $query_total->num_rows;
        if($total_data < 1){$total_data = 0;}

        //get total page
        $total_page  = ceil($total_data / $this->itemPerPageAdmin);
        $limitBefore = $page <= 1 || $page == null ? 0 : ($page-1) * $this->itemPerPageAdmin;

        $text = "SELECT role_id, role_title, role_create_date, COUNT(ata_id) AS total_access FROM $this->table LEFT JOIN AT_Access ON role_id = ata_ref_role GROUP BY role_id ORDER BY total_access DESC, role_create_date LIMIT $limitBefore, $this->itemPerPageAdmin";
        $query = $crud->conn()->query($text);
        if($query->num_rows >= 1){
            $result = array();
            while($row = $query->fetch_assoc()){
                $result[] = $row;
            }
        }

        if(is_array($result)){
            $result[0]['total_page'] = $total_page;
            $result[0]['total_data_all'] = $total_data;
            $result[0]['total_data'] = count($result);
        }
        return $result;
    }

    public function get_select_list($crud){
        $result = 0;

        $text = "SELECT * FROM $this->table ORDER BY role_title ASC";
        $query = $crud->conn()->query($text);
        if($query->num_rows >= 1){
            $result = array();
            while($row = $query->fetch_assoc()){
                $result[] = $row;
            }
        }
        return $result;
    }

    public function get_data_detail($crud, $id){
        $result = 0;

        $text = "SELECT * FROM $this->table WHERE role_id = '$id'";
        $query = $crud->conn()->query($text);
        if($query->num_rows >= 1){
            $result = array();
            while($row = $query->fetch_assoc()){
                $result[] = $row;
            }
        }
        return $result;
    }

    public function insert_data($crud, $title, $slug){
        $result = 0;
        date_default_timezone_set("Asia/Jakarta");
        $now = date("Y-m-d H:i:s");

        $text = "INSERT INTO $this->table (role_title, role_slug, role_create_date) VALUES('$title', '$slug', '$now')";
        $result = $crud->execute_id($text);
        return $result;
    }

    public function update_data($crud, $id, $title, $slug){
        $result = 0;

        $text = "UPDATE $this->table SET role_title = '$title', role_slug = '$slug' WHERE role_id = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }

    public function delete_data($crud, $id){
        $result = 0;

        $text = "DELETE FROM $this->table WHERE role_id = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }
//END FUNCTION FOR ADMIN PAGE
}
?>