<?php
session_start();
require_once('../includes/admin-security.php');
include_once('includes/config.php');

$id = intval($_GET['id']);

if (isset($_POST['submit'])) {
    $store_name = mysqli_real_escape_string($con, $_POST['store_name']);
    $business_email = mysqli_real_escape_string($con, $_POST['business_email']);
    $login_email = mysqli_real_escape_string($con, $_POST['login_email']);
    $business_phone = mysqli_real_escape_string($con, $_POST['business_phone']);
    $address = mysqli_real_escape_string($con, $_POST['business_address']);
    $status = mysqli_real_escape_string($con, $_POST['status']);

    // Get User ID for password reset
    $vQuery = mysqli_query($con, "SELECT user_id FROM vendors WHERE id='$id'");
    $vRow = mysqli_fetch_array($vQuery);
    $uid = $vRow['user_id'];

    // Update login email in users table
    mysqli_query($con, "UPDATE users SET email='$login_email' WHERE id='$uid'");

    if (!empty($_POST['new_password'])) {
        $password = md5($_POST['new_password']);
        mysqli_query($con, "UPDATE users SET password='$password' WHERE id='$uid'");
        $_SESSION['msg'] = "Vendor updated successfully (Password Changed)";
    } else {
        $_SESSION['msg'] = "Vendor updated successfully";
    }

    $updateVendor = mysqli_query($con, "UPDATE vendors SET store_name='$store_name', business_email='$business_email', business_phone='$business_phone', business_address='$address', status='$status' WHERE id='$id'");

    if ($updateVendor) {
        // Msg set above
    } else {
        $_SESSION['error'] = "Error updating vendor";
    }
}

$query = mysqli_query($con, "SELECT v.*, u.email as login_email FROM vendors v JOIN users u ON v.user_id = u.id WHERE v.id='$id'");
$row = mysqli_fetch_array($query);
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>Edit Vendor | Admin</title>
    <link href="css/styles.css" rel="stylesheet" />
    <link href="css/admin-professional.css" rel="stylesheet" />
    <script src="js/all.min.js" crossorigin="anonymous"></script>
</head>

<body class="sb-nav-fixed">
    <?php include_once('includes/header.php'); ?>
    <div id="layoutSidenav">
        <?php include_once('includes/sidebar.php'); ?>
        <div id="layoutSidenav_content">
            <main>
                <div class="container-fluid px-4">
                    <div class="page-header mt-4">
                        <div class="d-flex justify-content-between align-items-center">
                            <h1><i class="fas fa-store-alt me-2"></i>Edit Vendor</h1>
                            <a href="all-vendors.php" class="btn btn-secondary">Back</a>
                        </div>
                    </div>

                    <?php if (isset($_SESSION['msg'])) { ?>
                        <div class="alert alert-success alert-dismissible fade show">
                            <?php echo $_SESSION['msg'];
                            unset($_SESSION['msg']); ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php } ?>

                    <div class="card mb-4">
                        <div class="card-header">Vendor Details</div>
                        <div class="card-body">
                            <form method="post">
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">Store Name</label>
                                        <input type="text" name="store_name" class="form-control"
                                            value="<?php echo htmlentities($row['store_name']); ?>" required>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">Status</label>
                                        <select name="status" class="form-control">
                                            <option value="active" <?php if ($row['status'] == 'active')
                                                echo 'selected'; ?>>Active</option>
                                            <option value="suspended" <?php if ($row['status'] == 'suspended')
                                                echo 'selected'; ?>>Suspended</option>
                                            <option value="pending" <?php if ($row['status'] == 'pending')
                                                echo 'selected'; ?>>Pending</option>
                                            <option value="rejected" <?php if ($row['status'] == 'rejected')
                                                echo 'selected'; ?>>Rejected</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">User Login Email</label>
                                    <input type="email" name="login_email" class="form-control"
                                        value="<?php echo htmlentities($row['login_email']); ?>" required>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Business Email</label>
                                    <input type="email" name="business_email" class="form-control"
                                        value="<?php echo htmlentities($row['business_email']); ?>" required>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Business Phone</label>
                                    <input type="text" name="business_phone" class="form-control"
                                        value="<?php echo htmlentities($row['business_phone']); ?>">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Business Address</label>
                                    <textarea name="business_address" class="form-control"
                                        rows="3"><?php echo htmlentities($row['business_address']); ?></textarea>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Reset Password (Optional)</label>
                                    <input type="password" name="new_password" class="form-control"
                                        placeholder="Leave blank to keep current password">
                                </div>
                                <button type="submit" name="submit" class="btn btn-primary">Update Vendor</button>
                            </form>
                        </div>
                    </div>
                </div>
            </main>
            <?php include_once('includes/footer.php'); ?>
        </div>
    </div>
    <script src="js/bootstrap.bundle.min.js"></script>
    <script src="js/scripts.js"></script>
</body>

</html>