<?php 
include "header.php";
include "sidebar.php";
?>
<div class="main-panel">
    <div class="content">
        <div class="panel-header bg-primary-gradient">
            <div class="page-inner py-1">
                <div class="d-flex align-items-left align-items-md-center flex-column flex-md-row">
                    <div>
                        <h2 class="text-white pb-2 fw-bold">Tambah Promosi</h2>
                    </div>
                </div>
            </div>
        </div>
        <div class="container mt-5">
        <div class="row">
            <div class="col-sm-6">
                <div class="card">
                    <div class="card-body">
                    <?php
                        error_reporting(0);

                        if (!empty($_GET['notif'])) {
                            $notif = $_GET['notif'];
                            $messages = [
                                1 => ['success', 'Well Done! Promotion Saved!'],
                                2 => ['warning', 'Max Image Size 2MB!'],
                                3 => ['warning', 'Only JPG or PNG!'],
                            ];

                            if (isset($messages[$notif])) {
                                list($type, $message) = $messages[$notif];
                                echo "<div class=\"alert alert-$type d-flex align-items-center\" role=\"alert\">
                                    <span class=\"alert-icon text-$type me-2\">
                                        <i class=\"ti ti-" . ($type == 'success' ? 'check' : 'bell') . " ti-xs\"></i>
                                    </span>
                                    <span><strong>$message</strong></span>
                                </div>";
                            }
                        }

                        if (isset($_GET['postID'])) {
                            $catID = $_GET['postID'];
                            $sql_2 = mysqli_query($conn, "SELECT * FROM `tb_post` WHERE cuid = '$catID'") or die(mysqli_error());
                            $s2 = mysqli_fetch_array($sql_2);
                        }
                        ?>
                        <h2 class="text-primary">Tambah Promosi</h2>
                        <form action="tools/add-promo.php" method="post" enctype="multipart/form-data">
                            <div class="form-group">
                                <label for="image">Link Image :</label>
                                <input id="image" class="form-control" type="text" name="image">
                            </div>
                            <div class="form-group">
                                <label for="title">Promotion Title :</label>
                                <input id="title" class="form-control" type="text" name="title" value="<?= isset($_GET['postID']) ? $s2['nama_page'] : ''; ?>" required>
                                <input type="hidden" name="postID" value="<?= isset($_GET['postID']) ? $s2['cuid'] : ''; ?>">
                            </div>
                            <div class="form-group">
                                <label for="content">Description :</label>
                                <textarea id="content" class="form-control" name="content"><?= isset($_GET['postID']) ? $s2['content'] : ''; ?></textarea>
                            </div>
                            <div class="form-group">
                                <label for="kategori">Jenis Promo :</label>
                                <select id="kategori" name="kategori" class="form-control" required>
                                    <option> Pilih </option>
                                    <option value="0" <?= (isset($_GET['postID']) && $s2['kategori'] == 0) ? 'selected' : ''; ?>> Deposit </option>
                                    <option value="1" <?= (isset($_GET['postID']) && $s2['kategori'] == 1) ? 'selected' : ''; ?>> Lainnya </option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="persen">Percentage (%) :</label>
                                <input id="persen" class="form-control" type="text" name="persen" value="<?= isset($_GET['postID']) ? $s2['persen'] : ''; ?>">
                            </div>
                            <div class="form-group">
                                <label for="satuan">Satuan Turn Over :</label>
                                <select id="satuan" name="satuan" class="form-control" required>
                                    <option> Pilih </option>
                                    <option value="0" <?= (isset($_GET['postID']) && $s2['satuan'] == 0) ? 'selected' : ''; ?>> kali </option>
                                    <option value="1" <?= (isset($_GET['postID']) && $s2['satuan'] == 1) ? 'selected' : ''; ?>> Rupiah </option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="min_to">Minimal Turn Over :</label>
                                <input id="min_to" class="form-control" type="text" name="min_to" value="<?= isset($_GET['postID']) ? $s2['min_to'] : ''; ?>">
                            </div>                            
                            <button type="submit" name="submit" class="btn btn-primary">Publish</button>
                            <a href="promosi.php" class="btn btn-light">Cancel</a>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <!-- Invoice List Table -->
                <div class="card">
                    <div class="card-datatable table-responsive">
                        <table id="default-datatable" class="invoice-list-table table border-top">
                            <thead>
                                <tr class="bg-info">
                                    <th class="text-center">Image</th>
                                    <th class="text-center">Promotion Title</th>
                                    <th class="text-center">Jenis Promo</th>
                                    <th class="text-center">Min Turn Over</th>
                                    <th class="text-center">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $sql_1 = mysqli_query($conn, "SELECT * FROM `tb_post` ORDER BY cuid ASC") or die(mysqli_error());
                                $no = 0;
                                while ($s1 = mysqli_fetch_array($sql_1)) {
                                    $no++;
                                    $idkategori = $s1['cuid'];
                                ?>
                                    <tr>
                                        <td class="text-left" style="vertical-align: middle; white-space: normal; font-size: 14px;">
                                            <img src="<?= $s1['image']; ?>" alt="Promotion Image" style="max-width: 100px; max-height: 100px;">
                                        </td>

                                        <td class="text-left" style="vertical-align: middle; white-space: normal; font-size: 14px;">
                                            <?= $s1['title']; ?>
                                        </td>
                                        <td class="text-center" style="vertical-align: middle; white-space: normal; font-size: 14px;">
                                            <?php
                                            if ($s1['kategori'] == 0) {
                                                echo' Deposit';
                                            } else {
                                                echo 'Promo Web';
                                            }
                                            ?>
                                        </td>                                                                                
                                        <td class="text-center" style="vertical-align: middle; white-space: normal; font-size: 14px;">
                                            <?php
                                            if ($s1['satuan'] == 0) {
                                                echo $s1['min_to'] . ' Kali';
                                            } else {
                                                echo number_format((float)$s1['min_to']);
                                            }
                                            ?>
                                        </td>
                                        <td class="text-center" style="vertical-align: middle; font-size: 14px;">
                                            <a href="promosi.php?postID=<?= $s1['cuid']; ?>" class="btn btn-primary btn-sm">
                                                <i class="fa fa-edit"></i>
                                            </a>
                                            <a href="tools/del-post.php?cuid=<?= $s1['cuid']; ?>&jenis=1" class="btn btn-danger btn-sm" onclick="return confirm('Apakah anda yakin ingin menghapus data ini?');">
                                                <i class="fa fa-trash"></i>
                                            </a>
                                        </td>
                                    </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php include "footer.php";?>
    
    <!-- Summernote JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.18/summernote-bs4.min.js"></script>
    <script>
        $(document).ready(function () {
            $('#content').summernote();
        });
    </script>
</body>

</html>
