<?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');
    }

    /* ===============================================================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 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');
    }

    public function sexualdetail() {

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

        $allphsexual = R::findall('phsexual', 'del = ? and patientid = ? order by id desc', [$del, $patientId]);
        foreach ($allphsexual as $data) {
            if (!empty($data->sexual_type)) {
                $phsexualtypes = R::load('phsexualtypes', $data->sexual_type);
                $data->sexual_type = $phsexualtypes->name;
            }
        }
        $this->smarty->assign('allphsexualcount', count($allphsexual));
        $this->smarty->assign('allphsexual', $allphsexual);
        $this->smarty->display('_phmain/sexual.html');
    }

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

}
