<?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 {
    if (isset($_GET['edit'])) {
        $editid = intval($_GET['edit']);    
    }

    $msg = "";

    // Check if the form for updating delivery text is submitted
    if (isset($_POST['submit'])) {
        $deliveryText = $_POST['userid_text'];
        date_default_timezone_set('Asia/Dhaka');
        $updateDate = date('Y-m-d H:i:s');

        // Update delivery text and status (Approved)
        $sql = "UPDATE userid_sub SET userid_text = :userid_text, fileup_at = :update_date, status = 2 WHERE id = :idedit";
        $query = $dbh->prepare($sql);
        $query->bindParam(':userid_text', $deliveryText, PDO::PARAM_STR);
        $query->bindParam(':update_date', $updateDate, PDO::PARAM_STR);
        $query->bindParam(':idedit', $editid, PDO::PARAM_INT);
        $query->execute();

        // Success message for delivery text update
        $msg = "Delivery Text Updated Successfully";

        // Add success alert and redirect
        echo "<script>alert('Delivery Text Updated Successfully!'); window.location.href='userid_pending.php?show_all=1';</script>";
        exit();
    }

    // Check if the "Received" button was clicked
    if (isset($_POST['received'])) {
        $newStatus = 4; // "Received" status
        $sqlUpdateStatus = "UPDATE userid_sub SET status = :status WHERE id = :idedit";
        $updateStatusQuery = $dbh->prepare($sqlUpdateStatus);
        $updateStatusQuery->bindParam(':status', $newStatus, PDO::PARAM_INT);
        $updateStatusQuery->bindParam(':idedit', $editid, PDO::PARAM_INT);
        $updateStatusQuery->execute();

        // Success message for received status update
        $msg = "Status updated to Received successfully!";

        // Add success alert and redirect
        echo "<script>alert('Status updated to Received successfully!'); window.location.href='userid_pending.php?show_all=1';</script>";
        exit();
    }

    // Fetch data to display in the form
    $sql = "SELECT a.*, b.name as Sub_name FROM userid_sub a JOIN users b ON Sub_by = email WHERE a.id = :editid";
    $query = $dbh->prepare($sql);
    $query->bindParam(':editid', $editid, PDO::PARAM_INT);
    $query->execute();
    $result = $query->fetch(PDO::FETCH_OBJ);
?>
<!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">
    <title>Update Delivery Text</title>

    <link rel="stylesheet" href="css/font-awesome.min.css">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/style.css">

    <style>
        .errorWrap {
            padding: 10px;
            margin: 0 0 20px 0;
            background: #dd3d36;
            color: #fff;
            box-shadow: 0 1px 1px 0 rgba(0,0,0,.1);
        }
        .succWrap {
            padding: 10px;
            margin: 0 0 20px 0;
            background: #5cb85c;
            color: #fff;
            box-shadow: 0 1px 1px 0 rgba(0,0,0,.1);
        }
    </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">
                    <h3 class="page-title">Edit User : <?php echo htmlentities($result->name); ?></h3>
                    <div class="panel panel-default">
                        <div class="panel-heading">Edit Info</div>
                        <?php if ($msg) { ?>
                            <div class="succWrap"><strong>SUCCESS</strong>: <?php echo htmlentities($msg); ?></div>
                        <?php } ?>
                        <div class="panel-body">
                            <form method="post" class="form-horizontal">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">Req Type</label>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control" value="<?php echo htmlentities($result->sub_type); ?>" readonly>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">Data</label>
                                    <div class="col-sm-4">
                                        <input type="text" class="form-control" value="<?php echo htmlentities($result->nfvb); ?>" readonly>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-8 col-sm-offset-2">
                                        <button class="btn btn-success" name="received" type="submit">Mark as Received</button>
                                </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">Delivery Text<span style="color:red">*</span></label>
                                    <div class="col-sm-8">
                                        <input type="text" name="userid_text" class="form-control" placeholder="Enter delivery text">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-8 col-sm-offset-2">
                                        <button class="btn btn-primary" name="submit" type="submit">Update Delivery</button>
                                         </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
    $(document).ready(function() {
        setTimeout(function() {
            $('.succWrap').slideUp("slow");
        }, 3000);
    });
</script>
</body>
</html>
<?php } ?>
