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

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

     public function register(array $values) {
        $sql = "INSERT INTO staff_login ";
        $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,$staff_id) {
        
            $sql = "UPDATE staff_login SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE staff_id = '" .$staff_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 auth($staff_id,$password) {
        $statement = $this->db->prepare("SELECT * FROM staff_login WHERE staff_id=:staff_id AND password=:password  LIMIT 1");
        $statement->bindParam(':staff_id', $staff_id);
        $statement->bindParam(':password', $password);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

      }

$auth = new Auth;
?>