<?php
require_once 'ConnectionDao.php';
class LogBook{
	  private $db = null;
    private $rows = null;
    private $success = false;

    public function __construct() {
        $this->db=ConnectionDao::getDb();
    }

     public function Signin(array $values) {
        $sql = "INSERT INTO logbook ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $sql .= '(' . implode(',', $fields) . ') ';
        $arr = array();
        foreach ($fields as $f) {
            $arr[] = '?';
        }
        $sql .= 'VALUES (' . implode(',', $arr) . ') ';
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i => $v) {
            $statement->bindValue($i + 1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }


    public function update(array $values,$log_id) {
        
            $sql = "UPDATE logbook SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE log_id = '" .$log_id."' LIMIT 1 ";
            $statement = $this->db->prepare($sql);
            foreach ($vals as $i=>$v) {
            $statement->bindValue($i+1, $v);
            }
           $this->success= $statement->execute();
            return $this->success;

            }


    public function get_staff_log($staff_id) {
        $statement = $this->db->prepare("SELECT * FROM Logbook WHERE staff_id=:staff_id");
        $statement->bindParam(':staff_id', $staff_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

     public function get_all_staff_log() {
        $statement = $this->db->prepare("SELECT a.*,b.* FROM Logbook a JOIN staff_detail b ON a.staff_id=b.staff_id ");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

     public function get_staff_details($staff_id) {
        $statement = $this->db->prepare("SELECT a.*,b.* FROM Logbook a JOIN staff_detail b ON a.staff_id=b.staff_id WHERE a.staff_id=:staff_id LIMIT 1");
        $statement->bindParam(':staff_id', $staff_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

      }

$logbook = new LogBook;
?>