<?php

//the global file operation

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'];

$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

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




$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;
    }
    $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();
    }


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

    $smarty->display('rb_doc_kashf/table.html');
} $smarty->assign("clinicValidation", 1);

$smarty->assign("customKashf", 1);
?>