<?php
session_start();
error_reporting(0);
include('includes/config.php');
include('includes/db.php');

if (strlen($_SESSION['adminlogin']) == 0) {
    header('location:index.php');
    die();
} else {
    $signConSql = "select * from sign_control where id=1 limit 1";
    $signCon_result = mysqli_query($link, $signConSql);
    if (mysqli_num_rows($signCon_result) > 0) {
        $signCon_row = mysqli_fetch_array($signCon_result);
    } else {
        $signCon_row = [];
    }

    // Handle confirm request
    if (isset($_REQUEST['confirm'])) {
        $aeid = intval($_GET['confirm']);
        $getuserSql = "select b.* from text_sub a join users b on a.Sub_by = b.email where a.id = $aeid";
        $user_result = mysqli_query($link, $getuserSql);
        if (mysqli_num_rows($user_result) > 0) {
            $user_row = mysqli_fetch_array($user_result);
        }
        $user_bal = $user_row['balance'];

        // Get cost
        $select = "SELECT * FROM text_control";
        $g = mysqli_query($link, $select);
        $data = mysqli_fetch_assoc($g);
        $ssprice = $data['cc_location_price'];

        $update_balance = $user_row['balance'] + $ssprice;
        $user_id = $user_row['id'];

        $query = "UPDATE users SET balance = $update_balance WHERE id = $user_id";
        mysqli_query($link, $query);

        $comment = ($_GET['comment']);
        $memstatus = 3;
        $sql = "UPDATE text_sub SET delivery_text=:delivery_text, status=:status WHERE id=:aeid";
        $query = $dbh->prepare($sql);
        $query->bindParam(':delivery_text', $comment, PDO::PARAM_STR);
        $query->bindParam(':status', $memstatus, PDO::PARAM_INT);
        $query->bindParam(':aeid', $aeid, PDO::PARAM_INT);
        $query->execute();
        $msg = "Changes Successfully";
        echo '<script type="text/javascript">
                setTimeout(function() {
                    window.location.href = "text-pending.php";
                }, 1000);
              </script>';
    }
?>
<!doctype html>
<html lang="en" class="no-js">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <meta name="theme-color" content="#3e454c">
    <title>Pending</title>
    <script src="https://code.jquery.com/jquery-3.6.4.slim.min.js"></script>
    <!-- Font awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" />
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="//cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css">
    <script src="//cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
    <link rel="stylesheet" href="css/style.css">
    <style>
        .errorWrap { padding: 10px; margin: 0 0 20px 0; background: #dd3d36; color: #fff; }
        .succWrap { padding: 10px; margin: 0 0 20px 0; background: #5cb85c; color: #fff; }
        .top-left-btn {
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
<?php include('includes/header.php'); ?>
<div class="ts-main-content">
    <?php include('includes/leftbar.php'); ?>
    <div class="content-wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <h2 class="page-title">Pending List</h2>
                    <div class="top-left-btn">
                        <a href="?show_all=1" class="btn btn-primary">Show All Orders</a>
                        <a href="text-pending.php" class="btn btn-danger">Show Pending Orders</a>
                    </div>
                    <div class="panel panel-default">
                        <div class="panel-heading">Order List</div>
                        <div class="panel-body">
                            <?php if ($error) { ?>
                                <div class="errorWrap" id="msgshow"><?php echo htmlentities($error); ?></div>
                            <?php } else if ($msg) { ?>
                                <div class="succWrap" id="msgshow"><?php echo htmlentities($msg); ?></div>
                            <?php } ?>
                            <table id="myTable" class="display table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                                <thead>
                                <tr>
                                    <th>#</th>
                                    <th>Req Type</th>
                                    <th>Name</th>
                                    <th>NID/BRN/Form/Voter</th>
                                    <th>Phone Number</th>
                                    <th>Father NID</th>
                                    <th>Mother NID</th>
                                    <th>Submitted By</th>
                                    <th>Sub At</th>
                                    <th>Upload</th>
                                    <th>Cancel</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php
                                // Check if "show_all" parameter is set
                                $show_all = isset($_GET['show_all']) && $_GET['show_all'] == 1;
                                $sql = $show_all
                                    ? "SELECT a.*, b.name as Sub_name FROM text_sub a JOIN users b ON Sub_by = email"
                                    : "SELECT a.*, b.name as Sub_name FROM text_sub a JOIN users b ON Sub_by = email WHERE a.status = 1";
                                $query = $dbh->prepare($sql);
                                $query->execute();
                                $results = $query->fetchAll(PDO::FETCH_OBJ);
                                $cnt = 1;
                                if ($query->rowCount() > 0) {
                                    foreach ($results as $result) { ?>
                                        <tr>
                                            <td><?php echo htmlentities($cnt); ?></td>
                                            <td><?php echo htmlentities($result->sub_type); ?></td>
                                            <td><?php echo htmlentities($result->Name); ?></td>
                                            <td><?php echo htmlentities($result->nfvb); ?></td>
                                            <td><?php
                                                if ($result->status == 1) {
                                                    echo "<p class='btn btn-warning'>Processing</p>";
                                                } elseif ($result->status == 0) {
                                                    echo "<p class='btn btn-danger'>Deleted</p>";
                                                } elseif ($result->status == 2) {
                                                    echo "<p class='btn btn-success'>Delivered</p>";
                                                } elseif ($result->status == 4) {
                                                    echo "<p class='btn btn-primary'>Received</p>";
                                                } elseif ($result->status == 3) {
                                                    echo "<p class='btn btn-danger'>Not Found</p>";
                                                } ?>
                                            </td>
                                            <td><?php echo htmlentities($result->Father_nid); ?></td>
                                            <td><?php echo htmlentities($result->Mother_nid); ?></td>
                                            <td><?php echo htmlentities($result->Sub_name); ?></td>
                                            <td><?php echo htmlentities($result->sub_at); ?></td>
                                            <td>
                                                <a href="text_edit.php?edit=<?php echo $result->id; ?>">&nbsp; <i class="fa-solid fa-circle-check text-success"></i></a>&nbsp;&nbsp;
                                            </td>
                                            <td>
                                                <a href="#" onclick="confirmAndModifyLink(<?php echo htmlentities($result->id); ?>)"><i class="fa-solid fa-circle-xmark text-danger"></i></a>
                                            </td>
                                        </tr>
                                        <?php $cnt = $cnt + 1;
                                    }
                                } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Loading Scripts -->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/main.js"></script>
<script type="text/javascript">
    let table = new DataTable('#myTable');

    $(document).ready(function() {
        setTimeout(function() {
            $('.succWrap').slideUp("slow");
        }, 3000);
    });

    function confirmAndModifyLink(id) {
        if (confirm('Do you really want to Cancel the Account')) {
            var inputText = prompt('Reason Of Cancel:');
            if (inputText !== null) {
                var newHref = 'text-pending.php?comment=' + encodeURIComponent(inputText) + '&confirm=' + id;
                window.location.href = newHref;
            }
        }
    }
</script>
</body>
</html>
<?php } ?>
