<?php

//the global file operation
include("../public/impOpreation.php");
include_once("../library/uploadImages.php");

//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");


//Breadcrumb
//require_once("../library/breadcrumb.php");
//get the do the action
$do = $_GET['do'];

$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");

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

$clinic = new Clinic();
$clinicDAO = new ClinicMySqlDAO();
$clinicDAOEx = new ClinicMySqlExtDAO();
/* ======================

  Controller Name :- saveCTRL اضافة وتعديل وعرض الخزن

  OPERTATION in Controller

  1-display add form
  2- add in Save tbl
  3-insert into savedaily tbl
  4-display sucess or error
  5-display show form
  6-edit in Save data

  ======================== */


//here the global templates
$smarty->display("header.html");


//here goes the instances and general variables
//Breadcrumb
//$breadcrumbObject = new Breadcrumb();
//check and use the condetion that suite this action
if (empty($do)) {
    //here the permssion check


    $smarty->display("rb_clinic/add.html");
} elseif ($do == 'add') {
    $clinic_name = filter_input(INPUT_POST, 'clinic_name');
    $clinic_description = filter_input(INPUT_POST, 'clinic_description');


    // $loaddoctordata = R::findOne('clinic', 'name=? ', [$clinic_name]);
    $loaddoctordata = $clinicDAOEx->loadClinicByName($clinic_name);

    if (!empty($loaddoctordata)) {


        // echo ' <meta http-equiv="refresh" content="0.0000001;URL=http://localhost/erp2016/controllers/rb_clinics.php" />';

        header("location:?do=empty");
    } else {
        // $clinic_obj = R::dispense('clinic');
        $clinic->name = $clinic_name;
        $clinic->description = $clinic_description;
        $clinic->tempdel = 0;

        $clinicDAO->insert($clinic);
        // R::store($clinic_obj);

        header("location:?do=show");
    }
} elseif ($do == 'empty') {
    $smarty->assign('msg', 'تم ادخال  اسم العيادة  من قبل ');
    $smarty->display("rb_clinic/add.html");
} elseif ($do == 'show') {

    // $selectClinic = R::findAll('clinic', 'tempdel = 0');
    $selectClinic = $clinicDAO->queryAll();

    $smarty->assign('select', $selectClinic);


    //here the smarty templates
    $smarty->display("rb_clinic/show.html");
} elseif ($do == 'delete') {

    $clinicDAO->delete($id);
//    $loadrow = R::load('clinic', $id);
//    R::trash($loadrow);

    header('location:?do=show');

    // echo ' <meta http-equiv="refresh" content="0.0000001;URL=http://localhost/erp2016/controllers/rb_clinics.php?do=show" />';
} elseif ($do == 'edit') {
    // $loadrow = R::load('clinic', $id);
    $loadrow = $clinicDAO->load($id);

    $smarty->assign('load', $loadrow);
    $smarty->display('rb_clinic/edit.html');
} elseif ($do == 'update') {

    //$dataid = filter_input(INPUT_POST, 'dataid');

    $id_value = filter_input(INPUT_POST, 'id_value');
    $clinic_name = filter_input(INPUT_POST, 'clinic_name');
    $clinic_description = filter_input(INPUT_POST, 'clinic_description');


    //$loaddoctordata = R::findOne('clinic', 'name=? ', [$clinic_name]);
    $loaddoctordata = $clinicDAOEx->loadClinicByName($clinic_name);
    $id = $loaddoctordata->id;
    if ($id_value == $id) {


        //$update_clinic = R::load('clinic', $id_value);
        $update_clinic = $clinicDAO->load($id_value);
        $update_clinic->id = $id_value;
        $update_clinic->name = $clinic_name;
        $update_clinic->description = $clinic_description;

        // R::store($update_clinic);
        $clinicDAO->update($update_clinic);
        header("location:?do=show");
    } else {

        // $getdata = R::findOne('clinic', 'name=? ', [$clinic_name]);
        $getdata = $clinicDAOEx->loadClinicByName($clinic_name);
        if (!empty($getdata)) {

            $smarty->assign('msg', 'تم ادخال  اسم العيادة من قبل ');

            // $loadrow = R::load('clinic', $id);
            $loadrow = $clinicDAO->load($id);
            $smarty->assign('load', $loadrow);
            $smarty->display('rb_clinic/edit.html');
            // echo ' <meta http-equiv="refresh" content="0.0000001;URL=http://localhost/erp2016/controllers/rb_clinics.php" />';
        } else {
            //  $update_clinic = R::load('clinic', $id_value);
            $update_clinic = $clinicDAO->load($id_value);
            $update_clinic->id = $id_value;
            $update_clinic->name = $clinic_name;
            $update_clinic->description = $clinic_description;

            //  R::store($update_clinic);
            $clinicDAO->update($update_clinic);
            header("location:?do=show");
        }
        //   header("location:?do=edit");
    }
} elseif ($do == 'tempdel') {
    $id = filter_input(INPUT_POST, 'id');
    //$clinicdata = R::load('clinic', $id);
    $clinicdata = $clinicDAO->load($id);

    $clinicdata->id = $id;
    if ($clinicdata->tempdel == 0) {
        $clinicdata->tempdel = 1;
    } else {
        $clinicdata->tempdel = 0;
    }
    // R::store($clinicdata);
    $clinicDAO->update($clinicdata);
} elseif ($do == "sucess") {

    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {

    //here the smarty templates
    $smarty->display("error.html");
}




$smarty->assign("clinicValidation", 1);
$smarty->assign("clinic", 1);

//here the global templates
$smarty->display("footer.html");
?>