<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$clinicScheduleObj =  new clinicSchedule();


if(isset($_GET['act']) && $_GET['act']=='edit' && isset($_GET['clinicSchedule_id'])){
    $clinicScheduleDetail = $clinicScheduleObj->fromId($_GET['clinicSchedule_id']);
    $clinicSchedule_detailObj = new clinicSchedule_detail();
    $where =array('clinicSchedule_detail_schedule_id='=>$clinicScheduleDetail->clinicSchedule_id);
    $arg =array('where'=>$where);
    $allDetails = $clinicSchedule_detailObj->getAllRecords($arg);
}
$clinicObj = new clinic();
$clinicDetails = $clinicObj->fromId($_REQUEST['clinic_id']);


$cconsultationObj = new cconsultation();
$where = "cconsultation_clinic_id=".$_REQUEST['clinic_id']." AND cconsultation_status=1";
$arg = array('where'=>$where);
$allconsultation  =$cconsultationObj->getAllRecords($arg);

if(isset($_POST['submit'])){
 
  if(isset($_POST['clinicSchedule_id'])){

    $days =explode("-", $_POST['clinicSchedule_day']);
    $data=array(
      'clinicSchedule_clinic_id'=>trim($_POST['clinic_id']),
      'clinicSchedule_doctor_id'=>trim($_POST['clinicSchedule_doctor_id']),
      'clinicSchedule_day_id'=>trim($days[0]),
      'clinicSchedule_day_label'=>trim($days[1]),
      'clinicSchedule_startHour'=>trim($_POST['clinicSchedule_startHour']),
      'clinicSchedule_endHour'=>trim($_POST['clinicSchedule_endHour']),
    );
   
    $id=$_POST['clinicSchedule_id'];
    $rs=$clinicScheduleObj->updateRecords($data,$id);
    $successMsg = base64_encode(RECORD_UPDATE);
    if($rs){
      $types =$_POST['clinicSchedule_detail_consultation_id'];
      $openSlots = $_POST['clinicSchedule_detail_openSlot'];
      $uIds = $_POST['clinicSchedule_detail_id'];
      if(isset($types) && count($types)>0){
        foreach ($types as $key => $value) {
          $blockArray =array();
          if($value!=""){
            if(isset($openSlots) && count($openSlots)>0){
              $openSlotvalue  =$openSlots[$key];
            }else{
              $openSlotvalue  =0;
            }
            if(isset($uIds) && count($uIds)>0){
              $uid  =$uIds[$key];
            }
            $conType =$value;

            $blockStart = $_POST['clinicSchedule_detail_blockSlotstart'.($key+1)];
            $blockEnd = $_POST['clinicSchedule_detail_blockSlotend'.($key+1)];
            $blockArray =array();
            if(isset($blockStart) && count($blockStart)>0){
              foreach ($blockStart as $bskey => $bs) {
                if(isset($blockEnd[$bskey])){
                  $blockString  =$bs."-".$blockEnd[$bskey]; 
                }
                 array_push($blockArray, $blockString);
              }
            }
            $blockStore =implode(",", $blockArray);
            $clinicSchedule_detailObj = new clinicSchedule_detail();
            $data=array(
              "clinicSchedule_detail_consultation_id"=>$conType,
              "clinicSchedule_detail_openSlot"=>$openSlotvalue,
              "clinicSchedule_detail_blockSlot"=>$blockStore
            );
            $rs = $clinicSchedule_detailObj->updateRecords($data,$uid);
          }
        }
      }
      $types =$_POST['clinicSchedule_detail_consultation_id_edit_add'];
      $openSlots = $_POST['clinicSchedule_detail_openSlot_edit_add'];
      
      if(isset($types) && count($types)>0){
        foreach ($types as $key => $value) {
          $blockArray =array();
          if($value!=""){
            if(isset($openSlots) && count($openSlots)>0){
              $openSlotvalue  =$openSlots[$key];
            }else{
              $openSlotvalue  =0;
            }
            $conType =$value;
            $blockStart = $_POST['clinicSchedule_detail_blockSlotstart_edit_add'.($key+1)];
            $blockEnd = $_POST['clinicSchedule_detail_blockSlotend_edit_add'.($key+1)];
            $blockArray =array();
            if(isset($blockStart) && count($blockStart)>0){
              foreach ($blockStart as $bskey => $bs) {
                if(isset($blockEnd[$bskey])){
                  $blockString  =$bs."-".$blockEnd[$bskey]; 
                }
                array_push($blockArray, $blockString);
              }
            }
            $blockStore =implode(",", $blockArray);
            $clinicSchedule_detailObj = new clinicSchedule_detail();
            $data=array(
              "clinicSchedule_detail_schedule_id"=>$id,
              "clinicSchedule_detail_consultation_id"=>$conType,
              "clinicSchedule_detail_openSlot"=>$openSlotvalue,
              "clinicSchedule_detail_blockSlot"=>$blockStore
            );
            $rs = $clinicSchedule_detailObj->addRecords($data);
          }
        }
      }
        header('Location:clinic_schedulelist.php?clinic_id='.$_POST['clinic_id'].'&successMsg='.$successMsg);
            exit;
    }
	}else{
    $allProviders = $_POST['clinicSchedule_doctor_id'];
    if($allProviders && count($allProviders)>0){
      foreach ($allProviders as $prokey => $provider) {
        $allselectDays = $_POST['clinicSchedule_day'];
        if($allselectDays && count($allselectDays)>0){
          foreach ($allselectDays as $daykey => $singleDays) {
            $days =explode("-", $singleDays);
            $data=array(
              'clinicSchedule_clinic_id'=>trim($_POST['clinic_id']),
              'clinicSchedule_doctor_id'=>$provider,
              'clinicSchedule_day_id'=>trim($days[0]),
              'clinicSchedule_day_label'=>trim($days[1]),
              'clinicSchedule_startHour'=>trim($_POST['clinicSchedule_startHour']),
              'clinicSchedule_endHour'=>trim($_POST['clinicSchedule_endHour']),
            );
            $rs = $clinicScheduleObj->addRecords($data);
            if($rs){
              if($allconsultation && count($allconsultation)>0){
                foreach ($allconsultation as $cokey => $singleCon) {
                  $types =$_POST['clinicSchedule_detail_consultation_id'.($cokey+1)];
                  $openSlots = $_POST['clinicSchedule_detail_openSlot'.($cokey+1)];
                  $blockStart = $_POST['clinicSchedule_detail_blockSlotstart'.($cokey+1)];
                  $blockEnd = $_POST['clinicSchedule_detail_blockSlotend'.($cokey+1)];
                  if(isset($types) && count($types)>0){
                    foreach ($types as $key => $value) {
                      if($value!=""){
                        // if(isset($openSlots) && count($openSlots)>0){
                          $openSlotvalue  =$openSlots[0];
                        // }else{
                        //   $openSlotvalue  =0;
                        // }
                        $conType =$value;
                        
                        $blockArray =array();
                        if(isset($blockStart) && count($blockStart)>0){
                          foreach ($blockStart as $bskey => $bs) {
                            if(isset($blockEnd[$bskey])){
                              $blockString  =$bs."-".$blockEnd[$bskey]; 
                            }
                             array_push($blockArray, $blockString);
                          }
                        }
                        $blockStore =implode(",", $blockArray);
                        
                        $clinicSchedule_detailObj = new clinicSchedule_detail();
                        $data=array(
                          "clinicSchedule_detail_schedule_id"=>$rs,
                          "clinicSchedule_detail_consultation_id"=>$conType,
                          "clinicSchedule_detail_openSlot"=>$openSlotvalue,
                          "clinicSchedule_detail_blockSlot"=>$blockStore
                        );
                        $prs = $clinicSchedule_detailObj->addRecords($data);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      $successMsg = base64_encode(RECORD_ADD);      
      header('Location:clinic_schedulelist.php?clinic_id='.$_POST['clinic_id'].'&successMsg='.$successMsg);
      exit;
    }
  }
}
$cdoctorObj = new cdoctor();
$where =array('cdoctor_clinic_id='=>$_REQUEST['clinic_id']);
$allProviders = $cdoctorObj->getAllRecords(array('where'=>$where));
// Include Template 
include_once('../libs/routs.php')

?>