<?php
$page_title = "Pengaturan Integrasi (Fonnte/SMTP)";
include 'layout/header.php';
include 'layout/sidebar.php';
require_once '../config.php';

if ($_SESSION['level'] != 'administrator') {
    echo "<script>alert('Akses Ditolak!'); window.location.href='index.php';</script>";
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    foreach ($_POST as $key => $val) {
        $stmt = $pdo->prepare("UPDATE tbl_pengaturan SET meta_value = ? WHERE meta_key = ?");
        $stmt->execute([$val, $key]);
    }
    echo "<script>
        document.addEventListener('DOMContentLoaded', function() {
            Swal.fire({
                icon: 'success',
                title: 'Berhasil!',
                text: 'Pengaturan Integrasi Berhasil Disimpan!',
                timer: 2000,
                showConfirmButton: false,
                position: 'center'
            }).then(function() {
                window.location.href = 'pengaturan.php';
            });
        });
    </script>";
}
?>

<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <h1 class="m-0">Pengaturan Sistem & Integrasi Backend</h1>
        </div>
    </div>

    <section class="content">
        <div class="container-fluid">
            <form action="pengaturan.php" method="POST">
                <div class="row">
                    <div class="col-md-6">
                        <div class="card card-warning">
                            <div class="card-header"><h3 class="card-title">API Fonnte & SMTP Email</h3></div>
                            <div class="card-body">
                                <div class="form-group">
                                    <label>Token Fonnte (WhatsApp)</label>
                                    <input type="text" name="fonnte_token" class="form-control" value="<?= htmlspecialchars(getSetting($pdo, 'fonnte_token')) ?>">
                                </div>
                                <hr>
                                <div class="form-group">
                                    <label>SMTP Host</label>
                                    <input type="text" name="smtp_host" class="form-control" value="<?= htmlspecialchars(getSetting($pdo, 'smtp_host')) ?>">
                                </div>
                                <div class="form-group">
                                    <label>SMTP Username (Email Pengirim)</label>
                                    <input type="text" name="smtp_user" class="form-control" value="<?= htmlspecialchars(getSetting($pdo, 'smtp_user')) ?>">
                                </div>
                                <div class="row">
                                    <div class="col-8 form-group">
                                        <label>SMTP Password</label>
                                        <input type="text" name="smtp_pass" class="form-control" value="<?= htmlspecialchars(getSetting($pdo, 'smtp_pass')) ?>">
                                    </div>
                                    <div class="col-4 form-group">
                                        <label>SMTP Port</label>
                                        <input type="text" name="smtp_port" class="form-control" value="<?= htmlspecialchars(getSetting($pdo, 'smtp_port')) ?>">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="card card-success">
                            <div class="card-header"><h3 class="card-title">Template Pesan Pengajuan Baru</h3></div>
                            <div class="card-body">
                                <small class="text-muted d-block mb-2">Gunakan tag: <b>[NAMA], [TIKET], [TUJUAN], [STATUS], [LINK_TRACKING]</b></small>
                                <div class="form-group">
                                    <label>Template WhatsApp</label>
                                    <textarea name="tpl_wa_baru" class="form-control" rows="4"><?= htmlspecialchars(getSetting($pdo, 'tpl_wa_baru')) ?></textarea>
                                </div>
                                <div class="form-group">
                                    <label>Template Email (HTML)</label>
                                    <textarea name="tpl_email_baru" class="form-control" rows="4"><?= htmlspecialchars(getSetting($pdo, 'tpl_email_baru')) ?></textarea>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-md-6">
                        <div class="card card-info">
                            <div class="card-header"><h3 class="card-title">Template Pesan Update Status</h3></div>
                            <div class="card-body">
                                <small class="text-muted d-block mb-2">Gunakan tag: <b>[NAMA], [TIKET], [STATUS], [CATATAN], [LINK_TRACKING], [LINK_SURAT]</b></small>
                                <div class="form-group">
                                    <label>Template WhatsApp</label>
                                    <textarea name="tpl_wa_update" class="form-control" rows="4"><?= htmlspecialchars(getSetting($pdo, 'tpl_wa_update')) ?></textarea>
                                </div>
                                <div class="form-group">
                                    <label>Template Email (HTML)</label>
                                    <textarea name="tpl_email_update" class="form-control" rows="4"><?= htmlspecialchars(getSetting($pdo, 'tpl_email_update')) ?></textarea>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="mb-4 text-center">
                    <button type="submit" class="btn btn-primary btn-lg w-50"><i class="fas fa-save"></i> Simpan Pengaturan Integrasi</button>
                </div>
            </form>
        </div>
    </section>
</div>

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