<?php
class Client{

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

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

        $text = "SELECT client_email FROM $this->table WHERE client_email = '$email'";
        $query = $crud->conn()->query($text);
        if($query->num_rows >= 1){
            $result = 1;//can be used
        }
        return $result;
    }
    public function get_data_select($crud, $keyword){
        $result = 0;
        
        $cond_keyword = "";
        if($keyword != ""){
            $cond_keyword = "WHERE client_name LIKE '%$keyword%'";
        }

        $text = "SELECT * FROM $this->table LEFT JOIN T_Region ON client_ref_region = region_id $cond_keyword ORDER BY client_name ASC, client_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, $region, $keyword){
        $result = 0;

        $cond_region = "";
        $cond_keyword = "";
        
        if($region != ""){
            $cond_region = "WHERE client_ref_region = '$region'";
            if($keyword != ""){
                $cond_keyword = "AND client_name LIKE '%$keyword%'";
            }
        } else {
            if($keyword != ""){
                $cond_keyword = "WHERE client_name LIKE '%$keyword%'";
            }
        }

        //get total data
        $text_total = "SELECT * FROM $this->table $cond_region $cond_keyword";
        $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 LEFT JOIN T_Region ON client_ref_region = region_id $cond_region $cond_keyword GROUP BY client_id ORDER BY client_create_date DESC, client_name ASC 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 client_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_client_email($crud, $email){
        $result = 0;

        $text = "SELECT * FROM $this->table WHERE client_email = '$email' 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 create_client($crud,$cname,$cemail,$cphone,$cregion,$caddress){
        $result = 0;
        date_default_timezone_set("Asia/Jakarta");
        $now = date("Y-m-d H:i:s");

        $text = "INSERT INTO $this->table (client_name, client_phone, client_email, client_address, client_ref_region, client_create_date) VALUES('$cname', '$cphone', '$cemail', '$caddress', '$cregion', '$now')";
        $result = $crud->execute_id($text);
        return $result;
    }
    public function check_client($crud, $region, $client_name){
        $result = 0;

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

        $text = "INSERT INTO $this->table (client_name, client_phone, client_email, client_address, client_ref_region, client_create_date) VALUES('$name', '$phone', '$email', '$address', '$region_id', '$now')";
        $result = $crud->execute_id($text);
        return $result;
    }

    public function update_data($crud, $id, $name, $phone, $email, $address, $region_id){ 
        $result = 0;

        $text = "UPDATE $this->table SET client_name = '$name', client_phone = '$phone', client_email = '$email', client_address = '$address', client_ref_region = '$region_id' WHERE client_id = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }

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

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

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

        $text = "UPDATE $this->table SET client_active = '$activate' WHERE client_id = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }

    public function generate_token(){
        $result = 0;

        date_default_timezone_set("Asia/Jakarta");
        $now = date("dmy");

        $result = $now.strtoupper(substr(sha1(mt_rand()),17,14));

        return $result;
    }

    public function generate_id(){
        $result = 0;

        date_default_timezone_set("Asia/Jakarta");
        $now = date("dny");

        $result = "CLT".$now."-".strtoupper(substr(sha1(mt_rand()),17,4));

        return $result;
    }

    public function client_reset_code($crud, $email, $token){
        $result = 0;
        $cond = "";

        $text = "UPDATE $this->table SET client_reset_code = '$token' WHERE client_email = '$email'";
        $result = $crud->execute_query($text);
        return $result;
    }

    public function update_password($crud, $id, $password){ //$subtitle,
        $result = 0;
        $cond = "";

        $text = "UPDATE $this->table SET client_password = '$password' WHERE client_id = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }

    public function set_new_password($crud, $email, $token, $password){ //$subtitle,
        $result = 0;
        $cond = "";

        $text = "UPDATE $this->table SET client_password = '$password', client_reset_code = '' WHERE client_email = '$email'";
        $result = $crud->execute_query($text);
        return $result;
    }
//END FUNCTION FOR ADMIN PAGE
}
?>