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

$vendorId = $_SESSION['vendor_id'];

// Handle profile update
if (isset($_POST['update_profile'])) {
    $storeName = db_prepare($con, $_POST['store_name']);
    $phone = db_prepare($con, $_POST['business_phone']);
    $address = db_prepare($con, $_POST['business_address']);
    $bankName = db_prepare($con, $_POST['bank_name']);
    $bankAccountName = db_prepare($con, $_POST['bank_account_name']);
    $bankAccountNumber = db_prepare($con, $_POST['bank_account_number']);

    // Update vendor table
    $query = "UPDATE vendors SET 
              store_name='$storeName', 
              business_phone='$phone', 
              business_address='$address',
              bank_name='$bankName',
              bank_account_name='$bankAccountName',
              bank_account_number='$bankAccountNumber'
              WHERE id='$vendorId'";

    if (mysqli_query($con, $query)) {
        log_activity("Updated Store Profile", "Store Name: $storeName", "vendor");
        $_SESSION['msg'] = "Store profile updated successfully!";
        // Refresh session data if needed or reload page
        echo "<script>window.location.href='profile.php'</script>";
        exit;
    } else {
        $_SESSION['error'] = "Error updating profile: " . mysqli_error($con);
    }
}

// Fetch latest data
$vendorQuery = mysqli_query($con, "SELECT * FROM vendors WHERE id='$vendorId'");
$vendor = mysqli_fetch_array($vendorQuery);
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>Store Profile - Vendor Portal</title>
    <link href="../../__admin__/css/styles.css" rel="stylesheet" />
    <link href="../../__admin__/css/admin-professional.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"></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>
                            <h1><i class="fas fa-store me-2"></i> Store Profile</h1>
                            <p>Manage your public store information and banking details</p>
                        </div>
                    </div>

                    <?php if (isset($_SESSION['msg'])): ?>
                        <div class="alert alert-success alert-dismissible fade show">
                            <i class="fas fa-check-circle me-2"></i>
                            <?php echo $_SESSION['msg'];
                            unset($_SESSION['msg']); ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>

                    <div class="row">
                        <!-- Public Profile -->
                        <div class="col-lg-6">
                            <div class="table-card mb-4">
                                <div class="card-header">
                                    <h5 class="mb-0">Public Store Information</h5>
                                </div>
                                <div class="card-body">
                                    <form method="post">
                                        <div class="mb-3">
                                            <label class="form-label">Store Name</label>
                                            <input type="text" name="store_name" class="form-control"
                                                value="<?php echo htmlentities($vendor['store_name']); ?>" 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($vendor['business_phone']); ?>" required>
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Business Address</label>
                                            <textarea name="business_address" class="form-control"
                                                rows="3"><?php echo htmlentities($vendor['business_address']); ?></textarea>
                                        </div>

                                        <hr>
                                        <h5 class="mb-3">Banking Details (For Payouts)</h5>

                                        <div class="mb-3">
                                            <label class="form-label">Bank Name / Mobile Money Provider</label>
                                            <input type="text" name="bank_name" class="form-control"
                                                value="<?php echo htmlentities($vendor['bank_name']); ?>"
                                                placeholder="e.g. EcoBank or MTN Mobile Money">
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Account Name</label>
                                            <input type="text" name="bank_account_name" class="form-control"
                                                value="<?php echo htmlentities($vendor['bank_account_name']); ?>">
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Account Number</label>
                                            <input type="text" name="bank_account_number" class="form-control"
                                                value="<?php echo htmlentities($vendor['bank_account_number']); ?>">
                                        </div>

                                        <button type="submit" name="update_profile" class="btn btn-primary">
                                            <i class="fas fa-save me-2"></i>Save Changes
                                        </button>
                                    </form>
                                </div>
                            </div>
                        </div>

                        <!-- Read-only Info -->
                        <div class="col-lg-6">
                            <div class="table-card bg-light">
                                <div class="card-header">
                                    <h5 class="mb-0">System Information</h5>
                                </div>
                                <div class="card-body">
                                    <table class="table table-borderless">
                                        <tr>
                                            <th>Store ID:</th>
                                            <td>#
                                                <?php echo $vendor['id']; ?>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>Status:</th>
                                            <td><span class="badge bg-success">
                                                    <?php echo ucfirst($vendor['status']); ?>
                                                </span></td>
                                        </tr>
                                        <tr>
                                            <th>Commission Rate:</th>
                                            <td>
                                                <?php echo $vendor['commission_rate']; ?>%
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>Joining Date:</th>
                                            <td>
                                                <?php echo date('F d, Y', strtotime($vendor['created_at'])); ?>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>Approval Date:</th>
                                            <td>
                                                <?php echo date('F d, Y', strtotime($vendor['approved_at'])); ?>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
            <?php include_once('../includes/footer.php'); ?>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="../../__admin__/js/scripts.js"></script>
</body>

</html>