<?php
$font_family = 'font-family:Segue UI, Roboto, Tahoma, sans-serif;';
$table_style = 'style="border:1px solid #ccc;border-collapse:collapse;width:100%;'.$font_family.'"';
$th_style = 'style="padding:3px 5px;font-size:12px;border:1px solid #ccc;font-weight:bold;'.$font_family.'"';
$td_style = 'style="padding:3px 5px;font-size:12px;border:1px solid #ccc;'.$font_family.'"';
$h3_style = 'style="padding:3px 5px;font-size:14px;font-weight:bold;margin-bottom:10px;'.$font_family.'"';
$p_style = 'style="padding:3px 5px;font-size:12px;margin-bottom:10px;'.$font_family.'"';
?>

<h3 <?php echo $h3_style; ?>>End of Day Report Summary</h3>
<p <?php echo $p_style; ?>>Dear Concern, <br/><br/><br/>These are the invoices generated today.</p>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr>
        <td <?php echo $th_style; ?>>#</td>
        <td <?php echo $th_style; ?>>Date</td>
        <td <?php echo $th_style; ?>>Invoice NO</td>
        <td <?php echo $th_style; ?>>Client Title</td>
        <td <?php echo $th_style; ?>>Bill Period</td>
        <td <?php echo $th_style; ?>>How often</td>
        <td <?php echo $th_style; ?>>Amount</td>
        <td <?php echo $th_style; ?>>Status</td>        
        <td <?php echo $th_style; ?>>Created By</td>        
    </tr>
   <?php 
   if(empty($invoices)){
	   ?>
   <tr>
        <td colspan="7" <?php echo $td_style; ?>>No Invoice is generated today.</td>
   </tr>
    <?php }else{ 
	$invoice_amount = 0;
    foreach ($invoices as $key => $invoice) {
        $total = ($invoice->price * $invoice->quantity) + ($invoice->price * $invoice->quantity * $invoice->vat / 100);
		$invoice_amount += $total;
    ?>
        <tr>
            <td <?php echo $td_style; ?>><?php echo $key+1; ?></td>
            <td <?php echo $td_style; ?>><?php echo date("d-m-Y", strtotime($invoice->invoice_created_date)); ?></td>
            <td <?php echo $td_style; ?>><?php echo "IL ".$invoice->invoice_id; ?></td>
            <td <?php echo $td_style; ?>><?php echo $invoice->client_title.' ('.$invoice->branch_name.')'; ?></td>
            <td <?php echo $td_style; ?>><?php echo $invoice->bill_period; ?></td>
            <td <?php echo $td_style; ?>>
                <?php 
                if($invoice->frequency == 'm1'){
                    echo 'Monthly';
                }elseif($invoice->frequency == 'y1'){
                    echo 'Yearly';
                }elseif($invoice->frequency == 'p1'){
                    echo 'Prepaid';
                }else{
                    echo 'Not Define';
                }
                ?>
            </td>
            <td <?php echo $td_style; ?>><?php echo number_format((float)$total, 2, '.', ''); ?></td>
            <td <?php echo $td_style; ?>>
            <?php
                if($invoice->status==1) {
                    echo "<span style='color:red;'>Due</span>";
                } elseif($invoice->status==3){
                    echo "<span style='color:green;'>Paid</span>";
                } elseif($invoice->status==2){
                    echo "<span style='color:blue;'>Partialy Paid</span>";
                } elseif($invoice->status==0){
                    echo "<span style='color:blue;'>Pending</span>";
                } elseif($invoice->status==4){
                    echo "Canceled";
                }
            ?>                
            </td>
			<td <?php echo $td_style; ?>>
			<?php 
			if($invoice->created_by){
    			$admin = DB::table('admin')->where('admin_id',$invoice->created_by)->first();
				echo $admin->user_name.'('.$invoice->created_by.')';
			}
			?>
			</td>
        </tr>
    <?php 
	} 
	?>
	<tr>
        <th colspan="6" <?php echo $th_style; ?>>Total :</th>
        <th <?php echo $th_style; ?>><?php echo number_format((float)$invoice_amount, 2, '.', '');?></th>
        <th colspan="2" <?php echo $th_style; ?>></th>
    </tr>	
	<?php 
	}
	?>
</table>

<p <?php echo $p_style; ?>>These are the payments received today.</p>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr>
        <td <?php echo $th_style; ?>>#</td>
        <td <?php echo $th_style; ?>>Date</td>
        <td <?php echo $th_style; ?>>Payment ID</td>
        <td <?php echo $th_style; ?>>Client Title</td>
        <td <?php echo $th_style; ?>>Status</td>
        <td <?php echo $th_style; ?>>Reference NO</td>
        <td <?php echo $th_style; ?>>Cheque Status</td>
        <td <?php echo $th_style; ?>>Amount</td>
        <td <?php echo $th_style; ?>>Payment Type</td>
        <td <?php echo $th_style; ?>>Received By</td>
    </tr>    
    <?php if(empty($payments)){ ?>
    <tr>
        <td colspan="10" <?php echo $td_style; ?>>No payment information is available for today.</td>
    </tr>
    <?php 
	}else{ 
	$payment_amount = 0;
    foreach ($payments as $key => $payment) { 
	$payment_amount += $payment->amount;
	?>
        <tr>
            <td <?php echo $td_style; ?>><?php echo $key+1; ?></td>
            <td <?php echo $td_style; ?>><?php echo date("d-m-Y", strtotime($payment->payment_date)); ?></td>
            <td <?php echo $td_style; ?>><?php echo "T.ID-".$payment->id.'-IL'.$payment->invoice_id.'-'.$payment->accept_by; ?></td>
            <td <?php echo $td_style; ?>><?php echo $payment->client_title.' ('.$payment->branch_name.')'; ?></td>
            <td <?php echo $td_style; ?>>
            <?php
                if($payment->invoice_status==1) {
                    echo "<span style='color:red;'>Due</span>";
                } elseif($payment->invoice_status==3){
                    echo "<span style='color:green;'>Paid</span>";
                } elseif($payment->invoice_status==2){
                    echo "<span style='color:blue;'>Partialy Paid</span>";
                } elseif($payment->invoice_status==0){
                    echo "<span style='color:blue;'>Pending</span>";
                } elseif($payment->invoice_status==4){
                    echo "Canceled";
                }
            ?>
            </td>
            <td <?php echo $td_style; ?>><?php echo $payment->reference_number; ?></td>
            <td <?php echo $td_style; ?>>
                <?php 
                if($payment->payment_status==0) {
                    $payment_status = "Open";
                } elseif($payment->payment_status==1) {
                    $payment_status = "Clear";
                } elseif($payment->payment_status==2) {
                    $payment_status = "Bounced";
                } else {
                    $payment_status =  'Not Defined';
                }
                echo $payment_status;
                ?>
            </td>
			<td <?php echo $td_style; ?>><?php echo $payment->amount; ?></td>
			<td <?php echo $td_style; ?>>
                <?php 
                if($payment->payment_type==0) {
                   echo "Cash";
                }elseif($payment->payment_type==1) {
                    echo "Cheque";
                }elseif($payment->payment_type==2) {
                    echo "Pay Order";
                }elseif($payment->payment_type==3) {
                    echo "Online Payment";
                }elseif($payment->payment_type==4) {
                    echo "bKash";
                }elseif($payment->payment_type==5) {
                    echo "Bank Guaranty";
                }elseif($payment->payment_type==6) {
                    echo "Card";
                }elseif($payment->payment_type==7) {
                    echo "Bank Transfer";
                }elseif($payment->payment_type==8) {
                    echo "Others";
                }
                ?>
            </td>
            <td <?php echo $td_style; ?>>
			<?php 
			if($payment->admin_id){
				$admin = DB::table('admin')->where('admin_id',$payment->admin_id)->first();
				echo $admin->user_name.'('.$payment->admin_id.')';
			}
			?>
			</td>
        </tr>
    <?php } ?>
		<tr>
        <th colspan="7" <?php echo $th_style; ?>>Total :</th>
        <th <?php echo $th_style; ?>><?php echo number_format((float)$payment_amount, 2, '.', '');?></th>
        <th colspan="2" <?php echo $th_style; ?>></th>
    </tr>
	<?php 
	}
	?>
</table>

<p <?php echo $p_style; ?>>These are the clients generated today.</p>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr>
        <td <?php echo $th_style; ?>>#</td>
        <td <?php echo $th_style; ?>>Client ID</td>
        <td <?php echo $th_style; ?>>Client Title</td>
        <td <?php echo $th_style; ?>>Branch</td>
        <td <?php echo $th_style; ?>>Contact Person</td>
        <td <?php echo $th_style; ?>>Phone</td>
        <td <?php echo $th_style; ?>>Email</td>
        <td <?php echo $th_style; ?>>DOB</td>
        <td <?php echo $th_style; ?>>Reference</td>
        <td <?php echo $th_style; ?>>Status</td>
    </tr>    
    <?php if(empty($clients)){ ?>
    <tr>
        <td colspan="10" <?php echo $td_style; ?>>No Client information is available for today.</td>
    </tr>
    <?php } ?>
    <?php foreach ($clients as $key => $client) { ?>
        <tr>
            <td <?php echo $td_style; ?>><?php echo $key+1; ?></td>
            <td <?php echo $td_style; ?>><?php echo 'IL-C'.$client->client_id; ?></td>
            <td <?php echo $td_style; ?>><?php echo $client->client_title; ?></td>
            <td <?php echo $td_style; ?>><?php echo $client->branch_name; ?></td>
            <td <?php echo $td_style; ?>><?php echo $client->contact_person; ?></td>
            <td <?php echo $td_style; ?>>
			<?php
			echo $client->client_phone1;		
			if($client->client_phone2){
				echo '<br/>'.$client->client_phone2;
			}
			?>
			</td>
			<td <?php echo $td_style; ?>>
			<?php
			echo $client->client_email1;		
			if($client->client_email2){
				echo '<br/>'.$client->client_email2;
			}
			?>
			</td>
			<td <?php echo $td_style; ?>><?php echo $client->client_dob; ?></td>
			<td <?php echo $td_style; ?>>
			<?php 
			if($client->reference_by == 1){
    				$reference = DB::table('admin')->where('admin_id',$client->reference_id)->first();
				if($reference){
					echo $reference->user_name;
				}
			  }elseif($client->reference_by == 2){
				  $reference = DB::table('client')->where('client_id',$client->reference_id)->first();
					if($reference){
						echo $reference->client_title.'(IL-C'.$client->reference_id.')';
					}
			  }
			?>
			</td>
            <td <?php echo $td_style; ?>>
            <?php
                if($client->client_status==0) {
                    echo "New Created";
                } elseif($client->client_status==1){
                    echo "<span style='color:green;'>Active</span>";
                } elseif($client->client_status==2){
                    echo "<span style='color:red;'>Inactive</span>";
                }elseif($client->client_status==3){
                    echo "Suspend";
                }
            ?>
            </td>
        </tr>
    <?php } ?>
</table>

<p <?php echo $p_style; ?>>These are the connections generated today.</p>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr>
        <td <?php echo $th_style; ?>>#</td>
        <td <?php echo $th_style; ?>>Client</td>
        <td <?php echo $th_style; ?>>Connection ID</td>
        <td <?php echo $th_style; ?>>Connection Address</td>
        <td <?php echo $th_style; ?>>Recurring Invoice</td>
        <td <?php echo $th_style; ?>>Contact Person Name</td>
        <td <?php echo $th_style; ?>>Contact Number</td>
        <td <?php echo $th_style; ?>>Status</td>
        <td <?php echo $th_style; ?>>Created By</td>
    </tr>    
    <?php if(empty($connections)){ ?>
    <tr>
        <td colspan="9" <?php echo $td_style; ?>>No connection information is available for today.</td>
    </tr>
    <?php } ?>
    <?php foreach ($connections as $key => $connection) { ?>
        <tr>
            <td <?php echo $td_style; ?>><?php echo $key+1; ?></td>
            <td <?php echo $td_style; ?>>
                <?php 
                $client_info = DB::table('client')
                                ->join('branch', 'client.branch_id', '=', 'branch.branch_id')
                                ->select('client.client_title','branch.branch_name')
                                ->where('client.client_id',$connection->client_id)
                                ->first();
                if(!empty($client_info)){
                    echo $client_info->client_title.' (IL-C'.$connection->client_id.')<br/>Branch :'.$client_info->branch_name;
                }
                
                ?>
            </td>
            <td <?php echo $td_style; ?>><?php echo 'CID'.$connection->connection_id; ?></td>
            <td <?php echo $td_style; ?>><?php echo $connection->connection_address; ?></td>
            <td <?php echo $td_style; ?>>
			<?php 
			if($connection->invoice_details_id != 0){
				echo 'IL-'.$connection->invoice_details_id;
				$inv_details = DB::table('invoice_recurring_details')->where('invoice_details_id',$connection->invoice_details_id)->get();
				foreach($inv_details as $invoice_info)
				{
				$price = number_format(($invoice_info->price*$invoice_info->quantity)+($invoice_info->quantity*$invoice_info->price*$invoice_info->vat*.01),2);
				echo '('.$price.')'; 
				
				}
			}
			?>
			</td>
            <td <?php echo $td_style; ?>><?php echo $connection->contact_person_name; ?></td>
            <td <?php echo $td_style; ?>><?php echo $connection->contact_number; ?></td>
            
            <td <?php echo $td_style; ?>>
            <?php
               if($connection->link_status==1){
                    echo "<span style='color:green;'>Active</span>";
                }elseif($connection->link_status==2){
                    echo "<span style='color:red;'>Inactive</span>";
                }elseif($connection->link_status==3){
                    echo "Pending";
                }elseif($connection->link_status==4){
                    echo "Cancel";
                }
            ?>
            </td>
			<td <?php echo $td_style; ?>>
				<?php
				$connection_employee = DB::table('connection_employee')->where(array('connection_id'=>$connection->connection_id,'is_del'=>0))->get();
				if($connection_employee){
					foreach($connection_employee as $c_employee){
						$admin = DB::table('admin')->where('admin_id',$c_employee->admin_id)->first();
						echo $admin->user_name.'('.$c_employee->admin_id.')';
					}
				}
				?>
			</td>
        </tr>
    <?php } ?>
</table>

<p <?php echo $p_style; ?>>These are the Leads generated today.</p>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr>
        <td <?php echo $th_style; ?>>#</td>
        <td <?php echo $th_style; ?>>Title</td>
        <td <?php echo $th_style; ?>>Contact Person</td>
        <td <?php echo $th_style; ?>>Designation</td>
        <td <?php echo $th_style; ?>>Phone</td>
        <td <?php echo $th_style; ?>>Email</td>
        <td <?php echo $th_style; ?>>Address</td>
        <td <?php echo $th_style; ?>>Leads By</td>
        <td <?php echo $th_style; ?>>Status</td>
    </tr>    
    <?php if(empty($leads)){ ?>
    <tr>
        <td colspan="9" <?php echo $td_style; ?>>No Leads information is available for today.</td>
    </tr>
    <?php } ?>
    <?php foreach ($leads as $key => $lead) { ?>
        <tr>
            <td <?php echo $td_style; ?>><?php echo $key+1; ?></td>
            <td <?php echo $td_style; ?>><?php echo $lead['title']; ?></td>
            <td <?php echo $td_style; ?>><?php echo $lead['contact_person']; ?></td>
            <td <?php echo $td_style; ?>><?php echo $lead['designation']; ?></td>
            <td <?php echo $td_style; ?>><?php echo $lead['phone']; ?></td>
            <td <?php echo $td_style; ?>><?php echo $lead['email']; ?></td>
            <td <?php echo $td_style; ?>><?php echo $lead['address']; ?></td>
            <td <?php echo $td_style; ?>>
			<?php 
			if($lead->assigned_to){
				$admin = DB::table('admin')->where('admin_id',$lead->assigned_to)->first();
				echo $admin->user_name.'('.$lead->assigned_to.')';
			}
			?>
			</td>
            
            <td <?php echo $td_style; ?>>
            <?php
               if($lead->status==1){
                    echo "<span style='color:green;'>Opened</span>";
                }elseif($lead->status==0){
                    echo "<span style='color:red;'>Inactive</span>";
                }elseif($lead->status==2){
                    echo "Pending";
                }elseif($lead->status==3){
                    echo "Completed";
                }elseif($lead->status==4){
                    echo "Canceled";
                }elseif($lead->status==9){
                    echo "Deleted";
                }
            ?>
            </td>
        </tr>
    <?php } ?>
</table>

<p <?php echo $p_style; ?>>These are the Expense generated today.</p>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr> 	 	 	 	
        <td <?php echo $th_style; ?>>#</td>
        <td <?php echo $th_style; ?>>Expense ID</td>
        <td <?php echo $th_style; ?>>Category</td>
        <td <?php echo $th_style; ?>>Account</td>
        <td <?php echo $th_style; ?>>Amount</td>
        <td <?php echo $th_style; ?>>Created By</td>
    </tr>    
    <?php if(empty($expenses)){ ?>
    <tr>
        <td colspan="9" <?php echo $td_style; ?>>No Leads information is available for today.</td>
    </tr>
    <?php } ?>
    <?php 
    $expense_amount_tot = 0;
    foreach ($expenses as $key => $expense) { 
    $expense_amount_tot += $expense->amount;
    ?>
    
        <tr>
            <td <?php echo $td_style; ?>><?php echo $key+1; ?></td>
            <td <?php echo $td_style; ?>>AE-<?php echo $expense->expense_id; ?></td>
            <td <?php echo $td_style; ?>>
                <?php 
                    if($expense->income_expense_category_id){
    				$acc_name = DB::table('income_expense_category')->where('income_expense_category_id',$expense->income_expense_category_id)->first();
    				echo $acc_name->category_name;
    			}
    			
    			if($expense->payee_type == 2){
    				$vendor_name = DB::table('vendor')->where('vendor_id',$expense->payee)->first();
    				echo ' ('.$vendor_name->title.')';
    			}
    			
    			if($expense->payee_type == 1){
    				$emp_name = DB::table('admin')->where('admin_id',$expense->payee)->first();
    				echo ' ('.$emp_name->user_name.')';
    			}
    			
    			if($expense->reason_of_expense){
    				echo '<br>'.$expense->reason_of_expense;
    			}
    			
                ?>
            </td>
            <td <?php echo $td_style; ?>>
                <?php 
                if($expense->account_id){
    				$acc_name = DB::table('accounts')->where('account_id',$expense->account_id)->first();
    				echo $acc_name->account_title;
    			}
    			$payment_type = DB::table('payment_type')->select('payment_type_title')->where('type_id',$expense->payment_type)->first();
				echo '<br>'.$payment_type->payment_type_title;
					
                ?>

            </td>
            <td <?php echo $td_style; ?>>
                <?php 
                echo $expense->amount; 
                if($expense->payment_status == 1){
							echo '<br><b style="color:red">Open</b>';
						}elseif($expense->payment_status == 3){
							echo '<br><b style="color:green">Clear</b>';
						}
                ?>
            </td>
            <td <?php echo $td_style; ?>>
                <?php 
			if($expense->admin_id){
				$admin = DB::table('admin')->where('admin_id',$expense->admin_id)->first();
				echo $admin->user_name.'('.$expense->admin_id.')';
			}
			?>
            </td>
        </tr>
    <?php } ?>
    <tr>
        <th colspan="4" <?php echo $th_style; ?>>Total :</th>
        <th <?php echo $th_style; ?>><?php echo number_format((float)$expense_amount_tot, 2, '.', '');?></th>
        <th colspan="1" <?php echo $th_style; ?>></th>
    </tr>
</table>

<p <?php echo $p_style; ?>>Cron Health Snapshot (latest run status)</p>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr>
        <td <?php echo $th_style; ?>>#</td>
        <td <?php echo $th_style; ?>>Cron Name</td>
        <td <?php echo $th_style; ?>>Status</td>
        <td <?php echo $th_style; ?>>Latest Log Line</td>
        <td <?php echo $th_style; ?>>Log Path</td>
    </tr>
    <?php if (empty($cron_health)) { ?>
    <tr>
        <td colspan="5" <?php echo $td_style; ?>>No cron health data found.</td>
    </tr>
    <?php } else { ?>
    <?php foreach ($cron_health as $idx => $cronItem) { ?>
    <tr>
        <td <?php echo $td_style; ?>><?php echo $idx + 1; ?></td>
        <td <?php echo $td_style; ?>><?php echo $cronItem['name']; ?></td>
        <td <?php echo $td_style; ?>>
            <?php
            $status = $cronItem['status'];
            if ($status === 'OK') {
                echo "<span style='color:green;'>OK</span>";
            } elseif ($status === 'ISSUE') {
                echo "<span style='color:red;'>ISSUE</span>";
            } elseif ($status === 'NO_LOG') {
                echo "<span style='color:#cc8400;'>NO_LOG</span>";
            } else {
                echo "<span style='color:#0056b3;'>UNKNOWN</span>";
            }
            ?>
        </td>
        <td <?php echo $td_style; ?>><?php echo !empty($cronItem['latest_line']) ? htmlspecialchars($cronItem['latest_line']) : '-'; ?></td>
        <td <?php echo $td_style; ?>><?php echo $cronItem['path']; ?></td>
    </tr>
    <?php } ?>
    <?php } ?>
</table>

<p <?php echo $p_style; ?>>Recent Successful Cron Runs (last <?php echo (int)($cron_lookback_hours ?? 24); ?> hours)</p>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr>
        <td <?php echo $th_style; ?>>#</td>
        <td <?php echo $th_style; ?>>Success Log Line</td>
    </tr>
    <?php if (empty($cron_recent_successes)) { ?>
    <tr>
        <td colspan="2" <?php echo $td_style; ?>>No successful cron completion lines found in selected window.</td>
    </tr>
    <?php } else { ?>
    <?php foreach ($cron_recent_successes as $idx => $successLine) { ?>
    <tr>
        <td <?php echo $td_style; ?>><?php echo $idx + 1; ?></td>
        <td <?php echo $td_style; ?>><?php echo htmlspecialchars($successLine); ?></td>
    </tr>
    <?php } ?>
    <?php } ?>
</table>

<p <?php echo $p_style; ?>>Disconnection/Reconnection Detail (last <?php echo (int)($cron_lookback_hours ?? 24); ?> hours)</p>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr>
        <td <?php echo $th_style; ?>>Metric</td>
        <td <?php echo $th_style; ?>>Value</td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Disconnect total devices processed</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_disconnect_stats['total_devices'] ?? 0); ?></td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Disconnect success devices</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_disconnect_stats['success_devices'] ?? 0); ?></td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Disconnect failed devices</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_disconnect_stats['failed_devices'] ?? 0); ?></td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Disconnect apply failures</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_disconnect_stats['disconnect_apply_failures'] ?? 0); ?></td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Disconnect IP success actions</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_disconnect_stats['disconnect_ip_success'] ?? 0); ?></td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Disconnect PPOE success actions</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_disconnect_stats['disconnect_pppoe_success'] ?? 0); ?></td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Disconnect PPOE failures</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_disconnect_stats['disconnect_pppoe_failures'] ?? 0); ?></td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Disconnect not applied cases</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_disconnect_stats['disconnect_not_applied'] ?? 0); ?></td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Disconnect device connection failures</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_disconnect_stats['device_connect_failures'] ?? 0); ?></td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Reconnect success</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_reconnect_stats['success'] ?? 0); ?></td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Reconnect IP success actions</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_reconnect_stats['ip_success'] ?? 0); ?></td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Reconnect PPOE success actions</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_reconnect_stats['pppoe_success'] ?? 0); ?></td>
    </tr>
    <tr>
        <td <?php echo $td_style; ?>>Reconnect failures</td>
        <td <?php echo $td_style; ?>><?php echo (int)($cron_reconnect_stats['failed'] ?? 0); ?></td>
    </tr>
</table>

<p <?php echo $p_style; ?>>Disconnection Failure Reasons</p>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr>
        <td <?php echo $th_style; ?>>#</td>
        <td <?php echo $th_style; ?>>Reason</td>
        <td <?php echo $th_style; ?>>Count</td>
    </tr>
    <?php $dReasons = $cron_disconnect_stats['failure_reasons'] ?? []; ?>
    <?php if (empty($dReasons)) { ?>
    <tr>
        <td colspan="3" <?php echo $td_style; ?>>No disconnection failure reason found in selected window.</td>
    </tr>
    <?php } else { $i=1; foreach ($dReasons as $reason => $count) { ?>
    <tr>
        <td <?php echo $td_style; ?>><?php echo $i++; ?></td>
        <td <?php echo $td_style; ?>><?php echo htmlspecialchars((string)$reason); ?></td>
        <td <?php echo $td_style; ?>><?php echo (int)$count; ?></td>
    </tr>
    <?php }} ?>
</table>

<p <?php echo $p_style; ?>>Reconnection Failure Reasons</p>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr>
        <td <?php echo $th_style; ?>>#</td>
        <td <?php echo $th_style; ?>>Reason</td>
        <td <?php echo $th_style; ?>>Count</td>
    </tr>
    <?php $rReasons = $cron_reconnect_stats['failure_reasons'] ?? []; ?>
    <?php if (empty($rReasons)) { ?>
    <tr>
        <td colspan="3" <?php echo $td_style; ?>>No reconnection failure reason found in selected window.</td>
    </tr>
    <?php } else { $i=1; foreach ($rReasons as $reason => $count) { ?>
    <tr>
        <td <?php echo $td_style; ?>><?php echo $i++; ?></td>
        <td <?php echo $td_style; ?>><?php echo htmlspecialchars((string)$reason); ?></td>
        <td <?php echo $td_style; ?>><?php echo (int)$count; ?></td>
    </tr>
    <?php }} ?>
</table>

<p <?php echo $p_style; ?>>Recent Cron Errors (last <?php echo (int)($cron_lookback_hours ?? 24); ?> hours)</p>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr>
        <td <?php echo $th_style; ?>>#</td>
        <td <?php echo $th_style; ?>>Error Log Line</td>
    </tr>
    <?php if (empty($cron_recent_errors)) { ?>
    <tr>
        <td colspan="2" <?php echo $td_style; ?>>No cron-related errors found in selected window.</td>
    </tr>
    <?php } else { ?>
    <?php foreach ($cron_recent_errors as $idx => $errorLine) { ?>
    <tr>
        <td <?php echo $td_style; ?>><?php echo $idx + 1; ?></td>
        <td <?php echo $td_style; ?>><?php echo htmlspecialchars($errorLine); ?></td>
    </tr>
    <?php } ?>
    <?php } ?>
</table>

<?php 
$date = date('Y-m-d');
$task = DB::select("SELECT * FROM task_list WHERE task_status != 3 AND DATE(task_list.task_status_complete_time) = '$date'");
if($task){
?>
<h3 <?php echo $h3_style; ?>>Todays Incomplete Task</h3>
<table cellpadding="0" cellspacing="0" <?php echo $table_style; ?>>
    <tr>
        <td <?php echo $th_style; ?>>#</td>
        <td <?php echo $th_style; ?>>Task Title</td>
        <td <?php echo $th_style; ?>>Task For</td>
        <td <?php echo $th_style; ?>>Time Left</td>
        <td <?php echo $th_style; ?>>Status</td>
        <td <?php echo $th_style; ?>>Created By</td>       
    </tr>
   <?php
		if(!empty($task))
		{
			$i = 0;
			foreach($task as $value)
			{
				$i++;
	?>
	<tr>
		<td <?php echo $td_style;?>><?php echo $i;?></td>
		<td <?php echo $td_style;?>>
		<a href="">
		<?php echo ucwords($value->task_title).' ('.$value->task_list_id.')'; ?>
		</a>
		</td>
		<td <?php echo $td_style;?>>
			<?php 
			if($value->task_for == 1 AND $value->client_id != 0){
				$result = $this->db->where('client_id',$value->client_id)->get('client')->row();
				echo $result->client_title;
				echo '(IL-C'.$value->client_id.')';
			}else{
				echo $value->task_for_name;
			}
			?>
		</td>
		
		<td <?php echo $td_style;?>>
		<?php
		if($value->task_status == 3){
			$date = $value->task_status_complete_time;
			$datetime1 = new DateTime($date);//start time
			$datetime2 = new DateTime($value->task_deadline);//end time
			$interval = $datetime2->diff($datetime1);
			//echo $interval->format('%Y years %m months %d days %H hours %i minutes %s seconds');
			echo $interval->format('%dD, %HH, %iM');
		}else{
			$date = date('Y-m-d H:i:s');
			$datetime1 = new DateTime($date);//start time
			$datetime2 = new DateTime($value->task_deadline);//end time
			$interval = $datetime2->diff($datetime1);
			//echo $interval->format('%Y years %m months %d days %H hours %i minutes %s seconds');
			echo $interval->format('%dD, %HH, %iM');
		}
		?>
		</td>
		<td <?php echo $td_style;?>>
		<?php 
			if($value->task_status == 1){
				echo '<span class="label label-primary">Scheduled</span>';
			}elseif($value->task_status == 2){
				echo '<span class="label label-warning">Processing</span>';
			}elseif($value->task_status == 3){
				echo '<span class="label label-success">Complete</span>';
			}
			?>
		</td>
		<td <?php echo $td_style;?>>
		<?php
		$result = DB::table('admin')->where('admin_id',$value->task_added_by_admin_id)->first();
		if($result){
		echo $result->user_name.'('.$value->task_added_by_admin_id.')'; 
		}
		?>
		</td>
	</tr>  
	<?php
			}
		}else{
	?>
	<tr>
		<td colspan="6">No Result Found</td>
	</tr>
	<?php 
		}
	?>
</table>
<?php 
}
?>