<?php

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

include("../public/include_dao.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');
$ClientDAO = new ClientMySqlDAO();

$kashf = new Kashf();
$kashfDAO = new KashfMySqlDAO();
$kashfDAOEx = new KashfMySqlExtDAO();

$doctorDAO = new UserMySqlDAO ();
$doctorDAOEx = new UserMySqlExtDAO ();

$doctorKashf = new Doctorkashf();
$doctorKashfDAO = new DoctorkashfMySqlDAO();
$doctorKashfDAex = new DoctorkashfMySqlExtDAO();


//Branch
$Branch = new Branch();
$BranchDAO = new BranchMySqlDAO();
$BranchEX = new BranchMySqlExtDAO();
/* ======================

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

$userId = $_SESSION['userid'];
$branchId = $_SESSION['branchId'];

if (empty($do)) {


    $datetoday = date('Y-m-d');
    $editkasf = $kashfDAOEx->loadAllByDate($datetoday);
    foreach ($editkasf as $kdata) {
        $custid = $kdata->customerid;
        $custData = $ClientDAO->load($custid);
        $kdata->custname = $custData->clientname;
        $kdata->custid = $custData->clientid;

        $docid = $kdata->doctorid;
        $docData = $doctorDAO->load($docid);
        $kdata->docname = $docData->username;
        $kdata->doctorid = $docData->username;
    }


    $smarty->assign('data', $editkasf);
//////////////////////////taaaaaaaaaaaaaab2////////////////////////////////////////////

    $datefrom = filter_input(INPUT_POST, 'datefrom');
    $dateto = filter_input(INPUT_POST, 'dateto');
    $selectedBranch = filter_input(INPUT_POST, 'branchId');
    if ($selectedBranch == 0 || $selectedBranch == -1) {
        $selectedBranch = '';
    }
    $branchData = $BranchDAO->queryAll();
    $smarty->assign('branchData', $branchData);

    $queryString = '';
    if (isset($datefrom) && !empty($datefrom) && empty($dateto) && empty($selectedBranch)) {
        $queryString = ' doctorkashf.date >= "' . $datefrom . '"';
        $showkashf = $doctorKashfDAex->querybyDate($queryString);
    } else if (isset($dateto) && !empty($dateto) && empty($datefrom) && empty($selectedBranch)) {
        $queryString = ' doctorkashf.date <= "' . $dateto . '"';
        $showkashf = $doctorKashfDAex->querybyDate($queryString);
    } else if (isset($selectedBranch) && !empty($selectedBranch) && empty($datefrom) && empty($dateto)) {
        $queryString = ' doctorkashf.branchid = "' . $selectedBranch . '"';
        $showkashf = $doctorKashfDAex->queryByBranchOnly($queryString);
    } else if (isset($dateto) && !empty($dateto) && isset($datefrom) && !empty($datefrom) && empty($selectedBranch)) {
        $queryString = ' doctorkashf.date >= "' . $datefrom . '" AND doctorkashf.date <= "' . $dateto . '"';
        $showkashf = $doctorKashfDAex->querybyDate($queryString);
    } else if (isset($selectedBranch) && !empty($selectedBranch) && isset($datefrom) && !empty($datefrom) && empty($dateto)) {
        $queryString = ' doctorkashf.branchid = "' . $selectedBranch . '"' . ' AND ' . ' doctorkashf.date >= "' . $datefrom . '"';
        $showkashf = $doctorKashfDAex->queryByBranchOnly($queryString);
    } else if (isset($selectedBranch) && !empty($selectedBranch) && isset($dateto) && !empty($dateto) && empty($datefrom)) {
        $queryString = ' doctorkashf.branchid = "' . $selectedBranch . '"' . ' AND ' . ' doctorkashf.date <= "' . $dateto . '"';
        $showkashf = $doctorKashfDAex->queryByBranchOnly($queryString);
    } else if (isset($selectedBranch) && !empty($selectedBranch) && isset($datefrom) && !empty($datefrom) && isset($dateto) && !empty($dateto)) {
        $queryString = ' doctorkashf.branchid = "' . $selectedBranch . '"' . ' AND ' . ' doctorkashf.date >= "' . $datefrom . '" AND doctorkashf.date <= "' . $dateto . '"';
        $showkashf = $doctorKashfDAex->queryByBranchOnly($queryString);
    } else {
        $showkashf = $doctorKashfDAex->queryAllData();
    }


    $allKashf = $kashfDAO->queryAll();
    $smarty->assign('allKashf', $allKashf);

    //$smarty->assign('customerdata', $custData);
    $smarty->assign('doctorkashf', $showkashf);

    $smarty->display('rb_doc_kashf/show.html');
}

###
elseif ($do == 'add') {

    //  $custselect = filtter_input(INPUT_POST, 'custselect');

    $datetoday = date('Y-m-d');

    $clientId = filter_input(INPUT_POST, 'clientId');
    $kashId = filter_input(INPUT_POST, 'kashId');
    $docSel = filter_input(INPUT_POST, 'docSel');
    $kashfdate = filter_input(INPUT_POST, 'kashfdate');


    //$loadkashf = R::findOne('kashf', 'customerid=? and kashfdate=?', [$custselect, $datetoday]);
    print_r('$kashId = ' . $kashId);
    $oldData = $kashfDAO->load($kashId);


    $loadkashf = $kashfDAOEx->loadKashf($custselect, $datetoday);

    $loadkashf->entered = 1;
    $loadkashf->id = $kashId;

    $loadkashf->kashfvalue = $oldData->kashfvalue;
    $loadkashf->kashftype = $oldData->kashftype;
    $loadkashf->customerid = $oldData->customerid;
    $loadkashf->doctorid = $oldData->doctorid;
    $loadkashf->kashfdate = $oldData->kashfdate;
    $loadkashf->paystatus = $oldData->paystatus;
    // R::store($loadkashf);



    $kashfDAO->update($loadkashf);
    // $doctorKashf = R::dispense('doctorkashf');

    $doctorKashf->customerid = $clientId;
    $doctorKashf->kashfid = $kashId;
    $doctorKashf->date = $kashfdate;
    $doctorKashf->doctorid = $docSel;
    $doctorKashf->loguserid = $userId;
    $doctorKashf->branchid = $branchId;

    //Left eye المسافة(D)

    $D_Lsphere = filter_input(INPUT_POST, 'D_Lsphere');
    $D_Lcylinder = filter_input(INPUT_POST, 'D_Lcylinder');
    $D_Laxis = filter_input(INPUT_POST, 'D_Laxis');
    $D_Lprism = filter_input(INPUT_POST, 'D_Lprism');
    $D_Lva = filter_input(INPUT_POST, 'D_Lva');

    $doctorKashf->ldsphere = $D_Lsphere;
    $doctorKashf->ldcylinder = $D_Lcylinder;
    $doctorKashf->ldaxis = $D_Laxis;
    $doctorKashf->ldprism = $D_Lprism;
    $doctorKashf->ldva = $D_Lva;

    //Left eye القراءة (N)


    $N_Lsphere = filter_input(INPUT_POST, 'N_Lsphere');
    $N_Lcylinder = filter_input(INPUT_POST, 'N_Lcylinder');
    $N_Laxis = filter_input(INPUT_POST, 'N_Laxis');
    $N_Lprism = filter_input(INPUT_POST, 'N_Lprism');
    $N_Lva = filter_input(INPUT_POST, 'N_Lva');

    $doctorKashf->lnsphere = $N_Lsphere;
    $doctorKashf->lncylinder = $N_Lcylinder;
    $doctorKashf->lnaxis = $N_Laxis;
    $doctorKashf->lnprism = $N_Lprism;
    $doctorKashf->lnva = $N_Lva;


    //Right eye المسافة(D)

    $D_Rsphere = filter_input(INPUT_POST, 'D_Rsphere');
    $D_Rcylinder = filter_input(INPUT_POST, 'D_Rcylinder');
    $D_Raxis = filter_input(INPUT_POST, 'D_Raxis');
    $D_Rprism = filter_input(INPUT_POST, 'D_Rprism');
    $D_Rva = filter_input(INPUT_POST, 'D_Rva');

    $doctorKashf->rdsphere = $D_Rsphere;
    $doctorKashf->rdcylinder = $D_Rcylinder;
    $doctorKashf->rdaxis = $D_Raxis;
    $doctorKashf->rdprism = $D_Rprism;
    $doctorKashf->rdva = $D_Rva;


    //Left eye القراءة (N)

    $N_Rsphere = filter_input(INPUT_POST, 'N_Rsphere');
    $N_Rcylinder = filter_input(INPUT_POST, 'N_Rcylinder');
    $N_Raxis = filter_input(INPUT_POST, 'N_Raxis');
    $N_Rprism = filter_input(INPUT_POST, 'N_Rprism');
    $N_Rva = filter_input(INPUT_POST, 'N_Rva');

    $doctorKashf->rnsphere = $N_Rsphere;
    $doctorKashf->rncylinder = $N_Rcylinder;
    $doctorKashf->rnaxis = $N_Raxis;
    $doctorKashf->rnprism = $N_Rprism;
    $doctorKashf->rnva = $N_Rva;


    $radd = filter_input(INPUT_POST, 'radd');
    $rbiofocal = filter_input(INPUT_POST, 'rbiofocal');
    $ladd = filter_input(INPUT_POST, 'ladd');
    $lbiofocal = filter_input(INPUT_POST, 'lbiofocal');
    $pd = filter_input(INPUT_POST, 'pd');
    $bvd = filter_input(INPUT_POST, 'bvd');
    $notes = filter_input(INPUT_POST, 'notes');

    $doctorKashf->radd = $radd;
    $doctorKashf->rbiofocal = $rbiofocal;
    $doctorKashf->ladd = $ladd;
    $doctorKashf->lbiofocal = $lbiofocal;
    $doctorKashf->pd = $pd;
    $doctorKashf->bvd = $bvd;
    $doctorKashf->notes = $notes;




    $doctorKashf->offline = 0;


    $doctorKashfDAO->insert($doctorKashf);
    header('location:?do=');
}

###
elseif ($do == 'addnew') {

    $id = filter_input(INPUT_GET, 'id');
    $kashData = $kashfDAOEx->getDocClientName($id);
    $smarty->assign('id', $id);

    $smarty->assign('get', $kashData);

    $smarty->display("rb_doc_kashf/doc_kasfh_add.html");
}

###
elseif ($do == 'details') {



    //$kashfload = R::load('doctorkashf', $id);
    $kashfload = $doctorKashfDAO->load($id);
    $custid = $kashfload->customerid;
    $loadcustomer = $ClientDAO->load($custid);
    $smarty->assign('custname', $loadcustomer);

    $doctId = $kashfload->doctorid;
    $docName = $doctorDAO->load($doctId);
    $smarty->assign('docName', $docName);

    $smarty->assign('myalldata', $kashfload);

    $smarty->display('rb_doc_kashf/doc_kasfh_details2.html');
}

###
elseif ($do == 'edit') {



    //$editkashf = R::load('doctorkashf', $id);
    $editkashf = $doctorKashfDAO->load($id);
    $custid = $editkashf->customerid;
    $allcustmoer = $ClientDAO->queryAll();
    $smarty->assign('allcust', $allcustmoer);
    $smarty->assign('cust', $custid);


    $docId = $editkashf->doctorid;
    $allDoct = $doctorDAO->queryAll();
    $smarty->assign('allDoct', $allDoct);
    $smarty->assign('docId', $docId);

    $smarty->assign('editdata', $editkashf);
    $smarty->display('rb_doc_kashf/doc_kasfh_edit.html');
}

###
elseif ($do == 'update') {
    $kashfid = filter_input(INPUT_POST, 'kashfid');

    $custselect = filter_input(INPUT_POST, 'custselect');
    //$updatedata = R::load('doctorkashf', $kashfid);
    $updatedata = $doctorKashfDAO->load($kashfid);

    $updatedata->customerid = $updatedata->customerid;
    //Left eye المسافة(D)
    $updatedata->id = $kashfid;
    $D_Lsphere = filter_input(INPUT_POST, 'D_Lsphere');
    $D_Lcylinder = filter_input(INPUT_POST, 'D_Lcylinder');
    $D_Laxis = filter_input(INPUT_POST, 'D_Laxis');
    $D_Lprism = filter_input(INPUT_POST, 'D_Lprism');
    $D_Lva = filter_input(INPUT_POST, 'D_Lva');

    $updatedata->ldsphere = $D_Lsphere;
    $updatedata->ldcylinder = $D_Lcylinder;
    $updatedata->ldaxis = $D_Laxis;
    $updatedata->ldprism = $D_Lprism;
    $updatedata->ldva = $D_Lva;


    //Left eye القراءة (N)


    $N_Lsphere = filter_input(INPUT_POST, 'N_Lsphere');
    $N_Lcylinder = filter_input(INPUT_POST, 'N_Lcylinder');
    $N_Laxis = filter_input(INPUT_POST, 'N_Laxis');
    $N_Lprism = filter_input(INPUT_POST, 'N_Lprism');
    $N_Lva = filter_input(INPUT_POST, 'N_Lva');

    $updatedata->lnsphere = $N_Lsphere;
    $updatedata->lncylinder = $N_Lcylinder;
    $updatedata->lnaxis = $N_Laxis;
    $updatedata->lnprism = $N_Lprism;
    $updatedata->lnva = $N_Lva;


    //Right eye المسافة(D)

    $D_Rsphere = filter_input(INPUT_POST, 'D_Rsphere');
    $D_Rcylinder = filter_input(INPUT_POST, 'D_Rcylinder');
    $D_Raxis = filter_input(INPUT_POST, 'D_Raxis');
    $D_Rprism = filter_input(INPUT_POST, 'D_Rprism');
    $D_Rva = filter_input(INPUT_POST, 'D_Rva');

    $updatedata->rdsphere = $D_Rsphere;
    $updatedata->rdcylinder = $D_Rcylinder;
    $updatedata->rdaxis = $D_Raxis;
    $updatedata->rdprism = $D_Rprism;
    $updatedata->rdva = $D_Rva;


    //Left eye القراءة (N)

    $N_Rsphere = filter_input(INPUT_POST, 'N_Rsphere');
    $N_Rcylinder = filter_input(INPUT_POST, 'N_Rcylinder');
    $N_Raxis = filter_input(INPUT_POST, 'N_Raxis');
    $N_Rprism = filter_input(INPUT_POST, 'N_Rprism');
    $N_Rva = filter_input(INPUT_POST, 'N_Rva');

    $updatedata->rnsphere = $N_Rsphere;
    $updatedata->rncylinder = $N_Rcylinder;
    $updatedata->rnaxis = $N_Raxis;
    $updatedata->rnprism = $N_Rprism;
    $updatedata->rnva = $N_Rva;

    $updatedata->loguserid = $userId;
    $updatedata->branchid = $branchId;




    $radd = filter_input(INPUT_POST, 'radd');
    $rbiofocal = filter_input(INPUT_POST, 'rbiofocal');
    $ladd = filter_input(INPUT_POST, 'ladd');
    $lbiofocal = filter_input(INPUT_POST, 'lbiofocal');
    $pd = filter_input(INPUT_POST, 'pd');
    $bvd = filter_input(INPUT_POST, 'bvd');
    $notes = filter_input(INPUT_POST, 'notes');

    $updatedata->radd = $radd;
    $updatedata->rbiofocal = $rbiofocal;
    $updatedata->ladd = $ladd;
    $updatedata->lbiofocal = $lbiofocal;
    $updatedata->pd = $pd;
    $updatedata->bvd = $bvd;
    $updatedata->notes = $notes;

    // R::store($updatedata);
    try {
        $doctorKashfDAO->update($updatedata);
        header('location:?do=');
    } catch (Exception $ex) {
        header('location:?do=');
    }
}

###
elseif ($do == 'delete') {
    $loaddelete = R::load('doctorkashf', $id);
    R::trash($loaddelete);
    header('location:?do=');
}

###
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("customKashf", 1);

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