<?php
class Address{

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

        //get total data
        $text_total = "SELECT address_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_detail($crud, $id){
        $result = 0;

        $text = "SELECT * FROM $this->table WHERE address_id = '$id' 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 get_data($crud, $keyword){
        $result = 0;
        $cond_keyword = "";
        if($keyword != ""){
            $cond_keyword = "WHERE address_name LIKE '%$keyword%'";
        }

        $text = "SELECT * FROM $this->table $cond_keyword ORDER BY address_name ASC, address_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 check_address($crud, $address){
        $result = 0;

        $text = "SELECT * FROM $this->table WHERE address_name = '$address' 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, $name, $desc, $contact, $phone){
        $result = 0;
        date_default_timezone_set("Asia/Jakarta");
        $now = date("Y-m-d H:i:s");

        $text = "INSERT INTO $this->table (address_name, address_desc, address_contact, address_phone, address_create_date) VALUES('$name', '$desc', '$contact', '$phone', '$now')";
        $result = $crud->execute_id($text);
        return $result;
    }

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

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