<?php global $wpdb;
$orderActive = $wpdb->get_results("SELECT * FROM courses_paid WHERE userid = '" . get_current_user_id() . "' AND coursid= '" . get_the_ID() . "'  And status='true' ");
$orderWait = $wpdb->get_results("SELECT * FROM courses_paid WHERE userid = '" . get_current_user_id() . "' AND coursid= '" . get_the_ID() . "'  And status='false' ");
$courserates = $wpdb->get_results("SELECT * FROM  courses_rate WHERE course_id= '" . get_the_ID() . "'");
$course_cost_free = get_field('course_cost') == 'course_free';
$course_cost_paid = get_field('course_cost') == 'course_paid';
$userid = get_current_user_id();
$courseid = get_the_ID();
$select = get_sub_field_object('uniqeid', '5fc654c1c1788');
get_header(); ?>
<?php //setPostViews(get_the_ID()); ?>
<h1 class="pagetit text-center">
    <?php the_title(); ?>
</h1>
<section class="content">


    <div class="page-main">
        <div class="container">
            <div class="row">
                <div class="col-lg-8">
                    <!-- Start intro Video || image -->
                    <?php if (get_field('courses_video_intro')) : ?>
                        <div class="video-intro wow fadeInUp" data-wow-delay=".2s">
                            <video controls poster="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'full') ?>">
                                <source src="<?php the_field('courses_video_intro') ?>" type="video/mp4">
                                <source src="<?php the_field('courses_video_intro') ?>" type="video/ogg">
                                Your browser does not support HTML video.
                            </video>
                        </div>
                    <?php elseif (has_post_thumbnail()): ?>
                        <div class="page-thumb wow fadeInUp" data-wow-delay=".2s">
                            <?php the_post_thumbnail('full', ['class' => 'img-fluid mx-auto d-block']); ?>
                        </div>
                    <?php endif; ?>
                    <!-- End intro Video || image -->

                    <h2 class="tittso wow fadeInUp" data-wow-delay=".2s"><?php the_title(); ?></h2>

                    <div class="course-meta">
                        <div class="meta-item wow fadeInUp" data-wow-delay=".2s">
                            <svg width="19" height="18" viewBox="0 0 19 18" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path fill-rule="evenodd" clip-rule="evenodd" d="M9.01181 0.75C7.14267 0.75 6.96595 3.85321 5.85315 4.94963C4.74035 6.04605 1.28077 4.79243 0.80011 6.73852C0.320454 8.68561 3.43266 9.34625 3.81639 11.0168C4.20213 12.6872 2.30067 15.0343 3.92544 16.2024C5.55021 17.3694 7.29919 14.7279 9.01181 14.7279C10.7244 14.7279 12.4734 17.3694 14.0982 16.2024C15.7229 15.0343 13.8225 12.6872 14.2072 11.0168C14.592 9.34625 17.7032 8.68561 17.2235 6.73852C16.7439 4.79243 13.2833 6.04605 12.1715 4.94963C11.0587 3.85321 10.881 0.75 9.01181 0.75Z" stroke="black" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
                            </svg>

                            <span><?php the_field('course_ratng'); ?></span>
                        </div>
                        <div class="meta-item wow fadeInUp" data-wow-delay=".4s">
                            <svg width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <mask id="mask0_70_2080" style="mask-type:luminance" maskUnits="userSpaceOnUse" x="1" y="4" width="15" height="14">
                                    <path fill-rule="evenodd" clip-rule="evenodd" d="M1.75 4.37524H15.3127V17.9389H1.75V4.37524Z" fill="white"/>
                                </mask>
                                <g mask="url(#mask0_70_2080)">
                                    <path fill-rule="evenodd" clip-rule="evenodd" d="M5.99122 5.68892C5.60709 5.68892 5.35947 5.68892 5.31659 5.69067C4.67084 5.7178 4.07497 5.99167 3.64534 6.46067C3.24459 6.89905 3.03809 7.45905 3.06522 8.04005C3.06522 10.5714 3.06609 14.3707 3.07222 14.5789C3.17197 15.7908 4.30772 16.7174 5.60797 16.6177C8.01422 16.6212 11.579 16.629 11.7461 16.6229C12.3892 16.5967 12.986 16.3237 13.4156 15.8555C13.8172 15.4189 14.0237 14.8589 13.9983 14.2805C13.9975 14.2709 13.9975 14.2613 13.9975 14.2517C13.9983 11.7492 13.9983 7.95255 13.9922 7.74867C13.8933 6.53067 12.7523 5.59267 11.4451 5.69505C9.63734 5.69242 7.17072 5.68892 5.99122 5.68892ZM5.37784 17.9389C3.52809 17.9398 1.91459 16.5223 1.76234 14.6629C1.75972 14.627 1.75097 14.5177 1.75359 8.0698C1.71159 7.16767 2.03884 6.27167 2.67759 5.5743C3.34434 4.8463 4.26397 4.42192 5.26759 4.37905C5.38922 4.3703 9.59622 4.3808 11.3978 4.3843C13.3666 4.24605 15.1437 5.7073 15.3021 7.66817C15.3091 7.76355 15.3117 9.97292 15.31 14.2385C15.3467 15.1625 15.0177 16.0524 14.3825 16.7437C13.7148 17.4708 12.7943 17.8934 11.7916 17.9345C11.6691 17.9433 7.46034 17.9319 5.65609 17.9284C5.56334 17.9354 5.47059 17.9389 5.37784 17.9389Z" fill="black"/>
                                </g>
                                <path fill-rule="evenodd" clip-rule="evenodd" d="M18.2161 16.0556C17.8215 16.0556 17.4321 15.92 17.111 15.6566L14.2401 13.2976C13.9601 13.0675 13.9199 12.6536 14.15 12.3736C14.3793 12.0945 14.7931 12.0525 15.0731 12.2835L17.9431 14.6425C18.1365 14.8 18.3308 14.7353 18.406 14.6985C18.4813 14.6635 18.6554 14.5541 18.6554 14.3056L18.6659 7.87527C18.6668 7.62677 18.4918 7.5174 18.4165 7.48152C18.3421 7.44652 18.1453 7.3809 17.9536 7.5384L15.0723 9.89652C14.7905 10.1266 14.3784 10.0855 14.1491 9.80377C13.919 9.52377 13.961 9.11077 14.241 8.88152L17.1224 6.52252C17.6509 6.0894 18.3623 6.00452 18.9791 6.2959C19.596 6.58902 19.9793 7.19452 19.9784 7.87702L19.9679 14.3074C19.967 14.9899 19.5829 15.5954 18.9669 15.8859C18.7254 16.0005 18.4699 16.0556 18.2161 16.0556Z" fill="black"/>
                            </svg>

                            <span>
                                                        <?php if (get_field('courses_paid')):
                                                            $count = count(get_field('courses_paid'));
                                                            echo $count;
                                                        else :
                                                            echo '0';
                                                        endif;
                                                        ?>
                                                        <?php  _e('فيديو', 'm7md') ?>

                                                    </span>
                        </div>
                        <div class="meta-item wow fadeInUp" data-wow-delay=".6s">
                            <svg width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path fill-rule="evenodd" clip-rule="evenodd" d="M10.5 3.0625C6.39888 3.0625 3.0625 6.39888 3.0625 10.5C3.0625 14.6011 6.39888 17.9375 10.5 17.9375C14.6011 17.9375 17.9375 14.6011 17.9375 10.5C17.9375 6.39888 14.6011 3.0625 10.5 3.0625ZM10.5 19.25C5.67525 19.25 1.75 15.3247 1.75 10.5C1.75 5.67525 5.67525 1.75 10.5 1.75C15.3247 1.75 19.25 5.67525 19.25 10.5C19.25 15.3247 15.3247 19.25 10.5 19.25Z" fill="black"/>
                                <path fill-rule="evenodd" clip-rule="evenodd" d="M13.5024 13.7309C13.3877 13.7309 13.2722 13.7011 13.1664 13.639L9.86761 11.6711C9.66986 11.5521 9.54736 11.3377 9.54736 11.1067V6.86475C9.54736 6.5025 9.84136 6.2085 10.2036 6.2085C10.5667 6.2085 10.8599 6.5025 10.8599 6.86475V10.734L13.8392 12.5102C14.1499 12.6966 14.2522 13.0991 14.0667 13.4106C13.9434 13.6162 13.7255 13.7309 13.5024 13.7309Z" fill="black"/>
                            </svg>

                            <span><?php the_field('course_duration'); ?></span>
                        </div>




                    </div>

                    <div class="conteall wow fadeInUp" data-wow-delay=".2s">
                        <?php the_content(); ?>
                    </div>
                    <?php if(get_field('fwhatlean')): ?>
                    <div class="whtaileavn">
                        <h3 class="wow fadeInUp" data-wow-delay=".2s"><?php  _e('ما ستتعلمه', 'm7md') ?></h3>
                        <?php $dc = .2;  while(has_sub_field('fwhatlean')): ?>
                        <div class="allwhusjds wow fadeInUp" data-wow-delay="<?php echo $dc; ?>s">
                            <svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M12.7306 23C12.2687 23 11.8069 22.843 11.4423 22.5067L7.5287 18.8965C6.82377 18.2462 6.82377 17.1699 7.5287 16.5196C8.23363 15.8693 9.4004 15.8693 10.1053 16.5196L12.7306 18.9414L21.8947 10.4877C22.5996 9.83743 23.7664 9.83743 24.4713 10.4877C25.1762 11.138 25.1762 12.2143 24.4713 12.8646L14.0189 22.5067C13.6543 22.843 13.1924 23 12.7306 23Z" fill="#0D47A1"/>
                                <circle cx="16" cy="16" r="15.25" stroke="#0D47A1" stroke-width="1.5"/>
                            </svg>
                            <span><?php the_sub_field('pointofwhatlean') ?></span>
                        </div>
                        <?php $dc +=.2; endwhile; ?>
                    </div>
                    <?php endif; ?>

                </div>
                <div class="col-lg-4">
                    <div class="coursigmon">
                        <div class="boxesdata pricbos wow fadeInUp" data-wow-delay=".2s">
                            <div class="privsec">
                                <?php if ($course_cost_paid) : ?>
                                    <span><?php the_field('course_price') ?><?php _e('ريال', 'm7md') ?></span>
                                    <?php  if (get_field('course_discount')): ?>
                                        <span class="dispon"><?php the_field('course_discount') ?><?php _e('ريال', 'm7md') ?></span>
                                    <?php endif; ?>
                                <?php elseif ($course_cost_free) : ?>
                                    <?php _e('مجانى', 'm7md') ?>
                                <?php endif; ?>
                            </div>
                             <?php if (  ( $course_cost_paid && !$orderActive   ) || $course_cost_free ): ?>


                             <?php if ( $course_cost_paid && !$orderActive   ): ?>
                            <div class="inordingjks">


                                <?php if ( is_user_logged_in() ): ?>

                                    <span class="adc addcart" data-id="<?php the_ID() ?>"
                                          data-price="<?php the_field("course_price"); ?>" data-cart-url="<?php echo esc_url( get_page_link( 398 ) ); ?>"> <?php _e('إضافة إلى السلة', 'm7md') ?></span>

                                <a  class="adc addcart" href="<?php echo get_page_link(143); ?>?&id=<?php echo get_the_ID(); ?>&price=<?php the_field('course_price') ?>" data-id="<?php the_ID() ?>"
                                   data-price="<?php the_field("course_price"); ?>" >
                                    <span class="dch"><?php _e('شراء الآن', 'm7md') ?></span>
                                </a>
                                <?php else: ?>
                                    <a   href="<?php echo get_page_link(84); ?>">
                                        <span class="adc"><?php _e('إضافة إلى السلة', 'm7md') ?></span>
                                    </a>
                                    <a   href="<?php echo get_page_link(84); ?>">
                                        <span class="dch"><?php _e('شراء الآن', 'm7md') ?></span>
                                    </a>
                                <?php endif; ?>


                            </div>
                            <?php endif; ?>

                             <?php elseif ($orderActive): ?>
                            <div class="inordingjks">
                                <a    >
                                    <span class="dch"><?php _e('انت مشترك في الدورة', 'm7md') ?></span>
                                </a>
                            </div>
                        <?php endif; ?>
                            <div class="sharinbo socialfixedicon">
                                <h5> <?php _e('مشاركة على :', 'm7md') ?> </h5>
                                <div class="social text-center">
                                    <a href="#" onclick="shareOnTikTok(); return false;">
                                        <i class="fa-classic fa-brands fa-tiktok"></i>
                                    </a>
                                    <a href="#" onclick="shareOnInstagram(); return false;">
                                        <i class="fa-classic fa-brands fa-instagram"></i>
                                    </a>
                                    <a href="#" onclick="shareOnFacebook(); return false;">
                                        <i class="fa-classic fa-brands fa-facebook-f"></i>
                                    </a>
                                    <a href="#" onclick="shareOnTwitter(); return false;">
                                        <i class="fa-classic fa-brands fa-x-twitter"></i>
                                    </a>
                                    <a href="#" onclick="shareOnSnapchat(); return false;">
                                        <i class="fa-classic fa-brands fa-snapchat"></i>
                                    </a>
                                </div>

                                <!-- رسالة النسخ اللطيفة -->
                                <div id="copy-notification" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 25px 40px; border-radius: 15px; box-shadow: 0 10px 40px rgba(0,0,0,0.3); z-index: 9999; font-size: 18px; font-weight: 500; text-align: center; animation: slideIn 0.3s ease-out;">
                                    <i class="fa-solid fa-check fa-shake" style="color: #ffffff;"></i>
                                    <div>تم نسخ الرابط بنجاح! ✨</div>
                                    <div style="font-size: 14px; margin-top: 8px; opacity: 0.9;">جاري التحويل...</div>
                                </div>

                                <style>
                                    @keyframes slideIn {
                                        from {
                                            opacity: 0;
                                            transform: translate(-50%, -60%);
                                        }
                                        to {
                                            opacity: 1;
                                            transform: translate(-50%, -50%);
                                        }
                                    }

                                    @keyframes slideOut {
                                        from {
                                            opacity: 1;
                                            transform: translate(-50%, -50%);
                                        }
                                        to {
                                            opacity: 0;
                                            transform: translate(-50%, -40%);
                                        }
                                    }
                                </style>

                                <script>
                                    const currentUrl = window.location.href;
                                    const pageTitle = document.title;

                                    // دالة عرض رسالة النسخ
                                    function showCopyNotification(platform) {
                                        const notification = document.getElementById('copy-notification');
                                        notification.style.display = 'block';

                                        // نسخ الرابط
                                        navigator.clipboard.writeText(currentUrl).then(() => {
                                            // بعد ثانية ونص نخفي الرسالة ونفتح المنصة
                                            setTimeout(() => {
                                                notification.style.animation = 'slideOut 0.3s ease-out';
                                                setTimeout(() => {
                                                    notification.style.display = 'none';
                                                    notification.style.animation = 'slideIn 0.3s ease-out';
                                                    window.open(platform, '_blank');
                                                }, 300);
                                            }, 1500);
                                        });
                                    }

                                    // Facebook - يفتح صفحة إضافة بوست جديد
                                    function shareOnFacebook() {
                                        showCopyNotification('https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(currentUrl));
                                    }

                                    // Twitter/X - مع الرسالة والرابط في البوست
                                    function shareOnTwitter() {
                                        const twitterUrl = `https://twitter.com/intent/tweet?url=${encodeURIComponent(currentUrl)}&text=${encodeURIComponent(pageTitle)}`;
                                        showCopyNotification(twitterUrl);
                                    }

                                    // TikTok
                                    function shareOnTikTok() {
                                        showCopyNotification('https://www.tiktok.com');
                                    }

                                    // Instagram
                                    function shareOnInstagram() {
                                        showCopyNotification('https://www.instagram.com');
                                    }

                                    // Snapchat
                                    function shareOnSnapchat() {
                                        showCopyNotification('https://www.snapchat.com');
                                    }
                                </script>
                            </div>

                        </div>
                        <div class="boxesdata autttys wow fadeInUp" data-wow-delay=".2s">
                            <div class="autosec">
                                <?php
                                global $post;
                                $author_id = $post->post_author; // جيب الـ author من الـ post مباشرة

                                // جيب الصورة
                                $local_avatars = get_user_meta($author_id, 'basic_user_avatar', true);
                                if (isset($local_avatars['full']) && !empty($local_avatars['full'])) {
                                    $img = $local_avatars['full'];
                                } else {
                                    $img = THEME_IMAGES_DIR . 'imgauthoe.png';
                                }
                                ?>
                                <img class="img-fluid" src="<?php echo $img; ?>"/>
                                <span>
                                    <?php _e('تم الإنشاء بواسطة', 'm7md') ?>
                                    <?php
                                    global $post;
                                    $author_id = $post->post_author; // جيب الـ author من الـ post مباشرة
                                    $page_url = get_permalink(105); // استخدم get_permalink بدل get_page_link
                                    $first_name = get_the_author_meta('first_name', $author_id);
                                    $last_name = get_the_author_meta('last_name', $author_id);
                                    ?>

                                    <a href="<?php echo esc_url(add_query_arg('uId', $author_id, $page_url)); ?>">
                                        <span><?php echo esc_html($first_name); ?></span>
                                        <span><?php echo esc_html($last_name); ?></span>
                                    </a>
                                </span>
                            </div>
                            <div class="datalang">
                                <div class="course-date">
                                    <svg width="26" height="26" viewBox="0 0 26 26" fill="none" xmlns="http://www.w3.org/2000/svg">
                                        <path fill-rule="evenodd" clip-rule="evenodd" d="M22.3884 10.7297H3.0791C2.6306 10.7297 2.2666 10.3657 2.2666 9.91718C2.2666 9.46868 2.6306 9.10468 3.0791 9.10468H22.3884C22.8369 9.10468 23.2009 9.46868 23.2009 9.91718C23.2009 10.3657 22.8369 10.7297 22.3884 10.7297Z" fill="#0D47A1"/>
                                        <path fill-rule="evenodd" clip-rule="evenodd" d="M17.5512 14.9604C17.1027 14.9604 16.7344 14.5964 16.7344 14.1479C16.7344 13.6994 17.093 13.3354 17.5415 13.3354H17.5512C17.9997 13.3354 18.3637 13.6994 18.3637 14.1479C18.3637 14.5964 17.9997 14.9604 17.5512 14.9604Z" fill="#0D47A1"/>
                                        <path fill-rule="evenodd" clip-rule="evenodd" d="M12.7436 14.9604C12.2951 14.9604 11.9268 14.5964 11.9268 14.1479C11.9268 13.6994 12.2853 13.3354 12.7338 13.3354H12.7436C13.1921 13.3354 13.5561 13.6994 13.5561 14.1479C13.5561 14.5964 13.1921 14.9604 12.7436 14.9604Z" fill="#0D47A1"/>
                                        <path fill-rule="evenodd" clip-rule="evenodd" d="M7.92631 14.9604C7.47782 14.9604 7.1084 14.5964 7.1084 14.1479C7.1084 13.6994 7.46807 13.3354 7.91656 13.3354H7.92631C8.37481 13.3354 8.73881 13.6994 8.73881 14.1479C8.73881 14.5964 8.37481 14.9604 7.92631 14.9604Z" fill="#0D47A1"/>
                                        <path fill-rule="evenodd" clip-rule="evenodd" d="M17.5512 19.171C17.1027 19.171 16.7344 18.807 16.7344 18.3585C16.7344 17.91 17.093 17.546 17.5415 17.546H17.5512C17.9997 17.546 18.3637 17.91 18.3637 18.3585C18.3637 18.807 17.9997 19.171 17.5512 19.171Z" fill="#0D47A1"/>
                                        <path fill-rule="evenodd" clip-rule="evenodd" d="M12.7436 19.171C12.2951 19.171 11.9268 18.807 11.9268 18.3585C11.9268 17.91 12.2853 17.546 12.7338 17.546H12.7436C13.1921 17.546 13.5561 17.91 13.5561 18.3585C13.5561 18.807 13.1921 19.171 12.7436 19.171Z" fill="#0D47A1"/>
                                        <path fill-rule="evenodd" clip-rule="evenodd" d="M7.92631 19.171C7.47782 19.171 7.1084 18.807 7.1084 18.3585C7.1084 17.91 7.46807 17.546 7.91656 17.546H7.92631C8.37481 17.546 8.73881 17.91 8.73881 18.3585C8.73881 18.807 8.37481 19.171 7.92631 19.171Z" fill="#0D47A1"/>
                                        <path fill-rule="evenodd" clip-rule="evenodd" d="M17.1094 6.27356C16.6609 6.27356 16.2969 5.90956 16.2969 5.46106V1.89581C16.2969 1.44731 16.6609 1.08331 17.1094 1.08331C17.5579 1.08331 17.9219 1.44731 17.9219 1.89581V5.46106C17.9219 5.90956 17.5579 6.27356 17.1094 6.27356Z" fill="#0D47A1"/>
                                        <path fill-rule="evenodd" clip-rule="evenodd" d="M8.3584 6.27356C7.9099 6.27356 7.5459 5.90956 7.5459 5.46106V1.89581C7.5459 1.44731 7.9099 1.08331 8.3584 1.08331C8.8069 1.08331 9.1709 1.44731 9.1709 1.89581V5.46106C9.1709 5.90956 8.8069 6.27356 8.3584 6.27356Z" fill="#0D47A1"/>
                                        <mask id="mask0_173_1276" style="mask-type:luminance" maskUnits="userSpaceOnUse" x="2" y="2" width="22" height="23">
                                            <path fill-rule="evenodd" clip-rule="evenodd" d="M2.16699 2.79401H23.292V24.375H2.16699V2.79401Z" fill="white"/>
                                        </mask>
                                        <g mask="url(#mask0_173_1276)">
                                            <path fill-rule="evenodd" clip-rule="evenodd" d="M8.14808 4.41901C5.33899 4.41901 3.79199 5.91726 3.79199 8.63751V18.4406C3.79199 21.2204 5.33899 22.7501 8.14808 22.7501H17.3109C20.12 22.7501 21.667 21.2486 21.667 18.5229V8.63751C21.6713 7.29959 21.3117 6.25959 20.5977 5.54459C19.8632 4.80792 18.7312 4.41901 17.3207 4.41901H8.14808ZM17.3109 24.3751H8.14808C4.45933 24.3751 2.16699 22.1012 2.16699 18.4406V8.63751C2.16699 5.03217 4.45933 2.79401 8.14808 2.79401H17.3207C19.1721 2.79401 20.7028 3.34867 21.7482 4.39626C22.7633 5.41567 23.2974 6.88142 23.292 8.63967V18.5229C23.292 22.1326 20.9997 24.3751 17.3109 24.3751Z" fill="#0D47A1"/>
                                        </g>
                                    </svg>

                                    <span><?php the_field('course_timing'); ?></span>
                                </div>
                                <div class="course-date">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                        <path d="M12 2C14.5013 4.73835 15.9228 8.29203 16 12C15.9228 15.708 14.5013 19.2616 12 22M12 2C9.49872 4.73835 8.07725 8.29203 8 12C8.07725 15.708 9.49872 19.2616 12 22M12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22M12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22M2.50002 9H21.5M2.5 15H21.5" stroke="#0D47A1" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
                                    </svg>

                                    <span><?php the_field('course_langs'); ?></span>
                                </div>
                            </div>
                        </div>
                        <div class="boxesdata inclidecout  wow fadeInUp" data-wow-delay=".2s">
                            <?php if(get_field('fwhatleanincluded')): ?>
                                <div class="whtainclud">
                                    <h3><?php  _e('مضمون الدورة', 'm7md') ?></h3>
                                    <?php $dca = .2; while(has_sub_field('fwhatleanincluded')): ?>
                                        <div class="allwhusinclud wow fadeInUp" data-wow-delay="<?php echo $dca; ?>s">
                                            <svg width="18" height="13" viewBox="0 0 18 13" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                <path d="M5.73059 13C5.26874 13 4.80689 12.843 4.44227 12.5067L0.528697 8.89651C-0.176232 8.24623 -0.176232 7.1699 0.528697 6.51962C1.23363 5.86934 2.4004 5.86934 3.10533 6.51962L5.73059 8.94135L14.8947 0.48771C15.5996 -0.16257 16.7664 -0.16257 17.4713 0.48771C18.1762 1.13799 18.1762 2.21432 17.4713 2.8646L7.0189 12.5067C6.65429 12.843 6.19244 13 5.73059 13Z" fill="#0D47A1"/>
                                            </svg>

                                            <span><?php the_sub_field('pointofwinclided') ?></span>
                                        </div>
                                    <?php $dca +=.2; endwhile; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>


                <div class="col-lg-12">
                    <div class="cousesaocardion wow fadeInUp" data-wow-delay=".2s">
                        <h3><?php  _e('محتوي الدورة', 'm7md') ?></h3>

                        <?php if ($course_cost_free): ?>
                            <?php if ( is_user_logged_in() ): ?>

                                <?php if (get_field('allcousdksfjs')): ?>


                                        <?php while (has_sub_field('allcousdksfjs')): ?>
                                        <div class="courses-paid accrdsec">
                                        <div class="accrdsechead">
                                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                <path d="M6 15L12 9L18 15" stroke="#0F172A" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            </svg>

                                            <h4><?php the_sub_field('coursechaptername'); ?></h4>
                                        </div>
                                            <div class="accrdsecbody">
                                        <?php while (has_sub_field('courses_paid')): ?>

                                                <div class="course-single-item">
                                                    <a class="fancybox w-100 d-flex align-items-center justify-content-between"
                                                       data-fancybox="gallery"
                                                       href="<?php the_sub_field('course_paid_video'); ?>">
                                            <span class="course-detail-single-tit">
                                                <svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M22.667 5.33337H9.33366C5.33366 5.33337 2.66699 8.00004 2.66699 12V20C2.66699 24 5.33366 26.6667 9.33366 26.6667H22.667C26.667 26.6667 29.3337 24 29.3337 20V12C29.3337 8.00004 26.667 5.33337 22.667 5.33337ZM18.5203 17.3734L15.227 19.3467C13.8936 20.1467 12.8003 19.5334 12.8003 17.9734V14.0134C12.8003 12.4534 13.8936 11.8401 15.227 12.6401L18.5203 14.6134C19.787 15.3867 19.787 16.6134 18.5203 17.3734Z" fill="#0D47A1"/>
</svg>

                                                <?php the_sub_field('course_paid_tit'); ?>
                                            </span>

                                                    </a>
                                                </div>

                                        <?php endwhile; ?>
                                                <div class="allatahmej">
                                                    <div class="attachments-headddd">
                                                        <h4>
                                                            <i class="fa-classic fa-regular fa-file-pdf">  </i>
                                                            <?php  _e('المرفقات', 'm7md') ?>

                                                        </h4>
                                                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                            <path d="M6 15L12 9L18 15" stroke="#0F172A" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                                                        </svg>
                                                    </div>
                                                <div class="attachments-files">
                                                    <?php if(get_field('allcousdksfjs')): ?>
                                                    <?php while(has_sub_field('attachments')): ?>

                                                    <?php   if($orderActive  && $course_cost_paid ):?>
                                                    <a class="active-tab-attachment" target="_blank" href="<?php the_sub_field('afile') ?>" download >
                                                        <?php endif; ?>

                                                        <?php   if( $course_cost_free ):?>
                                                        <a  class="active-tab-attachment" target="_blank" href="<?php the_sub_field('afile') ?>" download >

                                                            <?php endif; ?>

                                                            <i class="<?php the_sub_field('aicon') ?>">  </i>
                                                            <h5><?php the_sub_field('atitle') ?></h5>

                                                        </a>
                                                        <?php   if(!$orderActive  && $course_cost_paid ):?>
                                                            <?php _e('عليك الاشتراك لرؤية  المرفقات', 'm7md') ?>
                                                        <?php endif; ?>
                                                        <?php endwhile; ?>
                                                        <?php endif; ?>
                                                </div>
                                                </div>
                                                <?php
                                                // Generate unique ID for this quiz instance
                                                $quiz_unique_id = 'quiz_' . uniqid();
                                                ?>

                                                <div class="certification" data-quiz-id="<?php echo $quiz_unique_id; ?>">
                                                    <?php if((is_user_logged_in() && $orderActive  && $course_cost_paid) ||  (is_user_logged_in() && $course_cost_free)) : ?>

                                                        <script>
                                                            if (window.location.hash.includes('certificate')) {
                                                                jQuery('#about-tab').removeClass('active');
                                                                jQuery('#certificate-tab').addClass('active');
                                                                jQuery('#about').removeClass('show active');
                                                                jQuery('#certificate').addClass('show active');
                                                            }
                                                        </script>

                                                    <?php
                                                    global $wpdb;
                                                    $userid = get_current_user_id();
                                                    $courseid = get_the_ID();

                                                    $previous_results = array();
                                                    $table_name = 'examresultes';

                                                    $user_exam_records = $wpdb->get_results( $wpdb->prepare(
                                                        "SELECT quiz_title, exam_result, total_questions, correct_answers FROM {$table_name} WHERE user_id = %d AND course_id = %d",
                                                        $userid,
                                                        $courseid
                                                    ) );

                                                    if ( ! empty( $user_exam_records ) ) {
                                                        foreach ( $user_exam_records as $record ) {
                                                            $previous_results[ $record->quiz_title ] = $record;
                                                        }
                                                    }
                                                    ?>

                                                    <?php if(get_sub_field("check_quiz") == true): ?>

                                                    <?php
                                                    $quiz_stable_id = 'quiz_section_' . get_row_index();
                                                    ?>

                                                        <div class="certification-quiz" data-quiz-stable-id-holder="<?php echo $quiz_stable_id; ?>">

                                                            <!-- حاوية الاختبار الكاملة -->
                                                            <div class="quiz-container-placeholder">
                                                                <h2><?php _e('اختبار  ', 'm7md') ?> -<?php the_title(); ?>-</h2>
                                                                <div class="the_quiz_resilts">
                                                                    <?php if(get_sub_field('quiz_coourse')): ?>
                                                                        <?php while(has_sub_field('quiz_coourse')): ?>
                                                                            <div class="question-wrapper">
                                                                                <h3><?php the_sub_field('question') ?></h3>
                                                                                <div class="answers">
                                                                                    <?php while(has_sub_field('aswers')): ?>
                                                                                        <span class="quiz-answer"><?php echo get_row_index() . "- "; ?><b><?php the_sub_field('one_answer') ?></b></span>
                                                                                    <?php endwhile; ?>
                                                                                </div>
                                                                                <p class="after-final"><?php _e('الاجابة التي تم أختيارها', 'm7md') ?> : <b class="choised"></b></p>
                                                                            </div>
                                                                        <?php endwhile; ?>
                                                                    <?php endif; ?>
                                                                    <h5 class="submit-quiz">
                                                                        ** <?php the_field("end_text"); ?>
                                                                        <button class="correct_quizes"><?php _e('الضغط هنا', 'm7md') ?></button>
                                                                        <span class="correction-loader" style="display: none; margin-right: 15px; font-weight: bold;">
                            <span class="percentage-counter">0</span>%
                        </span>
                                                                    </h5>
                                                                </div>
                                                                <div class="final-resltss"></div>
                                                            </div>

                                                            <!-- حاوية لعرض النتيجة السابقة -->
                                                            <div class="previous-result-placeholder" style="display: none;"></div>
                                                        </div>

                                                    <?php
                                                    $correctAnswersArray = array();
                                                    if (get_sub_field('quiz_coourse')):
                                                        while (has_sub_field('quiz_coourse')):
                                                            $correctAnswersArray[] = get_sub_field('correct_answer');
                                                        endwhile;
                                                    endif;
                                                    $correctAnswersJSON = json_encode($correctAnswersArray);
                                                    ?>

                                                        <script>
                                                            (function($) {
                                                                'use strict';

                                                                var stableId = '<?php echo $quiz_stable_id; ?>';
                                                                var $quizContainer = $('.certification-quiz[data-quiz-stable-id-holder="' + stableId + '"]');
                                                                $quizContainer.closest('.courses-paid.accrdsec').attr('data-quiz-stable-id', stableId);
                                                                $quizContainer.removeAttr('data-quiz-stable-id-holder');

                                                                var quizStableId = stableId;
                                                                var quizSelector = '.courses-paid.accrdsec[data-quiz-stable-id="' + quizStableId + '"]';
                                                                var previousResults = <?php echo json_encode($previous_results); ?>;

                                                                // ========================================================================
                                                                // !! تعديل نهائي: إصلاح هزة التمرير !!
                                                                // ========================================================================
                                                                $(window).on('load', function(event) {
                                                                    if (window.location.hash && window.location.hash.startsWith('#quiz_section_')) {
                                                                        // 1. منع المتصفح من القفز إلى الرابط
                                                                        event.preventDefault();

                                                                        var targetQuizId = window.location.hash.substring(1);
                                                                        var $targetSection = $('.courses-paid.accrdsec[data-quiz-stable-id="' + targetQuizId + '"]');

                                                                        if ($targetSection.length) {
                                                                            var $targetHeader = $targetSection.find('.accrdsechead');

                                                                            if (!$targetSection.hasClass('active')) {
                                                                                $('.courses-paid.accrdsec.active').find('.accrdsechead').trigger('click');
                                                                                $targetHeader.trigger('click');
                                                                            }

                                                                            setTimeout(function() {
                                                                                // 2. إيقاف أي أنيميشن سابق والبدء في أنيميشن جديد
                                                                                $('html, body').stop().animate({
                                                                                    scrollTop: $targetSection.offset().top - 100
                                                                                }, 800, 'swing'); // 'swing' يعطي إحساسًا أكثر نعومة
                                                                            }, 500);
                                                                        }
                                                                    }
                                                                });
                                                                // ========================================================================

                                                                if (previousResults.hasOwnProperty(quizStableId)) {
                                                                    var resultDetails = previousResults[quizStableId];

                                                                    $(quizSelector).find('.quiz-container-placeholder').hide();

                                                                    var resultStatusMessage = (resultDetails.exam_result == 1)
                                                                        ? '<div class="quiz-passed-message" style="padding: 20px; text-align: center; border: 1px solid green; background-color: #f0fff0;"><h2><i class="fas fa-check-circle" style="color: green;"></i> <?php echo esc_js(get_field("suc_txt")); ?></h2></div>'
                                                                        : '<div class="quiz-failed-message" style="padding: 20px; text-align: center; border: 1px solid red; background-color: #fff0f0;"><h2><i class="fas fa-times-circle" style="color: red;"></i> <?php echo esc_js(get_field("fal_txt")); ?></h2></div>';

                                                                    var incorrectAnswers = resultDetails.total_questions - resultDetails.correct_answers;
                                                                    var finalResultText = resultDetails.correct_answers + "/" + resultDetails.total_questions;
                                                                    var percentage = (resultDetails.correct_answers / resultDetails.total_questions) * 100;

                                                                    var resultDetailsHtml = `
                        <div class="results-quiz goopen" style="margin-top: 20px;">
                            <h6 class="incorrect-ans"><i class="fas fa-times-circle"></i> <?php echo esc_js(get_field("incorect_txt")); ?> <b>` + incorrectAnswers + `</b></h6>
                            <h6 class="correct-ans"><i class="fas fa-check-circle"></i> <?php echo esc_js(get_field("correct_txt")); ?> <b>` + resultDetails.correct_answers + `</b></h6>
                            <h6 class="final-results"><i class="fas fa-poll"></i> <?php echo esc_js(get_field("final_txt")); ?> <b>` + finalResultText + `</b></h6>
                            <h6 class="final-resultsby100"><i class="fas fa-percentage"></i> <?php echo esc_js(get_field("final_txt_100")); ?> <b>` + percentage.toFixed(2) + `%</b></h6>
                        </div>
                    `;

                                                                    var resultMessage = resultStatusMessage + resultDetailsHtml;
                                                                    $(quizSelector).find('.previous-result-placeholder').html(resultMessage).show();

                                                                } else {
                                                                    $(quizSelector).on('click', '.quiz-answer', function() {
                                                                        var $this = $(this);
                                                                        var answerText = $this.find("b").text();
                                                                        var $answersContainer = $this.parent('.answers');
                                                                        $answersContainer.find('.quiz-answer').css({"background": "transparent", "color": "#000"});
                                                                        $this.css({"background": "#242424", "color": "#fff"});
                                                                        $answersContainer.siblings('.after-final').find('.choised').text(answerText);
                                                                    });

                                                                    $(quizSelector + ' .correct_quizes').click(function() {
                                                                        var thisQuizContainer = $(quizSelector);
                                                                        var allChoisedFilled = true;
                                                                        thisQuizContainer.find(".choised").each(function() {
                                                                            if ($(this).text().trim().length === 0) { allChoisedFilled = false; return false; }
                                                                        });

                                                                        if (!allChoisedFilled) {
                                                                            alert("<?php the_field("checkall_quse") ?>");
                                                                            return;
                                                                        }

                                                                        var $loaderSpan = thisQuizContainer.find('.correction-loader');
                                                                        var $percentageCounter = $loaderSpan.find('.percentage-counter');
                                                                        var $submitButton = $(this);

                                                                        $submitButton.hide();
                                                                        $loaderSpan.show();

                                                                        var load = 0;
                                                                        var loaderInterval = setInterval(function() {
                                                                            load++;
                                                                            if (load <= 100) {
                                                                                $percentageCounter.text(load);
                                                                            } else {
                                                                                clearInterval(loaderInterval);
                                                                            }
                                                                        }, 30);

                                                                        var choisedTextArray = [];
                                                                        thisQuizContainer.find(".choised").each(function() { choisedTextArray.push($(this).text()); });
                                                                        var correctAnswersArray = <?php echo $correctAnswersJSON; ?>;
                                                                        var totalIndices = correctAnswersArray.length;
                                                                        var countOnes = 0;
                                                                        $.map(correctAnswersArray, function(value, index) { if(choisedTextArray[index] === value) countOnes++; });
                                                                        var wesoexamresult = totalIndices > 0 ? ((countOnes / totalIndices) * 100 >= 50 ? 1 : 0) : 0;

                                                                        setTimeout(function() {
                                                                            $.ajax({
                                                                                type: 'POST',
                                                                                url: '<?php echo admin_url('admin-ajax.php'); ?>',
                                                                                data: {
                                                                                    'action': 'save_my_quiz_title_action',
                                                                                    'userid': <?php echo $userid ?>,
                                                                                    'courseid': <?php echo $courseid ?>,
                                                                                    'quiz_title': quizStableId,
                                                                                    'totalquestions': totalIndices,
                                                                                    'corectansers': countOnes,
                                                                                    'timeexam': "<?php echo date('Y-m-d H:i:s') ?>",
                                                                                    'examresult': wesoexamresult,
                                                                                },
                                                                                success: function(data) {
                                                                                    window.location.hash = quizStableId;
                                                                                    location.reload();
                                                                                },
                                                                                error: function(data) {
                                                                                    console.log("Save Error:", data);
                                                                                    alert('حدث خطأ أثناء حفظ النتيجة.');
                                                                                    $loaderSpan.hide();
                                                                                    $submitButton.show();
                                                                                }
                                                                            });
                                                                        }, 3200);
                                                                    });
                                                                }
                                                            })(jQuery);
                                                        </script>

                                                    <?php endif; ?>
                                                    <?php endif; ?>
                                                </div>










                                            </div>
                                        </div>
                                        <?php endwhile; ?>

                                <?php endif; ?>

                            <?php else: ?>
                                <?php _e('عليك  التسجيل لرؤية باقي الدروس', 'm7md') ?>
                            <?php endif; ?>
                        <?php endif; ?>

                        <!-- End Free courses lessons -->

                        <!-- Start paid courses lessons -->
                        <?php if ($orderActive):
                            ?>
                            <?php if ( is_user_logged_in() ): ?>

                            <?php if (get_field('allcousdksfjs')): ?>


                                <?php while (has_sub_field('allcousdksfjs')): ?>
                                    <div class="courses-paid accrdsec">
                                        <div class="accrdsechead">
                                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                <path d="M6 15L12 9L18 15" stroke="#0F172A" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                            </svg>

                                            <h4><?php the_sub_field('coursechaptername'); ?></h4>
                                        </div>
                                        <div class="accrdsecbody">
                                            <?php while (has_sub_field('courses_paid')): ?>

                                                <div class="course-single-item">
                                                    <a class="fancybox w-100 d-flex align-items-center justify-content-between"
                                                       data-fancybox="gallery"
                                                       href="<?php the_sub_field('course_paid_video'); ?>">
                                            <span class="course-detail-single-tit">
                                                <svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M22.667 5.33337H9.33366C5.33366 5.33337 2.66699 8.00004 2.66699 12V20C2.66699 24 5.33366 26.6667 9.33366 26.6667H22.667C26.667 26.6667 29.3337 24 29.3337 20V12C29.3337 8.00004 26.667 5.33337 22.667 5.33337ZM18.5203 17.3734L15.227 19.3467C13.8936 20.1467 12.8003 19.5334 12.8003 17.9734V14.0134C12.8003 12.4534 13.8936 11.8401 15.227 12.6401L18.5203 14.6134C19.787 15.3867 19.787 16.6134 18.5203 17.3734Z" fill="#0D47A1"/>
</svg>

                                                <?php the_sub_field('course_paid_tit'); ?>
                                            </span>

                                                    </a>
                                                </div>

                                            <?php endwhile; ?>
                                            <div class="allatahmej">
                                                <div class="attachments-headddd">
                                                    <h4>
                                                        <i class="fa-classic fa-regular fa-file-pdf">  </i>
                                                        <?php  _e('المرفقات', 'm7md') ?>

                                                    </h4>
                                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                        <path d="M6 15L12 9L18 15" stroke="#0F172A" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
                                                    </svg>
                                                </div>
                                                <div class="attachments-files">
                                                    <?php if(get_field('allcousdksfjs')): ?>
                                                    <?php while(has_sub_field('attachments')): ?>

                                                    <?php   if($orderActive  && $course_cost_paid ):?>
                                                    <a class="active-tab-attachment" target="_blank" href="<?php the_sub_field('afile') ?>" download >
                                                        <?php endif; ?>

                                                        <?php   if( $course_cost_free ):?>
                                                        <a  class="active-tab-attachment" target="_blank" href="<?php the_sub_field('afile') ?>" download >

                                                            <?php endif; ?>

                                                            <i class="<?php the_sub_field('aicon') ?>">  </i>
                                                            <h5><?php the_sub_field('atitle') ?></h5>

                                                        </a>
                                                        <?php   if(!$orderActive  && $course_cost_paid ):?>
                                                            <?php _e('عليك الاشتراك لرؤية  المرفقات', 'm7md') ?>
                                                        <?php endif; ?>
                                                        <?php endwhile; ?>
                                                        <?php endif; ?>
                                                </div>
                                            </div>
                                            <?php
                                            // Generate unique ID for this quiz instance
                                            $quiz_unique_id = 'quiz_' . uniqid();
                                            ?>

                                            <div class="certification" data-quiz-id="<?php echo $quiz_unique_id; ?>">
                                                <?php if((is_user_logged_in() && $orderActive  && $course_cost_paid) ||  (is_user_logged_in() && $course_cost_free)) : ?>

                                                    <script>
                                                        if (window.location.hash.includes('certificate')) {
                                                            jQuery('#about-tab').removeClass('active');
                                                            jQuery('#certificate-tab').addClass('active');
                                                            jQuery('#about').removeClass('show active');
                                                            jQuery('#certificate').addClass('show active');
                                                        }
                                                    </script>

                                                <?php
                                                global $wpdb;
                                                $userid = get_current_user_id();
                                                $courseid = get_the_ID();

                                                $previous_results = array();
                                                $table_name = 'examresultes';

                                                $user_exam_records = $wpdb->get_results( $wpdb->prepare(
                                                    "SELECT quiz_title, exam_result, total_questions, correct_answers FROM {$table_name} WHERE user_id = %d AND course_id = %d",
                                                    $userid,
                                                    $courseid
                                                ) );

                                                if ( ! empty( $user_exam_records ) ) {
                                                    foreach ( $user_exam_records as $record ) {
                                                        $previous_results[ $record->quiz_title ] = $record;
                                                    }
                                                }
                                                ?>

                                                <?php if(get_sub_field("check_quiz") == true): ?>

                                                <?php
                                                $quiz_stable_id = 'quiz_section_' . get_row_index();
                                                ?>

                                                    <div class="certification-quiz" data-quiz-stable-id-holder="<?php echo $quiz_stable_id; ?>">

                                                        <!-- حاوية الاختبار الكاملة -->
                                                        <div class="quiz-container-placeholder">
                                                            <h2><?php _e('اختبار  ', 'm7md') ?> -<?php the_title(); ?>-</h2>
                                                            <div class="the_quiz_resilts">
                                                                <?php if(get_sub_field('quiz_coourse')): ?>
                                                                    <?php while(has_sub_field('quiz_coourse')): ?>
                                                                        <div class="question-wrapper">
                                                                            <h3><?php the_sub_field('question') ?></h3>
                                                                            <div class="answers">
                                                                                <?php while(has_sub_field('aswers')): ?>
                                                                                    <span class="quiz-answer"><?php echo get_row_index() . "- "; ?><b><?php the_sub_field('one_answer') ?></b></span>
                                                                                <?php endwhile; ?>
                                                                            </div>
                                                                            <p class="after-final"><?php _e('الاجابة التي تم أختيارها', 'm7md') ?> : <b class="choised"></b></p>
                                                                        </div>
                                                                    <?php endwhile; ?>
                                                                <?php endif; ?>
                                                                <h5 class="submit-quiz">
                                                                    ** <?php the_field("end_text"); ?>
                                                                    <button class="correct_quizes"><?php _e('الضغط هنا', 'm7md') ?></button>
                                                                    <span class="correction-loader" style="display: none; margin-right: 15px; font-weight: bold;">
                            <span class="percentage-counter">0</span>%
                        </span>
                                                                </h5>
                                                            </div>
                                                            <div class="final-resltss"></div>
                                                        </div>

                                                        <!-- حاوية لعرض النتيجة السابقة -->
                                                        <div class="previous-result-placeholder" style="display: none;"></div>
                                                    </div>

                                                <?php
                                                $correctAnswersArray = array();
                                                if (get_sub_field('quiz_coourse')):
                                                    while (has_sub_field('quiz_coourse')):
                                                        $correctAnswersArray[] = get_sub_field('correct_answer');
                                                    endwhile;
                                                endif;
                                                $correctAnswersJSON = json_encode($correctAnswersArray);
                                                ?>

                                                    <script>
                                                        (function($) {
                                                            'use strict';

                                                            var stableId = '<?php echo $quiz_stable_id; ?>';
                                                            var $quizContainer = $('.certification-quiz[data-quiz-stable-id-holder="' + stableId + '"]');
                                                            $quizContainer.closest('.courses-paid.accrdsec').attr('data-quiz-stable-id', stableId);
                                                            $quizContainer.removeAttr('data-quiz-stable-id-holder');

                                                            var quizStableId = stableId;
                                                            var quizSelector = '.courses-paid.accrdsec[data-quiz-stable-id="' + quizStableId + '"]';
                                                            var previousResults = <?php echo json_encode($previous_results); ?>;

                                                            // ========================================================================
                                                            // !! تعديل نهائي: إصلاح هزة التمرير !!
                                                            // ========================================================================
                                                            $(window).on('load', function(event) {
                                                                if (window.location.hash && window.location.hash.startsWith('#quiz_section_')) {
                                                                    // 1. منع المتصفح من القفز إلى الرابط
                                                                    event.preventDefault();

                                                                    var targetQuizId = window.location.hash.substring(1);
                                                                    var $targetSection = $('.courses-paid.accrdsec[data-quiz-stable-id="' + targetQuizId + '"]');

                                                                    if ($targetSection.length) {
                                                                        var $targetHeader = $targetSection.find('.accrdsechead');

                                                                        if (!$targetSection.hasClass('active')) {
                                                                            $('.courses-paid.accrdsec.active').find('.accrdsechead').trigger('click');
                                                                            $targetHeader.trigger('click');
                                                                        }

                                                                        setTimeout(function() {
                                                                            // 2. إيقاف أي أنيميشن سابق والبدء في أنيميشن جديد
                                                                            $('html, body').stop().animate({
                                                                                scrollTop: $targetSection.offset().top - 100
                                                                            }, 800, 'swing'); // 'swing' يعطي إحساسًا أكثر نعومة
                                                                        }, 500);
                                                                    }
                                                                }
                                                            });
                                                            // ========================================================================

                                                            if (previousResults.hasOwnProperty(quizStableId)) {
                                                                var resultDetails = previousResults[quizStableId];

                                                                $(quizSelector).find('.quiz-container-placeholder').hide();

                                                                var resultStatusMessage = (resultDetails.exam_result == 1)
                                                                    ? '<div class="quiz-passed-message" style="padding: 20px; text-align: center; border: 1px solid green; background-color: #f0fff0;"><h2><i class="fas fa-check-circle" style="color: green;"></i> <?php echo esc_js(get_field("suc_txt")); ?></h2></div>'
                                                                    : '<div class="quiz-failed-message" style="padding: 20px; text-align: center; border: 1px solid red; background-color: #fff0f0;"><h2><i class="fas fa-times-circle" style="color: red;"></i> <?php echo esc_js(get_field("fal_txt")); ?></h2></div>';

                                                                var incorrectAnswers = resultDetails.total_questions - resultDetails.correct_answers;
                                                                var finalResultText = resultDetails.correct_answers + "/" + resultDetails.total_questions;
                                                                var percentage = (resultDetails.correct_answers / resultDetails.total_questions) * 100;

                                                                var resultDetailsHtml = `
                        <div class="results-quiz goopen" style="margin-top: 20px;">
                            <h6 class="incorrect-ans"><i class="fas fa-times-circle"></i> <?php echo esc_js(get_field("incorect_txt")); ?> <b>` + incorrectAnswers + `</b></h6>
                            <h6 class="correct-ans"><i class="fas fa-check-circle"></i> <?php echo esc_js(get_field("correct_txt")); ?> <b>` + resultDetails.correct_answers + `</b></h6>
                            <h6 class="final-results"><i class="fas fa-poll"></i> <?php echo esc_js(get_field("final_txt")); ?> <b>` + finalResultText + `</b></h6>
                            <h6 class="final-resultsby100"><i class="fas fa-percentage"></i> <?php echo esc_js(get_field("final_txt_100")); ?> <b>` + percentage.toFixed(2) + `%</b></h6>
                        </div>
                    `;

                                                                var resultMessage = resultStatusMessage + resultDetailsHtml;
                                                                $(quizSelector).find('.previous-result-placeholder').html(resultMessage).show();

                                                            } else {
                                                                $(quizSelector).on('click', '.quiz-answer', function() {
                                                                    var $this = $(this);
                                                                    var answerText = $this.find("b").text();
                                                                    var $answersContainer = $this.parent('.answers');
                                                                    $answersContainer.find('.quiz-answer').css({"background": "transparent", "color": "#000"});
                                                                    $this.css({"background": "#242424", "color": "#fff"});
                                                                    $answersContainer.siblings('.after-final').find('.choised').text(answerText);
                                                                });

                                                                $(quizSelector + ' .correct_quizes').click(function() {
                                                                    var thisQuizContainer = $(quizSelector);
                                                                    var allChoisedFilled = true;
                                                                    thisQuizContainer.find(".choised").each(function() {
                                                                        if ($(this).text().trim().length === 0) { allChoisedFilled = false; return false; }
                                                                    });

                                                                    if (!allChoisedFilled) {
                                                                        alert("<?php the_field("checkall_quse") ?>");
                                                                        return;
                                                                    }

                                                                    var $loaderSpan = thisQuizContainer.find('.correction-loader');
                                                                    var $percentageCounter = $loaderSpan.find('.percentage-counter');
                                                                    var $submitButton = $(this);

                                                                    $submitButton.hide();
                                                                    $loaderSpan.show();

                                                                    var load = 0;
                                                                    var loaderInterval = setInterval(function() {
                                                                        load++;
                                                                        if (load <= 100) {
                                                                            $percentageCounter.text(load);
                                                                        } else {
                                                                            clearInterval(loaderInterval);
                                                                        }
                                                                    }, 30);

                                                                    var choisedTextArray = [];
                                                                    thisQuizContainer.find(".choised").each(function() { choisedTextArray.push($(this).text()); });
                                                                    var correctAnswersArray = <?php echo $correctAnswersJSON; ?>;
                                                                    var totalIndices = correctAnswersArray.length;
                                                                    var countOnes = 0;
                                                                    $.map(correctAnswersArray, function(value, index) { if(choisedTextArray[index] === value) countOnes++; });
                                                                    var wesoexamresult = totalIndices > 0 ? ((countOnes / totalIndices) * 100 >= 50 ? 1 : 0) : 0;

                                                                    setTimeout(function() {
                                                                        $.ajax({
                                                                            type: 'POST',
                                                                            url: '<?php echo admin_url('admin-ajax.php'); ?>',
                                                                            data: {
                                                                                'action': 'save_my_quiz_title_action',
                                                                                'userid': <?php echo $userid ?>,
                                                                                'courseid': <?php echo $courseid ?>,
                                                                                'quiz_title': quizStableId,
                                                                                'totalquestions': totalIndices,
                                                                                'corectansers': countOnes,
                                                                                'timeexam': "<?php echo date('Y-m-d H:i:s') ?>",
                                                                                'examresult': wesoexamresult,
                                                                            },
                                                                            success: function(data) {
                                                                                window.location.hash = quizStableId;
                                                                                location.reload();
                                                                            },
                                                                            error: function(data) {
                                                                                console.log("Save Error:", data);
                                                                                alert('حدث خطأ أثناء حفظ النتيجة.');
                                                                                $loaderSpan.hide();
                                                                                $submitButton.show();
                                                                            }
                                                                        });
                                                                    }, 3200);
                                                                });
                                                            }
                                                        })(jQuery);
                                                    </script>

                                                <?php endif; ?>
                                                <?php endif; ?>
                                            </div>










                                        </div>
                                    </div>
                                <?php endwhile; ?>

                            <?php endif; ?>

                        <?php else: ?>
                            <?php _e('عليك  التسجيل لرؤية باقي الدروس', 'm7md') ?>
                        <?php endif; ?>

                        <?php else: ?>
                            <?php if (!$orderActive && $course_cost_paid): ?>
                                <?php _e('عليك الاشتراك لرؤية باقي الدروس', 'm7md') ?>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>






                    <?php
                    // ========================================================================
                    // !! الجزء الخاص بالشهادة فقط (يوضع في نهاية ملف القالب) !!
                    // ========================================================================

                    // 1. جلب البيانات الأساسية
                    global $wpdb;
                    $userid = get_current_user_id();
                    $courseid = get_the_ID();
                    $table_name = 'examresultes';

                    // ========================================================================
                    // !! تعديل نهائي: استخدام الأسماء الصحيحة للحقول التي اكتشفناها !!
                    // ========================================================================
                    $total_quizzes_in_course = 0;

                    // الخطوة (أ): جلب الحقل المكرر الرئيسي بالاسم الصحيح 'allcousdksfjs'
                    $main_repeater_field = get_field('allcousdksfjs', $courseid);

                    // الخطوة (ب): التأكد من أن الناتج هو مصفوفة قبل المرور عليها
                    if (is_array($main_repeater_field)) {
                        // الخطوة (ج): المرور على كل "موضوع" أو "فصل" في الدورة
                        foreach ($main_repeater_field as $row) {
                            // الخطوة (د): التحقق من وجود حقل 'check_quiz' وأن قيمته تساوي 1
                            if (isset($row['check_quiz']) && $row['check_quiz'] == 1) {
                                $total_quizzes_in_course++;
                            }
                        }
                    }
                    // ========================================================================


                    // 3. جلب نتائج المستخدم من قاعدة البيانات
                    $user_exam_records = $wpdb->get_results( $wpdb->prepare(
                        "SELECT exam_result FROM {$table_name} WHERE user_id = %d AND course_id = %d",
                        $userid,
                        $courseid
                    ) );
                    $completed_quizzes_count = count($user_exam_records);


                    // 4. التحقق من الشروط وعرض النتيجة النهائية

                    // أولاً، تأكد من أن الدورة تحتوي على اختبارات أصلاً
                    if ( $total_quizzes_in_course > 0 ) {

                        // ثانيًا، تحقق إذا كان المستخدم قد أكمل كل الاختبارات
                        if ( $completed_quizzes_count >= $total_quizzes_in_course ) {

                            // ثالثًا، احسب عدد الاختبارات الناجحة
                            $passed_quizzes_count = 0;
                            if (!empty($user_exam_records)) {
                                foreach ( $user_exam_records as $record ) {
                                    if ( $record->exam_result == 1 ) {
                                        $passed_quizzes_count++;
                                    }
                                }
                            }

                            // رابعًا، قرار الشهادة: هل عدد الناجح أكبر من عدد الراسب؟
                            $failed_quizzes_count = $total_quizzes_in_course - $passed_quizzes_count;

                            if ( $passed_quizzes_count > $failed_quizzes_count ) {
                                // نعم، اعرض الشهادة
                                ?>
                              <?php  if (get_field('certapproveded') == 1): ?>
                                <div class="final-certification-section" style="margin-top: 40px; padding: 20px; border: 2px solid #0073aa;">
                                    <h2 style="text-align: center; margin-bottom: 20px;"><?php the_field('word_8'); ?></h2>
                                    <div class="printcirtification">
                                        <div class="certification-wrap">
                                            <div id="html-content-holder" class="certification-widget my-3">
                                                <div class="certification-head d-flex align-items-center mb-3">
                                                    <h2 class="certification-tit"><?php _e("<!--:en-->Certificate of Appreciation <!--:--><!--:ar-->شهادة تقدير <!--:-->"); ?></h2>
                                                </div>
                                                <div class="certification-body">
                                                    <div class="d-flex flex-column align-items-center text-center">
                                                        <p><?php the_field("word_1") ?> <span class="text-declaration"><?php bloginfo('name'); ?></span> <?php the_field("word_2") ?></p>
                                                        <p><span class="text-declaration"><?php the_author_meta('first_name', $userid); ?> <?php the_author_meta('last_name', $userid); ?></span></p>
                                                        <p><?php the_field("word_3") ?> <span class="text-declaration">( <?php the_title(); ?>)</span></p>
                                                        <p><?php the_field("word_4") ?></p>
                                                        <p><?php the_field("word_5") ?>
                                                            <?php the_field("word_6") ?></p>
                                                        <p><?php the_field("word_7") ?></p>
                                                        <p><span class="text-declaration"><?php echo get_the_author_meta('display_name'); ?></span></p>
                                                    </div>
                                                </div>
                                                <div class="certification-footer d-flex align-items-center">
                                                    <div class="col certification-signature d-flex flex-column align-items-center">
                                                        <div class=" text-center">
                                                            <img src="<?php echo defined('THEME_ROOT') ? THEME_ROOT : get_template_directory_uri(); ?>/assets/theme/images/signature.png">
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <button id="btn-Convert-Html2Image" class="main-btn d-block w-100"><?php _e("<!--:en--> Download the certificate <!--:--><!--:ar--> تحميل الشهادة <!--:-->"); ?></button>
                                    </div>
                                </div>
                                <?php endif; ?>
                                <?php
                            } else {
                                // لا، اعرض رسالة الرسوب
                                ?>
                                <div class="final-fail-message" style="margin-top: 40px; padding: 20px; border: 2px solid #d63638; background-color: #fff5f5; text-align: center;">
                                    <h2><?php the_field('word_9'); ?></h2>
                                    <p> <?php _e("<!--:en--> I succeeded in<!--:--><!--:ar-->لقد نجحت في <!--:-->"); ?>
                                        <strong><?php echo $passed_quizzes_count; ?></strong>  <?php _e("<!--:en--> I took tests and failed. <!--:--><!--:ar--> اختبارات ورسبت في <!--:-->"); ?>
                                        <strong><?php echo $failed_quizzes_count; ?></strong> <?php _e("<!--:en--> From the origin <!--:--><!--:ar-->من أصل <!--:-->"); ?>
                                        <strong><?php echo $total_quizzes_in_course; ?></strong><?php _e("<!--:en-->Tests. <!--:--><!--:ar--> اختبارات. <!--:-->"); ?> </p>
                                    <p><?php the_field('word_10'); ?></p>
                                </div>
                                <?php
                            }
                        }
                        // إذا لم يكمل المستخدم كل الاختبارات، لن يتم طباعة أي شيء هنا.
                    }
                    ?>
















                </div>









            </div>
        </div>
    </div>

</section>

<?php get_footer(); ?>
 <!-- استخدم نسخ حديثة فقط -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>

 
<script>
  jQuery(function () {
    jQuery("#btn-Convert-Html2Image").on("click", async function () {
      const $el = jQuery("#html-content-holder");
      if ($el.length === 0) { alert("العنصر #html-content-holder غير موجود"); return; }

      // انتظر الخطوط (مهم للعربي)
      if (document.fonts && document.fonts.ready) {
        try { await document.fonts.ready; } catch(e){}
      }

      const wasHidden = $el.is(":hidden");
      if (wasHidden) $el.css({ display: "block", visibility: "hidden" });

      try {
        const node = $el[0];

        // <-- الإضافة المهمة هنا: ثبّت عرض نافذة افتراضي كأنه ديسكتوب
        const DESKTOP_VW = 1280;   // غيّرها لقيمة عرض الديسكتوب اللي مصمّم عليه (مثلاً 1200/1366)
        const DESKTOP_VH = 900;    // رقم تقريبي، بس المهم windowWidth

        const canvas = await html2canvas(node, {
          scale: 2,
          useCORS: true,
          allowTaint: true,
          backgroundColor: "#ffffff",
          logging: false,

          // الجديد:
          windowWidth: DESKTOP_VW,
          windowHeight: DESKTOP_VH,
          scrollX: 0,
          scrollY: 0
        });

        const imgData = canvas.toDataURL("image/jpeg", 1);
        const { jsPDF } = window.jspdf;
        const pdf = new jsPDF("l", "mm", "a4");

        const pageW = pdf.internal.pageSize.getWidth();
        const pageH = pdf.internal.pageSize.getHeight();

        let imgW = pageW;
        let imgH = (canvas.height * imgW) / canvas.width;
        if (imgH > pageH) {
          imgH = pageH;
          imgW = (canvas.width * imgH) / canvas.height;
        }
        const x = (pageW - imgW) / 2;
        const y = (pageH - imgH) / 2;

        pdf.addImage(imgData, "JPEG", x, y, imgW, imgH);
      pdf.save("certificate(<?php  the_title(); ?>).pdf");

      } catch (err) {
        console.error(err);
        alert("تعذر إنشاء PDF. تأكد أن العنصر ظاهر وأن الصور مسموح تحميلها (CORS).");
      } finally {
        if (wasHidden) $el.css({ display: "none", visibility: "" });
      }
    });
  });
</script>


<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>-->
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>-->
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.8.1/html2pdf.bundle.min.js"></script>-->
 <script> 
/*<!--    jQuery(document).ready(function () {-->
<!--        jQuery("#btn-Convert-Html2Image").on('click', function () {-->
<!--            var element = jQuery("#html-content-holder");-->
<!--            html2canvas(element, {-->
<!--                onrendered: function (canvas) {-->
<!--                    var imgData = canvas.toDataURL("image/png");-->
<!--                    var pdf = new jsPDF('l', 'mm', 'a4');-->
<!--                    var width = pdf.internal.pageSize.getWidth();-->
<!--                    var height = pdf.internal.pageSize.getHeight();-->
<!--                    pdf.addImage(imgData, 'JPEG', 0, 0, width, height);-->
<!--                    pdf.save('cirtification.pdf');-->
<!--                }-->
<!--            });-->
<!--        });-->
<!--    });-->*/

    jQuery('.lecdone').on('click', function () {
        var uniqeid = jQuery(this).val();
        var thisb = jQuery(this);
        jQuery.ajax({
            type: 'POST',
            url: '<?php echo get_site_url()?>/wp-admin/admin-ajax.php',
            //dataType: 'json',
            data: {
                'action': 'coursepaid',
                'userid': <?php echo $userid ?> ,
                'courseid': <?php echo $courseid ?> ,
                'lectureid': uniqeid
            },
            success: function (data) {
                console.log(data);
                if (data == 'done') {
                    thisb.before("<h1>منتهي</h1>");
                    thisb.remove();
                }
            },
            error: function (data) {
                console.log(data);
            }

        });
    });

    

 
</script>



