<?php
include 'db_connect.php';
$qry = $conn->query("SELECT * from system_settings limit 1");
if($qry->num_rows > 0){
    foreach($qry->fetch_array() as $k => $val){
        $meta[$k] = $val;
    }
}
 ?>
<div class="container-fluid">
    <div class="col-lg-12 mt-3">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0"><b>Global System Configurations</b></h5>
            </div>
            <div class="card-body">
                <form action="" id="manage-settings">
                    <div class="form-group">
                        <label for="name" class="control-label">System Name</label>
                        <input type="text" class="form-control" id="name" name="name" value="<?php echo isset($meta['name']) ? $meta['name'] : '' ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="email" class="control-label">System Email Address</label>
                        <input type="email" class="form-control" id="email" name="email" value="<?php echo isset($meta['email']) ? $meta['email'] : '' ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="contact" class="control-label">Contact Hotline / Phone</label>
                        <input type="text" class="form-control" id="contact" name="contact" value="<?php echo isset($meta['contact']) ? $meta['contact'] : '' ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="about" class="control-label">Invoice Note / About Content</label>
                        <textarea name="about" class="text-jqte form-control" rows="5"><?php echo isset($meta['about_content']) ? $meta['about_content'] : '' ?></textarea>
                    </div>
                    <div class="form-group">
                        <label class="control-label">System Logo / Cover Graphic</label>
                        <div class="custom-file mb-2">
                            <input type="file" class="form-control" name="img" onchange="displayImg(this,$(this))">
                        </div>
                    </div>
                    <div class="form-group d-flex justify-content-center bg-light p-3 border rounded">
                        <img src="<?php echo isset($meta['cover_img']) && !empty($meta['cover_img']) ? 'assets/uploads/'.$meta['cover_img'] : 'assets/uploads/no-image.jpg' ?>" alt="System Logo Preview" id="cimg" class="img-thumbnail">
                    </div>
                    <hr>
                    <div class="row justify-content-center">
                        <button class="btn btn-primary btn-sm col-md-3 font-weight-bold">Update System Settings</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<style>
    img#cimg{
        max-height: 20vh;
        max-width: 200px;
        object-fit: contain;
    }
</style>

<script>
    function displayImg(input, _this) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('#cimg').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
        }
    }

    $(document).ready(function(){
        // Safe check initialization wrapper for jqte plugin component execution
        if(typeof $.fn.jqte === "function") {
            $('.text-jqte').jqte();
        }
    });

    $('#manage-settings').submit(function(e){
        e.preventDefault();
        start_load();
        
        $.ajax({
            url: 'ajax.php?action=save_settings',
            data: new FormData($(this)[0]),
            cache: false,
            contentType: false,
            processData: false,
            method: 'POST',
            type: 'POST',
            success: function(resp){
                if(resp == 1){
                    alert_toast('System parameters successfully updated.', 'success');
                    setTimeout(function(){
                        location.reload();
                    }, 1000);
                } else {
                    alert_toast('An error occurred while saving system settings.', 'danger');
                    end_load();
                }
            },
            error: function(err){
                console.error("Settings save runtime error:", err);
                alert_toast('Fatal network connection error occurred.', 'danger');
                end_load();
            }
        });
    });
</script>