<?php
$title = 'Orders';
// require_once('../admin/lib/table-traits.php'); 
// require_once('../admin/lib/basic-function.php'); 
// require_once('../admin/lib/user-functions.php'); 
require_once('header-links.php');

$current_pg = 9;


$purchases = $basicFunctn->selectWhere($basicFunctn->purchase_tb, 'user_id', $user_logged['unique_id']);

// $full_name = explode('++', $user_logged['full_name']);
// $first_name = $full_name[0];
// $last_name = $full_name[1];
// $date_joined = explode(',', $user_logged['created_at']);

// print_r($all_investments);
// die();



?>

<body>
    <!-- BEGIN LOADER -->
    <!-- <div id="load_screen">
        <div class="loader">
            <div class="loader-content">
                <div class="spinner-grow align-self-center"></div>
            </div>
        </div>
    </div> -->
    <!--  END LOADER -->

    <!--  BEGIN NAVBAR  -->
    <?php require_once('header.php'); ?>
    <!--  END NAVBAR  -->

    <!--  BEGIN MAIN CONTAINER  -->
    <div class="main-container" id="container">

        <div class="overlay"></div>
        <div class="search-overlay"></div>

        <!--  BEGIN SIDEBAR  -->
        <?php require_once('sidebar.php'); ?>
        <!--  END SIDEBAR  -->

        <!--  BEGIN CONTENT AREA  -->
        <div id="content" class="main-content">
            <div class="layout-px-spacing">

                <div class="row layout-spacing">

                    <!-- Content -->
                    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 layout-top-spacing">

                        <!-- <div class="user-profile layout-spacing"> -->
                        <div class="widget-content widget-content-area">
                            <div class="table-responsive">
                                <table class="table table-hover  mb-4">
                                    <thead>
                                        <tr>
                                            <th>S/N</th>
                                            <th>Order ID</th>
                                            <th>Includes</th>
                                            <th>Status</th>
                                            <th>Title</th>
                                            <th>Balance</th>
                                            <th>Price</th>
                                            <th>Log</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $i = 1;
                                        foreach ($purchases  as $e) {
                                            $product = ($basicFunctn->selectWhere($basicFunctn->product_tb, 'unique_id', $e['product_id'])[0]);
                                            $subcategory = ($basicFunctn->selectWhere($user->subcategory_tb, 'unique_id', $product['subcategory_id']))[0]; ?>
                                            <tr>
                                                <td><?php print $i; ?></td>
                                                <td><?php print $e['unique_id']; ?></td>
                                                <td class="text-info">
                                                    <?php print $product['includes']; ?>
                                                </td>
                                                <td>

                                                    <div class="relative mrg-top-15">

                                                        <?php if ($e['status'] == 'confirmed') { ?>
                                                            <span class="text-success">
                                                                <?php print $e['status']; ?>
                                                            </span>
                                                        <?php } elseif ($e['status'] == 'pending') { ?>
                                                            <span class="text-warning">
                                                                <?php print $e['status']; ?>
                                                            </span>
                                                        <?php } ?>

                                                    </div>

                                                </td>
                                                <td>
                                                    <?php print $subcategory['name']; ?>
                                                </td>
                                                <td><?php print $product['balance']; ?></td>
                                                <td><?php print $product['price']; ?></td>
                                                <td>
                                                    <?php if($e['status'] == 'confirmed'){?>
                                                    <a href="../redd/to/assets/logs/<?php print $product['file_name'] ?>">
                                                        <button class="btn btn-secondary font-weight-bolder ">Download</button>
                                                    </a>
                                                    <?php } ?>
                                                </td>
                                            </tr>
                                        <?php $i++;
                                        } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <!-- </div> -->

                    </div>

                </div>
            </div>
            <?php require_once('footer.php'); ?>
        </div>
        <!--  END CONTENT AREA  -->
    </div>
    <?php require_once('footer-links.php'); ?>
    <script>
        $('.edit_user_btn').click(async function(e) {
            e.preventDefault();
            let create_purchase_product_id = this.getAttribute("product_id");
            let subcategory_id = this.getAttribute("subcategory_id");
            let data = [{
                name: 'create_purchase_product_id',
                value: create_purchase_product_id
            }]
            let form_data = set_form_data(data);
            // console.log({subcategory_id});return;
            let returned = await ajaxRequest('ajax.php', form_data);
            // console.log(returned);return;
            // throwSweetalert(returned, 'product', 1);
            throwSweetalert(returned, 'product?subcategory=' + subcategory_id, 1);
        });
    </script>