<?php
session_start();

require_once '../../config/database.php';
require_once '../../includes/auth.php';

$db = (new Database())->connect();

$basePath = '../../';
include '../../includes/header.php';

$stmt = $db->prepare("
    SELECT *
    FROM settings
    LIMIT 1
");
$stmt->execute();

$settings = $stmt->fetch(PDO::FETCH_ASSOC) ?: [];
?>

<div class="container mt-4">

    <div class="d-flex justify-content-between align-items-center mb-3">
        <h3>Business Settings</h3>
    </div>

    <div class="card shadow-sm">
        <div class="card-body">

            <form action="save.php" method="POST">

                <div class="row mb-3">

                    <div class="col-md-6">
                        <label class="form-label">Business Name</label>
                        <input
                            type="text"
                            name="business_name"
                            class="form-control"
                            value="<?= htmlspecialchars($settings['business_name'] ?? '') ?>"
                            required
                        >
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Phone</label>
                        <input
                            type="text"
                            name="phone"
                            class="form-control"
                            value="<?= htmlspecialchars($settings['phone'] ?? '') ?>"
                        >
                    </div>

                </div>

                <div class="row mb-3">

                    <div class="col-md-6">
                        <label class="form-label">Email</label>
                        <input
                            type="email"
                            name="email"
                            class="form-control"
                            value="<?= htmlspecialchars($settings['email'] ?? '') ?>"
                        >
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Printer Name</label>
                        <input
                            type="text"
                            name="printer_name"
                            class="form-control"
                            value="<?= htmlspecialchars($settings['printer_name'] ?? '') ?>"
                        >
                    </div>

                </div>

                <div class="row mb-3">

                    <div class="col-md-6">
                        <label class="form-label">Paper Width (mm)</label>
                        <input
                            type="number"
                            name="paper_width"
                            class="form-control"
                            value="<?= htmlspecialchars($settings['paper_width'] ?? 80) ?>"
                        >
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Bill Type</label>
                        <select name="bill_type" class="form-select">
                            <option value="1"
                                <?= (($settings['bill_type'] ?? 1) == 1) ? 'selected' : '' ?>>
                                NON GST Invoice
                            </option>

                            <option value="2"
                                <?= (($settings['bill_type'] ?? 1) == 2) ? 'selected' : '' ?>>
                                GST Invoice
                            </option>
                        </select>
                    </div>

                </div>

                <div class="mb-3">
                    <label class="form-label">Address</label>

                    <textarea
                        name="address"
                        class="form-control"
                        rows="4"
                    ><?= htmlspecialchars($settings['address'] ?? '') ?></textarea>
                </div>

                <button type="submit" class="btn btn-primary">
                    Save Settings
                </button>

            </form>

        </div>
    </div>

</div>

<?php include '../../includes/footer.php'; ?>