<?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('btype', $programSetting->btype);
        $smarty->assign('hbtype', $programSetting->hbtype);
        $smarty->assign('risk', $programSetting->risk);
        $smarty->assign('notes', $programSetting->notes);





        $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");

        if ($programSetting->patient_doctor == 1 && $getAllPatientData['doctorid'] > 0) {
            $smarty->assign('patient_doctor', $programSetting->patient_doctor);

            $patient_doctor = R::getRow('SELECT name FROM awusers WHERE user_id = ? ', [$getAllPatientData['doctorid']]);
            $smarty->assign('patient_doctor_name', $patient_doctor['name']);
        }



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

        $today = new DateTime(date('d-m-Y'));
        if ($getAllPatientData['dateofbirth'] != '0000-00-00' && $getAllPatientData['dateofbirth'] != '' && $getAllPatientData['dateofbirth'] != Null) {
            $datewifebirth = date('d-m-Y ', strtotime($getAllPatientData['dateofbirth']));
            $dateWife = new DateTime($datewifebirth);

            ## calculate wife age
            $interva2 = $today->diff($dateWife);
            $wAge = $interva2->y . 'Y , ' . $interva2->m . 'M , ' . $interva2->d . 'D  ';
            $smarty->assign("new_dateofbirth_calc", $wAge);
        } else {
            $smarty->assign("new_dateofbirth_calc", '');
        }

        if ($getAllPatientData['husbanddateofbirth'] != '0000-00-00' && $getAllPatientData['husbanddateofbirth'] != '' && $getAllPatientData['husbanddateofbirth'] != Null) {
            $datehusbirth = date('d-m-Y ', strtotime($getAllPatientData['husbanddateofbirth']));
            $dateHusband = new DateTime($datehusbirth);
            ## calculate husband age

            $interval = $today->diff($dateHusband);
            $hAge = $interval->y . 'Y , ' . $interval->m . 'M , ' . $interval->d . 'D  ';
            $smarty->assign("new_dateofbirth_husband", $hAge);
        } else {
            $smarty->assign("new_dateofbirth_husband", '');
        }


//        if ($getAllPatientData['dateofbirth'] != '0000-00-00' && $getAllPatientData['dateofbirth'] != '' && $getAllPatientData['dateofbirth'] != Null) {
//            $currentdate_x = time();
//            $currentdateofbirth = strtotime($getAllPatientData['dateofbirth']);
//            $newage = $currentdate_x - $currentdateofbirth;
//            $y = ($newage / (60 * 60 * 24 * 365));
//            $smarty->assign("new_dateofbirth_calc", floor($y));
//        } else {
//            $smarty->assign("new_dateofbirth_calc", '');
//        }
//        if ($getAllPatientData['husbanddateofbirth'] != '0000-00-00' && $getAllPatientData['husbanddateofbirth'] != '' && $getAllPatientData['husbanddateofbirth'] != Null) {
//            $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 (!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) {
            $b = substr($examination->bP, 0, 3);
            $bRest = substr($examination->bP, 3);

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

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

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

            $smarty->assign('exmination', $examination);
            $smarty->assign('showExam', 1);
        } 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]);
        $svd = $getAllPatientData['svd'];
        $p = $getAllPatientData['pno'];

        $pcounter = count($svddata) + $cscounter + $p + $svd;
        $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);

        ###svd COUNT
        $svdcounter = count($svddata) + $svd;
        $smarty->assign('svdcounter', $svdcounter);


        $obstriccountnew = $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.phsurgicalyear asc, 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.gynecologicalyear ,  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.artyear , 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);


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


        $previous_marriages = R::getAll('select * from previous_marriage where deleted != 1  and patientid = ?
                 ', [$patientId]);
        $smarty->assign('previous_marriages', $previous_marriages);
        $smarty->assign('prevMarCount', count($previous_marriages));

        $hus_previous_marriages = R::getAll('select * from hus_previous_marriage where deleted != 1  
                and patientid = ? ', [$patientId]);
        $smarty->assign('hus_previous_marriages', $hus_previous_marriages);
        $smarty->assign('prevMarHCount', count($hus_previous_marriages));



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



        if ($programSetting->programview == 1) {
            $smarty->display('patients/patientdata.html');
        } else {
            $smarty->display('patients/patientdatageneral.html');
        }
    }

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

}
