<?php

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

class Controllers {

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

// 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;
        $this->today = date("Y-m-d");

//here name of controll to known
        $this->controllname = 'examination';
        $this->shcontrollname = 'examination';
        $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] = 'Examination'; //controller name or tab name
        $active[1] = 'Examination'; //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;



//here will call all php pages
//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");
        $this->patientid = patientdata::patientid();
    }

//here the frist page and
//the frist action in this class

    public function index() {

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

        $del = 0;
        $patientId = filter_input(INPUT_GET, "id");

        if (empty($this->patientid)) {
            header('Location:' . $this->hosturl . '/controllers/index.php?empty=1');
            exit();
        }

        $ProgramesettingData = R::findone('programesetting');
        $this->smarty->assign('ProgramesettingData', $ProgramesettingData);

        if ($ProgramesettingData->head != 1) {
            $examinationhead = R::findall('examinationhead', 'del = 0');
            $this->smarty->assign('examinationhead', $examinationhead);
        }

        if ($ProgramesettingData->chest != 1) {
            $examinationchest = R::findall('examinationchest', 'del = 0');
            $this->smarty->assign('examinationchest', $examinationchest);
        }

        if ($ProgramesettingData->abdomen != 1) {
            $examinationabdomen = R::findall('examinationabdomen', 'del = 0');
            $this->smarty->assign('examinationabdomen', $examinationabdomen);
        }

        if ($ProgramesettingData->pelvis != 1) {
            $examinationpelvis = R::findall('examinationpelvis', 'del = 0');
            $this->smarty->assign('examinationpelvis', $examinationpelvis);
        }

        if ($ProgramesettingData->extremitis != 1) {
            $examinationextremitis = R::findall('examinationextremitis', 'del = 0');
            $this->smarty->assign('examinationextremitis', $examinationextremitis);
        }

        $allexmination = R::findall('examination', 'del = ? and patientid = ? order by examinationdate desc, id desc', [$del, $this->patientid]);

        foreach ($allexmination as $myallexmination) {
            $userData = R::getRow('select user_name from awusers where user_id = ' . $myallexmination->doctorid);
            $myallexmination->username = $userData['user_name'];
            $myallexmination->examinationdate = date('Y/m/d', strtotime($myallexmination->examinationdate));
        }
        $this->smarty->assign('allexminationcount', count($allexmination));
        $this->smarty->assign('allexmination', $allexmination);
        $this->smarty->assign('patientId', $this->patientid);


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

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

        $itr = filter_input(INPUT_GET, "itr");
        $ProgramesettingData = R::findone('programesetting');
        $this->smarty->assign('ProgramesettingData', $ProgramesettingData);


        if ($ProgramesettingData->head != 1) {
            $examinationhead = R::findall('examinationhead', 'del = 0');
            $this->smarty->assign('examinationhead', $examinationhead);
        }

        if ($ProgramesettingData->chest != 1) {
            $examinationchest = R::findall('examinationchest', 'del = 0');
            $this->smarty->assign('examinationchest', $examinationchest);
        }

        if ($ProgramesettingData->abdomen != 1) {
            $examinationabdomen = R::findall('examinationabdomen', 'del = 0');
            $this->smarty->assign('examinationabdomen', $examinationabdomen);
        }

        if ($ProgramesettingData->pelvis != 1) {
            $examinationpelvis = R::findall('examinationpelvis', 'del = 0');
            $this->smarty->assign('examinationpelvis', $examinationpelvis);
        }

        if ($ProgramesettingData->extremitis != 1) {
            $examinationextremitis = R::findall('examinationextremitis', 'del = 0');
            $this->smarty->assign('examinationextremitis', $examinationextremitis);
        }

        $doctorid = $_SESSION['user_id'];
        $patientid = filter_input(INPUT_GET, "id");
        $exmination_obj = R::dispense('examination');
        $exmination_obj->patientid = $patientid;
        $exmination_obj->doctorid = $doctorid;
        $exmination_obj->sysdate = $this->today;
        $exmination_obj->examinationdate = $this->today;
        $exmination_obj->weight = '';
        $exmination_obj->height = '';
        $exmination_obj->bmi = '';
        $exmination_obj->bP = '';
        $exmination_obj->pulse = '';
        $exmination_obj->headandneck = '';
        $exmination_obj->chestandheart = '';
        $exmination_obj->abdomen = '';
        $exmination_obj->pelvis = '';
        $exmination_obj->extremitis = '';
        $exmination_obj->del = 0;


//call other class with other content
//try {
        $id = R::store($exmination_obj);
//echo $id;
        $exmination_obj->id = $id;
        $exmination_obj->examinationdate = date('Y/m/d', strtotime($exmination_obj->examinationdate));


        $this->smarty->assign('exmination_obj', $exmination_obj);

        $userData = R::getRow('select user_name from awusers where user_id = ' . $doctorid);
        $username = $userData['user_name'];
        $this->smarty->assign('username', $username);
        $this->smarty->assign('itr', $itr);
        $this->smarty->display('examination/newExamRow.html');
//header('Location:' . $this->hosturl . '/' . $this->controllname );
// } catch (Exception $e) {
//     header('Location:' . $this->hosturl . '/error/notupdate/' . $this->controllname);
//  }
    }

    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");
        $patientId = filter_input(INPUT_POST, "patientId");
        $dated = filter_input(INPUT_POST, "dated");

        if ($tableName == "examinationhead" || $tableName == "examinationchest" || $tableName == "examinationabdomen" || $tableName == "examinationpelvis" || $tableName == "examinationextremitis") {
            $mydata = R::load($tableName, $id);
        } else {

            if ($dated == 1) {
                $value = date("Y-m-d", strtotime($value));
            }

            $mydata = R::findone($tableName, 'id = ? and patientid = ?', [$id, $patientId]);
        }

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

    public function delData() {
//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");

        try {
            $myData = R::load($tableName, $id);
            $myData->del = 1;
            $myid = R::store($myData);

            echo 1;
            //header('Location:' . $this->hosturl . '/controllers/' . $this->controllname.'.php?ac=index' );
        } catch (Exception $e) {
            //header('Location:' . $this->hosturl . '/error/notupdate/' . $this->controllname);
        }
    }

    public function addexitem() {

        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        //      autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
        //vars here
        $name = filter_input(INPUT_POST, 'newitem');
        $inline = filter_input(INPUT_POST, 'inline');
        $selectid = filter_input(INPUT_POST, 'id');
        $table = filter_input(INPUT_POST, 'table');
        $colum = filter_input(INPUT_POST, 'colum');
        $patientId = filter_input(INPUT_GET, 'id');


        ### insert into table
        $newtable = R::dispense($table);
        $newtable->name = $name;
        $newtable->del = 0;

        $insertid = R::store($newtable);

        echo $insertid;

        ### update select in phmenstural for patient
        $mydata = R::findone('examination', 'id = ? and patientid = ?', [$selectid, $patientId]);
        $mydata->$colum = $insertid;
        R::store($mydata);
    }

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

}
