<?php
$created = can_action('100', 'created');
$edited = can_action('100', 'edited');
if (!empty($created) || !empty($edited)) {
    ?>
        <div class="p-0">
            <form role="form" id="form" data-parsley-validate="" novalidate="" enctype="multipart/form-data"
                  action="<?php echo base_url(); ?>admin/announcements/save_announcements/<?= (!empty($announcements->announcements_id) ? $announcements->announcements_id : ''); ?>"
                  method="post" class="space-y-6">

                <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                    <div class="space-y-6">
                        <div class="rounded-xl border border-border bg-card p-4 space-y-4">
                            <div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
                                <i data-lucide="megaphone" class="h-4 w-4 text-muted-foreground"></i>
                                <h4 class="text-sm font-semibold text-foreground"><?= lang('announcements') ?></h4>
                            </div>
                            <?= form_input_group(
                                'title',
                                lang('title'),
                                (!empty($announcements->title) ? $announcements->title : ''),
                                'text',
                                true,
                                array('required' => 'required'),
                                '',
                                '',
                                'horizontal'
                            ); ?>

                            <?= form_textarea_group(
                                'description',
                                lang('description'),
                                (!empty($announcements->description) ? $announcements->description : ''),
                                6,
                                false,
                                array('class' => 'textarea'),
                                'horizontal'
                            ); ?>
                        </div>

                        <div class="rounded-xl border border-border bg-card p-4 space-y-4">
                            <div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
                                <i data-lucide="calendar" class="h-4 w-4 text-muted-foreground"></i>
                                <h4 class="text-sm font-semibold text-foreground"><?= lang('schedule') ?></h4>
                            </div>
                            <?= form_datepicker(
                                'start_date',
                                lang('start_date'),
                                (!empty($announcements->start_date) ? $announcements->start_date : ''),
                                true,
                                array('class' => 'start_date', 'data-max' => '#end_date', 'data-auto-close' => 'true'),
                                '',
                                '',
                                'horizontal'
                            ); ?>

                            <?= form_datepicker(
                                'end_date',
                                lang('end_date'),
                                (!empty($announcements->end_date) ? $announcements->end_date : ''),
                                true,
                                array('class' => 'end_date', 'data-min' => '#start_date', 'data-auto-close' => 'true'),
                                '',
                                '',
                                'horizontal'
                            ); ?>
                        </div>
                    </div>

                    <div class="space-y-6">
                        <div class="rounded-xl border border-border bg-card p-4 space-y-4">
                            <div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
                                <i data-lucide="paperclip" class="h-4 w-4 text-muted-foreground"></i>
                                <h4 class="text-sm font-semibold text-foreground"><?= lang('attachment') ?></h4>
                            </div>
                            <?= form_attachment_group('files[]', lang('attachment'), 'dropzone', array(
                                'dropzone_id' => 'announcements-files-dropzone',
                                'list_id' => 'announcements-files-list',
                                'wrap' => false,
                            )); ?>

                            <?php
                            $uploaded_file = array();
                            if (!empty($announcements->attachment)) {
                                $decoded_files = json_decode($announcements->attachment);
                                $uploaded_file = is_array($decoded_files) ? $decoded_files : array();
                            }
                            if (!empty($uploaded_file)) { ?>
                                <div class="mt-4 grid grid-cols-1 gap-3">
                                    <?php foreach ($uploaded_file as $v_files_image) { ?>
                                        <div class="flex items-center gap-3 rounded-lg border border-border bg-muted/30 p-3">
                                            <?php if (!empty($v_files_image->is_image)) { ?>
                                                <img src="<?php echo base_url() . $v_files_image->path ?>"
                                                     class="h-10 w-10 rounded object-cover" alt="">
                                            <?php } else { ?>
                                                <span class="inline-flex h-10 w-10 items-center justify-center rounded bg-card text-muted-foreground">
                                                    <i data-lucide="file-text" class="h-5 w-5"></i>
                                                </span>
                                            <?php } ?>
                                            <span class="min-w-0 flex-1 truncate text-sm text-foreground"><?php echo html_escape($v_files_image->fileName); ?></span>

                                            <input type="hidden" name="path[]" value="<?php echo html_escape($v_files_image->path); ?>">
                                            <input type="hidden" name="fileName[]" value="<?php echo html_escape($v_files_image->fileName); ?>">
                                            <input type="hidden" name="fullPath[]" value="<?php echo html_escape($v_files_image->fullPath); ?>">
                                            <input type="hidden" name="size[]" value="<?php echo html_escape($v_files_image->size); ?>">
                                            <input type="hidden" name="is_image[]" value="<?php echo html_escape($v_files_image->is_image); ?>">
                                        </div>
                                    <?php } ?>
                                </div>
                            <?php } ?>
                        </div>

                        <div class="rounded-xl border border-border bg-card p-4 space-y-4">
                            <div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
                                <i data-lucide="share-2" class="h-4 w-4 text-muted-foreground"></i>
                                <h4 class="text-sm font-semibold text-foreground"><?= lang('share_with') ?></h4>
                            </div>
                            <div class="flex items-start gap-3 py-2">
                                <label class="w-48 text-sm font-medium text-foreground pt-1"><?= lang('share_with') ?></label>
                                <div class="flex items-center gap-3">
                                    <label class="relative inline-flex items-center cursor-pointer select-none">
                                        <input type="checkbox" name="all_client" value="1" class="sr-only peer" <?= (!empty($announcements->all_client) ? 'checked' : '') ?>>
                                        <span class="w-11 h-6 bg-muted rounded-full peer peer-checked:bg-indigo-600 transition-colors"></span>
                                        <span class="absolute left-1 top-1 h-4 w-4 rounded-full bg-white transition-transform peer-checked:translate-x-5"></span>
                                    </label>
                                    <span class="text-sm text-foreground"><?= lang('all_client') ?></span>
                                </div>
                            </div>

                            <?php
                            $status_selected = (!empty($announcements->status) && $announcements->status === 'published') ? 'Yes' : 'No';
                            ?>
                            <?= form_switch_radio(
                                'status',
                                lang('published'),
                                $status_selected,
                                false,
                                array(),
                                'horizontal',
                                array('Yes' => lang('published'), 'No' => lang('unpublished'))
                            ); ?>
                        </div>
                    </div>
                </div>

                <?php
                if (!empty($announcements)) {
                    $announcements_id = $announcements->announcements_id;
                } else {
                    $announcements_id = null;
                }
                ?>
                <?= custom_form_Fields(16, $announcements_id); ?>

                <div class="flex items-center justify-end gap-2 border-t border-border pt-4">
                    <button type="button" class="inline-flex items-center rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted" data-dismiss="modal"><?= lang('close') ?></button>
                    <button type="submit" id="file-save-button"
                            class="inline-flex items-center rounded-lg bg-indigo-600 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-indigo-500"><?= lang('save') ?></button>
                </div>
            </form>
        </div>
<?php } ?>

