

<?php $__env->startSection('title', 'Iklan'); ?>

<?php $__env->startSection('content_header'); ?>
    <h1><i class="fas fa-bullhorn mr-2"></i>Iklan</h1>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
    <?php if(session('success')): ?>
        <div class="alert alert-success alert-dismissible">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <?php echo e(session('success')); ?>

        </div>
    <?php endif; ?>

    <div class="row">
        <!-- Form Tambah Iklan -->
        <div class="col-md-5">
            <div class="card card-outline card-primary">
                <div class="card-header">
                    <h3 class="card-title"><i class="fas fa-plus-circle mr-1"></i> Tambah Iklan</h3>
                </div>
                <div class="card-body">
                    <?php if($errors->any()): ?>
                        <div class="alert alert-danger alert-dismissible">
                            <button type="button" class="close" data-dismiss="alert">&times;</button>
                            <ul class="mb-0">
                                <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <li><?php echo e($error); ?></li>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </ul>
                        </div>
                    <?php endif; ?>

                    <form action="<?php echo e(route('admin.iklan.store')); ?>" method="POST" enctype="multipart/form-data">
                        <?php echo csrf_field(); ?>

                        <div class="form-group">
                            <label>Tajuk Iklan <span class="text-danger">*</span></label>
                            <input type="text" name="tajuk" class="form-control <?php $__errorArgs = ['tajuk'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                value="<?php echo e(old('tajuk')); ?>" placeholder="Masukkan tajuk iklan">
                            <?php $__errorArgs = ['tajuk'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="form-group">
                            <label>Kandungan <span class="text-danger">*</span></label>
                            <textarea name="kandungan" rows="4" class="form-control <?php $__errorArgs = ['kandungan'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                placeholder="Masukkan kandungan iklan"><?php echo e(old('kandungan')); ?></textarea>
                            <?php $__errorArgs = ['kandungan'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="form-group">
                            <label>Target Iklan <span class="text-danger">*</span></label>
                            <div class="<?php $__errorArgs = ['target_role'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>">
                                <?php $__currentLoopData = ['admin' => 'Admin', 'pemohon' => 'Pemohon', 'checker' => 'Checker', 'supervisor' => 'Supervisor']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <div class="custom-control custom-checkbox mb-1">
                                        <input type="checkbox" class="custom-control-input" id="role_<?php echo e($value); ?>"
                                            name="target_role[]" value="<?php echo e($value); ?>"
                                            <?php echo e(is_array(old('target_role')) && in_array($value, old('target_role')) ? 'checked' : ''); ?>>
                                        <label class="custom-control-label"
                                            for="role_<?php echo e($value); ?>"><?php echo e($label); ?></label>
                                    </div>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </div>
                            <?php $__errorArgs = ['target_role'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="text-danger small mt-1"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label>Tarikh Mula <span class="text-danger">*</span></label>
                                <input type="date" name="tarikh_mula"
                                    class="form-control <?php $__errorArgs = ['tarikh_mula'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                    value="<?php echo e(old('tarikh_mula', date('Y-m-d'))); ?>">
                                <?php $__errorArgs = ['tarikh_mula'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                    <div class="invalid-feedback"><?php echo e($message); ?></div>
                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                            </div>
                            <div class="form-group col-md-6">
                                <label>Tarikh Tamat</label>
                                <input type="date" name="tarikh_tamat"
                                    class="form-control <?php $__errorArgs = ['tarikh_tamat'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                    value="<?php echo e(old('tarikh_tamat')); ?>">
                                <?php $__errorArgs = ['tarikh_tamat'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                    <div class="invalid-feedback"><?php echo e($message); ?></div>
                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="custom-control custom-switch">
                                <input type="checkbox" class="custom-control-input" id="is_aktif" name="is_aktif"
                                    value="1" <?php echo e(old('is_aktif', '1') ? 'checked' : ''); ?>>
                                <label class="custom-control-label" for="is_aktif">Aktifkan Iklan</label>
                            </div>
                        </div>

                        <div class="form-group">
                            <label>Lampiran <small class="text-muted">(PDF, Word, Gambar – maks. 5MB)</small></label>
                            <div class="custom-file">
                                <input type="file" class="custom-file-input <?php $__errorArgs = ['lampiran'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                    id="lampiran" name="lampiran" accept=".pdf,.doc,.docx,.jpg,.jpeg,.png">
                                <label class="custom-file-label" for="lampiran">Pilih fail...</label>
                            </div>
                            <?php $__errorArgs = ['lampiran'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="text-danger small mt-1"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <button type="submit" class="btn btn-primary btn-block">
                            <i class="fas fa-paper-plane mr-1"></i> Terbitkan Iklan
                        </button>
                    </form>
                </div>
            </div>
        </div>

        <!-- Senarai Iklan -->
        <div class="col-md-7">
            <div class="card card-outline card-primary">
                <div class="card-header">
                    <h3 class="card-title"><i class="fas fa-list mr-1"></i> Senarai Iklan</h3>
                </div>
                <div class="card-body table-responsive p-0">
                    <table class="table table-hover table-striped">
                        <thead class="thead-dark">
                            <tr>
                                <th width="40">#</th>
                                <th>Tajuk</th>
                                <th>Target</th>
                                <th>Tarikh</th>
                                <th width="60">Lampiran</th>
                                <th width="60">Status</th>
                                <th width="90">Tindakan</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $senarai; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $iklan): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td><?php echo e($index + 1); ?></td>
                                    <td>
                                        <strong><?php echo e($iklan->tajuk); ?></strong>
                                        <div class="text-muted small"><?php echo e(Str::limit($iklan->kandungan, 60)); ?></div>
                                    </td>
                                    <td>
                                        <?php $__currentLoopData = $iklan->target_role; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $role): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <span class="badge badge-info mb-1"><?php echo e(ucfirst($role)); ?></span>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </td>
                                    <td>
                                        <small><?php echo e($iklan->tarikh_mula->format('d/m/Y')); ?></small><br>
                                        <?php if($iklan->tarikh_tamat): ?>
                                            <small class="text-muted">hingga
                                                <?php echo e($iklan->tarikh_tamat->format('d/m/Y')); ?></small>
                                        <?php else: ?>
                                            <small class="text-muted">-</small>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <?php if($iklan->lampiran): ?>
                                            <a href="<?php echo e(Storage::url($iklan->lampiran)); ?>" target="_blank"
                                                class="btn btn-xs btn-outline-secondary" title="Lihat Lampiran">
                                                <i class="fas fa-paperclip"></i>
                                            </a>
                                        <?php else: ?>
                                            <span class="text-muted">-</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <?php if($iklan->is_aktif): ?>
                                            <span class="badge badge-success">Aktif</span>
                                        <?php else: ?>
                                            <span class="badge badge-secondary">Tidak Aktif</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <div class="d-flex">
                                            <button type="button" class="btn btn-warning btn-sm mr-1"
                                                data-toggle="modal" data-target="#modalEdit"
                                                data-id="<?php echo e($iklan->id); ?>" data-tajuk="<?php echo e($iklan->tajuk); ?>"
                                                data-kandungan="<?php echo e($iklan->kandungan); ?>"
                                                data-target-role="<?php echo e(json_encode($iklan->target_role)); ?>"
                                                data-tarikh-mula="<?php echo e($iklan->tarikh_mula->format('Y-m-d')); ?>"
                                                data-tarikh-tamat="<?php echo e($iklan->tarikh_tamat ? $iklan->tarikh_tamat->format('Y-m-d') : ''); ?>"
                                                data-is-aktif="<?php echo e($iklan->is_aktif ? '1' : '0'); ?>"
                                                data-lampiran="<?php echo e($iklan->lampiran ? Storage::url($iklan->lampiran) : ''); ?>"
                                                data-lampiran-nama="<?php echo e($iklan->lampiran ? basename($iklan->lampiran) : ''); ?>">
                                                <i class="fas fa-edit"></i>
                                            </button>
                                            <form action="<?php echo e(route('admin.iklan.destroy', $iklan->id)); ?>" method="POST"
                                                onsubmit="return confirm('Padam iklan ini?')">
                                                <?php echo csrf_field(); ?>
                                                <?php echo method_field('DELETE'); ?>
                                                <button type="submit" class="btn btn-danger btn-sm">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr>
                                    <td colspan="6" class="text-center text-muted">Tiada iklan ditemui.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Modal Edit -->
    <div class="modal fade" id="modalEdit" tabindex="-1" role="dialog">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header bg-warning">
                    <h5 class="modal-title"><i class="fas fa-edit mr-1"></i> Kemaskini Iklan</h5>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                <form id="formEdit" method="POST" enctype="multipart/form-data">
                    <?php echo csrf_field(); ?>
                    <?php echo method_field('PUT'); ?>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>Tajuk Iklan <span class="text-danger">*</span></label>
                            <input type="text" name="tajuk" id="edit_tajuk" class="form-control" required>
                        </div>
                        <div class="form-group">
                            <label>Kandungan <span class="text-danger">*</span></label>
                            <textarea name="kandungan" id="edit_kandungan" rows="4" class="form-control" required></textarea>
                        </div>
                        <div class="form-group">
                            <label>Target Iklan <span class="text-danger">*</span></label>
                            <?php $__currentLoopData = ['admin' => 'Admin', 'pemohon' => 'Pemohon', 'checker' => 'Checker', 'supervisor' => 'Supervisor']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <div class="custom-control custom-checkbox mb-1">
                                    <input type="checkbox" class="custom-control-input edit-role-check"
                                        id="edit_role_<?php echo e($value); ?>" name="target_role[]"
                                        value="<?php echo e($value); ?>">
                                    <label class="custom-control-label"
                                        for="edit_role_<?php echo e($value); ?>"><?php echo e($label); ?></label>
                                </div>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label>Tarikh Mula <span class="text-danger">*</span></label>
                                <input type="date" name="tarikh_mula" id="edit_tarikh_mula" class="form-control"
                                    required>
                            </div>
                            <div class="form-group col-md-6">
                                <label>Tarikh Tamat</label>
                                <input type="date" name="tarikh_tamat" id="edit_tarikh_tamat" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label>Lampiran Baharu <small class="text-muted">(kosongkan jika tidak tukar – PDF, Word,
                                    Gambar – maks. 5MB)</small></label>
                            <div id="edit_lampiran_semasa" class="mb-1 d-none">
                                <small class="text-muted">Semasa: </small>
                                <a id="edit_lampiran_link" href="#" target="_blank" class="small"></a>
                            </div>
                            <div class="custom-file">
                                <input type="file" class="custom-file-input" id="edit_lampiran" name="lampiran"
                                    accept=".pdf,.doc,.docx,.jpg,.jpeg,.png">
                                <label class="custom-file-label" for="edit_lampiran">Pilih fail...</label>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="custom-control custom-switch">
                                <input type="checkbox" class="custom-control-input" id="edit_is_aktif" name="is_aktif"
                                    value="1">
                                <label class="custom-control-label" for="edit_is_aktif">Aktifkan Iklan</label>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">
                            <i class="fas fa-times mr-1"></i> Batal
                        </button>
                        <button type="submit" class="btn btn-warning">
                            <i class="fas fa-save mr-1"></i> Kemaskini
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('js'); ?>
    <script>
        $('#modalEdit').on('show.bs.modal', function(e) {
            var btn = $(e.relatedTarget);
            var id = btn.data('id');
            var tajuk = btn.data('tajuk');
            var kandungan = btn.data('kandungan');
            var targetRole = btn.data('target-role');
            var tarikhMula = btn.data('tarikh-mula');
            var tarikhTamat = btn.data('tarikh-tamat');
            var isAktif = btn.data('is-aktif');

            $('#edit_tajuk').val(tajuk);
            $('#edit_kandungan').val(kandungan);
            $('#edit_tarikh_mula').val(tarikhMula);
            $('#edit_tarikh_tamat').val(tarikhTamat);
            $('#edit_is_aktif').prop('checked', isAktif == '1');

            // Reset all checkboxes then tick the correct ones
            $('.edit-role-check').prop('checked', false);
            if (targetRole) {
                $.each(targetRole, function(i, role) {
                    $('#edit_role_' + role).prop('checked', true);
                });
            }

            var lampiran = btn.data('lampiran');
            var lampiranNama = btn.data('lampiran-nama');

            if (lampiran) {
                $('#edit_lampiran_link').attr('href', lampiran).text(lampiranNama);
                $('#edit_lampiran_semasa').removeClass('d-none');
            } else {
                $('#edit_lampiran_semasa').addClass('d-none');
                $('#edit_lampiran_link').attr('href', '#').text('');
            }

            // Reset file input label
            $('#edit_lampiran').val('');
            $('#edit_lampiran').next('.custom-file-label').text('Pilih fail...');

            var url = '<?php echo e(url('admin/iklan')); ?>/' + id;
            $('#formEdit').attr('action', url);
        });

        // Update custom-file-label when file selected
        $(document).on('change', '.custom-file-input', function() {
            var fileName = $(this).val().split('\\').pop();
            $(this).next('.custom-file-label').text(fileName || 'Pilih fail...');
        });
    </script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('adminlte::page', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\myspbt\resources\views/admin/iklan/index.blade.php ENDPATH**/ ?>