<?php
session_start();
require_once '../../config.php';
load('middleware', 'user');

if ($_SESSION['user']['level'] !== "Admin") {
    header('location:' . base_url('id'));
}
if (isset($_GET['hapus'])) {
    $hapus = addslashes(trim(htmlspecialchars(mysqli_real_escape_string($db, $_GET['hapus']))));
    
    $query_slide = mysqli_query($db, "SELECT * FROM gambar_produk WHERE id = '$hapus'");
    
    if (mysqli_num_rows($query_slide) === 1) {
        $fetch_slide = mysqli_fetch_assoc($query_slide);
        
        unlink('../../assets/produk/' . $fetch_slide['img']);
        
        mysqli_query($db, "DELETE FROM gambar_produk WHERE id = '$hapus'");
        
        $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di hapus.'];
        exit(header('location: /admin/guest/gambar-produk'));
    } else {
        $_SESSION['alert'] = ['danger', 'Failed!', 'Data slide tidak ditemukan.'];
        exit(header('location: /admin/guest/gambar-produk'));
    }
}

if (isset($_POST['tombol_gambar'])) {
    
    if (isset($_FILES['gambar'])) {
        if ($_FILES['gambar']['error'] !== 4) {
            
            $format_file = end(explode('.', strtolower($_FILES['gambar']['name'])));
        
            if (in_array($format_file, ['jpg', 'jpeg', 'png'])) {
                
                $nama_file = uniqid() . '.' . $format_file;
                
                move_uploaded_file($_FILES['gambar']['tmp_name'], '../../assets/produk/' . $nama_file);
                
                mysqli_query($db, "INSERT INTO gambar_produk VALUES ('','$nama_file','".$_POST['nama']."')");
                
                $_SESSION['alert'] = ['success', 'Success!', 'Gambar slide berhasil ditambah.'];
                exit(header('location: /admin/guest/gambar-produk'));
    
            } else {
                $_SESSION['alert'] = ['danger', 'Failed!', 'Format gambar tidak sesuai.'];
                exit(header('location: /admin/guest/gambar-produk'));
            }
        } else {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Gambar tidak boleh kosong.'];
            exit(header('location: /admin/guest/gambar-produk'));
        }
    } else {
        $_SESSION['alert'] = ['danger', 'Failed!', 'Gambar tidak boleh kosong.'];
        exit(header('location: /admin/guest/gambar-produk'));
    }
}
include_once '../../layouts/header-admin.php'; ?>
<div class="row">
    <div class="col-md-12 col-12">
        <div class="card bg-dark shadow mb-3">
            <div class="card-header">
                <h4 class="card-title">Data Gambar Produk</h4>
            </div>
            <div class="card-body card-dashboard">
                <div class="table-responsive">
                    <table class="table table-striped table-dark table-bordered table-hover" id="tbb">
                        <thead>
                            <tr>
                                <th>Gambar</th>
                                <th>Nama Gambar</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php 
                        $no = 1;
                        $query_pembelian_guest = mysqli_query($db, "SELECT * FROM gambar_produk ORDER BY id DESC");
                        while($fetch_pembelian_guest = mysqli_fetch_assoc($query_pembelian_guest)): 
                        ?>
                        <tr>
                            <td><img src="../../assets/produk/<?= $fetch_pembelian_guest['img']; ?>" style="max-height: 30px"></td>
                            <td><?= $fetch_pembelian_guest['nama_gambar'];?></td>
                            <td><a class="btn btn-danger btn-sm" href="/admin/guest/gambar-produk?hapus=<?= $fetch_pembelian_guest['id']; ?>">Hapus</a></td>
                        </tr>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="card bg-dark shadow mt-3">
            <div class="card-header">
                <h4 class="card-title">Tambah Gambar</h4>
            </div>
            <div class="card-body">
                <form action="" method="POST" enctype="multipart/form-data">
                    <div class="form-group mb-3">
                        <label class="mb-1">Nama Gambar</label>
                            <input type="text" class="form-control" name="nama" placeholder="Diamond">
                    </div>
                    <div class="form-group mb-3">
                        <label class="mb-1">Gambar Produk</label>
                        <div class="custom-file">
                            <input type="file" class="custom-file-input" id="customFile" name="gambar">
                            <label class="custom-file-label" for="customFile">Pilih Gambar</label>
                        </div>
                    </div>
                    <div class="text-right">
                        <button class="btn btn-light" type="reset">Batal</button>
                        <button class="btn btn-primary" type="submit" name="tombol_gambar">Tambahkan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<?php include '../../layouts/footer-admin.php';?>
<script>
$(".custom-file-input").on("change", function() {
    var fileName = $(this).val().split("\\").pop();
    $(this).siblings(".custom-file-label").addClass("selected").html(fileName);
});
</script>
<script type="text/javascript">
$(document).ready(function() {
    $("#tbb").dataTable();
});
</script>
<script type="text/javascript">
    function modal(name, link, size) {
        var sizes = '';
        if (size == 'smaller' || size == 'xs') sizes = 'modal-xs';
        if (size == 'small' || size == 'sm') sizes = 'modal-sm';
        if (size == 'large' || size == 'lg') sizes = 'modal-lg';
        if (size == 'larger' || size == 'xl') sizes = 'modal-xl';

        $.ajax({
            type: "GET",
            url: link,
            beforeSend: function() {
                $('#SModal-body').html('Loading...');
            },
            success: function(result) {
                $('#SModal-body').html(result);
            },
            error: function() {
                $('#SModal-body').html('Failed to get contents...');
            }
        });

        $('#SModal-title').html(name);
        $('#SModal-size').removeClass('modal-xs');
        $('#SModal-size').removeClass('modal-sm');
        $('#SModal-size').removeClass('modal-lg');
        $('#SModal-size').removeClass('modal-xl');
        $('#SModal-size').addClass(sizes);
        $('#SModal').modal('show');
    }
</script>
<div class="modal fade text-left" id="SModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true" data-keyboard="false" data-backdrop="static">
    <div class="modal-dialog" id="SModal-size" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="SModal-title"></h4>
                <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close"><span aria-hidden="true" style="text-shadow: none; color:white;">&times;</span></button>
            </div>
            <div class="modal-body" id="SModal-body"></div>
        </div>
    </div>
</div>