<?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/Document.php");
$obj_document = new Document();

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_document == 1){
        if($O_po != ""){
            $data = $obj_po->get_detail_po($obj_crud, $O_po);
            if(is_array($data)){
            	$documents = $obj_document->get_data_po($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'] == "upload"){

        $po_id = $obj_crud->escape_string(check_input($_POST['po_id']));
        $filename = $obj_crud->escape_string(check_input($_POST['filenames']));
        $N_user_id = $_SESSION['admin_id'];
        $N_user_name = $_SESSION['admin_username'];
        
        if($_FILES["document"]["name"]!=null){
            $ran = rand();
            $timestamp = time();
            $doc_link = "uploads/document/".$timestamp.$ran.cleanSpace($_FILES["document"]["name"]);

            if ((($_FILES["document"]["type"] == "application/pdf") || ($_FILES["document"]["type"] == "application/doc") || ($_FILES["document"]["type"] == "application/docx") || ($_FILES["document"]["type"] == "image/gif") || ($_FILES["document"]["type"] == "image/jpeg") || ($_FILES["document"]["type"] == "image/jpg") || ($_FILES["document"]["type"] == "image/JPG") || ($_FILES["document"]["type"] == "image/png")) && ($_FILES["document"]["size"] < 10485760) && ($_FILES["document"]["name"] != "")){

                if (move_uploaded_file($_FILES["document"]["tmp_name"], "../../".$doc_link)) {
                    $result = $obj_document->insert_data($obj_crud, $po_id, $filename, $doc_link);
                    if($result != 0){
                        $act_type = "upload";
                        $act_notes = "Upload document ".$filename;
                        $result = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);
                        $documents = $obj_document->get_data_po($obj_crud, $po_id);
                        if(is_array($documents)){
                            $doc_info = $obj_po->update_doc_info($obj_crud, $po_id, 'yes');
                        }

                        $_SESSION['vmessage'][] = "File successfully uploaded.";
                        $_SESSION['valert'][] = "success";
                    } else {
                        $_SESSION['vmessage'][] = "Failed to upload document! Something is wrong with the code.";
                        $_SESSION['valert'][] = "success";
                    }
                } else {
                    $_SESSION['vmessage'][]= "Failed to upload document! Something is wrong with the files."; 
                    $_SESSION['valert'][] = "error";
                }
            } else {
                $_SESSION['vmessage'][]= "Failed to upload document! File format has to be pdf, doc, docx, jpg, jpeg, png, gif and less than 2Mb."; 
                $_SESSION['valert'][] = "error";
            }
        } else {
            $_SESSION['vmessage'][]= "Please upload the file document!"; 
            $_SESSION['valert'][] = "error";
        }
        // $_SESSION['vmessage'][]= "Please upload the file document!"; 
        // $_SESSION['valert'][] = "error";

        header("Location:".$path['po-document'].$po_id."/");

    } else if($_GET['action'] == "delete"){

        $po_id = $obj_crud->escape_string(check_input($_GET['poid']));
        $docid = $obj_crud->escape_string(check_input($_GET['docid']));
        $N_user_id = $_SESSION['admin_id'];
        $N_user_name = $_SESSION['admin_username'];

        $result = $obj_document->delete_data($obj_crud, $docid, $global['root']);
        if($result != 0){
            $act_type = "delete";
            $act_notes = "Delete 1 document from the list";
            $result = $obj_activity->insert_activity($obj_crud, $po_id, $N_user_id, $N_user_name, $act_type, $act_notes);
            $documents = $obj_document->get_data_po($obj_crud, $po_id);
            if(is_array($documents)){
                $doc_info = $obj_po->update_doc_info($obj_crud, $po_id, 'yes');
            } else {
                $doc_info = $obj_po->update_doc_info($obj_crud, $po_id, 'no');
            }
            $_SESSION['vmessage'][] = "Successfully deleted files.";
            $_SESSION['valert'][] = "success";
        } else {
            $_SESSION['vmessage'][] = "Failed to delete files! Something is wrong with the code.";
            $_SESSION['valert'][] = "success";
        }
        header("Location:".$path['po-document'].$po_id."/");
    }
}
?>