<?php
class Document{

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


    public function get_data_po($crud,$po_id){
        $result = 0;

        $text = "SELECT * FROM $this->table WHERE document_ref_po = '$po_id' ORDER BY document_create_date ASC";
        $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, $po_id, $filename, $doc_link){
        $result = 0;
        date_default_timezone_set("Asia/Jakarta");
        $now = date("Y-m-d H:i:s");

        $text = "INSERT INTO $this->table (document_ref_po, document_title, document_file, document_create_date) VALUES('$po_id', '$filename', '$doc_link', '$now')";
        $result = $crud->execute_id($text);
        return $result;
    }

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

        $text = "SELECT document_id, document_file FROM $this->table WHERE document_id = '$id'";
        $query = $crud->conn()->query($text);
        if($query->num_rows == 1){
            $row = $query->fetch_assoc();
            $deleteImg = $path.$row['document_file'];
            if (file_exists($deleteImg)) {
                unlink($deleteImg);
                $result = 1;
            }
        }
        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 document_id = '$id'";
        $result = $crud->execute_query($text);
        return $result;
    }
//END FUNCTION FOR ADMIN PAGE
}
?>