<?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() {

        $today = date('Y-m-d');
        $roles = R::findAll('awrole', 'screen_show = 1');
        foreach ($roles as $role) {
            $visits_role = R::findAll('visits',
                            'for_department = ? and deleted = 0 and view = 0 and visitdate = date("' . $today . '") 
                    order by visitorder ASC limit 5',
                            [$role->id]);
            foreach ($visits_role as $item) {
                $patient_info = R::findOne('patients', 'id = ?', [$item->patientid]);
                if ($item->for_husband == 1) {
                    $item->patient_name = $patient_info->husdandname;
                } else {
                    $item->patient_name = $patient_info->wifename;
                }

                $doctor = R::getRow('select  name from awusers where user_id = ?', [$item->for_doctor]);
                $item->for_doctor_name = $doctor['name'];
            }
            $role->visits_count = R::count('visits',
                            'for_department = ? and deleted = 0 and view = 0 and visitdate = date("' . $today . '")', [$role->id]);
            $role->visits = $visits_role;
        }
        $this->smarty->assign('roles', $roles);

        $this->refresh(30);


        $this->smarty->display('screen_ordering/show.html.tpl');
    }

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

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

}
