<?php 
include 'template/header.php';
check_user_access('admin'); // Only admin can access this page
?>

<br>

<?php
if(isset($_POST['update_device'])){
    $printer_name = htmlspecialchars($_POST['printer_name']);
    $printer_ip = htmlspecialchars($_POST['printer_ip']);
    $printer_port = (int)$_POST['printer_port'];
    $printer_type = htmlspecialchars($_POST['printer_type']);
    $scanner_type = htmlspecialchars($_POST['scanner_type']);
    
    // Update or insert device settings
    $check = mysqli_query($conn, "SELECT id FROM device_settings LIMIT 1");
    if(mysqli_num_rows($check) > 0) {
        $update = mysqli_query($conn, "UPDATE device_settings SET 
            printer_name='$printer_name', 
            printer_ip='$printer_ip', 
            printer_port=$printer_port, 
            printer_type='$printer_type', 
            scanner_type='$scanner_type'
            WHERE id=1");
    } else {
        $update = mysqli_query($conn, "INSERT INTO device_settings 
            (printer_name, printer_ip, printer_port, printer_type, scanner_type) 
            VALUES ('$printer_name', '$printer_ip', $printer_port, '$printer_type', '$scanner_type')");
    }
    
    if($update){
        echo '<script>alert("Pengaturan perangkat berhasil diperbarui");window.location="device_management.php"</script>';
    } else {
        echo '<script>alert("Gagal memperbarui pengaturan perangkat");history.go(-1);</script>';
    }
}

// Get current device settings
$device_settings = mysqli_query($conn, "SELECT * FROM device_settings LIMIT 1");
$settings = mysqli_fetch_assoc($device_settings);
?>

<div class="card">
    <div class="card-header">
        <div class="card-tittle"><i class="fa fa-print me-2"></i> Pengaturan Perangkat</div>
    </div>
    <form method="post">
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <h5>Pengaturan Printer</h5>
                    <div class="form-group mb-3">
                        <label>Nama Printer:</label>
                        <input type="text" name="printer_name" class="form-control" 
                               value="<?php echo $settings['printer_name'] ?? 'Default Printer'; ?>" required>
                        <small class="form-text text-muted">Nama printer yang akan digunakan</small>
                    </div>
                    
                    <div class="form-group mb-3">
                        <label>Tipe Printer:</label>
                        <select name="printer_type" class="form-control" required>
                            <option value="usb" <?php echo ($settings['printer_type'] ?? 'usb') == 'usb' ? 'selected' : ''; ?>>USB</option>
                            <option value="bluetooth" <?php echo ($settings['printer_type'] ?? 'usb') == 'bluetooth' ? 'selected' : ''; ?>>Bluetooth</option>
                            <option value="network" <?php echo ($settings['printer_type'] ?? 'usb') == 'network' ? 'selected' : ''; ?>>Jaringan (IP)</option>
                        </select>
                    </div>
                    
                    <div class="form-group mb-3" id="printer_ip_section" style="<?php echo ($settings['printer_type'] ?? 'usb') == 'network' ? '' : 'display:none;'; ?>">
                        <label>Alamat IP Printer:</label>
                        <input type="text" name="printer_ip" class="form-control" 
                               value="<?php echo $settings['printer_ip'] ?? '192.168.1.100'; ?>" 
                               placeholder="Contoh: 192.168.1.100">
                    </div>
                    
                    <div class="form-group mb-3" id="printer_port_section" style="<?php echo ($settings['printer_type'] ?? 'usb') == 'network' ? '' : 'display:none;'; ?>">
                        <label>Port Printer:</label>
                        <input type="number" name="printer_port" class="form-control" 
                               value="<?php echo $settings['printer_port'] ?? 9100; ?>" 
                               placeholder="Contoh: 9100">
                    </div>
                </div>
                
                <div class="col-md-6">
                    <h5>Pengaturan Scanner Barcode</h5>
                    <div class="form-group mb-3">
                        <label>Tipe Scanner:</label>
                        <select name="scanner_type" class="form-control" required>
                            <option value="usb" <?php echo ($settings['scanner_type'] ?? 'usb') == 'usb' ? 'selected' : ''; ?>>USB</option>
                            <option value="bluetooth" <?php echo ($settings['scanner_type'] ?? 'usb') == 'bluetooth' ? 'selected' : ''; ?>>Bluetooth</option>
                        </select>
                    </div>
                    
                    <div class="card bg-light p-3 mt-4">
                        <h6><i class="fas fa-info-circle"></i> Panduan Konfigurasi</h6>
                        <ul class="mb-0">
                            <li><strong>USB Printer:</strong> Hubungkan printer secara langsung ke komputer</li>
                            <li><strong>Bluetooth Printer:</strong> Pastikan printer terhubung ke komputer via Bluetooth</li>
                            <li><strong>Network Printer:</strong> Masukkan IP printer dan pastikan terhubung ke jaringan</li>
                            <li><strong>USB Scanner:</strong> Scanner akan otomatis terdeteksi saat digunakan</li>
                            <li><strong>Bluetooth Scanner:</strong> Pair scanner dengan komputer terlebih dahulu</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="card-footer text-right">
            <button type="submit" name="update_device" class="btn btn-primary px-3">Simpan Pengaturan</button>
        </div>
    </form>
</div>

<script>
// Show/hide IP and port fields based on printer type
document.querySelector('select[name="printer_type"]').addEventListener('change', function() {
    const selectedValue = this.value;
    const ipSection = document.getElementById('printer_ip_section');
    const portSection = document.getElementById('printer_port_section');
    
    if(selectedValue === 'network') {
        ipSection.style.display = 'block';
        portSection.style.display = 'block';
    } else {
        ipSection.style.display = 'none';
        portSection.style.display = 'none';
    }
});
</script>

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