<?php
class Access{

    private $table = "T_Access";
    private $itemPerPageAccess = 10;

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

        //get total data
        $text_total = "SELECT * FROM $this->table";
        $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->itemPerPageAccess);
        $limitBefore = $page <= 1 || $page == null ? 0 : ($page-1) * $this->itemPerPageAccess;

        $text = "SELECT * FROM $this->table ORDER BY access_module ASC, access_create_date ASC LIMIT $limitBefore, $this->itemPerPageAccess";
        $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_data_module($crud, $module){
        $result = 0;

        $text = "SELECT * FROM $this->table WHERE access_module = '$module' ORDER BY access_sort ASC, access_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 access_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, $module, $slug){
        $result = 0;
        date_default_timezone_set("Asia/Jakarta");
        $now = date("Y-m-d H:i:s");

        $text = "INSERT INTO $this->table (access_title, access_module, access_slug, access_create_date) VALUES('$title', '$module', '$slug', '$now')";
        $result = $crud->execute_query($text);
        return $result;
    }

    public function check_access($crud, $user, $slug){
        $result = 0;

        $text = "SELECT admin_id, admin_email, admin_name, role_title, access_title FROM T_Admin LEFT JOIN T_Role ON admin_role = role_id LEFT JOIN AT_Access ON ata_ref_role = role_id LEFT JOIN T_Access ON ata_ref_access = access_id WHERE admin_id = '$user' AND access_slug = '$slug' GROUP BY admin_id";
        $query = $crud->conn()->query($text);
        if (!empty($query) && $query->num_rows > 0) {
            $result = 1;
        }
        return $result;
    }

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

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

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

        $text = "INSERT INTO AT_Access (ata_ref_role, ata_ref_access, ata_create_date) VALUES('$role_id', '$access_id', '$now')";
        $result = $crud->execute_query($text);
        return $result;
    }

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

        $text = "DELETE FROM AT_Access WHERE ata_ref_role = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }

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

        $text = "UPDATE $this->table SET access_title = '$title', access_module = '$module', access_slug = '$slug' WHERE access_id = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }

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

        $text = "DELETE FROM $this->table WHERE access_id = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }
}
?>