<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/** MEDIA LIBRARY — Expects: $items, $folders, $cur_folder. */
?>
<div class="page-head">
	<div><h2>Media Library</h2><p>Central store for uploaded files and assets.</p></div>
</div>

<div class="card">
	<div class="card-head"><h3>Upload Files</h3></div>
	<div class="card-body">
		<form action="<?= base_url('admin/media/upload_files') ?>" method="post" enctype="multipart/form-data">
			<?= form_hidden($this->security->get_csrf_token_name(), $this->security->get_csrf_hash()) ?>
			<div class="form-grid cols-2">
				<div class="fld"><label>Files</label>
					<?php $this->load->view('admin/partials/_upload_field', array(
						'name'     => 'files',
						'multiple' => TRUE,
						'required' => TRUE,
						'label'    => 'Choose files',
						'hint'     => 'Select one or more files to add to the library.',
					)); ?></div>
				<div class="fld"><label>Folder</label>
					<select name="folder">
						<?php foreach (array('media','portfolio','events','talent','blog','partners','team') as $f): ?>
							<option value="<?= $f ?>"><?= ucfirst($f) ?></option>
						<?php endforeach; ?>
					</select>
				</div>
			</div>
			<div class="form-actions">
				<button type="submit" class="btn btn-primary"><?= bloom_icon('plus', 16) ?> Upload</button>
			</div>
		</form>
	</div>
</div>

<?php if ($folders): ?>
<div class="filter-row">
	<div class="tabs">
		<a href="<?= base_url('admin/media') ?>" class="tab <?= ! $cur_folder ? 'active' : '' ?>">All</a>
		<?php foreach ($folders as $f): ?>
			<a href="<?= base_url('admin/media?folder='.$f) ?>" class="tab <?= $cur_folder === $f ? 'active' : '' ?>"><?= ucfirst($f) ?></a>
		<?php endforeach; ?>
	</div>
</div>
<?php endif; ?>

<div class="card"><div class="card-body">
	<?php if ($items): ?>
		<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(150px,1fr));gap:14px;">
			<?php foreach ($items as $it):
				$ext = strtolower($it['file_type']);
				$is_img = in_array($ext, array('jpg','jpeg','png','webp','gif','svg'));
			?>
				<div style="border:1.5px solid var(--line);border-radius:11px;overflow:hidden;">
					<div style="height:120px;background:#f4f1f5;display:grid;place-items:center;">
						<?php if ($is_img): ?>
							<img src="<?= upload_url($it['file_path']) ?>" style="width:100%;height:100%;object-fit:cover;">
						<?php else: ?>
							<span style="text-transform:uppercase;font-weight:800;color:var(--iris);"><?= html_escape($ext) ?></span>
						<?php endif; ?>
					</div>
					<div style="padding:8px 10px;">
						<div style="font-size:.76rem;font-weight:700;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"><?= html_escape($it['file_name']) ?></div>
						<div style="display:flex;justify-content:space-between;align-items:center;margin-top:5px;">
							<span class="muted" style="font-size:.72rem;"><?= number_format($it['file_size']/1024, 0) ?> KB</span>
							<a href="<?= base_url('admin/media/delete/'.$it['id']) ?>" class="icon-btn danger" style="width:26px;height:26px;" data-confirm="Delete this file?"><?= bloom_icon('trash', 13) ?></a>
						</div>
					</div>
				</div>
			<?php endforeach; ?>
		</div>
	<?php else: ?>
		<div class="empty"><?= bloom_icon('camera', 44) ?><p>No media files yet. Upload your first files above.</p></div>
	<?php endif; ?>
</div></div>
