<?php

/*
 * awframework from aw.inc present aw controller
 * it will help you develop easy and fast with secure tools
 * you can edit and add what you feel right
 * don't change in strucure of file
 */

require_once("../../core/controllers/imp/_imp.php");

class Controllers {

   //public vars
   public $smarty;
   public $hosturl;
   public $controllname;
   public $shcontrollname;
   public $active;

   // it will run in everytime with
   // default requried
   public function __construct() {
      require_once("../public/aw_config.php");
      require_once("../../core/controllers/imp/_autho.php"); //general functions
      $this->smarty = $smarty;
      $this->hosturl = $hosturl;

      //here name of controll to known
      $this->controllname = 'index';
      $this->shcontrollname = 'index';
      $this->smarty->assign('controllname', $this->controllname);
      $this->smarty->assign('shcontrollname', $this->shcontrollname);

      //breadcrumbs
      require_once("../../_library/plugins/Breadcrumbs/Breadcrumbs.php");
      $acty = filter_input(INPUT_GET, 'ac');
      $bc = new breadcrumbs($hosturl, '', '', $acty);
      $bc->crumbs();
      $breadcrumbs = $bc->breadcrumbs;
      $smarty->assign('breadcrumbs', $breadcrumbs);

      # here show the active page in menu
      // array called active[] with level of active element
      $active[0] = 'index'; //controller name or tab name
      $this->active = $active;
   }

   //here the frist page and
   //the frist action in this class
   public function index() {
      // get room id
      $room_name = filter_input(INPUT_GET, 'room_id');
      $this->smarty->assign('room_name', $room_name);
      $residence_room = R::findOne('residence_rooms', 'name = ?', [$room_name]);
      $this->smarty->assign('residence_room', $residence_room);
      // then check if time now return patient name , doctor name
      /*     $time_now = date('H:i');
        $today = date('Y-m-d');
        //old query
        //$room_info = R::findOne('visits', 'res_room_no = ? and (visitdate = "'.$today.'" and ADDTIME(res_p_exit, "0:0")  >= "' . $time_now . '" ) and ( TIME(res_enterance) <= "' . $time_now . '" visitdate = "'.$today.'" ) and deleted = 0 order by res_p_exit desc, id desc', [$residence_room->id]);
        //        $room_info = R::findOne('visits', 'res_room_no = ? and (TIME(res_enterance) <= TIME("'.$time_now.'") and visitdate = "'.$today.'") and
        //        (DATE_FORMAT(res_p_exit,"%Y-%m-%d %H:%i") >= DATE_FORMAT("'.$today.' '.$time_now.'","%Y-%m-%d %H:%i")) and deleted = 0 order by res_p_exit desc, id desc', [$residence_room->id]);

        //and (TIME(res_enterance_date) <= DATE_FORMAT("' . $today . ' ' . $time_now . '","%Y-%m-%d %H:%i"))  and
        //        (DATE_FORMAT(res_p_exit,"%Y-%m-%d %H:%i") >= DATE_FORMAT("' . $today . ' ' . $time_now . '","%Y-%m-%d %H:%i"))
        $room_info = R::findOne('visits', 'res_room_no = ? and res_enterance_date is not null and res_p_exit is null and deleted = 0 order by id desc', [$residence_room->id]);

        if ($room_info) {
        $patient_info = R::findOne('patients', 'id = ?', [$room_info->patientid]);
        $doctor_info = R::findOne('awusers', 'user_id = ?', [$room_info->for_doctor]);
        $room_info->patient_name = ($room_info->for_husband == 1) ? $patient_info->husdandname : $patient_info->wifename;
        $room_info->doctor_name = $doctor_info->name;
        }



        $this->smarty->assign('room_info', $room_info); */

      $this->refresh(300);
      $this->smarty->display('index.html');
   }

   public function roomimage() {
      $id = filter_input(INPUT_POST, 'room_id');
      $residence_room = R::load('residence_rooms', $id);
      echo $this->hosturl . '/../upload/residence_rooms/' . $residence_room->image;
   }

   public function roominfo() {
      // get room id
      $id = filter_input(INPUT_POST, 'room_id');
      // then check if time now return patient name , doctor name
      $time_now = date('H:i');
      $today = date('Y-m-d');

//        $room_info = R::findOne('visits', 'res_room_no = ? and (TIME(res_enterance_date) <= DATE_FORMAT("' . $today . ' ' . $time_now . '", "%Y-%m-%d %H:%i")) and
//        (DATE_FORMAT(res_p_exit,"%Y-%m-%d %H:%i") >= DATE_FORMAT("' . $today . ' ' . $time_now . '","%Y-%m-%d %H:%i")) and deleted = 0 order by id desc', [$id]);

      $room_info = R::findOne('visits', 'res_room_no = ? and res_enterance_date is not null and res_p_exit is null and deleted = 0 order by id desc', [$id]);

      $info = ['patient_name' => '', 'doctor_name' => ''];
      if ($room_info) {
         $patient_info = R::findOne('patients', 'id = ?', [$room_info->patientid]);
         $doctor_info = R::findOne('awusers', 'user_id = ?', [$room_info->for_doctor]);
         $room_info->patient_name = ($room_info->for_husband == 1) ? $patient_info->husdandname : $patient_info->wifename;
         $room_info->doctor_name = $doctor_info->name;

         $info = ['patient_name' => $room_info->patient_name, 'doctor_name' => $doctor_info->name];
      }

      echo json_encode($info);
   }

   // at the end of class call
   public function __destruct() {
      //close connection
      R::close();
   }

   // write the function
   public function refresh($time) {
      $current_url = $_SERVER['REQUEST_URI'];
      return header("Refresh: " . $time . "; URL=$current_url");
   }
}
