<?php
/*Template Name: tab-success  */
get_header();
?>
<section class="content">

    <div class="container">


        <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>
                <h1 class="sec-tit text-center">
                    <?php the_content(); ?>
                </h1>


            <?php endwhile; ?>

        <?php endif; ?>

    </div>

</section>

<?php get_footer(); ?>



<?php
global $wpdb;
$k=stripslashes($_COOKIE["cart"]);
$courses = json_decode($k);
    $coupon = $_GET['coup'];
    $price = $_GET['pric'];
 
foreach($courses as $course){
$data = get_field('courses_paid', $course->courseid);
    $data1 = [];
    foreach($data as $dataa){
    //    $dataa['iswatched'] = 'false';
        $data1[] = (object)$dataa;
    }

$Today=date('Y-m-d');

$wpdb->insert('courses_paid',
    array(
        'coursid' => $course->courseid,
        'userid' => get_current_user_id(),
        'price' => $course->price,
        'coupon' => $coupon,
        'created_at' => $Today,
        'lectures' => json_encode($data1),
        'status' => 'true',
    ));
    ?>
 
    <?php
$format = array(
    '%s', '%s', '%s', '%s', '%s', '%s', '%s'
);


}

$message  = '<html><body>';

$message .="<img class='img-fluid' src='https://learning-arabia-language.com/wp-content/uploads/2023/10/logo.png'><br>";

$message .="نفيدكم بتأكيد تسجيلكم وستصلكم كافة التفاصيل عبر بريدكم الالكتروني";
$message .="<table> <tr> <th>اسم الدرس</th> <th>سعر الدرس </th> </tr>";

foreach ($courses as $course){
    $message .="<tr><td>".get_the_title($course->courseid)."</td><td>".$course->price."</td></tr>";
};


$message .="</table>";
if( $coupon !='null' ) : 
$message .= '<p style="color:#080;font-size:18px;">كود الخصم : '.$coupon.'</p>';
endif;
$message .= '<p style="color:#080;font-size:18px;">اجمالى المبلغ : '.$price.'</p>';
$message .= '</body></html>';




$current_user = wp_get_current_user();
$useremail = $current_user->user_email;

$to = $useremail;
$subject = 'تم استلام طلبك';
$body = $message;

        $headers = array('Content-Type: text/html; charset=UTF-8');
        
        wp_mail($to, $subject, $body, $headers);
        

    unset($_COOKIE['cart']); 
    setcookie('cart', null, -1, '/'); 

wp_redirect( home_url(), '302' );
exit;
?>




