<?php
class Admin{

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

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

        $text = "SELECT * FROM T_Admin WHERE admin_pic_client = 'yes' ORDER BY admin_name 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_admin_pic($crud){
        $result = 0;

        $text = "SELECT * FROM T_Admin WHERE admin_pic = 'yes' AND admin_pic_client = 'no' ORDER BY admin_name 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_list_admin_pallet($crud){
        $result = 0;

        $text = "SELECT admin_id, admin_name, admin_email, role_slug, 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 role_slug = 'warehouse_man_power' OR role_slug = 'warehouse_manager' OR role_slug = 'developer' GROUP BY admin_id ORDER BY admin_name 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_list_email_v2($crud, $slug){
        $result = 0;

        $text = "SELECT access_title, GROUP_CONCAT(admin_email) AS emails 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_active = 'active' AND access_slug = '$slug' GROUP BY access_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_list_picker($crud){
        $result = 0;

        $text = "SELECT admin_id, admin_email, admin_name, role_title, role_slug, 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 access_slug = 'picker' GROUP BY admin_name 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_list_email($crud, $slug){
        $result = 0;

        $text = "SELECT admin_name, admin_email, 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 access_slug = '$slug' GROUP BY admin_id";
        $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_reset($crud, $token){
        $result = 0;

        $text = "SELECT * FROM $this->table WHERE admin_reset_code = '$token'";
        $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_create($crud, $token){
        $result = 0;

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

    public function check_email($crud, $email){
        $result = 0;

        $text = "SELECT admin_email FROM $this->table WHERE admin_email = '$email'";
        $query = $crud->conn()->query($text);
        if($query->num_rows >= 1){
            $result = 1;//can be used
        }
        return $result;
    }

    public function login($crud, $email, $password){
        $result = 0; //FAILED

        $text = "SELECT * FROM $this->table LEFT JOIN T_Role ON admin_role = role_id WHERE admin_email = '$email' AND admin_password = '$password' AND admin_active = 'active' LIMIT 0,1";
        $query = $crud->conn()->query($text);
        if($query->num_rows == 1){//HAS TO BE EXACT 1 RESULT
            $row = $query->fetch_assoc();
            $_SESSION['admin_username'] = $row['admin_name'];
            $_SESSION['admin_email'] = $row['admin_email'];
            $_SESSION['admin_id'] = $row['admin_id'];
            $_SESSION['admin_role'] = $row['admin_role'];
            $_SESSION['admin_role_name'] = $row['role_title'];
            $_SESSION['admin_img'] = $row['admin_img'];
            $_SESSION['admin_img_thmb'] = $row['admin_img_thmb'];
            $_SESSION['admin_pic'] = $row['admin_pic'];
            $_SESSION['admin_region'] = $row['admin_ref_region'];
            $result = 1;
        }
        return $result;
    }

    public function admin_check_login(){
        $result = 0;

        if(isset($_SESSION['admin_id'])){
            $result = 1;
        }
        return $result;
    }

    public function admin_logout(){
        unset($_SESSION['admin_username']);
        unset($_SESSION['admin_email']);
        unset($_SESSION['admin_id']);
        unset($_SESSION['admin_img']);
        unset($_SESSION['admin_img_thmb']);
        unset($_SESSION['admin_role']);
        unset($_SESSION['admin_pic']);
        unset($_SESSION['admin_region']);
        return true;
    }

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

        //get total data
        $text_total = "SELECT * FROM $this->table LEFT JOIN T_Role ON role_id = admin_role GROUP BY admin_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 * FROM $this->table LEFT JOIN T_Role ON role_id = admin_role LEFT JOIN T_Region ON admin_ref_region = region_id GROUP BY admin_id ORDER BY admin_email ASC, admin_create_date ASC 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 insert_reset_code($crud, $email, $token){
        $result = 0;
        $cond = "";

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

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

        $text = "SELECT * FROM $this->table WHERE admin_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 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 = "ADM".$now."-".strtoupper(substr(sha1(mt_rand()),17,4));

        return $result;
    }

    public function create_data_admin($crud, $email, $username, $role, $tokens, $pic, $adminc, $region){
        $result = 0;
        date_default_timezone_set("Asia/Jakarta");
        $now = date("Y-m-d H:i:s");

        $activate = "inactive";

        $admin_id = $this->generate_id();

        $text = "INSERT INTO $this->table (admin_id, admin_email, admin_name, admin_role, admin_token, admin_active, admin_pic, admin_pic_client, admin_ref_region, admin_create_date) VALUES('$admin_id', '$email', '$username', '$role', '$tokens', '$activate', '$pic', '$adminc', '$region', '$now')";
        $result = $crud->execute_query($text);
        return $result;
    }

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

        $admin_id = $this->generate_id();

        $text = "INSERT INTO $this->table (admin_id, admin_email, admin_name, admin_password, admin_img, admin_img_thmb, admin_create_date) VALUES('$admin_id', '$email', '$username', '$password', '$img', '$img_thmb', '$now')";
        $result = $crud->execute_query($text);
        return $result;
    }

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

        $text = "UPDATE $this->table SET admin_token = '', admin_active = 'active', admin_password = '$password' WHERE admin_email = '$email' AND admin_token = '$token'";
        $result = $crud->execute_query($text);
        return $result;
    }

    public function update_data($crud, $id, $role, $email, $username, $pic, $adminc, $region, $img, $img_thmb, $path){ //$subtitle,
        $result = 0;
        $cond = "";

        if($img != ""){
            $this->remove_image($crud, $id, $path); //remove image before
            $cond = ", admin_img = '$img', admin_img_thmb = '$img_thmb' ";
        }
        $text = "UPDATE $this->table SET admin_email = '$email', admin_name = '$username', admin_role = '$role', admin_pic = '$pic', admin_ref_region = '$region', admin_pic_client = '$adminc' $cond WHERE admin_id = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }

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

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

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

        $text = "UPDATE $this->table SET admin_active = '$activate', admin_token = '' WHERE admin_id = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }

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

        $text = "UPDATE $this->table SET admin_token = '$token' WHERE admin_id = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }

    public function delete_data($crud, $id, $path){
        $result = 0;
        $this->remove_image($crud, $id, $path); //remove image before

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

    public function remove_image($crud, $id, $path){
        $result = 0;
        $flag_img = 0;
        $flag_img_thmb = 0;

        $text = "SELECT admin_id, admin_img, admin_img_thmb FROM $this->table WHERE admin_id = '$id'";
        $query = $crud->conn()->query($text);
        if($query->num_rows == 1){
            $row = $query->fetch_assoc();
            $deleteImg = $path.$row['admin_img'];
            if (file_exists($deleteImg)) {
                unlink($deleteImg);
                $flag_img = 1;
            }
            $deleteImgThmb = $path.$row['admin_img_thmb'];
            if (file_exists($deleteImgThmb)) {
                unlink($deleteImgThmb);
                $flag_img_thmb = 1;
            }
            if($flag_img == 1 && $flag_img_thmb ==1){
                $result = 1;
            }
        }
        return $result;
    }

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

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