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

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

     public function compose(array $values) {
        $sql = "INSERT INTO mail ";
        $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_seen(array $values,$mail_id) {
        
            $sql = "UPDATE mail SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE mail_id = '" .$mail_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 update_deleted(array $values,$mail_id) {
        
            $sql = "UPDATE mail SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE mail_id = '" .$mail_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_inbox($staff_id) {
        $statement = $this->db->prepare("SELECT a.*,b.* FROM mail a JOIN staff_detail b ON a.user_from=b.staff_id WHERE a.user_to=:staff_id and deleted='no' ORDER BY mail_id DESC");
        $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_inbox_unseen($staff_id) {
        $statement = $this->db->prepare("SELECT a.*,b.* FROM mail a JOIN staff_detail b ON a.user_from=b.staff_id WHERE a.user_to=:staff_id and deleted='no' AND seen='no' ORDER BY mail_id DESC");
        $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_trash($staff_id) {
        $statement = $this->db->prepare("SELECT a.*,b.* FROM mail a JOIN staff_detail b ON a.user_from=b.staff_id WHERE a.user_to=:staff_id and deleted='yes' ORDER BY mail_id DESC");
        $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 open_email($mail_id) {
        $statement = $this->db->prepare("SELECT a.*,b.* FROM mail a JOIN staff_detail b ON a.user_from=b.staff_id WHERE a.mail_id=:mail_id LIMIT 1");
        $statement->bindParam(':mail_id', $mail_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function get_inbox_count($staff_id) {
        $statement = $this->db->prepare("SELECT * FROM mail WHERE user_to=:staff_id AND deleted='no' ORDER BY mail_id DESC");
        $statement->bindParam(':staff_id', $staff_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $statement->rowCount();            
        }
        return $statement->rowCount();
    }

     public function get_trash_count($staff_id) {
        $statement = $this->db->prepare("SELECT * FROM mail WHERE user_to=:staff_id AND deleted='yes' ORDER BY mail_id DESC");
        $statement->bindParam(':staff_id', $staff_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $statement->rowCount();            
        }
        return $statement->rowCount();
    }

    public function get_sent($staff_id) {
        $statement = $this->db->prepare("SELECT * FROM mail WHERE user_from=:staff_id ORDER BY mail_id DESC");
        $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;
    }

      }

$mail = new Mail;
?>