<?php

require_once("imp/_imp.php");

class Controllers {

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

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

//here name of controll to known
      $this->controllname = 'reserve_clinic';
      $this->shcontrollname = 'reserve_clinic';
      $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] = 'Reserve Clinic'; //controller name or tab name
      $active[1] = 'Reserve Clinic'; //controller name
// do name  // switch($do)
      switch ($acty) {
         case '':$active[2] = 'show';
            break;
         case 'show':$active[2] = 'show';
            break;
         default :$active[2] = 'show';
            break;
      }
      $this->active = $active;

      R::ext('xdispense', function ($type) {
         return R::getRedBean()->dispense($type);
      });

//here will call all php pages
      require_once("_header.php");
      require_once("_patientdata.php");
      require_once("_sidebar.php");
      require_once("_leftsidebar.php");
      require_once("_footer.php");

      //find loading time
      $this->time = microtime(true);
      $this->today = date('Y-m-d');
   }

   ##############################################################

   private function getDayN0($date) {
      $timestamp = strtotime($date);
      $day = date('w', $timestamp);

      switch ($day) {
         case 0:
            $name = 'الأحد  ';
            $no = 2;
            break;
         case 1:
            $name = ' الإثنين';
            $no = 3;
            break;
         case 2:
            $name = ' الثلاثاء';
            $no = 4;
            break;
         case 3:
            $name = 'الأربعاء ';
            $no = 5;
            break;
         case 4:
            $name = 'الخميس  ';
            $no = 6;
            break;
         case 5:
            $name = 'الجمعة ';
            $no = 7;
            break;
         case 6:
            $name = 'السبت ';
            $no = 1;
            break;
      }

      $res = array('dayNo' => $no, 'dayName' => $name);

      return $res;
   }

   public function del() {

      //check login
      autho::checkautho($this->hosturl);
      //check authoize
      autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

      $id = filter_input(INPUT_POST, "invId");
      $tableName = filter_input(INPUT_POST, "tableName");

      try {
         $table_obj = R::load($tableName, $id);
         $table_obj->deleted = 1;
         R::store($table_obj);

         echo 1;
      } catch (Exception $e) {
         echo -1;
      }
   }

   public function update() {

      //check login
      autho::checkautho($this->hosturl);
      //check authoize
      autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

      $id = filter_input(INPUT_POST, "id");
      $tableName = filter_input(INPUT_POST, "tableName");
      $colName = filter_input(INPUT_POST, "colName");
      $value = filter_input(INPUT_POST, "value");

      try {
         $table_obj = R::load($tableName, $id);
         $table_obj->$colName = $value;
         R::store($table_obj);

         echo 1;
      } catch (Exception $e) {
         echo -1;
      }
   }

   private function getDayName($dayNo) {
      switch ($dayNo) {
         case 1:
            $name = ' السبت';
            break;
         case 2:
            $name = ' الأحد';
            break;
         case 3:
            $name = 'الإثنين ';
            break;
         case 4:
            $name = 'الثلاثاء  ';
            break;
         case 5:
            $name = 'الأربعاء ';
            break;
         case 6:
            $name = 'الخميس ';
            break;
         case 7:
            $name = ' الجمعة ';
            break;
      }

      return $name;
   }

   ###############################  doctord days off ########################################

   public function vacations() {
      //check login
      autho::checkautho($this->hosturl);
      //check authoize
      autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

      ####################################################################
      $today = date("Y/m/d");
      $this->smarty->assign('today', date('Y/m/d', strtotime($today)));

      $allVacations = $this->getDrVacations(0, 0);
      $this->smarty->assign('allVacations', $allVacations);

      $days = $this->loadLimits();
      $this->smarty->assign('days', $days);

      $programesetting = R::findOne('programesetting');
      $this->smarty->assign('programesetting', $programesetting);
      ####################################################################
      //call other class with other content
      header::headershow($this->smarty, $this->hosturl);
      sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
      $this->smarty->display('vacations/show.html');
      //  $this->smarty->display('reserve_clinic/add.html');
      leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
      footer::footershow($this->smarty, $this->hosturl);
   }

   function loadLimits($branchId = 0) {
      $days = array();
      for ($i = 1; $i <= 7; $i++) {
         $limit = r::findOne('visits_limit', ' branch_id = ? and  day_no = ? and deleted =  0 ', [$branchId, $i]);
         $day = array();
         $day['dayNo'] = $i;
         $day['dayName'] = $this->getDayName($i);
         $day['max_visits'] = $limit->max_visits;

         array_push($days, $day);
      }
      return $days;
   }

   function getDrVacations($doctorId, $branchId) {
      $drVacations = r::findAll('visits_vacation', ' doctor_id = ? and branch_id = ? and  
                   (  day_date >= ?  || day_date is Null ) and deleted =  0 ', [$doctorId, $branchId, date('Y-m-d')]);
      return $drVacations;
   }

   function updateDayLimit($dayNo, $branchId, $value) {
      try {

         $limit = r::findOne('visits_limit', '   branch_id = ? and  day_no = ? and deleted =  0 ', [$branchId, $dayNo]);
         if (count($limit) > 0) {
            $limit->deleted = 1;
            R::store($limit);
         }

         $updated = R::xdispense('visits_limit');
         $updated->branch_id = $branchId;
         $updated->day_no = $dayNo;
         $updated->max_visits = $value;
         $updated->user_id = $_SESSION['user_id'];
         $updated->create_date = date('Y-m-d h:i:s');
         $updated->deleted = 0;

         R::store($updated);
         echo 1;
      } catch (Exception $ex) {
         echo -1;
      }
   }

   function updateLimit() {
      autho::checkautho($this->hosturl);        ## check login
      $branchId = (filter_input(INPUT_POST, 'branchId') > 0) ? filter_input(INPUT_POST, 'branchId') : 0;
      $dayNo = filter_input(INPUT_POST, "dayNo");
      $value = filter_input(INPUT_POST, "value");

      $this->updateDayLimit($dayNo, $branchId, $value);
   }

   function addVacations() {
      autho::checkautho($this->hosturl);        ## check login
      $doctorId = (filter_input(INPUT_POST, 'doctorId') > 0) ? filter_input(INPUT_POST, 'doctorId') : 0;
      $branchId = (filter_input(INPUT_POST, 'branchId') > 0) ? filter_input(INPUT_POST, 'branchId') : 0;
      try {
         $updated = R::xdispense('visits_vacation');
         $updated->doctor_id = $doctorId;
         $updated->branch_id = $branchId;
         $updated->user_id = $_SESSION['user_id'];
         $updated->create_date = date('Y-m-d h:i:s');
         $updated->deleted = 0;

         R::store($updated);
         $drVacations = r::findAll('visits_vacation', ' doctor_id = ? and branch_id = ? and  
                   (  day_date >= ?  || day_date is Null ) and deleted =  0 ', [$doctorId, $branchId, date('Y-m-d')]);
         $this->smarty->assign('drVacations', $drVacations);
         $this->smarty->display('vacations/vacation_row.html');
      } catch (Exception $ex) {
         echo -1;
      }
   }

   public function delItem() {

      //check login
      autho::checkautho($this->hosturl);

      $id = filter_input(INPUT_POST, "id");
      $tableName = filter_input(INPUT_POST, "tableName");

      try {
         $table_obj = R::load($tableName, $id);
         $table_obj->deleted = 1;
         R::store($table_obj);

         echo 1;
      } catch (Exception $e) {
         echo -1;
      }
   }

   #######################################################################
// at the end of class call

   public function __destruct() {
      //echo "Time Elapsed: ".(microtime(true) - $this->time)."s";
//close connection
      R::close();
   }
}
