<?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("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("imp/_autho.php"); //general functions
        $this->smarty = $smarty;
        $this->hosturl = $hosturl;

        //here name of controll to known
        $this->controllname = 'investigation';
        $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] = 'Investigations'; //controller name or tab name
        $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");
        $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']);
        //here will load the menu show
        $doctorid = $_SESSION['user_id'];
        $doctorData = R::getRow('select user_name from awusers where user_id = ' . $doctorid);
        $this->smarty->assign('doctorData', $doctorData);

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

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

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


        //$patientid = 1;
        $this->smarty->assign('patientid', $this->patientid);

        $allsemen = R::findAll('semen', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
//        if (empty($allsemen)) {
//            $semenData = R::dispense('semen');
//            $semenData->sdate = $today;
//            $semenData->patientid = $patientid;
//            $semenData->doctorid = $doctorid;
//            $semenData->del = 0;
//            $semenid = R::store($semenData);
//
//            $this->smarty->assign('semenid', $semenid);
//        }
        $allsemen = R::findAll('semen', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
        foreach ($allsemen as $data) {

            $data->sdate = date("Y/m/d", strtotime($data->sdate));

            $doctordata = R::getRow('select user_name from awusers where user_id = ' . $data->doctorid);
            $data->docname = $doctordata['user_name'];
        }
        $this->smarty->assign('allsemen', $allsemen);
        $this->smarty->assign('semencount', count($allsemen));


        $allhsg = R::findAll('hsg', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);

//        if (empty($allhsg)) {
//            $hsgData = R::dispense('hsg');
//            $hsgData->sdate = $today;
//            $hsgData->patientid = $this->patientid;
//            $hsgData->doctorid = $doctorid;
//            $hsgData->del = 0;
//            $hsgid = R::store($hsgData);
//
//            $this->smarty->assign('hsgid', $hsgid);
//        }
        $allhsg = R::findAll('hsg', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
        foreach ($allhsg as $data) {
            $data->sdate = date("Y/m/d", strtotime($data->sdate));

            $doctordata = R::getRow('select user_name from awusers where user_id = ' . $data->doctorid);
            $data->docname = $doctordata['user_name'];
        }
        $this->smarty->assign('allhsg', $allhsg);
        $this->smarty->assign('hsgcount', count($allhsg));

        $allustv = R::findAll('ustv', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
//        if (empty($allustv)) {
//            $ustvData = R::dispense('ustv');
//            $ustvData->sdate = $today;
//            $ustvData->patientid = $this->patientid;
//            $ustvData->doctorid = $doctorid;
//            $ustvData->del = 0;
//            $ustvid = R::store($ustvData);
//
//            $this->smarty->assign('ustvid', $ustvid);
//        }
        $allustv = R::findAll('ustv', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
        foreach ($allustv as $data) {
            $data->sdate = date("Y/m/d", strtotime($data->sdate));

            $doctordata = R::getRow('select user_name from awusers where user_id = ' . $data->doctorid);
            $data->docname = $doctordata['user_name'];
        }
        $this->smarty->assign('allustv', $allustv);
        $this->smarty->assign('ustvcount', count($allustv));

        $alllaparoscopy = R::findAll('laparoscopy', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
//        if (empty($alllaparoscopy)) {
//            $lapData = R::dispense('laparoscopy');
//            $lapData->sdate = $today;
//            $lapData->patientid = $this->patientid;
//            $lapData->doctorid = $doctorid;
//            $lapData->del = 0;
//            $lapid = R::store($lapData);
//
//            $this->smarty->assign('lapid', $lapid);
//        }
        $alllaparoscopy = R::findAll('laparoscopy', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
        foreach ($alllaparoscopy as $data) {
            $data->sdate = date("Y/m/d", strtotime($data->sdate));

            $doctordata = R::getRow('select user_name from awusers where user_id = ' . $data->doctorid);
            $data->docname = $doctordata['user_name'];
        }
        $this->smarty->assign('alllaparoscopy', $alllaparoscopy);
        $this->smarty->assign('lapcount', count($alllaparoscopy));

        $allhysteroscopy = R::findAll('hysteroscopy', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
//        if (empty($allhysteroscopy)) {
//            $hystData = R::dispense('hysteroscopy');
//            $hystData->sdate = $today;
//            $hystData->patientid = $this->patientid;
//            $hystData->doctorid = $doctorid;
//            $hystData->del = 0;
//            $hystid = R::store($hystData);
//
//            $this->smarty->assign('hystid', $hystid);
//        }
        $allhysteroscopy = R::findAll('hysteroscopy', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
        foreach ($allhysteroscopy as $data) {
            $data->sdate = date("Y/m/d", strtotime($data->sdate));

            $doctordata = R::getRow('select user_name from awusers where user_id = ' . $data->doctorid);
            $data->docname = $doctordata['user_name'];
        }
        $this->smarty->assign('allhysteroscopy', $allhysteroscopy);
        $this->smarty->assign('hystcount', count($allhysteroscopy));

        $allmrict = R::findAll('mrict', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
//        if (empty($allmrict)) {
//            $mriData = R::dispense('mrict');
//            $mriData->sdate = $today;
//            $mriData->patientid = $this->patientid;
//            $mriData->doctorid = $doctorid;
//            $mriData->del = 0;
//            $mrid = R::store($mriData);
//
//            $this->smarty->assign('mrid', $mrid);
//        }
        $allmrict = R::findAll('mrict', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
        foreach ($allmrict as $data) {
            $data->sdate = date("Y/m/d", strtotime($data->sdate));

            $doctordata = R::getRow('select user_name from awusers where user_id = ' . $data->doctorid);
            $data->docname = $doctordata['user_name'];
        }
        $this->smarty->assign('allmrict', $allmrict);
        $this->smarty->assign('mricount', count($allmrict));

        $allpathology = R::findAll('pathology', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
//        if (empty($allpathology)) {
//            $pathData = R::dispense('pathology');
//            $pathData->sdate = $today;
//            $pathData->patientid = $this->patientid;
//            $pathData->doctorid = $doctorid;
//            $pathData->del = 0;
//            $pathid = R::store($pathData);
//
//            $this->smarty->assign('pathid', $pathid);
//        }
        $allpathology = R::findAll('pathology', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
        foreach ($allpathology as $data) {
            $data->sdate = date("Y/m/d", strtotime($data->sdate));

            $doctordata = R::getRow('select user_name from awusers where user_id = ' . $data->doctorid);
            $data->docname = $doctordata['user_name'];
        }
        $this->smarty->assign('allpathology', $allpathology);
        $this->smarty->assign('pathcount', count($allpathology));

        $allhormon = R::findAll('hormon', 'patientid = ? and del = 0 order by sdate desc, id desc', [$this->patientid]);
        foreach ($allhormon as $data) {
            $data->sdate = date("Y/m/d", strtotime($data->sdate));

            $doctordata = R::getRow('select user_name from awusers where user_id = ' . $data->doctorid);
            $data->docname = $doctordata['user_name'];
        }
        $this->smarty->assign('allhormon', $allhormon);
        $this->smarty->assign('hormoncount', count($allhormon));


        $allOtherInvests = R::findAll('otherinvestigationsrows', 'patientid = ? and deleted = 0 order by date desc , id desc  ', [$this->patientid]);
        foreach ($allOtherInvests as $invest) {
            $invest->date = date("Y/m/d", strtotime($invest->date));
            ## get doctor name
            $doctordata = R::getRow('select user_name from awusers where user_id = ' . $invest->doctorid);
            $invest->docname = $doctordata['user_name'];

            $investValues = R::findAll('otherinvestigationsvalues', 'investrowid = ? ', [$invest->id]);
            $invest->invsCount = 0;
            $invest->invsWidth = 100;
            foreach ($investValues as $data) {
                $other = R::load('otherinvestigations', $data->investid);
                $data->investName = $other->name;
                if ($data->value) {
                    $invest->invsCount++;
                }
            }
            $td = count($investValues);
            $td = floor(11 / $td);
            if ($td == 0)
                $td = 1;
            $mod = round(11 % $td);
            $last = $td + $mod;
            $invest->last = $last;
            $invest->td = $td;
            $invest->invs = $investValues;
            if ($invest->invsCount > 0) {
                $width = 100;
                $invest->invsWidth = ($width / $invest->invsCount);
                if ($invest->invsWidth < 10) {
                    $invest->invsWidth = 10;
                }
            }
        }


        $this->smarty->assign('otherInvests', $allOtherInvests);
        $this->smarty->assign('allOthersCount', count($allOtherInvests));


        $patient = R::load('patients', $this->patientid);
        $this->smarty->assign('is_phpasthx', $patient->phpasthx);

        $phpasthx = R::findall('phpasthx', 'del = ? and patientid = ? order by id DESC', [0, $this->patientid]);
        foreach ($phpasthx as $data) {
            $userData = R::getRow('select user_name from awusers where user_id = ' . $data->doctorid);
            $data->username = $userData['user_name'];
        }


        $phpasthx_types = R::findall('phpasthxtypes', 'del = 0 order by name');
        $this->smarty->assign('phpasthx_types', $phpasthx_types);


        $this->smarty->assign('phpasthxcount', count($phpasthx));
        $this->smarty->assign('phpasthx', $phpasthx);



        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        patientdata::patientdatashow($this->smarty, $this->hosturl);
        if ($ProgramesettingData->investshow == 0)
            $this->smarty->display('investigation/investigation.html');
        else
            $this->smarty->display('investigation/investigation1.html');
        //$this->smarty->display('investigation/investigation1.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

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

        $doctorid = $_SESSION['user_id'];

        $today = date("Y/m/d");
        $patientid = filter_input(INPUT_POST, "patientId");
        $tableName = filter_input(INPUT_POST, "tableName");

        $semenData = R::dispense($tableName);
        $semenData->sdate = date("Y-m-d", strtotime($today));
        $semenData->patientid = $patientid;
        $semenData->doctorid = $doctorid;
        $semenData->del = 0;
        $semenid = R::store($semenData);

        echo $semenid;
    }

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

        $id = filter_input(INPUT_GET, "id");
        $tableName = filter_input(INPUT_GET, "tableName");
        $patientid = filter_input(INPUT_GET, "patientid");

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

        header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?patientid=' . $patientid);
    }

    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");
        $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 ($dated == 1) {
            $value = date("Y-m-d", strtotime($value));
        }

        if ($tableName == 'otherinvestigations' || $tableName == 'otherinvestigationsvalues' || $tableName == 'otherinvestigationsrows' || $tableName == 'patients') {
            $mydata = R::load($tableName, $id);
            $mydata->$colName = $value;
            R::store($mydata);
        } else {
            $mydata = R::findone($tableName, 'id = ? and patientid = ?', [$id, $patientId]);
            $mydata->$colName = $value;
            R::store($mydata);
        }
    }

    public function onesetup() {

        //check login
        autho::checkautho($this->hosturl);

        try {
            //delete all but with only default
            #1 clean roles only managment
            #2 clean menu only default
            #3 clean controls only default
            #4 users clean users
            #delete all menus
            $menus = R::findAll('awmenu', 'id > 15');
            #delete from awrolemenu
            foreach ($menus as $one) {
                //get propes
                $rolemenu = R::findAll('awrolemenu', 'menu_id = ?', [$one->id]);
                R::trashAll($rolemenu);
            }
            R::trashAll($menus);
            #delete controles
            $controlls = R::findAll('awcontroll', 'id > 4 and id < 18');
            foreach ($controlls as $one) {
                //get propes
                $inprop = R::findAll('awcontrollprop', 'awcontroll_id = ?', [$one->id]);
                #delete from awrolecontroleprop
                foreach ($inprop as $one) {
                    //get propes
                    $roleprop = R::findAll('awrolecontrollprop', 'controllprop_id = ?', [$one->id]);
                    R::trashAll($roleprop);
                }
                R::trashAll($inprop);
            }
            R::trashAll($controlls);
            #delete othre roles
            $roles = R::findAll('awrole', 'id > 1');
            R::trashAll($roles);
            #delete other users
            //$users = R::findAll('awusers', 'user_id > 1');
            //R::exec('Delete from awusers where user_id > 1');
            //R::trashAll($users);
            ############################
            //new one
            $menusx = R::findAll('awmenu', ' id = 4 or id = 5 or id = 7 or id = 8 or id = 10 or id = 11 or id = 13 or id = 14 or id = 15 ');
            #delete from awrolemenu
            foreach ($menusx as $one) {
                //get propes
                $rolemenu = R::findAll('awrolemenu', 'menu_id = ?', [$one->id]);
                R::trashAll($rolemenu);
            }
            R::trashAll($menusx);

            print_r('trash all , install is good');
        } catch (Exception $ex) {
            print_r('something go wrong  , try again');
        }
    }

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

        $allOthers = R::findAll('otherinvestigations', ' deleted = ?', [0]);
        $this->smarty->assign('allOthers', $allOthers);
        $this->smarty->display('investigation/otherinvests.html');
    }

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

        $newOther = R::dispense('otherinvestigations');
        $newOther->deleted = 0;
        $newId = R::store($newOther);
        echo $newId;
    }

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

        $patientId = filter_input(INPUT_POST, "patientId");
        $otheritr = filter_input(INPUT_POST, "otheritr");
        #########  otherinvestigationsrows   object
        $newRow = R::dispense('otherinvestigationsrows');
        $newRow->date = date("Y-m-d");
        $newRow->patientid = $patientId;
        $newRow->doctorid = $_SESSION['user_id'];
        ;
        $newRow->deleted = 0;
        try {
            $newRowId = R::store($newRow);

            $allOthers = R::findAll('otherinvestigations', ' deleted = ?', [0]);
            $allOthersValues = array();

            foreach ($allOthers as $other) {
                $newOther = R::dispense('otherinvestigationsvalues');
                $newOther->investrowid = $newRowId;
                $newOther->investid = $other->id;
                $newId = R::store($newOther);
                $newOther->id = $newId;
                $newOther->investName = $other->name;
                array_push($allOthersValues, $newOther);
            }

            R::commit();
        } catch (Exception $ex) {
            R::rollback();
        }
        if (count($allOthers) > 0) {
            $width = 100 / (count($allOthers));
            if ($width < 10) {
                $width = 10;
            }
        } else {
            $width = 0;
        }


        $newRow->date = date("Y/m/d", strtotime($newRow->date));
        $this->smarty->assign('width', $width);
        $this->smarty->assign('othersRow', $newRow);
        $this->smarty->assign('allOthers', $allOthersValues);
        $this->smarty->assign('otheritr', $otheritr);
        $this->smarty->display('investigation/newrow.html');
    }

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

        $id = filter_input(INPUT_GET, "id");
        $tableName = filter_input(INPUT_GET, "tableName");
        $patientid = filter_input(INPUT_GET, "patientid");

        #########  otherinvestigationsrows   object
        $investRow = R::load($tableName, $id);
        try {
            $investRow->deleted = 1;
            R::store($investRow);
            R::commit();
            header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?ac=index&patientid=' . $patientid);
        } catch (Exception $ex) {
            R::rollback();
            header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?ac=index&patientid=' . $patientid);
        }
    }

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

        $invest = R::load('otherinvestigations', $id);
        try {
            $invest->deleted = 1;
            R::store($invest);
            R::commit();
            echo 1;
        } catch (Exception $ex) {
            R::rollback();
            echo 3;
        }
    }

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

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

        $doctorid = $_SESSION['user_id'];

        $patientid = filter_input(INPUT_GET, "id");
        $itr = filter_input(INPUT_POST, "itr");
        $case_summary = filter_input(INPUT_POST, "case_summary");

        $obj = R::dispense('phpasthx');
        $obj->patientid = $patientid; //
        $obj->doctorid = $doctorid;
        $obj->sysdate = date("Y-m-d");
        $obj->pasthx_type = '';
        $obj->pasthxnote = '';
        $obj->del = 0;

        $id = R::store($obj);
        $obj->id = $id;

        $phpasthxtypes = R::findall('phpasthxtypes', 'del = 0 order by name');
        $this->smarty->assign('phpasthxtypes', $phpasthxtypes);

        $userData = R::getRow('select user_name from awusers where user_id = ' . $doctorid);
        $username = $userData['user_name'];
        $this->smarty->assign('username', $username);

        $this->smarty->assign('phpasthx_obj', $obj);
        $this->smarty->assign('itr', $itr);
        $this->smarty->assign('case_summary', $case_summary);
        $this->smarty->display('investigation/newphpasthxrow.html');
    }

    public function addphpasthxType() {

        //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');
        $patientId = filter_input(INPUT_GET, 'id');


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

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

        echo $insertid;


        $mydata = R::findone('phpasthx', 'id = ? and patientid = ?', [$selectid, $patientId]);
        $mydata->pasthx_type = $insertid;
        R::store($mydata);
    }

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

}
