<?php
require_once($global['root-model']."Crud.php");
$obj_crud = new Crud();

require_once($global['root']."model/PO.php");
$obj_po = new PO();

require_once($global['root']."model/Activity.php");
$obj_activity = new Activity();

require_once($global['root-model']."Access.php");
$obj_access = new Access();

if(!isset($_GET['action'])){ 
    $O_po = isset($_GET['po']) ? $_GET['po'] : "";

    $acsp_edit = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'edit_po');
    $acsp_print = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'print_po');
    $acsp_viewact = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'view_activity');
    $acsp_createact = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'create_activity');
    $acsp_document = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'upload_document');
    $acsp_prepared = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'change_stat_prepare');
    $acsp_progress = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'change_stat_progress');
    $acsp_complete = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'change_stat_complete');
    $acsp_cancel = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'change_stat_cancel');
    $acsp_paid = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'change_stat_paid');
    $acsp_invoice = $obj_access->check_access($obj_cruds,$_SESSION['admin_id'],'update_invoice');

    if($acsp_viewact == 1){
        if($O_po != ""){
            $data = $obj_po->get_detail_po($obj_crud, $O_po);
            if(is_array($data)){
            	$activitys = $obj_activity->get_data($obj_crud, $O_po);
            }
        } else {
            header("Location:".$path['dashboard']);
        }
    } else {
        header("Location:".$path['dashboard']);
    }

    if(isset($_SESSION['vmessage'])){
        foreach($_SESSION['vmessage'] as $svm){
            $vmessage[] = $svm;
        }
        unset($_SESSION['vmessage']);
    } else {
        $vmessage = "";
    }

    if(isset($_SESSION['valert'])){
        foreach($_SESSION['valert'] as $vlr){
            $valert[] = $vlr;
        }
        unset($_SESSION['valert']);
    } else {
        $valert = "";
    }
} else {
    if($_GET['action'] == "notes"){

        $po_id = $obj_crud->escape_string(check_input($_POST['po_id']));
        $act_notes = $obj_crud->escape_string(check_input($_POST['note']));
        $act_type = "notes";
        $N_user_id = $_SESSION['admin_id'];
        $N_user_name = $_SESSION['admin_username'];

        $result = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);
        if($result > 0){
            $_SESSION['vmessage'][] = "Successfully create notes in PO ".$po_id.".";
            $_SESSION['valert'][] = "success";
        } else {
            $_SESSION['vmessage'][]= "Failed to create new notes!"; 
            $_SESSION['valert'][] = "error";
        }
        header("Location:".$path['po-activity'].$po_id."/");

    }
}
?>