<?php
$file_exists = function ($file) {
    if (!empty($file->uploaded_path) && is_file($file->uploaded_path)) {
        return true;
    }
    if (!empty($file->files)) {
        $relative = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, ltrim($file->files, '/\\'));
        $full = getcwd() . DIRECTORY_SEPARATOR . $relative;
        if (is_file($full)) {
            return true;
        }
    }
    if (!empty($file->file_name)) {
        $fallback = getcwd() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $file->file_name;
        if (is_file($fallback)) {
            return true;
        }
    }
    return false;
};

if (!empty($files_info)) {
    foreach ($files_info as $key => $v_files_info) {
        if (empty($v_files_info) || empty($v_files_info[0])) {
            continue;
        }
        $uploader_name = !empty($v_files_info[0]->fullname) ? $v_files_info[0]->fullname : '';
        if ($uploader_name === '' && !empty($v_files_info[0]->user_id)) {
            $uploader = $this->db->select('fullname')->where('user_id', $v_files_info[0]->user_id)->get('tbl_account_details')->row();
            $uploader_name = !empty($uploader->fullname) ? $uploader->fullname : '';
        }
        if ($uploader_name === '') {
            $uploader_name = lang('unknown');
        }
        $group_has_file = false;
        if (!empty($v_files_info)) {
            foreach ($v_files_info as $entry) {
                if ($file_exists($entry)) {
                    $group_has_file = true;
                    break;
                }
            }
        }
        ?>
        <div id="attachment_container-<?= $v_files_info[0]->attachments_id ?>" class="project-inline-card min-w-0 rounded-2xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-800/90">
            <div class="flex flex-col gap-3 border-b border-slate-200 pb-3 sm:flex-row sm:items-start sm:justify-between dark:border-slate-700">
                <div>
                    <?= btn_modal(
                        'admin/common/attachment_details/g/' . $v_files_info[0]->attachments_id,
                        html_escape($v_files_info[0]->title),
                        html_escape($v_files_info[0]->title),
                        '2xl',
                        '',
                        array(
                            'class' => 'text-sm font-semibold text-slate-900 hover:underline dark:text-slate-100',
                        )
                    ) ?>
                    <div class="text-xs text-slate-500 dark:text-slate-400">
                        <span class="font-medium text-slate-700 dark:text-slate-200"><?= html_escape($uploader_name) ?></span>
                        &bull; <?= count($v_files_info) . ' ' . lang('attachment') ?>
                        &bull; <?= display_datetime($v_files_info[0]->upload_time) ?>
                    </div>
                </div>
                <div class="flex flex-wrap items-center gap-2">
                    <?php if ($group_has_file) { ?>
                        <a data-tooltip="<?= lang('download') . ' ' . lang('all') ?>"
                           href="<?= base_url() ?>admin/common/download_all_files/<?= $v_files_info[0]->attachments_id ?>"
                           class="inline-flex items-center justify-center rounded-lg border border-slate-200 bg-white p-2 text-slate-600 hover:bg-slate-50 dark:border-slate-700 mb-1 h-7 dark:bg-slate-900 dark:text-slate-200 dark:hover:bg-slate-700">
                            <i data-lucide="download" class="h-4 w-4"></i>
                        </a>
                    <?php } ?>
                    <?php if ($v_files_info[0]->user_id == $this->session->userdata('user_id')) { ?>
                        <?php echo ajax_anchor(
                            base_url("admin/common/delete_attach_files/" . $v_files_info[0]->attachments_id),
                            "",
                            array(
                                "class" => "inline-flex items-center justify-center rounded-lg border border-rose-200 bg-rose-50 p-2 text-rose-600 hover:bg-rose-100 dark:border-rose-900/60 dark:bg-rose-950/30 dark:text-rose-300 dark:hover:bg-rose-900/40",
                                "title" => lang('delete'),
                                "data-fade-out-on-success" => "#attachment_container-" . $v_files_info[0]->attachments_id
                            )
                        ); ?>
                    <?php } ?>
                </div>
            </div>
            <?php
            $limit = 3;
            shuffle($v_files_info);
            if (!empty($v_files_info)) {
                ?>
                <div class="mt-4 grid grid-cols-1 gap-3 sm:grid-cols-2">
                    <?php
                    foreach ($v_files_info as $l_key => $v_files) {
                        if (!empty($v_files) && $l_key <= $limit) {
                            $rawFileName = isset($v_files->file_name) ? (string) $v_files->file_name : '';
                            $fileName = (strlen($rawFileName) >= 19)
                                ? substr($rawFileName, 0, 15) . '...'
                                : $rawFileName;
                            $exists = $file_exists($v_files);
                            ?>
                            <?php if ($exists) { ?>
                                <a data-toggle="modal" data-target="#myModal_extra_lg"
                                   href="<?= base_url() ?>admin/common/attachment_details/r/<?= $v_files->attachments_id . '/' . $v_files->uploaded_files_id ?>"
                                   class="group relative overflow-hidden rounded-xl border border-slate-200 bg-slate-50 dark:border-slate-700 dark:bg-slate-900/40">
                                    <?php if ($v_files->is_image == 1) : ?>
                                        <img class="h-28 w-full object-cover" src="<?= base_url() . $v_files->files ?>" alt="">
                                    <?php else : ?>
                                        <div class="flex h-28 items-center justify-center text-slate-400 dark:text-slate-500">
                                            <i data-lucide="file" class="h-8 w-8"></i>
                                        </div>
                                    <?php endif; ?>
                                    <div class="border-t border-slate-200 bg-white px-3 py-2 dark:border-slate-700 dark:bg-slate-800">
                                        <div class="flex items-center justify-between text-xs text-slate-500 dark:text-slate-400">
                                            <span class="truncate text-slate-700 dark:text-slate-200"><?= $fileName ?></span>
                                            <span><?= $v_files->size ?> <?= lang('kb') ?></span>
                                        </div>
                                    </div>
                                    <span class="pointer-events-none absolute inset-0 bg-slate-900/0 transition group-hover:bg-slate-900/10 dark:group-hover:bg-slate-50/5"></span>
                                </a>
                            <?php } else { ?>
                                <div class="overflow-hidden rounded-xl border border-amber-300 bg-amber-50 dark:border-amber-700/60 dark:bg-amber-950/20">
                                    <div class="flex h-28 flex-col items-center justify-center gap-2 text-amber-700 dark:text-amber-300">
                                        <i data-lucide="file-warning" class="h-8 w-8"></i>
                                        <span class="text-xs font-semibold">File not found</span>
                                    </div>
                                    <div class="border-t border-amber-200 bg-white px-3 py-2 dark:border-amber-800/70 dark:bg-slate-800">
                                        <div class="flex items-center justify-between text-xs text-slate-500 dark:text-slate-400">
                                            <span class="truncate text-slate-700 dark:text-slate-200"><?= $fileName !== '' ? $fileName : '-' ?></span>
                                            <span><?= !empty($v_files->size) ? $v_files->size : 0 ?> <?= lang('kb') ?></span>
                                        </div>
                                    </div>
                                </div>
                            <?php } ?>
                        <?php } ?>
                    <?php } ?>
                </div>
                <?php
                $more = count($v_files_info) - 4;
                if (!empty($more) && $more > 0) {
                    ?>
                    <a data-toggle="modal" data-target="#myModal_extra_lg"
                       href="<?= base_url() ?>admin/common/attachment_details/g/<?= $v_files_info[0]->attachments_id ?>"
                       class="mt-3 inline-flex items-center gap-2 text-xs font-semibold text-primary hover:underline dark:text-sky-400">
                        +<?= $more ?> <?= lang('more') ?>
                    </a>
                    <?php
                }
            } ?>
        </div>
        <?php
    }
} ?>
