<?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
 */

class patientdata {

    public static function patientdatashow($smarty, $hosturl) {

        $programSetting = R::findOne('programesetting', 'limit 1'); //getAll('SELECT  simpleview FROM programesetting limit 1');
        $simpleView = $programSetting->simpleview;     ## 0 for simple form  , 1 for complete form
        $smarty->assign('simpleView', $simpleView);
        $smarty->assign('hospitalView', $programSetting->visitsform);
        $smarty->assign('share', $programSetting->system_type);
        $smarty->assign('obstetricView', $programSetting->obstetric);
        $smarty->assign('programSetting', $programSetting);
        
           $programesettingex = R::findOne('programesettingex');
        $smarty->assign('programesettingex', $programesettingex);


        $id = filter_input(INPUT_GET, 'patientid');
        //GET ALL PATIENT DATA TO SHOW IT IN ALL PROGRAM
        $getAllPatientData = R::getRow("SELECT * FROM patients WHERE id = $id");

        //GET WIFE TYPE
        if (!empty($getAllPatientData['wiftypeid'])) {
            $wifetype = R::findOne('wifetypes', 'id = ? ', [$getAllPatientData['wiftypeid']]);
            $getAllPatientData['wifetype'] = $wifetype->title;
        }

        if ($getAllPatientData['dateofbirth'] == '0000-00-00' || is_null($getAllPatientData['dateofbirth'])) {
            $smarty->assign("new_dateofbirth_calc", '');
        } else {
            ## calculate wife age
            $dateWife = new DateTime($getAllPatientData['dateofbirth']);
            $today1 = new DateTime(date('Y-m-d'));
            $interva2 = $today1->diff($dateWife);
            $smarty->assign('new_dateofbirth_calc', $interva2->y);


//            $currentdate_x = time();
//            $currentdateofbirth = strtotime($getAllPatientData['dateofbirth']);
//            $newage = $currentdate_x - $currentdateofbirth;
//            $y = ($newage / (60 * 60 * 24 * 365));
//            $smarty->assign("new_dateofbirth_calc", floor($y));
        }

        if ($getAllPatientData['husbanddateofbirth'] != '0000-00-00') {
            $currentdate_x = time();
            $currentdateofbirth_husband = strtotime($getAllPatientData['husbanddateofbirth']);
            $new_husbandage = $currentdate_x - $currentdateofbirth_husband;
            $husband_hh = ($new_husbandage / (60 * 60 * 24 * 365));
            $smarty->assign("new_dateofbirth_husband", floor($husband_hh));
        } else {
            $smarty->assign("new_dateofbirth_husband", '');
        }

        if ($getAllPatientData['husbanddateofbirth'] == '0000-00-00' || is_null($getAllPatientData['husbanddateofbirth'])) {
            $smarty->assign('new_dateofbirth_husband', 0);
        } else {
            ## calculate husband age
            $dateHusband = new DateTime($getAllPatientData['husbanddateofbirth']);
            $today1 = new DateTime(date('Y-m-d'));
            $interval = $today1->diff($dateHusband);
            $smarty->assign('new_dateofbirth_husband', $interval->y);
        }

        if (!empty($getAllPatientData['mirragefromdate']) && $getAllPatientData['mirragefromdate'] != '0000-00-00') {
            $today = new DateTime(date('Y-m-d'));
            $mirragedate = new DateTime($getAllPatientData['mirragefromdate']);
            $interval = $today->diff($mirragedate);
            $smarty->assign("mirrageyears", $interval->y);
            $smarty->assign("mirragemonths", $interval->m);
        } else {
            $smarty->assign("mirrageyears", '');
            $smarty->assign("mirragemonths", '');
        }

        if (!empty($getAllPatientData['statuesid'])) {
            $status = R::load('wifestatus', $getAllPatientData['statuesid']);
            $smarty->assign("wifeStatus", $status->title);
        } else {
            $smarty->assign("wifeStatus", '');
        }

        //GET HUSBAND TYPE
        if (!empty($getAllPatientData['husbandtypeid'])) {
            $husbandType = R::findOne('husbandtypes', 'id = ? ', [$getAllPatientData['husbandtypeid']]);
            $getAllPatientData['husbandtype'] = $husbandType->title;
        }
        //GET WIFE JOB
        if (!empty($getAllPatientData['wifejob'])) {
            $wifeJob = R::findOne('wifejobs', 'id = ? ', [$getAllPatientData['wifejob']]);
            $getAllPatientData['wifejob'] = $wifeJob->title;
        }
        //GET HUSBAND JOB
        if (!empty($getAllPatientData['husbandjob'])) {
            $husbandJob = R::findOne('husbandjobs', 'id = ? ', [$getAllPatientData['husbandjob']]);
            $getAllPatientData['husbandjob'] = $husbandJob->title;
        }

        //GET BL JOB

        if (!empty($getAllPatientData['husbandbl'])) {

            $wifeblpdHus = R::getRow('SELECT * FROM bloodtypes WHERE id = "' . $getAllPatientData['husbandbl'] . '"');
            $getAllPatientData['husblpatientdata'] = $wifeblpdHus['title'];
        } else {
            $getAllPatientData['husblpatientdata'] = 0;
        }
        if (!empty($getAllPatientData['wifebl'])) {
            $wifeblpd = R::getRow('SELECT * FROM bloodtypes WHERE id = "' . $getAllPatientData['wifebl'] . '"');
            $getAllPatientData['wifeblpatientdata'] = $wifeblpd['title'];
        } else {
            $getAllPatientData['wifeblpatientdata'] = 0;
        }

        //$examination = R::findone('examination', 'del = ? and patientid = ? and examinationdate = ? order by id DESC limit 1', [0, $id, date('Y-m-d')]);
        $examination = R::findone('examination', 'del = ? and patientid = ? order by id DESC limit 1', [0, $id]);
        if (count($examination) > 0) {
            $smarty->assign('exmination', $examination);
        } else {
            $exmination_obj = R::dispense('examination');
            $exmination_obj->patientid = $id;
            $exmination_obj->doctorid = $_SESSION['user_id'];
            $exmination_obj->sysdate = date('Y-m-d');
            $exmination_obj->examinationdate = date('Y-m-d');
            $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;

            //R::store($exmination_obj);
            $smarty->assign('exmination', $exmination_obj);
        }


        $del = 0;
        $patientId = self::patientid();
        $smarty->assign('patientId', $patientId);

        ### ================================================================examination for patient ========================================*/
        ### ================================================================phmenstural for patient ========================================*/
        $myphmenstrual = R::findone('phmenstrual', 'del = ? and patientid = ? order by phl_mp  DESC, id desc limit 1', [$del, $patientId]);

        $smarty->assign('myphmenstrual', $myphmenstrual);
        $smarty->assign('phmenstrualcount', count($myphmenstrual));
        if (count($myphmenstrual) > 0) {
            if (empty($myphmenstrual->menarche) && empty($myphmenstrual->duration) && empty($myphmenstrual->length) && empty($myphmenstrual->phregularity) && empty($myphmenstrual->phamount) && empty($myphmenstrual->phdysmenorrhoea)) {
                $smarty->assign('idData', "no");
            } else {
                $smarty->assign('idData', "yes");
                $phregualrity = R::findone('phregularity', 'id = ?', [$myphmenstrual->phregularity]);
                $smarty->assign('phregualrity', $phregualrity);


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


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


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

        if (count($myallcontract) > 0) {
            if (empty($myallcontract->contype) && empty($myallcontract->contduration) && empty($myallcontract->contstopped) && empty($myallcontract->contranote)) {
                $smarty->assign('idData1', "no");
            } else {
                $smarty->assign('idData1', "yes");
                $phcontracttype = R::load('phcontracttype', $myallcontract->contype);
                //R::findall('phcontracttype', 'id = ?', [$myallcontract->contype]);
                $smarty->assign('phcontracttype', $phcontracttype);
            }
        }


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

        $myallobsteric = R::getAll('select id  from phobstetric where del = ?  and patientid = ? ', [$del, $patientId]);


        $allphobstericterminate = R::findall('phobstericterminate', 'del = 0');


        #### c.s count
        $csdata = R::getAll('select id from phobstetric where obstermination =? and del = 0  and patientid = ?  ', [2, $patientId]);

        $cs = $getAllPatientData['cs'];

        $cscounter = count($csdata) + $cs;
        $smarty->assign('cscounter', $cscounter);

        #### p count
        $svddata = R::getAll('select id from phobstetric where obstermination =? and del = 0  and patientid = ?  ', [1, $patientId]);

        $p = $getAllPatientData['pno'];

        $pcounter = count($svddata) + $cscounter + $p;
        $smarty->assign('pcounter', $pcounter);


        ###AB COUNT
        $Abortiondata = R::getAll('select id from phobstetric where obstermination =? and del = 0  and patientid = ?  ', [4, $patientId]);
        $ab = $getAllPatientData['ab'];
        $abcounter = count($Abortiondata) + $ab;
        $smarty->assign('abcounter', $abcounter);


        ###Ectopic COUNT
        $Ectopicdata = R::getAll('select id from phobstetric where obstermination =? and del = 0  and patientid = ?  ', [3, $patientId]);
        $Ectopic = $getAllPatientData['ectopic'];
        $Ectopiccounter = count($Ectopicdata) + $Ectopic;
        $smarty->assign('Ectopiccounter', $Ectopiccounter);


        ###Vmole COUNT
        $Vmoledata = R::getAll('select id from phobstetric where obstermination =? and del = 0  and patientid = ?  ', [5, $patientId]);
        $vmodel = $getAllPatientData['vmodel'];
        $vmodelcounter = count($Vmoledata) + $vmodel;
        $smarty->assign('vmodelcounter', $vmodelcounter);

        $currently = 0;
        if ($getAllPatientData['currently'] == 1) {
            $currently = 1;
        }

        $obstriccountnew = $currently + $pcounter + $abcounter + $Ectopiccounter + $vmodelcounter; //+ $cs count($myallobsteric) +
        $smarty->assign('myallobsteric', $obstriccountnew);


        /* =====================================================Past Medical H=========================================================================== */
        $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]);

        $smarty->assign('allpastmedical', $allpastmedical);
        $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 = ? order by  phpastsurgical.id asc limit 0,5 ', [$del, $patientId]);

        $smarty->assign('allpastsurgicalcount', count($allpastsurgical));
        $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 = ? order by   phpastgynecological.id  limit 0,5 ', [$del, $patientId]);

        $smarty->assign('allphpastgynecologicalcount', count($allphpastgynecological));
        $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 = ? order by phpastart.id  limit 0,5 ', [$del, $patientId]);

        $smarty->assign('allphpastartcount', count($allphpastart));
        $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]);
        $smarty->assign('allphfamilycount', count($allphfamily));
        $smarty->assign('allphfamily', $allphfamily);

        $allphsexual = R::getAll('select DISTINCT(phsexual.sexual_type) AS sexual_type ,phsexual.id,phsexualtypes.name 
                from  phsexual,phsexualtypes where phsexualtypes.id =phsexual.sexual_type 
                and phsexual.del = ? and phsexual.patientid = ? limit 0,5', [$del, $patientId]);
        $smarty->assign('allphsexualcount', count($allphsexual));
        $smarty->assign('allphsexual', $allphsexual);


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


        $smarty->assign('patientdata', $getAllPatientData);
        $smarty->assign('patientid', self::patientid());

        if ($programSetting->system_type == 2) {
            $smarty->display('patients/patientdatashare.html');
        } else {
            $smarty->display('patients/patientdata.html');
        }
    }

    public static function patientid() {
        $id = filter_input(INPUT_GET, 'patientid');
        return $id;
    }

}
