<?php

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

//here name of controll to known
        $this->controllname = '_phmain';
        $this->shcontrollname = '_phmain';
        $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] = '_phmain'; //controller name or tab name
        $active[1] = '_phmain'; //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("_sidebar.php");
        require_once("_leftsidebar.php");
        require_once("_footer.php");
    }

//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");
        $this->smarty->assign('patientId', $patientId);

        ### ================================================================phmenstural for patient ========================================*/
        $myphmenstrual = R::findone('phmenstrual', 'del = ? and patientid = ? order by phl_mp  DESC and id desc', [$del, $patientId]);
        $myphmenstrual->phl_mp = date('d-m-Y', strtotime($myphmenstrual->phl_mp));
        $this->smarty->assign('myphmenstrual', $myphmenstrual);
        $this->smarty->assign('phmenstrualcount', count($myphmenstrual));

        if ($myphmenstrual->phregularity != '') {
            $phregualrity = R::findone('phregularity', 'id = ?', [$myphmenstrual->phregularity]);
            $this->smarty->assign('phregualrity', $phregualrity);
        }

        if ($myphmenstrual->phamount != '') {
            $phamount = R::findone('phamount', 'id = ?', [$myphmenstrual->phamount]);
            $this->smarty->assign('phamount', $phamount);
        }

        if ($myphmenstrual->phdysmenorrhoea != '') {
            $phdysmenorrhoea = R::findone('phdysmenorrhoea', 'id = ?', [$myphmenstrual->phdysmenorrhoea]);
            $this->smarty->assign('phdysmenorrhoea', $phdysmenorrhoea);
        }

        /* ================================================================================================================================ */
        $myallcontract = R::findall('phcontraception', 'del = ? and patientid = ? order by id DESC', [$del, $patientId]);
        $this->smarty->assign('myallcontract', $myallcontract);
        $this->smarty->assign('myallcontractcount', count($myallcontract));

        if ($myallcontract->contype != '') {
            $phcontracttype = R::findall('phcontracttype', 'id = ?', [$myallcontract->contype]);
            $this->smarty->assign('phcontracttype', $phcontracttype);
        }


        /* =====================================================phobstetric=========================================================================== */

        $myallobsteric = R::getAll('select id  from phobstetric where del = ?  and patientid = ? ', [$del, $patientId]);
        $this->smarty->assign('myallobsteric', count($myallobsteric));

        $allphobstericterminate = R::findall('phobstericterminate', 'del = 0');
        foreach ($allphobstericterminate as $myterminate) {
            $obserticcounter = R::getAll('select id from phobstetric where obstermination =? ', [$myterminate->id]);
            $myterminate->obcounter = count($obserticcounter);
        }
        $this->smarty->assign('allphobstericterminate', $allphobstericterminate);

        /* =====================================================Past Medical H=========================================================================== */
        // R::debug();
        $allpastmedical = R::getAll('SELECT DISTINCT(phpastmedical.medicaldisease) AS medicaldisease,phpastmedical.id ,phpastmedicaldisease.name FROM phpastmedical,phpastmedicaldisease where phpastmedical.medicaldisease = phpastmedicaldisease.id  and  phpastmedical.del = ? and phpastmedical.patientid = ? limit 0,5', [$del, $patientId]);

        $this->smarty->assign('allpastmedical', $allpastmedical);
        $this->smarty->assign('allpastmedicalcount', count($allpastmedical));

        /* =====================================================Past surgical H=========================================================================== */
        $allpastsurgical = R::getAll('select DISTINCT(phpastsurgical.surgicaloperation) AS surgicaloperation ,phpastsurgical.id ,phpastsurgicaloperation.name from  phpastsurgical join phpastsurgicaloperation on phpastsurgicaloperation.id = phpastsurgical.surgicaloperation where phpastsurgical.del = ? and phpastsurgical.patientid = ? limit 0,5 ', [$del, $patientId]);

        $this->smarty->assign('allpastsurgicalcount', count($allpastsurgical));
        $this->smarty->assign('allpastsurgical', $allpastsurgical);




        /* =====================================================Past gynecological H=========================================================================== */

        $allphpastgynecological = R::getAll('select DISTINCT(phpastgynecological.gynecologicaloperation) AS gynecologicaloperation,phpastgynecological.id,phpastgynecologicaloperation.name from  phpastgynecological join phpastgynecologicaloperation on phpastgynecologicaloperation.id= phpastgynecological.gynecologicaloperation where phpastgynecological.del = ? and phpastgynecological.patientid = ? limit 0,5 ', [$del, $patientId]);

        $this->smarty->assign('allphpastgynecologicalcount', count($allphpastgynecological));
        $this->smarty->assign('allphpastgynecological', $allphpastgynecological);


        /* =====================================================Past art H=========================================================================== */
        $allphpastart = R::getall('select DISTINCT(phpastart.arttype) AS arttype ,phpastart.id,phpastarttype.name from  phpastart join phpastarttype on phpastarttype.id= phpastart.arttype where phpastart.del = ? and phpastart.patientid = ? limit 0,5 ', [$del, $patientId]);

        $this->smarty->assign('allphpastartcount', count($allphpastart));
        $this->smarty->assign('allphpastart', $allphpastart);



        /* =====================================================familyH=========================================================================== */

        $allphfamily = R::getall('select DISTINCT(phfamily.family_disease) AS family_disease ,phfamily.id,phfamilydisease.name from  phfamily,phfamilydisease where phfamilydisease.id =phfamily.family_disease and phfamily.del = ? and phfamily.patientid = ? limit 0,5', [$del, $patientId]);

        $this->smarty->assign('allphfamilycount', count($allphfamily));
        $this->smarty->assign('allphfamily', $allphfamily);








        /* ============================================================================formodels ========================================================== */


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

    /* ===============================================================Menstrual H ======================================================== */

    public function mensturaldetail() {

        $del = 0;
        $patientId = filter_input(INPUT_GET, "id");
        $this->smarty->assign('patientId', $patientId);


        ### phmenstural for patient
        $allphmenstrual = R::findall('phmenstrual', 'del = ? and patientid = ? order by phl_mp DESC,id DESC', [$del, $patientId]);

        foreach ($allphmenstrual as $myallphmenstrual) {
            $myallphmenstrual->phl_mp = date('d-m-Y', strtotime($myallphmenstrual->phl_mp));

            ##### ph regualrity
            if (!empty($myallphmenstrual->phregularity)) {
                $phregualrity = R::findone('phregularity', 'id = ?', [$myallphmenstrual->phregularity]);
                $myallphmenstrual->regualrityname = $phregualrity->name;
            }
            ##### phamount
            if (!empty($myallphmenstrual->phamount)) {
                $phamount = R::findone('phamount', 'id = ?', [$myallphmenstrual->phamount]);
                $myallphmenstrual->amountname = $phamount->name;
            }
            ##### phdysmenorrhoea
            if (!empty($myallphmenstrual->phdysmenorrhoea)) {
                $phdysmenorrhoea = R::findone('phdysmenorrhoea', 'id = ?', [$myallphmenstrual->phdysmenorrhoea]);
                $myallphmenstrual->dysmenorrhoeaname = $phdysmenorrhoea->name;
            }
        }

        $this->smarty->assign('allphmenstrualcount', count($allphmenstrual));
        $this->smarty->assign('allphmenstrual', $allphmenstrual);


        $this->smarty->display('_phmain/menstrual.html');
    }

    /* ===============================================================Contraception H ======================================================== */

    public function contractdetail() {

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

        ### phcontraction for patient
        $allcontract = R::findall('phcontraception', 'del = ? and patientid = ? order by id DESC', [$del, $patientId]);
        foreach ($allcontract as $myallcontract) {

            ##### ph contracttype
            if (!empty($myallcontract->contype)) {
                $phcontracttype = R::findone('phcontracttype', 'id = ?', [$myallcontract->contype]);
                $myallcontract->contracttypename = $phcontracttype->name;
            }
        }
        $this->smarty->assign('allcontractcount', count($allcontract));
        $this->smarty->assign('allcontract', $allcontract);
        $this->smarty->display('_phmain/contract.html');
    }

    /* ===============================================================obstetric H ======================================================== */

    public function obstetricdetail() {

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

        ### phcontraction for patient
        $allobsteric = R::findall('phobstetric', 'del = ? and patientid = ? order by obsdate desc, id DESC', [$del, $patientId]);
        foreach ($allobsteric as $myallobsteric) {
            if (!empty($myallobsteric->obstermination)) {
                $phobstericterminate = R::findone('phobstericterminate', 'id = ?', [$myallobsteric->obstermination]);
                $myallobsteric->terminatename = $phobstericterminate->name;
            }
            if (!empty($myallobsteric->obsplace)) {
                $phobstericterplace = R::findone('phobstericterplace', 'id = ?', [$myallobsteric->obsplace]);
                $myallobsteric->placename = $phobstericterplace->name;
            }
        }
        $this->smarty->assign('allobstericcount', count($allobsteric));
        $this->smarty->assign('allobsteric', $allobsteric);
        $this->smarty->display('_phmain/obstetric.html');
    }

    /* =============================================================== examination ======================================================== */

    public function examdetail() {

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

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

        ### phcontraction for patient
        $allexam = R::findall('examination', 'del = ? and patientid = ? order by examinationdate desc, id DESC', [$del, $patientId]);
        foreach ($allexam as $data) {

            $b = substr($data->bP, 0, 3);
            $bRest = substr($data->bP, 3);

            $data->bP = $b . ' / ' . $bRest;

            $b = substr($data->bp2, 0, 3);
            $bRest = substr($data->bp2, 3);

            $data->bp2 = $b . ' / ' . $bRest;



            $data->examinationdate = date('Y/m/d', strtotime($data->examinationdate));

            if ($programeSetting->head != 1 && $data->headandneck > 0) {
                $headandneck = R::findone('examinationhead', 'id = ?', [$data->headandneck]);
                $data->headandneck = $headandneck->name;
            }

            if ($programeSetting->chest != 1 && $data->chestandheart > 0) {
                $chestandheart = R::findone('examinationchest', 'id = ?', [$data->chestandheart]);
                $data->chestandheart = $chestandheart->name;
            }

            if ($programeSetting->abdomen != 1 && $data->abdomen > 0) {
                $abdomen = R::findone('examinationabdomen', 'id = ?', [$data->abdomen]);
                $data->abdomen = $abdomen->name;
            }

            if ($programeSetting->pelvis != 1 && $data->pelvis > 0) {
                $pelvis = R::findone('examinationpelvis', 'id = ?', [$data->pelvis]);
                $data->pelvis = $pelvis->name;
            }

            if ($programeSetting->extremitis != 1 && $data->extremitis > 0) {
                $extremitis = R::findone('examinationextremitis', 'id = ?', [$data->extremitis]);
                $data->extremitis = $extremitis->name;
            }
        }
        $this->smarty->assign('allexamcount', count($allexam));
        $this->smarty->assign('allexam', $allexam);
        $this->smarty->display('_phmain/exam.html');
    }

    /* ===============================================================pastmedical ======================================================== */

    public function pastmedicaldetail() {

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

        $allpastmedical = R::findall('phpastmedical', 'del = ? and patientid = ? order by id DESC', [$del, $patientId]);
        foreach ($allpastmedical as $myallpastmedical) {

            $phmedicaldisease = R::findone('phpastmedicaldisease', 'id = ?', [$myallpastmedical->medicaldisease]);
            $myallpastmedical->diseasename = $phmedicaldisease->name;
        }
        $this->smarty->assign('phpastmedicalcount', count($allpastmedical));
        $this->smarty->assign('allpastmedical', $allpastmedical);
        $this->smarty->display('_phmain/pastmedical.html');
    }

    /* ===============================================================pastsurgical======================================================== */

    public function pastsurgicaldetail() {

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

        $allpastsurgical = R::findall('phpastsurgical', 'del = ? and patientid = ? order by phsurgicalyear desc, id DESC', [$del, $patientId]);
        foreach ($allpastsurgical as $myallpastsurgical) {
            if (!empty($myallpastsurgical->surgicaloperation)) {
                $phsurgicaloperation = R::findone('phpastsurgicaloperation', 'id = ?', [$myallpastsurgical->surgicaloperation]);
                $myallpastsurgical->operationname = $phsurgicaloperation->name;
            }
        }
        $this->smarty->assign('allpastsurgicalcount', count($allpastsurgical));
        $this->smarty->assign('allpastsurgical', $allpastsurgical);
        $this->smarty->display('_phmain/pastsurgical.html');
    }

    /* ===============================================================pastgynecological======================================================== */

    public function pastgynecologicaldetail() {

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

        $allphpastgynecological = R::findall('phpastgynecological', 'del = ? and patientid = ? order by gynecologicalyear desc, id DESC', [$del, $patientId]);
        foreach ($allphpastgynecological as $myallphpastgynecological) {
            if (!empty($myallphpastgynecological->gynecologicaloperation)) {
                $phgynecologicaloperation = R::findone('phpastgynecologicaloperation', 'id = ?', [$myallphpastgynecological->gynecologicaloperation]);
                $myallphpastgynecological->operationname = $phgynecologicaloperation->name;
            }
        }
        $this->smarty->assign('allphpastgynecologicalcount', count($allphpastgynecological));
        $this->smarty->assign('allphpastgynecological', $allphpastgynecological);
        $this->smarty->display('_phmain/pastgynecological.html');
    }

    /* ===============================================================pastart======================================================== */

    public function pastartdetail() {

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

        $allphpastart = R::findall('phpastart', 'del = ? and patientid = ? order by artyear desc, id desc', [$del, $patientId]);
        foreach ($allphpastart as $myallphpastart) {
            if (!empty($myallphpastart->arttype)) {
                $phpastarttype = R::findone('phpastarttype', 'id = ?', [$myallphpastart->arttype]);
                $myallphpastart->arttypename = $phpastarttype->name;
            }
            if (!empty($myallphpastart->artcenter)) {
                $phpastartcenter = R::findone('phpastartcenter', 'id = ?', [$myallphpastart->artcenter]);
                $myallphpastart->artcentername = $phpastartcenter->name;
            }
        }
        $this->smarty->assign('allphpastartcount', count($allphpastart));
        $this->smarty->assign('allphpastart', $allphpastart);
        $this->smarty->display('_phmain/pastart.html');
    }

    public function pastartinternaldetail() {
        $patientId = filter_input(INPUT_GET, "id");

        $monitorSheets = R::findAll('mointoringsheet', 'status = 1 AND patientid = ? and deleted = 0 
                  order by monitordate desc ', [$patientId]);

        foreach ($monitorSheets as $data) {
            $procedure = R::load('mointoringsheetprocedure', $data->procedure);
            $data->procedure = $procedure->name;

            $protocol = R::load('mointoringsheetprotocol', $data->protocol);
            $data->protocol = $protocol->name;
        }

        $this->smarty->assign('internalcount', count($monitorSheets));
        $this->smarty->assign('internalart', $monitorSheets);
        $this->smarty->display('_phmain/internalart.html');
    }

    public function familydetail() {

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

        $allphfamily = R::findall('phfamily', 'del = ? and patientid = ? order by id desc', [$del, $patientId]);
        foreach ($allphfamily as $myallphfamily) {
            if (!empty($myallphfamily->familyDisease)) {
                $phfamilydisease = R::findone('phfamilydisease', 'id = ?', [$myallphfamily->familyDisease]);
                $myallphfamily->familydiseasename = $phfamilydisease->name;
            }
            if (!empty($myallphfamily->familyRelative)) {
                $phfamilyrelative = R::findone('phfamilyrelative', 'id = ?', [$myallphfamily->familyRelative]);
                $myallphfamily->familyrelativename = $phfamilyrelative->name;
            }
        }
        $this->smarty->assign('allphfamilycount', count($allphfamily));
        $this->smarty->assign('allphfamily', $allphfamily);
        $this->smarty->display('_phmain/family.html');
    }

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

}
