<?php
include 'template/header.php';
include 'template/h_menu.php';
include 'sidebar.php';
include 'koneksi.php';

// AMBIL DATA SETTING
$data = mysqli_fetch_assoc(
    mysqli_query($koneksi,"
    SELECT * FROM setting
    LIMIT 1
    ")
);
?>

<div class="content-wrapper p-3">

<!-- HEADER -->
<section class="content-header">

    <div class="container-fluid">

        <div class="row mb-2">

            <div class="col-sm-6">
                <h1>Pengaturan Sistem</h1>
            </div>

            <div class="col-sm-6">

                <ol class="breadcrumb float-sm-right">

                    <li class="breadcrumb-item">
                        <a href="index.php">Home</a>
                    </li>

                    <li class="breadcrumb-item active">
                        Pengaturan
                    </li>

                </ol>

            </div>

        </div>

    </div>

</section>

<!-- CONTENT -->
<section class="content">

<div class="container-fluid">

<div class="card">

<div class="card-header bg-primary">

    <h3 class="card-title text-white">

        <i class="fas fa-cogs"></i>
        Setting Aplikasi

    </h3>

</div>

<div class="card-body">

<form method="POST"
      enctype="multipart/form-data">

<div class="row">

<!-- KOLOM KIRI -->
<div class="col-md-6">

    <!-- NAMA APLIKASI -->
    <div class="form-group">

        <label>Nama Aplikasi</label>

        <input type="text"
               name="nama_aplikasi"
               class="form-control"
               value="<?= $data['nama_aplikasi'] ?>"
               placeholder="Masukkan nama aplikasi"
               required>

    </div>

    <!-- NAMA SEKOLAH -->
    <div class="form-group">

        <label>Nama Kampus / Sekolah</label>

        <input type="text"
               name="nama_sekolah"
               class="form-control"
               value="<?= $data['nama_sekolah'] ?>"
               placeholder="Masukkan nama sekolah"
               required>

    </div>

    <!-- NO HP -->
    <div class="form-group">

        <label>No HP</label>

        <input type="text"
               name="no_hp"
               class="form-control"
               value="<?= $data['no_hp'] ?>"
               placeholder="Masukkan nomor HP">

    </div>

</div>

<!-- KOLOM KANAN -->
<div class="col-md-6">

    <!-- ALAMAT -->
    <div class="form-group">

        <label>Alamat</label>

        <textarea name="alamat"
                  class="form-control"
                  rows="5"
                  placeholder="Masukkan alamat"><?= $data['alamat'] ?></textarea>

    </div>

    <!-- LOGO -->
    <div class="form-group">

        <label>Upload Logo</label>

        <input type="file"
               name="logo"
               class="form-control">

    </div>

    <!-- PREVIEW LOGO -->
    <?php if($data['logo']){ ?>

    <div class="mt-3">

        <label>Logo Saat Ini :</label>

        <br>

        <img src="upload/<?= $data['logo'] ?>"
             alt="Logo"
             class="img-thumbnail"
             width="120">

    </div>

    <?php } ?>

</div>

</div>

<hr>

<!-- BUTTON -->
<button type="submit"
        name="simpan"
        class="btn btn-success">

    <i class="fas fa-save"></i>
    Simpan Pengaturan

</button>

<button type="reset"
        class="btn btn-secondary">

    <i class="fas fa-undo"></i>
    Reset

</button>

</form>

</div>
</div>
</div>
</section>
</div>

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

<?php

// SIMPAN DATA
if(isset($_POST['simpan'])){

    $nama_aplikasi = $_POST['nama_aplikasi'];
    $nama_sekolah  = $_POST['nama_sekolah'];
    $alamat        = $_POST['alamat'];
    $no_hp         = $_POST['no_hp'];

    // LOGO LAMA
    $logo = $data['logo'];

    // JIKA UPLOAD LOGO BARU
    if($_FILES['logo']['name'] != ""){

        $file = $_FILES['logo']['name'];
        $tmp  = $_FILES['logo']['tmp_name'];

        move_uploaded_file(
            $tmp,
            "upload/".$file
        );

        $logo = $file;
    }

    // UPDATE DATABASE
    mysqli_query($koneksi,"
    UPDATE setting SET

        nama_aplikasi='$nama_aplikasi',
        nama_sekolah='$nama_sekolah',
        alamat='$alamat',
        no_hp='$no_hp',
        logo='$logo'

    WHERE id_setting='1'
    ");

    echo "

    <script>

        alert('Pengaturan berhasil disimpan');

        window.location='pengaturan.php';

    </script>

    ";
}
?>