<?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 = 'Home';
        $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() {
        //check login
        //autho::checkautho($th


        $screen_sliders = R::findAll('screen_slider');
        $this->smarty->assign('screen_sliders',$screen_sliders);

        $columns = R::inspect('visits');
        if (!array_key_exists('end_visit', $columns)) {
            R::exec('ALTER TABLE visits ADD end_visit int(11) NOT NULL DEFAULT 0');
        }

        $last_entered = R::findOne('visits',' deleted = 0 and visitdate = ? and view = 1 and end_visit = 0 order by id desc', [date('Y-m-d')]);
        $patient_info = R::getRow('select * from patients where id = ? ', [$last_entered['patientid']]);
        $last_entered->patient_name = $patient_info['wifename'];
        $this->smarty->assign('last_entered',$last_entered);

        $result = R::findAll('visits',' deleted = 0 and visitdate = ? and view = 0 order by id limit 0,5', [date('Y-m-d')]);
        foreach ($result as $single_visit) {
            $patient_info = R::getRow('select * from patients where id = ? ', [$single_visit['patientid']]);
            $single_visit->patient_name = $patient_info['wifename'];
        }
        $this->smarty->assign('result',$result);

        //call other class with other content
        $this->smarty->display('index.html.tpl');
    }


    public function loadInfo() {

        $screen_sliders = R::findAll('screen_slider');
        $this->smarty->assign('screen_sliders',$screen_sliders);


        $last_entered = R::findOne('visits',' deleted = 0 and visitdate = ? and view = 1 and end_visit = 0 order by id desc', [date('Y-m-d')]);
        $patient_info = R::getRow('select * from patients where id = ? ', [$last_entered['patientid']]);
        $last_entered->patient_name = $patient_info['wifename'];
        $this->smarty->assign('last_entered',$last_entered);

        $result = R::findAll('visits',' deleted = 0 and visitdate = ? and view = 0 order by id limit 0,5', [date('Y-m-d')]);
        foreach ($result as $single_visit) {
            $patient_info = R::getRow('select * from patients where id = ? ', [$single_visit['patientid']]);
            $single_visit->patient_name = $patient_info['wifename'];
        }
        $this->smarty->assign('result',$result);
        $this->smarty->display('result.html');
    }

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

}
