<?php
$attachments=array();

function countdown($renewal){
    $renewal_date = date('d-m-Y',$renewal); 
    $current_date = date("d-m-Y");
    $datetime1 = new DateTime($current_date );    
    $datetime2 = new DateTime($renewal_date);    
    $difference = $datetime1->diff($datetime2);    
    $days=$difference->format("%r%a");
    
    if(($days<30 && $days>0) && (date('m',$renewal)==date("m")) ){return '<div class="alert alert-warning text-center" style="padding:8px">Renewal due in '.$days.' days ('.$renewal_date.')</div>';}
    if(date('m-Y',$renewal)==date("m-Y")){return '<div class="alert alert-danger text-center" style="padding:8px">Renewal due in '.$days.' days ('.$renewal_date.')</div>';}
    else{return 'NULL';}
    }

    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `clientdetails` ORDER BY abs(renewal_date) ASC")or die(mysqli_error("GET_INV_NO: ".$GLOBALS['con']));
    $stmt->execute();$iv=$stmt->get_result();$stmt->close();
    while($list=mysqli_fetch_array($iv)){
        if($list['renewal_date']!=="" && $list['renewal_status']!="LAPSED"){
        $status=countdown($list['renewal_date']);
        
        if($status=="NULL"){}
        
        else{

            $rdata=json_decode($list['renewOptions'],true);

            if($rdata['notice']==0){$sendMode=="client";}
            if($rdata['notice']==1){$sendMode=="agent";}

            if($rdata['schedule']==1){$attachments[]='schedule';}
            if($rdata['plant']==1){$attachments[]='plant';}
            if($rdata['inspection']==1){$attachments[]='inspection';}

            if($rdata['nag']==1){}
            $firstReminder=$rdata['R1'];
            $secondReminder=$rdata['R2'];
            if($rdata['lapse']==1){}
            $lapseCOmmit=$rdata['lapseDays'];

        }
    }
}