<?php
global $db;
$page_title = 'Master Shop';
require_once('includes/load.php');
// Checkin What level user has permission to view this page
// page_require_level(1);
$data = find_all('settings');

if (isset($_GET['delete'])) {
    delete_by_id('settings', $_GET['delete']);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action'])) {
        $action = $_POST['action'];
        if ($action === 'add') {
            $key = $_POST['key'];
            $value = $_POST['value'];
            $description = $_POST['description'];
            $stmt = $db->prepare("INSERT INTO settings (setting_key, setting_value, description) VALUES (?, ?, ?)");
            $stmt->bind_param("sss", $key, $value, $description); // "sss" untuk tiga string
            $stmt->execute();
        }
    }
}
?>
<?php include_once('layouts/header.php'); ?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        <div class="panel-heading clearfix">
            <strong>
                <span class="glyphicon glyphicon-th"></span>
                <span>List Setting</span>
            </strong>
        </div>
        <div class="panel-body">
            <div id="action">
                <button type="button" class="btn btn-primary mb-4" data-toggle="modal" data-target="#createModal">
                    Create New Setting
                </button>
            </div>

            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>Key</th>
                        <th>Value</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($data as $row): ?>
                        <tr>
                            <td><?php echo $row['setting_key']; ?></td>
                            <td><?php echo $row['setting_value']; ?></td>
                            <td>
                                <a href="edit_setting.php?id=<?php echo $row['id']; ?>" class="btn btn-warning btn-sm"><i
                                        class="glyphicon glyphicon-pencil"></i></a>
                                <a href="?delete=<?php echo $row['id']; ?>" class="btn btn-danger btn-sm"
                                    onclick="return confirm('Are you sure?')"><i class="glyphicon glyphicon-remove"></i></a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<!-- Modal -->
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModalLabel"
    aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="createModalLabel">Create New Setting</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"
                    style="right: 15px;top: 15px;position:absolute">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form method="POST" class="mb-4">
                    <input type="hidden" value="add" name="action">
                    <div class="form-group">
                        <label for="shop_name">Key</label>
                        <input type="text" name="key" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <label for="kode">Value</label>
                        <input type="text" name="value" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="kode">Description</label>
                        <textarea type="text" name="description" class="form-control"></textarea>
                    </div>
                    <button type="submit" name="create" class="btn btn-primary">Create</button>
                </form>
            </div>
        </div>
    </div>
</div>
<?php include_once('layouts/footer.php'); ?>