<?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 = 'archive';
        $this->shcontrollname = 'archive';
        $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] = 'programesetting'; //controller name or tab name
        $active[1] = 'archive'; //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);
        });


        require_once("visits_common.php"); //general functions
//here will call all php pages
        require_once("_header.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');
    }

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

        #################################################################### 
        $patientNo = filter_input(INPUT_GET, 'patientNo');
        $deviceNo = filter_input(INPUT_GET, 'deviceId');

        if ($patientNo > 0) {
            $patientId = getPatientId($patientNo);
        }

        if ($deviceNo > 0) {
            $deviceId = getDeviceId($deviceNo);
        }

        if ($deviceId > 0 && $patientId > 0) {
            saveTrackingRecord($patientId, $deviceId, 0, '', 'archive');
            archiveRecord($patientId, $deviceId, 0, '', 'archive');
        }

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

        ####################################################################
        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        $this->smarty->display('archive/show.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

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

        $date = filter_input(INPUT_POST, 'date');
        $wId = filter_input(INPUT_POST, 'wId');
        $hId = filter_input(INPUT_POST, 'hId');
        $dateType = filter_input(INPUT_POST, 'dateType');

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

        if ($wId > 0 || $hId > 0) {
            if ($hId > 0) {
                $patientId = $hId;
            } else {
                $patientId = $wId;
            }
            $this->archiveSearch($dateType, $patientId);

            $sql = 'select  patients.id as  patient_id  , patients.wifename 
                                        , patients.husdandname      , patients.statusno
                                       from    patients
                         where patients.id =  ' . $patientId . '    ';

            $patient = R::getRow($sql);
            $this->smarty->assign('patient', $patient);

            $this->smarty->display('archive/archive_Patient.html');
        } else {
            $this->archiveSearch($dateType, 0, $date);
            $this->smarty->display('archive/archive_search.html');
        }
    }

    function archiveSearch($dateType, $patientId, $date = '') {

        $ext = ' ';
        if ($patientId > 0) {
            $ext = ' and  archive_tracking.patient_id = ' . $patientId;
        } else {
            switch ($dateType) {
                case 1:
                    $ext = ' and  archive_tracking.in_date = "' . $date . '" ';
                    break;
                case 2:
                    $ext = ' and  archive_tracking.out_date = "' . $date . '" ';
                    break;
                default:
                    $ext = ' and (  archive_tracking.out_date = "' . $date . '" ||  archive_tracking.in_date = "' . $date . '"   ) ';
                    break;
            }
        }

        $sql = 'select   archive_tracking.id   , patients.wifename  , archive_tracking.out_date
                                        , patients.husdandname      , patients.statusno  , archive_tracking.in_date
                                        ,  TIME_FORMAT(archive_tracking.out_time, "%h:%i %p") as  out_time
                                        ,  TIME_FORMAT(archive_tracking.in_time, "%h:%i %p") as  in_time 
                                       ,  out_l.name  as outLocation    ,  in_l.name  as inLocation 
                                        , archive_tracking.receiver  , archive_tracking.notes
                   from    archive_tracking
                               join  patients                               on  patients.id = archive_tracking.patient_id    
                             left  join  devices out_d                          on  out_d.id = archive_tracking.out_device_id 
                             left  join  devices in_d                          on  in_d.id = archive_tracking.in_device_id 
                              left   join  floors   out_l                       on  out_l.id = out_d.floor_no 
                             left  join  floors    in_l                      on  in_l.id = in_d.floor_no 
                         where archive_tracking.deleted = 0        ' . $ext . '        order by patients.statusno ';



        $archive_tracking = R::getAll($sql);

        $this->smarty->assign('archive_tracking', $archive_tracking);
    }

    public function archive_search_exit() {
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        //   autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
##############################################################################
        $this->archiveSearchExit();
        $this->smarty->display('archive/archive_search.html');
    }

    function archiveSearchExit() {

        $sql = 'select   archive_tracking.id   , patients.wifename  , archive_tracking.out_date
                                        , patients.husdandname      , patients.statusno  , archive_tracking.in_date
                                        ,  TIME_FORMAT(archive_tracking.out_time, "%h:%i %p") as  out_time
                                        ,  TIME_FORMAT(archive_tracking.in_time, "%h:%i %p") as  in_time 
                                       ,  out_l.name  as outLocation    ,  in_l.name  as inLocation 
                                        , archive_tracking.receiver  , archive_tracking.notes
                   from    archive_tracking
                               join  patients                               on  patients.id = archive_tracking.patient_id    
                             left  join  devices out_d                          on  out_d.id = archive_tracking.out_device_id 
                             left  join  devices in_d                          on  in_d.id = archive_tracking.in_device_id 
                              left   join  floors   out_l                       on  out_l.id = out_d.floor_no 
                             left  join  floors    in_l                      on  in_l.id = in_d.floor_no 
                         where archive_tracking.deleted = 0  
                         and  ( archive_tracking.in_time = ""  || archive_tracking.in_time IS  NULL  ) 
                         and   archive_tracking.out_time != ""  and  archive_tracking.out_time IS NOT   NULL  
                         order by patients.statusno ';



        $archive_tracking = R::getAll($sql);

        $this->smarty->assign('archive_tracking', $archive_tracking);
    }

    public function update() {
        $id = filter_input(INPUT_POST, "id");
        $value = filter_input(INPUT_POST, "value");
        $tableName = filter_input(INPUT_POST, "tableName");
        $colName = filter_input(INPUT_POST, "colName");

        //here the update
        $mydata = R::load($tableName, $id);

        $mydata->$colName = $value;
        R::store($mydata);
    }

//    public function new_requests() {
//        $date = date("Y-m-d");
//        $newRequests = $this->newRequests($date);
//        echo $newRequests;
//    }
//
//    public function newRequests($date) {
//        $newRequests = ' ';
//        $sql = 'select   archive_tracking.id   , patients.wifename  , archive_tracking.out_date
//                                        , patients.husdandname      , patients.statusno  , archive_tracking.in_date
//
//                                       ,  out_l.name  as outLocation    ,  in_l.name  as inLocation 
//                                        , archive_tracking.receiver  , archive_tracking.notes
//                   from    archive_tracking
//                               join  patients                               on  patients.id = archive_tracking.patient_id    
//                             left  join  devices out_d                          on  out_d.id = archive_tracking.out_device_id 
//                             left  join  devices in_d                          on  in_d.id = archive_tracking.in_device_id 
//                              left   join  floors   out_l                       on  out_l.id = out_d.floor_no 
//                             left  join  floors    in_l                      on  in_l.id = in_d.floor_no 
//                         where archive_tracking.deleted = 0   
//                         and (  archive_tracking.out_date = "' . $date . '" ||  archive_tracking.in_date = "' . $date . '"   )       '
//                . 'order by patients.statusno ';
//
//
//
//        $archive_tracking = R::getAll($sql);
//
//        return $newRequests;
//    }
    ######################### temporary cases report ##############################
    #######################################################
    #######################################################
// at the end of class call
    public function __destruct() {
        //echo "Time Elapsed: ".(microtime(true) - $this->time)."s";
//close connection
        R::close();
    }

}
