<?php
    // session_start();
    include "../db.php";
    require '../auth.php';
    restrictAccess(['Sub Associate']);
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('M j, Y g:i a');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {
        header("Location: logout.php");
        exit();
    }
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "include/meta.php"; ?>
    <?php include "include/css.php"; ?>
    <?php include "include/php-functions.php"; ?>
</head>
<style>
#ck-img-preview img {
    max-width: 100%;
    height: auto;
}
</style>

<body>
    <!--  Header -->
    <?php 
    include "include/header.php"; 
    ?>
    <!-- #header -->

    <section id="inner-body">
        <div class="body-content">
            <div class="container-fluid">
                <?php if(isset($_GET["view_id"])){
                    $view_id = $_GET["view_id"];
                    $view_run = mysqli_query($con, "SELECT * FROM payment_history WHERE ph_recycle = '1' AND phid = '$_GET[view_id]'");
                    
                    if($view_row = mysqli_fetch_assoc($view_run)){
                    ?>
                <div class="card mt-3">
                    <h5 class="card-header text-dark  text-center font-weight-bold">
                        Detailed Payout of <?php echo $view_row["ph_upDate"];?></h5>
                    <div class="card-body">
                        <div class="notice-image">
                            <img src="<?php echo $view_row["ph_doc"];?>" class="img-fluid" alt="">
                        </div>
                        <div class="notice-preiew">
                            <p><strong>Payout Amount : </strong><?php echo $view_row["ph_amount"];?></p>
                            <div id="ck-img-preview"><strong>Payout Details :
                                </strong><?php echo $view_row["ph_des"];?>
                            </div>
                            <p><strong>Payout Date : </strong><?php echo $view_row["ph_upDate"];?></p>
                        </div>

                        <a href="payout-history.php" class="btn btn-warning"> <i class="fa fa-arrow-left"></i>
                            Back
                        </a>
                    </div>
                </div>
                <?php }
                }  else{?>
                <div class="card mt-3 mb-3">
                    <div class="card-header">
                        <div class="row align-items-center">
                            <h4 class="text-gray-800">Payment History</h4>
                        </div>
                    </div>
                    <div class="card-body">
                        <ul class="mt-3" style="list-style:none;padding:0;">
                            <?php
                                $payment_sql = "SELECT * FROM payment_history WHERE ph_for = '{$_SESSION["uid"]}' AND ph_recycle = '1' ORDER BY phid DESC";
                                $payment_run = mysqli_query($con, $payment_sql);

                                if ($payment_run) {
                                    if (mysqli_num_rows($payment_run) > 0) {
                                        while ($payment_rows = mysqli_fetch_assoc($payment_run)) {
                            ?>
                            <li class="mb-2">
                                <a href="payout-history.php?view_id=<?php echo $payment_rows['phid']; ?>"
                                    style="text-decoration:none;">
                                    <div class="blog-preview">
                                        <h4 class="m-0">
                                            <i class="fa fa-inr"></i> <?php echo $payment_rows['ph_amount']; ?>
                                        </h4>
                                        <p class="m-0">
                                            <i class="fa fa-calendar"></i> <?php echo $payment_rows['ph_upDate']; ?>
                                        </p>
                                        <p class="m-0">
                                            <?php echo $payment_rows['ph_des']; ?>
                                        </p>
                                    </div>
                                </a>
                            </li>
                            <?php
                                        }
                                    } else {
                                        echo "<li>No payments Yet.</li>";
                                    }
                                } else {
                                    echo "<li>Error fetching payments.</li>";
                                }
                            ?>
                        </ul>
                    </div>
                </div>
                <?php }?>
            </div>
        </div>

        <?php include "include/footer.php"; ?>
    </section>

    <!-- JavaScript Libraries -->

    <?php include "include/js.php"; ?>

</body>

</html>