<?php
class Activity{

    private $table = "T_Activity";
    

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

        //get total data
        $text_total = "SELECT activity_ref_po, activity_type, activity_ref_user, activity_notes, activity_create_date, admin_name, admin_img FROM $this->table LEFT JOIN T_Admin ON activity_ref_user = admin_id WHERE activity_type IN ('open','progress','prepared','complete','cancel')";
        $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 activity_ref_po, activity_type, activity_ref_user, activity_notes, activity_create_date, admin_name, admin_img FROM $this->table LEFT JOIN T_Admin ON activity_ref_user = admin_id WHERE activity_type IN ('open','progress','prepared','complete','cancel') ORDER BY activity_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($crud, $po_id){
        $result = 0;

        $text = "SELECT activity_ref_po, activity_create_date FROM $this->table WHERE activity_ref_po = '$po_id' GROUP BY DATE_FORMAT(activity_create_date, '%Y-%m-%d') ORDER BY activity_create_date DESC";
        $query = $crud->conn()->query($text);
        if($query->num_rows >= 1){
            $result = array();
            $loop = 0;
            while($row = $query->fetch_assoc()){
                $result[$loop] = $row;

                //query assets
                $text_detail = "SELECT * FROM $this->table WHERE activity_ref_po = '$po_id' AND DATE_FORMAT(activity_create_date, '%Y-%m-%d') = DATE_FORMAT('{$row['activity_create_date']}', '%Y-%m-%d') ORDER BY activity_create_date DESC";
                $query_detail = $crud->conn()->query($text_detail);
                if($query_detail->num_rows >= 1){
                    while($row_detail = $query_detail->fetch_assoc()){
                        $result[$loop]['po'][] = $row_detail;
                    }
                }
                $loop++;
            }
        }
        return $result;
    }

    public function insert_activity($crud, $po_id, $user_id, $user_name, $type, $notes){
        $result = 0;
        date_default_timezone_set("Asia/Jakarta");
        $now = date("Y-m-d H:i:s");
        if($po_id != ""){
            if($type == 'progress' || $type == 'prepared' || $type == 'complete' || $type == 'cancel'){
                //save stat date
                $this->update_po_date($crud, $po_id, $type, $now);
            }
        }
        $text = "INSERT INTO $this->table (activity_ref_po, activity_ref_user, activity_ref_name, activity_type, activity_notes, activity_create_date) VALUES('$po_id', '$user_id', '$user_name', '$type', '$notes', '$now')";
        $result = $crud->execute_query($text);
        return $result;
    }
    public function insert_activity_date($crud, $po_id, $user_id, $user_name, $type, $notes, $cdate){
        $result = 0;
        date_default_timezone_set("Asia/Jakarta");
        $now = date("Y-m-d H:i:s");

        $text = "INSERT INTO $this->table (activity_ref_po, activity_ref_user, activity_ref_name, activity_type, activity_notes, activity_create_date) VALUES('$po_id', '$user_id', '$user_name', '$type', '$notes', '$cdate')";
        $result = $crud->execute_query($text);
        return $result;
    }

    public function update_po_date($crud, $po_id, $stat, $create_date){
        $result = 0;

        $cond_up = "";
        if($stat == 'progress'){
            $cond_up = "po_progress_date = '$create_date'";
        } else if ($stat == 'prepared'){
            $cond_up = "po_prepared_date = '$create_date'";
        } else if ($stat == 'complete'){
            $cond_up = "po_complete_date = '$create_date'";
        } else if ($stat == 'cancel'){
            $cond_up = "po_cancel_date = '$create_date'";
        }
        if($cond_up != ""){
            $text = "UPDATE T_Shipment SET $cond_up WHERE po_id = '$po_id'";
            $result = $crud->execute_query($text);
        }
        return $result;
    }

    public function get_po_stat($crud, $stat){
        $result = 0;

        $text = "SELECT * FROM $this->table WHERE activity_type = '$stat' ORDER BY activity_id 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_prepared_by($crud, $po_id){
        $result = 0;

        $text = "SELECT activity_id, activity_ref_user, admin_name, admin_email FROM $this->table LEFT JOIN T_Admin ON activity_ref_user = admin_id WHERE activity_ref_po = '$po_id' AND activity_type = 'prepared' ORDER BY activity_id ASC LIMIT 1";
        $query = $crud->conn()->query($text);
        if($query->num_rows >= 1){
            $result = array();
            while($row = $query->fetch_assoc()){
                $result[] = $row;
            }
        }
        return $result;
    }

}
?>