<?php 
 // Include main file
include_once('../libs/main.php');
// $zip = new ZipArchive;
// print_r("<pre>");
// print_r($zip);
// exit;
 
$startDate  = date("Y-m-d");
$future3months = date("Y-m-d", strtotime("+3 months"));

$reminderObj = new reminder();
// $reminderObj->resultAs ='array';
$fields =array('d.dependant_id','d.dependant_fname as fname','d.dependant_lname as lname','d.dependant_email_address as email','d.dependant_phone as phone','r.due_date as due_date','set_date','care_name','interval_length','scaling_interval');
$tables = array('tbl_dependant d');
$jfields = array('d.dependant_id = r.dependant_id');
$jtype   = array("INNER"); 
$where =" r.clinic_id='".$_GET['clinic_id']."' AND r.due_date between '".$startDate."' AND '".$future3months."' ";
$arg =array('fields'=>$fields,'where'=>$where,'joinTables' =>$tables,'joinFields' =>$jfields,'joinTypes' => $jtype);
$allreminder =$reminderObj->getAllRecords($arg);
$clinicObj = new clinic();
$clinicDetails =$clinicObj->fromId($_GET['clinic_id']);
if($allreminder && count($allreminder)>0){
    
        
        $delimiter = ","; 
        $filename =  $clinicDetails->clinic_name."--recall.csv";      
        // Create a file pointer 
        $f = fopen('php://memory', 'w'); 
        
        // Set column headers 
        $fields = array('FIRST NAME', 'LAST NAME', 'EMAIL','PHONE','LAST APPOINTMENT DATE','APPOINTMENT STATUS','SET DATE','DUE DATE','CARE NAME','INTERVAL LENGTH','SCALING INTERVAL'); 
        fputcsv($f, $fields, $delimiter); 
        foreach ($allreminder as $key => $d) {

            $appointmentObj = new appointment();
            $where =array('appointment_clinic_id='=>$_GET['clinic_id'],'appointment_dependant_id='=>$d->dependant_id);
            $orderBy =array('appointment_id DESC');
            $arg =array('where'=>$where,'orderBy'=>$orderBy);
            $appointmentDetails =$appointmentObj->fromFields($arg);

            $lineData = array(w::d($d->fname), w::d($d->lname),w::d($d->email),w::d($d->phone),$appointmentDetails->appointment_date,$appointmentDetails->appointment_status,$d->set_date,$d->due_date,$d->care_name,$d->interval_length,$d->scaling_interval);
            fputcsv($f, $lineData, $delimiter);         
        }
            // // Move back to beginning of file 
            fseek($f, 0); 

        
        // Set headers to download file rather than displayed 
        header('Content-Type: text/csv'); 
        header('Content-Disposition: attachment; filename="' . $filename . '";'); 
         
        //output all remaining data on a file pointer 
        fpassthru($f); 
        // fclose($f); 
        // $zipObj->addFile($filename);
        

    // }
    
    // $zipObj->close(); 
    // header('Content-Type: application/zip');
    // header('Content-disposition: attachment; filename='.$zipname);
    // header('Content-Length: ' . filesize($zipname));
    // readfile($zipname);
} 
exit; 
