<?php

require_once("imp/_imp.php");

class Controllers {

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

// 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 = 'and_history';
        $this->shcontrollname = 'and_history';
        $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] = 'andrology'; //controller name or tab name
        $active[1] = 'and_history'; //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
        require_once("_header.php");
        require_once("_patientdata.php");
        require_once("_sidebar.php");
        require_once("_leftsidebar.php");
        require_once("_footer.php");

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

    public function index() {
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
        ## check if patient is choosen 
        $patientId = $this->patientId;
        if (empty($patientId)) {
            header('Location:' . $this->hosturl . '/controllers/index.php?empty=1');
            exit();
        }
        $this->smarty->assign('patientid', $patientId);

        ################################ get Program setting Data  #################################

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

        ########################################  loading  data #################################################

        $this->history_data(0, $patientId, 0);
        $this->mProblems_data(0, $patientId);
        $this->erection_data(0, $patientId);
        $this->ejaculation_data(0, $patientId);
        $this->contraception_data(0, $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('andrology/history.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

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

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

        $patientId = filter_input(INPUT_POST, "patientId");
        $tableName = filter_input(INPUT_POST, "tableName");
        $type = filter_input(INPUT_POST, "type");

        $this->addTableRow($patientId, $tableName, $type);
        $this->smarty->display('andrology/newrow.html');
        ################################################################################################################################################################################
    }

    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 {
            $obj = R::load($tablename, $id);

            $obj->$colname = $value;
            R::store($obj);

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

    public function del() {      ## delete row from investcat table 
        //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");

        $ogj = R::load($tableName, $id);
        $ogj->deleted = 1;

        try {
            R::store($ogj);
            echo 1;
        } catch (Exception $e) {
            echo 22;
        }
    }

    public function addduration() {      ## delete row from investcat table 
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        $id = filter_input(INPUT_POST, "id");
        $patientId = filter_input(INPUT_POST, "patientId");
        $newitem = filter_input(INPUT_POST, "newitem");

        ### insert into table
        $table = R::dispense('infertileduration');
        $table->name = $newitem;
        $table->deleted = 0;

        $insertid = R::store($table);
        echo $insertid;

        ### update select in phmenstural for patient
        $andHistory = R::load('andhistory', $id);
        $andHistory->durationid = $insertid;
        R::store($andHistory);
    }

    public function adddmdisease() {      ## delete row from investcat table 
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        $id = filter_input(INPUT_POST, "id");
        $patientId = filter_input(INPUT_POST, "patientId");
        $newitem = filter_input(INPUT_POST, "newitem");

        ### insert into table
        $table = R::dispense('medicaldisease');
        $table->name = $newitem;
        $table->deleted = 0;

        $insertid = R::store($table);
        echo $insertid;

        ### update select in phmenstural for patient
        $andmedicalproblems = R::load('andmedicalproblems', $id);
        $andmedicalproblems->diseaseid = $insertid;
        R::store($andmedicalproblems);
    }

    public function addderectiondisease() {      ## delete row from investcat table 
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        $id = filter_input(INPUT_POST, "id");
        $patientId = filter_input(INPUT_POST, "patientId");
        $newitem = filter_input(INPUT_POST, "newitem");

        ### insert into table
        $table = R::dispense('erectiondisease');
        $table->name = $newitem;
        $table->deleted = 0;

        $insertid = R::store($table);
        echo $insertid;

        ### update select in phmenstural for patient
        $anderection = R::load('anderection', $id);
        $anderection->diseaseid = $insertid;
        R::store($anderection);
    }

    public function addejaculationdisease() {      ## delete row from investcat table 
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        $id = filter_input(INPUT_POST, "id");
        $patientId = filter_input(INPUT_POST, "patientId");
        $newitem = filter_input(INPUT_POST, "newitem");

        ### insert into table
        $table = R::dispense('ejaculationdisease');
        $table->name = $newitem;
        $table->deleted = 0;

        $insertid = R::store($table);
        echo $insertid;

        ### update select in phmenstural for patient
        $andejaculation = R::load('andejaculation', $id);
        $andejaculation->diseaseid = $insertid;
        R::store($andejaculation);
    }

    public function addcontraception() {      ## delete row from investcat table 
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);

        $id = filter_input(INPUT_POST, "id");
        $patientId = filter_input(INPUT_POST, "patientId");
        $newitem = filter_input(INPUT_POST, "newitem");

        ### insert into table
        $table = R::dispense('andcontratype');
        $table->name = $newitem;
        $table->deleted = 0;

        $insertid = R::store($table);
        echo $insertid;

        ### update select in phmenstural for patient
        $andcontraception = R::load('andcontraception', $id);
        $andcontraception->typeid = $insertid;
        R::store($andcontraception);
    }

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

        $tableName = filter_input(INPUT_POST, "tableName");
        $name = filter_input(INPUT_POST, "name");
        $patientid = filter_input(INPUT_POST, "patientid");
        $rowoftableid = filter_input(INPUT_POST, "rowoftableid");
        $maintableName = filter_input(INPUT_POST, "maintableName");


        $alldata = R::findAll($tableName, 'deleted = 0');

        $this->smarty->assign('tablename', $tableName);
        $this->smarty->assign('selectname', $name);
        $this->smarty->assign('alldata', $alldata);
        $this->smarty->assign('patientid', $patientid);
        $this->smarty->assign('rowoftableid', $rowoftableid);
        $this->smarty->assign('maintableName', $maintableName);

        $this->smarty->display('andrology/editmodel.html');
    }

    public function delRowsommedel() {
//check login
        autho::checkautho($this->hosturl);

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

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

        echo 1;
        //  header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php');
    }

    private function addTableRow($patientId, $tableName, $type) {
        $element = R::dispense($tableName);
        $element->doctorid = $_SESSION['user_id'];
        $element->patientid = $patientId;
        $element->date = $this->today;
        $element->deleted = 0;

        $id = R::store($element);
        $element->id = $id;
        $element->date = date("Y/m/d", strtotime($element->date));

        $userData = R::getRow('select user_name from awusers where user_id = ' . $_SESSION['user_id']);
        $element->doctorName = $userData['user_name'];

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

        $this->smarty->assign('element', $element);
        $this->smarty->assign('type', $type);

        $this->load_select_data($type, $patientId, $id);
    }

    private function load_select_data($type, $patientId, $id) {
        switch ($type) {
            case 'general_history':
                $this->history_data(1, $patientId, $id);
                break;
            case 'mProblems':
                $this->mProblems_data(1, $patientId);
                break;
            case 'erection':
                $this->erection_data(1, $patientId);
                break;
            case 'ejaculation':
                $this->ejaculation_data(1, $patientId);
                break;
            case 'contraception':
                $this->contraception_data(1, $patientId);
                break;
        }
    }

    private function history_data($select, $patientId, $id) {
        if ($select != 1) {
            $history = R::findall('andhistory', 'deleted = 0  and patientid = ' . $patientId . '   ORDER BY id DESC');
            foreach ($history as $element) {
                $userData = R::getRow('select user_name from awusers where user_id = ' . $element->doctorid);
                $element->doctorName = $userData['user_name'];
                if ($element->durationid == 0) {
                    $element->durationid = "";
                }
            }
            $this->smarty->assign('history', $history);
        } else {
            $allobsteric = R::findall('phobstetric', 'del = 0 and obstermination = "4"  and patientid =  ' . $patientId);
            $this->smarty->assign('abortionCount', count($allobsteric));

            $historyRow = R::load('andhistory', $id);
            if (count($historyRow) > 0) {
                $historyRow->abortionno = count($allobsteric);
                r::store($historyRow);
            }
        }

        $infertileduration = R::findall('infertileduration', 'deleted = 0');
        $this->smarty->assign('infertileduration', $infertileduration);
    }

    private function mProblems_data($select, $patientId) {
        if ($select != 1) {
            $mProblems = R::findall('andmedicalproblems', 'deleted = 0  and patientid = ' . $patientId . '   ORDER BY date , id DESC');
            foreach ($mProblems as $element) {
                $userData = R::getRow('select user_name from awusers where user_id = ' . $element->doctorid);
                $element->doctorName = $userData['user_name'];
            }
            $this->smarty->assign('mProblems', $mProblems);
        }
        $medicaldisease = R::findall('medicaldisease', 'deleted = 0');
        $this->smarty->assign('medicaldisease', $medicaldisease);
    }

    private function erection_data($select, $patientId) {
        if ($select != 1) {
            $erection = R::findall('anderection', 'deleted = 0  and patientid = ' . $patientId . '   ORDER BY date , id DESC');
            foreach ($erection as $element) {
                $userData = R::getRow('select user_name from awusers where user_id = ' . $element->doctorid);
                $element->doctorName = $userData['user_name'];
            }
            $this->smarty->assign('erection', $erection);
        }
        $erectiondisease = R::findall('erectiondisease', 'deleted = 0');
        $this->smarty->assign('erectiondisease', $erectiondisease);
    }

    private function ejaculation_data($select, $patientId) {
        if ($select != 1) {
            $ejaculation = R::findall('andejaculation', 'deleted = 0  and patientid = ' . $patientId . '   ORDER BY date , id DESC');
            foreach ($ejaculation as $element) {
                $userData = R::getRow('select user_name from awusers where user_id = ' . $element->doctorid);
                $element->doctorName = $userData['user_name'];
            }
            $this->smarty->assign('ejaculation', $ejaculation);
        }
        $ejaculationdisease = R::findall('ejaculationdisease', 'deleted = 0');
        $this->smarty->assign('ejaculationdisease', $ejaculationdisease);
    }

    private function contraception_data($select, $patientId) {
        if ($select != 1) {
            $contraception = R::findall('andcontraception', 'deleted = 0  and patientid = ' . $patientId . '   ORDER BY date , id DESC');
            foreach ($contraception as $element) {
                $userData = R::getRow('select user_name from awusers where user_id = ' . $element->doctorid);
                $element->doctorName = $userData['user_name'];
            }
            $this->smarty->assign('contraception', $contraception);
        }
        $andcontratype = R::findall('andcontratype', 'deleted = 0');
        $this->smarty->assign('andcontratype', $andcontratype);
    }

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

}
