<?php
session_start();
if(!isset($_SESSION['user_id'])){
    header("Location: /beauty_pos/login.php");
    exit();
}
include_once "../config/database.php";

/*
IMPORTANT:
Create this table once in DB:

CREATE TABLE settings (
    id INT PRIMARY KEY AUTO_INCREMENT,
    store_name VARCHAR(150),
    phone VARCHAR(50),
    address TEXT,
    currency VARCHAR(10),
    tax DECIMAL(5,2),
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Insert default row:
INSERT INTO settings (store_name) VALUES ('Glow Beauty Studio');
*/

$success = "";

// Fetch settings
$settingsQ = mysqli_query($conn, "SELECT * FROM settings LIMIT 1");
$data = mysqli_fetch_assoc($settingsQ);

// Save
if($_SERVER['REQUEST_METHOD'] == "POST"){
    $store = mysqli_real_escape_string($conn,$_POST['store_name']);
    $phone = mysqli_real_escape_string($conn,$_POST['phone']);
    $address = mysqli_real_escape_string($conn,$_POST['address']);
    $currency = mysqli_real_escape_string($conn,$_POST['currency']);
    $tax = mysqli_real_escape_string($conn,$_POST['tax']);

    $check = mysqli_query($conn, "SELECT id FROM settings LIMIT 1");

    if(mysqli_num_rows($check) > 0){
        mysqli_query($conn, "
            UPDATE settings SET 
            store_name='$store',
            phone='$phone',
            address='$address',
            currency='$currency',
            tax='$tax'
        ");
    } else {
        mysqli_query($conn, "
            INSERT INTO settings (store_name,phone,address,currency,tax)
            VALUES ('$store','$phone','$address','$currency','$tax')
        ");
    }

    header("Location: index.php?success=1");
    exit();
}

if(isset($_GET['success'])){
    $success = "Settings updated successfully";
}
?>

<?php include_once "../includes/header.php"; ?>
<?php include_once "../includes/sidebar.php"; ?>

<div class="overlay" id="overlay"></div>

<div class="main-content">
<?php include_once "../includes/topbar.php"; ?>

<div class="container-fluid py-4">

    <h4 class="fw-bold mb-4">Settings</h4>

    <?php if($success){ ?>
        <div class="alert alert-success"><?php echo $success; ?></div>
    <?php } ?>

    <div class="row g-4">

        <!-- Store Info -->
        <div class="col-xl-6 col-12">
            <div class="card rounded-4 shadow border-0">
                <div class="card-body">

                    <h6 class="fw-bold mb-3">Store Information</h6>

                    <form method="POST" class="row g-3">

                        <div class="col-12">
                            <label class="form-label">Store Name</label>
                            <input type="text" name="store_name" class="form-control"
                                value="<?php echo $data['store_name'] ?? ''; ?>">
                        </div>

                        <div class="col-12">
                            <label class="form-label">Phone</label>
                            <input type="text" name="phone" class="form-control"
                                value="<?php echo $data['phone'] ?? ''; ?>">
                        </div>

                        <div class="col-12">
                            <label class="form-label">Address</label>
                            <textarea name="address" class="form-control"><?php echo $data['address'] ?? ''; ?></textarea>
                        </div>

                        <div class="col-md-6 col-12">
                            <label class="form-label">Currency Symbol</label>
                            <input type="text" name="currency" class="form-control"
                                value="<?php echo $data['currency'] ?? '₹'; ?>">
                        </div>

                        <div class="col-md-6 col-12">
                            <label class="form-label">Tax Rate (%)</label>
                            <input type="number" step="0.01" name="tax" class="form-control"
                                value="<?php echo $data['tax'] ?? '0'; ?>">
                        </div>

                        <div class="col-12">
                            <button class="btn btn-primary">
                                <i class="fa fa-save"></i> Save Settings
                            </button>
                        </div>

                    </form>

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

        <!-- System -->
        <div class="col-xl-6 col-12">
            <div class="card rounded-4 shadow border-0">
                <div class="card-body">

                    <h6 class="fw-bold mb-3">System</h6>

                    <div class="alert alert-success">
                        <i class="fa fa-check-circle"></i>
                        All data is saved locally on this device
                    </div>

                    <a href="export.php" class="btn btn-outline-secondary w-100 mb-2">
                        <i class="fa fa-download"></i> Export All Data (JSON)
                    </a>

                    <a href="reset.php" class="btn btn-danger w-100"
                       onclick="return confirm('Are you sure to reset all data?')">
                        <i class="fa fa-trash"></i> Reset All Data
                    </a>

                    <hr>

                    <label class="form-label">App Version</label>
                    <input type="text" class="form-control"
                           value="GlowPOS v1.0 — Beauty Store Manager" readonly>

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

    </div>

</div>

<?php include_once "../includes/footer.php"; ?>
</div>