<?php
include_once('../components/connect.php');
require_once('../components/dompdf/autoload.inc.php');
use Dompdf\Dompdf;
use Dompdf\Options;


$child_id = $_POST['child_id'];
$selected_date = $_POST['date'];


$selected_date1 = explode('-',$selected_date);
$month = $selected_date1[0];
$day = $selected_date1[1];
$year = $selected_date1[2];

$date_created = $month. '' . $day.''.$year;

$gen = $_POST['generate'];
$unix_time = mktime(0, 0, 0, $month, $day, $year);

$monday_p = date("m-d-Y", strtotime('monday last week', $unix_time));
$tuesday_p = date("m-d-Y", strtotime('tuesday last week', $unix_time));
$wednesday_p = date("m-d-Y", strtotime('wednesday last week', $unix_time, ));
$thursday_p = date("m-d-Y", strtotime('thursday last week', $unix_time));
$friday_p = date("m-d-Y", strtotime('friday last week', $unix_time));

$monday = date("m-d-Y", strtotime('monday this week', $unix_time));
$tuesday = date("m-d-Y", strtotime('tuesday this week', $unix_time));
$wednesday = date("m-d-Y", strtotime('wednesday this week', $unix_time));
$thursday = date("m-d-Y", strtotime('thursday this week', $unix_time));
$friday = date("m-d-Y", strtotime('friday this week', $unix_time));

$date_k = Array($monday_p, $tuesday_p, $wednesday_p, $thursday_p, $friday_p, $monday, $tuesday, $wednesday, $thursday, $friday);
$dates = array($monday, $tuesday, $wednesday, $thursday, $friday, $monday_p, $tuesday_p, $wednesday_p, $thursday_p, $friday_p);
ksort($date_k);

$dates_array = array_fill_keys($dates, '');
ksort($dates_array);

$resp['success'] = false;

if(empty($child_id))
{
    $resp['msg'] = 'Child ID is required!';
    echo 'Child ID is required';
}

$sql = "SELECT * FROM children_details LEFT JOIN signup_types ON children_details.sign_up_option_id = signup_types.id  LEFT JOIN rate_list ON children_details.child_id = rate_list.child_id LEFT JOIN charges ON rate_list.child_id = charges.child_id WHERE children_details.child_id = ". $child_id." AND children_details.is_invoiced = 0";

$result = mysqli_query($conn, $sql);

$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$row_result = $rows[0];
$extra_days =  json_decode($row_result['extra_days']);
$extra_hours =  json_decode($row_result['extra_hours']);
$days_attended =  json_decode($row_result['days_attended']);

$extra_days_array = array();
$extra_hours_array = array();
$extra_hours_rate_array = array();
$days_attended_array = array();

foreach($extra_days as $day)
{
    $extra_days_array[$day->day] =  number_format((float)$day->rate, 2);
}

foreach ($extra_hours as $hours) 
{
    $extra_hours_array[$hours->day] = $hours->hours . 'hr(s)';
    $extra_hours_rate_array[$hours->day] = number_format((float)$hours->rate, 2);
}

foreach($days_attended as $day) 
{
    $days_attended_array[$day->day] = number_format((float)$day->rate, 2);
}

$extra_days_f = array_merge($dates_array, $extra_days_array);
$extra_hours_f = array_merge($dates_array, $extra_hours_array);
$extra_hours_rate_f = array_merge($dates_array, $extra_hours_rate_array);
$days_attended_f = array_merge($dates_array, $days_attended_array);
ksort($extra_days_f);
ksort($extra_hours_f);
ksort($extra_hours_rate_f);
ksort($days_attended_f);
//Totals
$r1_total = number_format((float)$extra_days_f[$date_k[0]] + (float)$extra_hours_rate_f[$date_k[0]],2);
$r2_total = number_format((float)$extra_days_f[$date_k[1]] + (float)$extra_hours_rate_f[$date_k[1]], 2);
$r3_total = number_format((float)$extra_days_f[$date_k[2]] + (float)$extra_hours_rate_f[$date_k[2]], 2);
$r4_total = number_format((float)$extra_days_f[$date_k[3]] + (float)$extra_hours_rate_f[$date_k[3]], 2);
$r5_total = number_format((float)$extra_days_f[$date_k[4]] + (float)$extra_hours_rate_f[$date_k[4]], 2);

$r6_total = number_format((float)$extra_days_f[$date_k[5]] + (float)$extra_hours_rate_f[$date_k[5]], 2);
$r7_total = number_format((float)$extra_days_f[$date_k[6]] + (float)$extra_hours_rate_f[$date_k[6]], 2);
$r8_total = number_format((float)$extra_days_f[$date_k[7]] + (float)$extra_hours_rate_f[$date_k[7]], 2);
$r9_total = number_format((float)$extra_days_f[$date_k[8]] + (float)$extra_hours_rate_f[$date_k[8]], 2);
$r10_total = number_format((float)$extra_days_f[$date_k[9]] + (float)$extra_hours_rate_f[$date_k[9]], 2);

if($r1_total == 0.00)
{
    $r1_total = '';
}   
if($r2_total == 0.00)
{
    $r2_total = '';
}
if($r3_total == 0.00)
{
    $r3_total = '';
}
if($r4_total == 0.00)
{
    $r4_total = '';
}
if($r5_total == 0.00)
{
    $r5_total = '';
}


if ($r6_total == 0.00) {
    $r6_total = '';
}
if ($r7_total == 0.00) {
    $r7_total = '';
}
if ($r8_total == 0.00) {
    $r8_total = '';
}
if ($r9_total == 0.00) {
    $r9_total = '';
}
if ($r10_total == 0.00) {
    $r10_total = '';
}


$html = '<div id="email" style="font-family: Poppins, sans-serif; width:700px; margin-left: auto; margin-right: auto; padding: 5px;">

    <table border="0" width="100%">
        <tr>
            <td align="left" width="100" style="padding-right: 20px;">
                <img height="120" src="../assets/imgs/KK-logo.jpg"/>
            </td>
            <td align="left">
                <p style="color: #1D5C8E; margin-bottom: 10px; font-weight: 600;">Kool Kids Day Care</p>
                <p style="margin-top: 10px;">
                1030 Cheyenne Drive,<br>
                Gilroy, CA, 95020, USA<br>
                510 565 8267<br>
                510 722 2223
                </p>
            </td>
            <td align="right" vertical-align="top" valign="top" style="padding-right: 5px;">
                
                <p style="font-weight: 600; padding-left: 5px; padding-right: 5px; margin-bottom: 0px; margin-top: 5px; width: fit-content;">
                   Date :  '. $selected_date.'
                </p>
                <p>
                    '. 'KK-'.$child_id .''. $date_created  . '
                </p>
                <p>
                    &nbsp;
                </p>
                <p style="font-weight: 600; margin-right: 5px; margin-bottom: 0;">
                    '. $row_result['child_name']. '
                </p>
                <p style="margin-right: 5px; margin-top: 0;">
                    Option: ' . $row_result['option_name'] . '
                </p>
            </td>
        </tr>
    </table>
    <table border="0" width="100%" style="padding: 5px; background-color: #1D5C8E;">
        <tr>
            <td>
                <p style="font-weight: 600; color: #fff; margin-bottom: 0px; margin-top: 0px;">Bill to</p>
            </td>
        </tr>
    </table>
    <table border="0" width="100%">
        <tr>
            <td>
                '. $row_result['father_name']. ' and ' . $row_result['mother_name'] . '
            </td>
        </tr>
    </table>
    <table border="0"  width="100%" >
        <tr>
            <td>
                <p style="font-weight: 600;">'. $row_result['street_address'].' '.$row_result['city']. ', ' . $row_result['state']. ' ' . $row_result['zipcode'] . '</p>
            </td>
        </tr>
    </table>
    <table border="1" cellpadding="5" style="border-collapse: collapse;" width="100%" >
        <thead style="background-color: #1D5C8E; color: #fff; font-size: 14px;">
        <tr>
            <th style="width:min-content;">
                Sr. No
            </th>
            <th>
                Week Of
            </th>
            <th style="width:min-content;">
                Weekly Rate
            </th>
            <th>
                Date
            </th>
            <th>
                Extra Days Rate
            </th>
            <th>
                Extra Hours
            </th>
            <th>
                Extra Hours Rate
            </th>
            <th style="padding-left: 20px; padding-right: 20px">
                Total
            </th>
        </thead>
        <tbody style=" text-align: center; font-size: 14px;">
            <tr>
                <td rowspan="6" scope="rowgroup">1</td>
                <td rowspan="6" scope="rowgroup">'. date_create_from_format("m-d-Y", $monday_p)->format("j-M").'</td>
                <td rowspan="6" scope="rowgroup">'. number_format((float)$row_result['weekly_charges'], 2).'</td>
            </tr>
            <tr>
                <td scope="row">'.date_create_from_format("m-d-Y", $date_k[0])->format("D, F j, Y"). '</td>
                <td>'. $extra_days_f[$date_k[0]]. '</td>
                <td>' . $extra_hours_f[$date_k[0]] . '</td>
                <td>' . $extra_hours_rate_f[$date_k[0]] . '</td>
                <td>'. $r1_total.'</td>
            </tr>
            <tr>
               <td scope="row">' . date_create_from_format("m-d-Y", $date_k[1])->format("D, F j, Y"). '</td>
               <td>' . $extra_days_f[$date_k[1]] . '</td>
               <td>' . $extra_hours_f[$date_k[1]] . '</td>
                <td>' . $extra_hours_rate_f[$date_k[1]] . '</td>
                <td>' . $r2_total . '</td>
            </tr>
            <tr>
                <td scope="row">' . date_create_from_format("m-d-Y", $date_k[2])->format("D, F j, Y") . '</td>
                <td>' . $extra_days_f[$date_k[2]] . '</td>
                <td>' . $extra_hours_f[$date_k[2]] . '</td>
                <td>' . $extra_hours_rate_f[$date_k[2]] . '</td>
                <td>' . $r3_total . '</td>
            </tr>
            <tr>
               <td scope="row">' . date_create_from_format("m-d-Y", $date_k[3])->format("D, F j, Y") . '</td>
               <td>' . $extra_days_f[$date_k[3]] . '</td>
               <td>' . $extra_hours_f[$date_k[3]] . '</td>
                <td>' . $extra_hours_rate_f[$date_k[3]] . '</td>
                <td>' . $r4_total . '</td>
            </tr>
            <tr>
                <td scope="row">' . date_create_from_format("m-d-Y", $date_k[4])->format("D, F j, Y"). '</td>
                <td>' . $extra_days_f[$date_k[4]] . '</td>
                <td>' . $extra_hours_f[$date_k[4]] . '</td>
                <td>' . $extra_hours_rate_f[$date_k[4]] . '</td>
                <td>' . $r5_total . '</td>
            </tr>
        </tbody>
        <tbody style="border-top: 2px solid #000; text-align: center; font-size: 14px;">
            <tr>
                <td rowspan="6" scope="rowgroup">2</td>
                <td rowspan="6" scope="rowgroup">' . date_create_from_format("m-d-Y", $monday)->format("j-M") . '</td>
                <td rowspan="6" scope="rowgroup">' . number_format((float)$row_result['weekly_charges'], 2) . '</td>
            </tr>
            <tr>
                <td scope="row">' . date_create_from_format("m-d-Y", $date_k[5])->format("D, F j, Y") . '</td>
                <td>' . $extra_days_f[$date_k[5]] . '</td>
                <td>' . $extra_hours_f[$date_k[5]] . '</td>
                <td>' . $extra_hours_rate_f[$date_k[5]] . '</td>
                <td>' . $r6_total . '</td>
            </tr>
            <tr>
               <td scope="row">' . date_create_from_format("m-d-Y", $date_k[6])->format("D, F j, Y") . '</td>
               <td>' . $extra_days_f[$date_k[6]] . '</td>
               <td>' . $extra_hours_f[$date_k[6]] . '</td>
                <td>' . $extra_hours_rate_f[$date_k[6]] . '</td>
                <td>' . $r7_total . '</td>
            </tr>
            <tr>
                <td scope="row">' . date_create_from_format("m-d-Y", $date_k[7])->format("D, F j, Y") . '</td>
                <td>' . $extra_days_f[$date_k[7]] . '</td>
                <td>' . $extra_hours_f[$date_k[7]] . '</td>
                <td>' . $extra_hours_rate_f[$date_k[7]] . '</td>
                <td>' . $r8_total . '</td>
            </tr>
            <tr>
               <td scope="row">' . date_create_from_format("m-d-Y", $date_k[8])->format("D, F j, Y") . '</td>
               <td>' . $extra_days_f[$date_k[8]] . '</td>
               <td>' . $extra_hours_f[$date_k[8]] . '</td>
                <td>' . $extra_hours_rate_f[$date_k[8]] . '</td>
                <td>' . $r9_total . '</td>
            </tr>
            <tr>
                <td scope="row">' . date_create_from_format("m-d-Y", $date_k[9])->format("D, F j, Y"). '</td>
                <td>' . $extra_days_f[$date_k[9]] . '</td>
                <td>' . $extra_hours_f[$date_k[9]] . '</td>
                <td>' . $extra_hours_rate_f[$date_k[9]] . '</td>
                <td>' . $r10_total . '</td>
            </tr>
        </tbody>
    </th>
    </table>
    <table border="0"  width="100%" >
        <tr>
            <td align="right">
                <p style="font-weight: 600; padding: 5px;">Total : $'. number_format((float)$r1_total + (float)$r2_total + (float)$r3_total + (float)$r4_total + (float)$r5_total + (float)$r6_total + (float)$r7_total + (float)$r8_total + (float)$r9_total + (float)$r10_total,2) . '</p>
            </td>
        </tr>
    </table>
    <table border="0"  width="100%" >
        <tr>
            <td align="left">
                <p style="padding: 0 5px; color: #1D5C8E;">Make checks Payable to Sunila Khanna</p>
            </td>
        </tr>
    </table>
    <table border="0"  width="100%" >
        <tr>
            <td align="left">
                <p style="padding: 0 5px; color: #1D5C8E;">Venmo sunila@koolkidsdaycare.us</p>
            </td>
        </tr>
    </table>

</div>';

if($gen)
{

    $formatted_send_date = date_create_from_format("m-d-Y", $selected_date)->format("Y-m-d"); 
    $sql_update_date = "UPDATE children_details SET invoice_due = '$formatted_send_date' WHERE child_id = '$child_id'";

    $options = new Options();
    $options->set('isRemoteEnabled', true);
    $dompdf = new Dompdf($options);
    $dompdf->loadHtml($html);
    $dompdf->render();
    $output = $dompdf->output();
    file_put_contents('../invoices/invoice_'.$child_id.''. $date_created.'.pdf', $output);

    $result = mysqli_query($conn, $sql_update_date);
    if ($result) {
        $resp['success'] = true; 
        $resp['msg'] = 'Email will be sent on ' . $selected_date;
    } else {
        $resp['msg'] = 'Something went wrong!';
    }

    // $to = 'vaibhavnanda2000@gmail.com';
    // $subject = 'Subject: Daycare Invoice for the weeks of '. $monday_p.' and '. $monday;
    // $message = 'Hi '. $row_result['mother_name'] .'and'. $row_result['father_name'].',
    //             Please find attached the daycare invoice for last two weeks.

    //             Thanks and Regards
    //             Rajeev Bharol
    //             Logistics and accounts receivable
    //             Kool Kids Day Care
    //             1030 Cheyenne Drive
    //             Gilroy CA 95020
    //             510 565 8260 (c)
    //             510 722 2223 (f)';
    // $headers = 'From: vaibhavnanda2000@gmail.com';
    // $file = '../invoices/invoice_' . $child_id . '' . $date_created . '.pdf';
    // $sent = 1;

    // if (mail($to, $subject, $message, $headers, $file)) {
        
    //     $sent = 1;
    //     $sql = "UPDATE children_details SET is_invoiced = '$sent' WHERE child_id = '$child_id'";
    //     $result = mysqli_query($conn, $sql);
    //     if ($result)
    //     {
    //         $resp['msg'] = 'Email sent successfully!';
    //     }
    //     else
    //     {
    //         $resp['msg'] = 'Something went wrong!';
    //     }
    // } else {
    //     $resp['msg'] = "Email sending failed";
    // }
    echo json_encode($resp);

}
else
{
    echo $html;
}

?>