<?php

    session_start();

    include "../db.php";

    date_default_timezone_set('Asia/Kolkata');

    $currentDate = date('M j, Y');

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



    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {

        header("Location: logout.php");

        exit();

    }

    if ( $_SESSION['utype'] != 'master_admin' && $_SESSION['uref'] != 'fssdf4543frts' ) {

        header("Location: logout.php");

        exit();

    }

    if (empty($_GET['uref_id'])) {

        header("Location: view-ref-users.php");

    }else{

        $p_sql = "SELECT * FROM users WHERE uref_code = '$_GET[uref_id]'";

        $p_res = mysqli_query($con,$p_sql);

        $p_row = mysqli_fetch_assoc($p_res);

    }



?>

<!DOCTYPE html>

<html lang="en">

<head>

 <?php include "includes/meta.php"; ?>        

    <?php include "includes/css.php"; ?>      

  



</head>

<body id="page-top" class="mt-55">



    <div style="margin-top: 70px;"></div>



      <?php include "includes/header2.php"; ?>



<div cl ass="container-fluid ">       

   <div class="row mx-0 justify-content-center mt-2">

        <div class="col-12 col-sm-8 col-md-8">

            <h1 class="h3 text-gray-800 text-center"><?php echo $p_row['uname']."'s"; ?> Payment  Details</h1>

                    <hr>

        <?php

            $pd_sql = "SELECT * FROM ref_payment WHERE rp_holder_ref = '$_GET[uref_id]' ORDER BY rp_id DESC";

            $pd_res = mysqli_query($con,$pd_sql);

            if (mysqli_num_rows($pd_res)>0) {

               

            ?>



            <div class="mx-0 justify-content-center gx-1 rd" style="padding-top: 10px;padding-bottom: 10px;">

            <?php  

                $total_send=0;

                $total_receive=0;

                while ($pd_rows= mysqli_fetch_assoc($pd_res)) { 

                        if ($pd_rows['rp_amount_send'] !=0 ) {

                            $total_send+=$pd_rows['rp_amount_send'];

                            $amount= $pd_rows['rp_amount_send'].'  (-)';

                        }

                        if ($pd_rows['rp_amount_receive'] !=0 ) {

                            $total_receive+=$pd_rows['rp_amount_receive'];

                            $amount= $pd_rows['rp_amount_receive'].'  (+)';

                        }

                ?>

                <div class="row">

                    <div class="col-md-4 col-xs-4 text-center"> <?php echo $pd_rows['rp_date']; ?>    

                    </div>

                    <div class="col-md-4 col-xs-4 tex t-center"><i class="fa fa-inr" aria-hidden="true"></i> <?php echo $amount; ?>          

                    </div>

                    <div class="col-md-4 col-xs-4 text-center"><?php echo $pd_rows['rp_paymethod']; ?>         

                    </div>

                </div>

            <?php } ?>

                <hr>

                <div class="row">

                    <div class="col-md-6 col-xs-6 text-center">Total Send : <i class="fa fa-inr" aria-hidden="true"></i>  <?php echo $total_send; ?>    

                    </div>

                    <div class="col-md-6 col-xs-6 text-center">Total Profit : <i class="fa fa-inr" aria-hidden="true"></i> <?php echo $total_profit= $total_receive - $total_send; ?>          

                    </div>

                </div>

                    

                    

            

            </div>





            <?php

           

            }     

        ?>



            <!--Payment Insert -->

        <div class="card shadow mt-3">

            <h5 class="card-header text-dark  text-center font-weight-bold"> Send Payment <span id="showUserNameDetails"></span></h5>

                <div class="card-body">

                    <form method="POST">   

                        <div class="row" id="Show_InsertUserName">

                            <div class="col-md-6 mt-2">

                                <label class="badge badge-warning">Send Amount</label>

                                <input type="number" class="form-control" name="amount" required>

                            </div>

                            <div class="col-md-6 mt-2">

                                <label class="badge badge-warning">Pay Method</label>

                                <select class="form-control" name="method" required>

                                    <option disabled selected>Select A Option</option>

                                    <option value="Cash">Cash</option>

                                    <option value="Net Banking">Net Banking</option>

                                    <option value="Google Pay">Google Pay</option>

                                    <option value="PhonePe">PhonePe</option>

                                </select>

                            </div>

                            <div class="col-md-12 mt-2">

                                <label class="badge badge-warning">Transaction ID</label>

                                <input type="text" class="form-control" name="TransactionID" required>

                            </div>

                        </div>



                            <div class=" row justify-content-center">

                                <button type="submit" class="btn btn-success text-sm-center mt-4" name="submit_send_payment">

                                     Submit

                                </button>

                            </div>

                    </form>

                </div>

            </div>



            <div class="card shadow mt-3">

            <h5 class="card-header text-dark  text-center font-weight-bold"> Received Payment <span id="showUserNameDetails"></span></h5>

                <div class="card-body">

                    <form method="POST">   

                        <div class="row" id="Show_InsertUserName">

                            <div class="col-md-6 mt-2">

                                <label class="badge badge-warning">Received Amount</label>

                                <input type="number" class="form-control" name="amount" required>

                            </div>

                            <div class="col-md-6 mt-2">

                                <label class="badge badge-warning">Pay Method</label>

                                <select class="form-control" name="method" required>

                                    <option disabled selected>Select A Option</option>

                                    <option value="Cash">Cash</option>

                                    <option value="Net Banking">Net Banking</option>

                                    <option value="Google Pay">Google Pay</option>

                                    <option value="PhonePe">PhonePe</option>

                                </select>

                            </div>

                            <div class="col-md-12 mt-2">

                                <label class="badge badge-warning">Transaction ID</label>

                                <input type="text" class="form-control" name="TransactionID" required>

                            </div>

                        </div>



                            <div class=" row justify-content-center">

                                <button type="submit" class="btn btn-success text-sm-center mt-4" name="submit_received_payment">

                                     Submit

                                </button>

                            </div>

                    </form>

                </div>

            </div>







        </div>

     </div>

</div> 



            <?php include "includes/ft.php"; ?>

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

</body>

</html>





<?php

    if(isset($_POST['submit_send_payment'])){



        $amount = mysqli_real_escape_string($con, strip_tags($_POST['amount']));

        $method = mysqli_real_escape_string($con, strip_tags($_POST['method']));

        $TransactionID = mysqli_real_escape_string($con, strip_tags($_POST['TransactionID']));



        $ins_sql = "INSERT INTO ref_payment (rp_holder_ref,rp_amount_send,   rp_paymethod,rp_transaction_id,rp_date,rp_date2) VALUES ('$_GET[uref_id]','$amount','$method','$TransactionID','$currentDate','$currentDate2')";



        if(mysqli_query($con, $ins_sql)){

            ?>

            <script>

                alert('Submit Successful');

                window.location = "add-admin-payment.php?uref_id=<?php echo $_GET['uref_id']; ?>";

            </script>

            <?php

        }

        else{

            ?>

            <script>

                alert('OOPS !!!!');

                setTimeout(() => {

                    window.location = "add-admin-payment.php?uref_id=<?php echo $_GET['uref_id']; ?>";

                }, 500);

            </script>

            <?php

        }

    }



    if(isset($_POST['submit_received_payment'])){



        $amount = mysqli_real_escape_string($con, strip_tags($_POST['amount']));

        $method = mysqli_real_escape_string($con, strip_tags($_POST['method']));

        $TransactionID = mysqli_real_escape_string($con, strip_tags($_POST['TransactionID']));



        $ins_sql = "INSERT INTO ref_payment (rp_holder_ref, rp_amount_receive,   rp_paymethod,rp_transaction_id,rp_date,rp_date2) VALUES ('$_GET[uref_id]','$amount','$method','$TransactionID','$currentDate','$currentDate2')";



        if(mysqli_query($con, $ins_sql)){

            ?>

            <script>

                alert('Submit Successful');

                window.location = "add-admin-payment.php?uref_id=<?php echo $_GET['uref_id']; ?>";

            </script>

            <?php

        }

        else{

            ?>

            <script>

                alert('OOPS !!!!');

                setTimeout(() => {

                    window.location = "add-admin-payment.php?uref_id=<?php echo $_GET['uref_id']; ?>";

                }, 500);

            </script>

            <?php

        }

    }

?>







<?php 







