<?php global $wpdb; ?>

<?php


$course_id = isset($_GET['coursid']) ? $_GET['coursid'] : null;

if ($course_id) :
    $subscribers = $wpdb->get_results("SELECT * FROM courses_paid WHERE  coursid= '" . $_GET['coursid'] . "'");
    ?>
    <link rel="stylesheet" type="text/css"
          href="https://cdn.datatables.net/r/dt/jq-2.1.4,jszip-2.5.0,pdfmake-0.1.18,dt-1.10.9,af-2.0.0,b-1.0.3,b-colvis-1.0.3,b-html5-1.0.3,b-print-1.0.3,se-1.0.1/datatables.min.css"/>

    <h2>
        <?php _e('المشتركين فى الدرس', 'a7md') ?>
        -
        <?php echo get_the_title($_GET['coursid']); ?>

        عدد المشتركين
        <?php echo count($subscribers); ?>
    </h2>

    <table id="employee_grid" class="wp-list-table widefat fixed striped pages">
        <thead>
        <tr>
            <td><?php _e('إسم المشترك', 'a7md') ?></td>
            <td><?php _e('الاسم الأول', 'a7md') ?></td>
            <td><?php _e('البريد الإلكتروني', 'a7md') ?></td>
            <td><?php _e('رقم الجوال', 'a7md') ?></td>
            <td><?php _e('التخصص', 'a7md') ?></td>
            <td><?php _e('رقم التصنيف', 'a7md') ?></td>
            <td><?php _e('عضوية  في الجمعية', 'a7md') ?></td>
            <td><?php _e('جهه العمل', 'a7md') ?></td>
            <td><?php _e('سعر الدرس', 'a7md') ?></td>
            <td><?php _e('كوبون الخصم', 'a7md') ?></td>
            <td><?php _e('سعر بعد الخصم', 'a7md') ?></td>
        </tr>
        </thead>
        <tbody id="the-list">
        <?php foreach ($subscribers as $subscriber) : ?>
            <tr class="">
                <td>
                    <?php the_author_meta('nickname', $subscriber->userid); ?>
                </td>
                <td>
                    <?php the_author_meta('first_name', $subscriber->userid); ?>
                </td>
                <td>
                    <?php the_author_meta('email', $subscriber->userid); ?>
                </td>
                <td>
                    <?php the_author_meta('user_mobile', $subscriber->userid); ?>
                </td>
                <td>
                    <?php the_author_meta('user_specialty', $subscriber->userid); ?>
                </td>
                <td>
                    <?php the_author_meta('user_classificationNum', $subscriber->userid); ?>
                </td>
                <td>
                    <?php the_author_meta('user_member_type', $subscriber->userid); ?>
                </td>
                <td>
                    <?php the_author_meta('user_job', $subscriber->userid); ?>
                </td>
                <td>
                    <?php if (get_field('course_discount', $_GET['coursid'])) : ?>
                        <?php the_field('course_discount', $_GET['coursid']) ?>
                    <?php else: ?>
                        <?php the_field('course_price', $_GET['coursid']) ?>
                    <?php endif; ?>
                </td>
                <td>
                    <?php if ($subscriber->coupon) : ?>
                        <?php echo $subscriber->coupon ?>
                    <?php else: ?>
                        لا يوجد كوبون مستخدم
                    <?php endif; ?>
                </td>
                <td>
                    <?php echo $subscriber->price ?>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.js"></script>
    <script type="text/javascript"
            src="https://cdn.datatables.net/r/dt/jq-2.1.4,jszip-2.5.0,pdfmake-0.1.18,dt-1.10.9,af-2.0.0,b-1.0.3,b-colvis-1.0.3,b-html5-1.0.3,b-print-1.0.3,se-1.0.1/datatables.min.js"></script>
    <script type="text/javascript">
        jQuery(document).ready(function () {
            jQuery('#employee_grid').DataTable({
                "processing": true,
                //"sAjaxSource":"datatables/response.php",
                "dom": 'lBfrtip',
                buttons: [
                    {
                        extend: 'excelHtml5',
                        text: 'Export to CSV',
                        title: '<?php echo get_the_title($_GET['coursid']); ?>'
                    }

                ]
            });
        });
    </script>


<?php else: ?>


    <?php $orders = $wpdb->get_results("SELECT * FROM courses_paid");
    if ($orders) : ?>
        <h2><?php _e('الاشتركات', 'a7md') ?></h2>
        <table class="wp-list-table widefat fixed striped pages">
            <thead>
            <tr>
                <td><?php _e('إسم الدرس', 'a7md') ?></td>
                <td><?php _e('عدد المشتركين', 'a7md') ?></td>
            </tr>
            </thead>
            <tbody id="the-list">
            <?php $unique_values_by_key = array_unique(array_column($orders, 'coursid')); ?>

            <?php foreach ($unique_values_by_key as $order) : ?>
                <tr class="">
                    <td>
                        <a href="admin.php?page=subscription&coursid=<?php echo $order; ?>">
                            <?php echo get_the_title($order); ?>
                        </a>
                    </td>
                    <td>
                        <?php
                        global $wpdb;
                        $regs = $wpdb->get_results("SELECT * FROM courses_paid WHERE  coursid= '" . $order . "'");
                        echo count($regs);
                        ?>
                        <?php _e(' مشترك', 'a7md') ?>

                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
<?php endif; ?>
