<?php

require_once("imp/_imp.php");

class Controllers {

//public vars
    public $smarty;
    public $hosturl;
    public $controllname;
    public $shcontrollname;
    public $active;
    public $time;
    public $today;
    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 = 'spermfreezing';
        $this->shcontrollname = 'spermfreezing';
        $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] = 'spermfreezing'; //controller name or tab name
        $active[1] = 'spermfreezing'; //controller name
// do name  // switch($do)
        switch ($acty) {
            case '':$active[2] = 'spermfreezingadd';
                break;
            case 'show':$active[2] = 'spermfreezingshow';
                break;
            default :$active[2] = 'spermfreezingshow';
                break;
        }
        $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();

        //find loading time
        $this->time = microtime(true);
        $this->today = date('Y-m-d');
    }

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

        $patientid = filter_input(INPUT_GET, "patientid");
         if (empty($this->patientid)) {
            header('Location:' . $this->hosturl . '/controllers/index.php?empty=1');
            exit();
        }
        $this->smarty->assign('patientId', $patientid);
        //load all parents here
//        $showElems = R::getAll('SELECT * FROM spermfreezingreport');
        $showElems = R::getAll('SELECT spermfreezingreport.id as spermfreezingreport_id ,spermfreezingreport.patient_id as patientId,
            patients.wifename as wifename ,patients.husdandname as husbandname from spermfreezingreport 
            LEFT JOIN patients ON spermfreezingreport.patient_id =patients.id where spermfreezingreport.patient_id=' . $patientid . '

         ');
        $this->smarty->assign('showElems', $showElems);

//        $patient = R::load("patients", $patientid);
//
//        $wifename = $patient->wifename;
//        $husdandname = $patient->husdandname;
//        $this->smarty->assign('wifename', $wifename);
//        $this->smarty->assign('husdandname', $husdandname);
        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        if ($patientid) {
            patientdata::patientdatashow($this->smarty, $this->hosturl);
        }
        $this->smarty->display('spermfreezing/show.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

    public function add() {
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
        $patientid = filter_input(INPUT_GET, "patientid");
        $this->smarty->assign('patientId', $patientid);
        if (empty($this->patientid)) {
            header('Location:' . $this->hosturl . '/controllers/index.php?empty=1');
            exit();
        }
        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        if ($patientid) {
            patientdata::patientdatashow($this->smarty, $this->hosturl);
        }
        //referred doctors
        $ref_dr_id = R::load(patients, $patientid);
        if (!empty($ref_dr_id->refer_doctor)) {
            $ref_dr = R::getAll("select* from awusers where user_id=$ref_dr_id->refer_doctor");
            $this->smarty->assign('ref_dr', $ref_dr[0]);
        } else {
            $this->smarty->assign('ref_dr', "لايوجد");
        }


        $embryologist = R::getAll("select* from awusers where role_id=11");
        $this->smarty->assign('embryologist', $embryologist);

        $users = R::getAll("select* from awusers");
        $this->smarty->assign('users', $users);


        $freezing_media = R::findAll("freezingmedia");
        $this->smarty->assign('freezing_media', $freezing_media);

        $freezingprotocol = R::findAll("freezingprotocol");
        $this->smarty->assign('freezingprotocol', $freezingprotocol);

        $this->smarty->display('spermfreezing/add.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

    public function addit() {

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

        //vars here

        $patientid = filter_input(INPUT_GET, "patientid");
        $ref_by_dr_id = filter_input(INPUT_POST, 'ref_by_dr_id');
        $sperm_source = filter_input(INPUT_POST, 'sperm_source');
        $count = filter_input(INPUT_POST, 'count');
        $motility = filter_input(INPUT_POST, 'motility');
        $a_plus_b = filter_input(INPUT_POST, 'a_plus_b');
        $morphology = filter_input(INPUT_POST, 'morphology');
        $comment = filter_input(INPUT_POST, 'comment');
        $samples_no = filter_input(INPUT_POST, 'samples_no');
        $media = filter_input(INPUT_POST, 'media');
        $protocol = filter_input(INPUT_POST, 'protocol');
        $purpose = filter_input(INPUT_POST, 'purpose');
        $location = filter_input(INPUT_POST, 'location');
        $color_of_pen = filter_input(INPUT_POST, 'color_of_pen');
        $embryology1 = filter_input(INPUT_POST, 'embryology1');
        $embryology2 = filter_input(INPUT_POST, 'embryology2');

        $this->smarty->assign('patientId', $patientid);

        //here the add
        $report = R::dispense('spermfreezingreport');

        $report->patient_id = $patientid;
        $report->sperm_source = $sperm_source;
        $report->ref_by_dr_id = $ref_by_dr_id;
        $report->count = $count;
        $report->motility = $motility;
        $report->a_plus_b = $a_plus_b;
        $report->morphology_abn = $morphology;
        $report->comment = $comment;
        $report->samples_no = $samples_no;
        $report->freezing_media_id = $media;
        $report->freezing_protocol_id = $protocol;
        $report->purpose_of_freezing = $purpose;
        $report->location = $location;
        $report->color_of_pen = $color_of_pen;
        $report->embryologist1 = $embryology1;
        $report->embryologist2 = $embryology2;
        $report->date = date('Y-m-d');

        try {
            $id = R::store($report);
            if (!empty($patientid)) {
                header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?ac=show&patientid=' . $patientid);
            } else {
                header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?ac=show');
            }
        } catch (Exception $e) {
            header('Location:' . $this->hosturl . '/error/notadd/' . $this->controllname);
        }
    }

    public function show() {

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

        $patientid = filter_input(INPUT_GET, "patientid");
        $this->smarty->assign('patientId', $patientid);
        //load all parents here
//        $showElems = R::getAll('SELECT * FROM spermfreezingreport');
        $showElems = R::getAll('SELECT spermfreezingreport.id as spermfreezingreport_id ,spermfreezingreport.patient_id as patientId,
            patients.wifename as wifename ,patients.husdandname as husbandname from spermfreezingreport 
            LEFT JOIN patients ON spermfreezingreport.patient_id =patients.id where spermfreezingreport.patient_id=' . $patientid . '

         ');
        $this->smarty->assign('showElems', $showElems);

//        $patient = R::load("patients", $patientid);
//
//        $wifename = $patient->wifename;
//        $husdandname = $patient->husdandname;
//        $this->smarty->assign('wifename', $wifename);
//        $this->smarty->assign('husdandname', $husdandname);
        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        if ($patientid) {
            patientdata::patientdatashow($this->smarty, $this->hosturl);
        }
        $this->smarty->display('spermfreezing/show.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

    public function showallreports() {
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);
        $patientid = filter_input(INPUT_GET, "patientid");
        $this->smarty->assign('patientId', $patientid);
        //load all parents here

        $spermfreezingreport = R::getAll('SELECT* spermfreezingreport from where patient_id=' . $patientid . '  ');
        $this->smarty->assign('spermfreezingreport', $spermfreezingreport);

        $embryologyreport = R::getAll('SELECT* embryologyreport from where patient_id=' . $patientid . '  ');
        $this->smarty->assign('embryologyreport', $embryologyreport);

        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        if ($patientid) {
            patientdata::patientdatashow($this->smarty, $this->hosturl);
        }
        $this->smarty->display('spermfreezing/show_all_reports.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

    public function edit() {

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

        //get id for item
        $id = filter_input(INPUT_GET, 'id');
        $patientid = filter_input(INPUT_GET, "patientid");

        //load all here
        $editElement = R::load('spermfreezingreport', $id);
        $this->smarty->assign('editElement', $editElement);
        $this->smarty->assign('patientId', $patientid);

        //referred doctors
        $ref_dr_id = R::load(patients, $patientid);
        if (!empty($ref_dr_id->refer_doctor)) {
            $ref_dr = R::getAll("select* from awusers where user_id=$ref_dr_id->refer_doctor");
            $this->smarty->assign('ref_dr', $ref_dr[0]);
        } else {
            $this->smarty->assign('ref_dr', "لايوجد");
        }

        $embryologist = R::getAll("select* from awusers where role_id=11");
        $this->smarty->assign('embryologist', $embryologist);

        $users = R::getAll("select* from awusers");
        $this->smarty->assign('users', $users);

        $freezing_media = R::findAll("freezingmedia");
        $this->smarty->assign('freezing_media', $freezing_media);

        $freezingprotocol = R::findAll("freezingprotocol");
        $this->smarty->assign('freezingprotocol', $freezingprotocol);
        //call other class with other content
        header::headershow($this->smarty, $this->hosturl);
        sidebar::sidebarshow($this->smarty, $this->hosturl, $this->active);
        if ($patientid) {
            patientdata::patientdatashow($this->smarty, $this->hosturl);
        }
        $this->smarty->display('spermfreezing/edit.html');
        leftsidebar::leftsidebarshow($this->smarty, $this->hosturl);
        footer::footershow($this->smarty, $this->hosturl);
    }

    public function updateit() {

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

        //vars here
        $patientid = filter_input(INPUT_GET, "patientid");
        $ref_by_dr_id = filter_input(INPUT_POST, 'ref_by_dr_id');
        $sperm_source = filter_input(INPUT_POST, 'sperm_source');
        $count = filter_input(INPUT_POST, 'count');
        $motility = filter_input(INPUT_POST, 'motility');
        $a_plus_b = filter_input(INPUT_POST, 'a_plus_b');
        $morphology = filter_input(INPUT_POST, 'morphology');
        $comment = filter_input(INPUT_POST, 'comment');
        $samples_no = filter_input(INPUT_POST, 'samples_no');
        $media = filter_input(INPUT_POST, 'media');
        $protocol = filter_input(INPUT_POST, 'protocol');
        $purpose = filter_input(INPUT_POST, 'purpose');
        $location = filter_input(INPUT_POST, 'location');
        $color_of_pen = filter_input(INPUT_POST, 'color_of_pen');
        $embryology1 = filter_input(INPUT_POST, 'embryology1');
        $embryology2 = filter_input(INPUT_POST, 'embryology2');

        $id = filter_input(INPUT_POST, 'id');

        $this->smarty->assign('patientId', $patientid);


        //here the update
        $report = R::load('spermfreezingreport', $id);

        $report->patient_id = $patientid;
        $report->sperm_source = $sperm_source;
        $report->ref_by_dr_id = $ref_by_dr_id;
        $report->count = $count;
        $report->motility = $motility;
        $report->a_plus_b = $a_plus_b;
        $report->morphology_abn = $morphology;
        $report->comment = $comment;
        $report->samples_no = $samples_no;
        $report->freezing_media_id = $media;
        $report->freezing_protocol_id = $protocol;
        $report->purpose_of_freezing = $purpose;
        $report->location = $location;
        $report->color_of_pen = $color_of_pen;
        $report->embryologist1 = $embryology1;
        $report->embryologist2 = $embryology2;
        $report->date = date('Y-m-d');


        try {
            R::store($report);
            if (!empty($patientid)) {
                header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?ac=show&patientid=' . $patientid);
            } else {
                header('Location:' . $this->hosturl . '/controllers/' . $this->controllname . '.php?ac=show');
            }
        } catch (Exception $e) {
            header('Location:' . $this->hosturl . '/error/notupdate/' . $this->controllname);
        }
    }

    public function del() {

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

        $id = filter_input(INPUT_POST, 'id');
        try {
            R::exec('DELETE from spermfreezingreport WHERE id = ' . $id);
            echo 1;
        } catch (Exception $e) {
            
        }
    }

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

        $protocol = filter_input(INPUT_POST, 'protocol');

        $freezingprotocol = R::dispense('freezingprotocol');
        $freezingprotocol->protocol = $protocol;
        $inserted = R::store($freezingprotocol);
        echo $inserted;
    }

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

        $media = filter_input(INPUT_POST, 'media');

        $freezingmedia = R::dispense('freezingmedia');
        $freezingmedia->media = $media;
        $inserted = R::store($freezingmedia);
        echo $inserted;
    }

    public function getprint() {

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

        $patientId = filter_input(INPUT_POST, "patientid");

        $patientData = R::load("patients", $patientId);

        $wifename = $patientData->wifename;
        $husbandname = $patientData->husdandname;
        $husbandage = $patientData->husbandage;

        $this->smarty->assign('wifename', $wifename);
        $this->smarty->assign('husbandname', $husbandname);
        $this->smarty->assign('husbandage', $husbandage);

        $id = filter_input(INPUT_POST, "id");
        $editElement = R::load('spermfreezingreport', $id);

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

        $freezing_media_id = $editElement->freezing_media_id;
        $freezing_protocol_id = $editElement->freezing_protocol_id;
        $ref_by_dr_id = $editElement->ref_by_dr_id;
        $embryologist1 = $editElement->embryologist1;
        $embryologist2 = $editElement->embryologist2;

        $ref_by_dr_name = R::getRow('select name from awusers where user_id = ' . $ref_by_dr_id);
        $this->smarty->assign('ref_by_dr_name', $ref_by_dr_name["name"]);
        $embryologist1_name = R::getRow('select name from awusers where user_id = ' . $embryologist1);
        $this->smarty->assign('embryologist1_name', $embryologist1_name["name"]);

        $embryologist2_name = R::getRow('select name from awusers where user_id = ' . $embryologist2);
        $this->smarty->assign('embryologist2_name', $embryologist2_name["name"]);

        $freezingmedia = R::load('freezingmedia', $freezing_media_id);
        $this->smarty->assign('freezingmedia', $freezingmedia->media);

        $freezingprotocol = R::load('freezingprotocol', $freezing_protocol_id);
        $this->smarty->assign('freezingprotocol', $freezingprotocol->protocol);

        $this->smarty->assign('editElement', $editElement);
        $this->smarty->display('spermfreezing/print.html');
    }

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


        $tableName = filter_input(INPUT_POST, "tableName");

        $colName = filter_input(INPUT_POST, "colName");


        $alldata = R::findAll($tableName);

        $this->smarty->assign('alldata', $alldata);

        $this->smarty->assign('colName', $colName);

        $this->smarty->assign('tableName', $tableName);


        $this->smarty->display('spermfreezing/editmodel.html');
    }

    public function addNewRow() {

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

        //vars here
        $table = filter_input(INPUT_POST, 'tablename');
        $columname = filter_input(INPUT_POST, 'colName');

        ### insert into table
        $newtable = R::dispense($table);
        $newtable->$columname = "";

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

        echo $insertid;
    }

//edit the new row of modal
    public function updateModal() {
        //check login
        autho::checkautho($this->hosturl);
        //check authoize
        autho::checkauthoize($this->controllname, $this->hosturl, $_SESSION['role_id']);


        $tableName = filter_input(INPUT_POST, "tablename");
        $value = filter_input(INPUT_POST, "value");
        $id = filter_input(INPUT_POST, "id");

        $colName = filter_input(INPUT_POST, "colName");
        print_r($tableName);

        $mydata = R::load($tableName, $id);
        $mydata->$colName = $value;
        R::store($mydata);
    }

    public function delRowsommedel() {

        $id = filter_input(INPUT_POST, "id");
        $tableName = filter_input(INPUT_POST, "tableName");
        $myData = R::load($tableName, $id);
        R::trash($myData);

        echo 1;
    }

    ### insert into table
    // at the end of class call

    public function __destruct() {
        //colse connection
        R::close();
    }

}
