<?php
class Region{

    private $table = "T_Region";
    private $itemPerPageAdmin = 10;
    private $itemPerPageClient = 6;
//START FUNCTION FOR ADMIN PAGE
    public function total_data($crud){
        $result = 0;

        //get total data
        $text_total = "SELECT region_id FROM $this->table";
        $query_total = $crud->conn()->query($text_total);
        $total_data  = $query_total->num_rows;
        if($total_data < 1){$total_data = 0;}

        $result = $total_data;
        return $result;
    }
    public function get_data_select($crud){
        $result = 0;

        $text = "SELECT * FROM $this->table ORDER BY region_title ASC, region_create_date DESC";
        $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($crud, $keyword){
        $result = 0;
        $cond_keyword = "";
        if($keyword != ""){
            $cond_keyword = "WHERE region_title LIKE '%$keyword%'";
        }

        $text = "SELECT * FROM $this->table $cond_keyword ORDER BY region_title ASC, region_create_date DESC";
        $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_list($crud, $page, $itemPerPage){
        $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 / $itemPerPage);
        $limitBefore = $page <= 1 || $page == null ? 0 : ($page-1) * $itemPerPage;

        $text = "SELECT * FROM $this->table ORDER BY region_create_date DESC LIMIT $limitBefore, $itemPerPage";
        $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_detail($crud, $id){
        $result = 0;

        $text = "SELECT * FROM $this->table WHERE region_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 check_brand($crud, $title){
        $result = 0;

        $text = "SELECT * FROM $this->table WHERE region_title = '$title' LIMIT 1";
        $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){
        $result = 0;
        date_default_timezone_set("Asia/Jakarta");
        $now = date("Y-m-d H:i:s");

        $text = "INSERT INTO $this->table (region_title, region_create_date) VALUES('$title', '$now')";
        $result = $crud->execute_id($text);
        return $result;
    }

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

        $text = "UPDATE $this->table SET region_title = '$title' WHERE region_id = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }

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

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