<?php
session_start();
error_reporting(0);
include("../config/theconfig.php");

include("header.php");

if (!($_SESSION["adminid"])) {
    header("Location:login.php");
    exit();
}



// Deleting customer if the Del parameter is set
if (isset($_REQUEST['Del'])) {
    $custid = $_REQUEST['custid'];
    $query = "DELETE FROM customers WHERE customerid = '$custid'";
    mysqli_query($conn, $query);
    $query = "DELETE FROM accounts WHERE customerid = '$custid'";
    mysqli_query($conn, $query);
    header("Location:viewcustomer.php?suc=1");
    exit(0);
}

// Block or unblock customer based on the action
if (isset($_POST['blockcustomer'])) {
    $custid = $_POST['hide_id'];
    $query = "UPDATE customers SET accstatus='blocked' WHERE customerid = '$custid'";
    mysqli_query($conn, $query);
}

if (isset($_POST['unblockcustomer'])) {
    $custid = $_POST['hide_id'];
    $query = "UPDATE customers SET accstatus='active' WHERE customerid = '$custid'";
    mysqli_query($conn, $query);
}

// Fetch customer details
$custid = $_GET['custid'];
$query = "SELECT * FROM customers WHERE customerid = '$custid'";
$results = mysqli_query($conn, $query);
if ($results) {
    $arrow = mysqli_fetch_array($results, MYSQLI_ASSOC);
    $custname = $arrow['firstname'] . " " . $arrow['lastname'];
    $ifsccode = $arrow['ifsccode'];
    $loginid = $arrow['loginid'];
    $accstatus = $arrow['accstatus'];
    $city = $arrow['city'];
    $state = $arrow['state'];
    $country = $arrow['country'];
    $cot = $arrow['cot'];
    $imf = $arrow['imf'];
    $activationcode = $arrow['activationcode'];
    $taxcode = $arrow['taxcode'];
    $accopendate = $arrow['accopendate'];
    $otp = $arrow['otp'];
    $lastlogin = $arrow['lastlogin'];
    $email = $arrow['email'];
}

// Fetch customer accounts
$queryd = "SELECT * FROM accounts WHERE customerid = '$custid'";
$resultsd = mysqli_query($conn, $queryd);
?>
<!-- Site Content Wrapper -->

<div class="dt-content-wrapper">
    <!-- Site Content -->
    <div class="dt-content">

        <!-- Page Header -->
        <div class="dt-page__header">
            <h1 class="dt-page__title">Customer's Details</h1>
        </div>

        <!-- Grid -->
        <div class="row">

            <div class="col-xl-12">

                <!-- Entry Header -->
                <div class="dt-entry__header">
                    <div class="dt-entry__heading">
                        <h3 class="dt-entry__title">Recent Transfer</h3>
                    </div>
                </div>

                <!-- Card -->
                <div class="dt-card">

                    <!-- Card Body -->
                    <div class="dt-card__body">

                        <!-- Tables -->
                        <div class="table-responsive">

                            <form id="form2" name="form2" method="post" action="">
                                <blockquote>
                                    <table id="data-table" class="table table-striped table-bordered table-hover">
                                        <tr>
                                            <th width="224" height="32" scope="col">CUSTOMER NAME</th>
                                            <td width="285"><?php echo $custname; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>BRANCH</strong></td>
                                            <td><?php echo $ifsccode; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>LOGIN ID</strong></td>
                                            <td><?php echo $loginid; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>ACCOUNT STATUS</strong></td>
                                            <td><?php echo $accstatus; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>CITY</strong></td>
                                            <td><?php echo $city; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>STATE</strong></td>
                                            <td><?php echo $state; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>COUNTRY</strong></td>
                                            <td><?php echo $country; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>OTP</strong></td>
                                            <td><?php echo $otp; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>COT</strong></td>
                                            <td><?php echo $cot; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>IMF Code</strong></td>
                                            <td><?php echo $imf; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Activation Code</strong></td>
                                            <td><?php echo $activationcode; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Tax Code</strong></td>
                                            <td><?php echo $taxcode; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>Account PIN</strong></td>
                                            <td><?php echo $otp; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>ACCOUNT OPEN DATE</strong></td>
                                            <td><?php echo $accopendate; ?></td>
                                        </tr>
                                        <tr>
                                            <td><strong>LAST LOGIN</strong></td>
                                            <td><?php echo $lastlogin; ?></td>
                                        </tr>
                                    </table>
                                </blockquote>

                                <br/><br/><br/>

                                <table id="data-table" class="table table-striped table-bordered table-hover">
                                    <tr>
                                        <th colspan="5"><strong>CUSTOMER ACCOUNTS</strong></th>
                                    </tr>
                                    <tr>
                                        <th width="100"><strong>ACCOUNT NO</strong></th>
                                        <th width="75"><strong>STATUS</strong></th>
                                        <th width="90"><strong>OPEN DATE</strong></th>
                                        <th width="90"><strong>ACCOUNT TYPE</strong></th>
                                        <th width="85"><strong>BALANCE</strong></th>
                                    </tr>

                                    <?php
                                    while ($arrow = mysqli_fetch_array($resultsd, MYSQLI_ASSOC)) {
                                        ?>
                                        <tr>
                                            <td><?php echo $arrow['accno']; ?></td>
                                            <td><?php echo $arrow['accstatus']; ?></td>
                                            <td><?php echo $arrow['accopendate']; ?></td>
                                            <td><?php echo $arrow['accounttype']; ?></td>
                                            <td><?php echo number_format($arrow['accountbalance'], 2); ?></td>
                                        </tr>
                                    <?php
                                    }
                                    ?>
                                </table>

                                <input type="hidden" value="<?php echo $custid ?>" name="custid">

                                <p>&nbsp;</p>

                                <tr>
                                    <td colspan="2">
                                        <div align="right">
                                            <input type="submit" class="btn btn-danger" value="Delete Customer" name="Del" style="display:inline-block;float:left;margin-left:10px">
                                        </div>
                                    </td>
                                </tr>
                            </form>

                            <a href="editaccount.php?custid=<?php echo $custid ?>"><button class="btn btn-primary" style="display:inline-block;float:left;margin-left:10px">Edit Account</button></a>

                            <form method="POST" action="">
                                <input type="hidden" value="<?php echo $custid ?>" name="hide_id">
                                <?php
                                if ($accstatus != 'blocked') {
                                    echo '<button class="btn btn-warning" style="display:inline-block;float:left;margin-left:10px" type="submit" name="blockcustomer">Unapprove Account</button>';
                                } else {
                                    echo '<button class="btn btn-success" style="display:inline-block;float:left;margin-left:10px" type="submit" name="unblockcustomer">Approve Account</button>';
                                }
                                ?>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

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

<?php include 'footer.php'; ?>

<script src="../assets/node_modules/datatables.net/js/jquery.dataTables.js"></script>
<script src="../assets/node_modules/datatables.net-bs4/js/dataTables.bootstrap4.js"></script>
<script src="../assets/js/custom/data-table.js"></script>

<?php
// Close MySQL connection
mysqli_close($conn);
?>
