<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * STYLED FILE UPLOAD FIELD — reusable partial.
 *
 * A professional dropzone-style upload control: the native file input is
 * visually hidden and a branded label acts as the button. The chosen
 * filename is shown live (handled by admin.js).
 *
 * IMPORTANT: CodeIgniter's loader ACCUMULATES view variables across every
 * $this->load->view() call within the same controller request. A value set
 * by one partial load (e.g. multiple=TRUE for a gallery field) would leak
 * into the next load. To stay safe, this partial reads its inputs from a
 * single $cfg array and resets every option to an explicit default — it
 * never relies on isset() of a loose variable.
 *
 * Usage:
 *   $this->load->view('admin/partials/_upload_field', array('cfg' => array(
 *       'name'     => 'cover_image',   // required
 *       'accept'   => 'image/*',       // optional
 *       'multiple' => FALSE,           // optional
 *       'required' => FALSE,           // optional
 *       'preview'  => '#imgPrev',      // optional — img selector
 *       'label'    => 'Choose image',  // optional — button text
 *       'hint'     => '...',           // optional — helper text
 *       'icon'     => 'image',         // optional — bloom_icon name
 *   )));
 */

// Pull config from $cfg if provided, else fall back to loose vars.
$_uf = array();
if (isset($cfg) && is_array($cfg))
{
	$_uf = $cfg;
}
else
{
	foreach (array('name','accept','multiple','required','preview','label','hint','icon') as $_k)
	{
		if (isset($$_k)) $_uf[$_k] = $$_k;
	}
}

// Explicit defaults for EVERY option — nothing inherits from a prior load.
$uf_name     = isset($_uf['name'])     ? $_uf['name']     : 'file';
$uf_accept   = isset($_uf['accept'])   ? $_uf['accept']   : '';
$uf_multiple = ! empty($_uf['multiple']);
$uf_required = ! empty($_uf['required']);
$uf_preview  = isset($_uf['preview'])  ? $_uf['preview']  : '';
$uf_label    = isset($_uf['label'])    ? $_uf['label']    : 'Choose file';
$uf_hint     = isset($_uf['hint'])     ? $_uf['hint']     : '';
$uf_icon     = isset($_uf['icon'])     ? $_uf['icon']     : 'upload';
$uf_uid      = 'up_' . substr(md5($uf_name . uniqid('', TRUE)), 0, 8);
?>
<div class="upload-field" data-upload>
	<input
		type="file"
		id="<?= $uf_uid ?>"
		name="<?= html_escape($uf_name) ?><?= $uf_multiple ? '[]' : '' ?>"
		class="upload-input"
		<?= $uf_accept ? 'accept="'.html_escape($uf_accept).'"' : '' ?>
		<?= $uf_multiple ? 'multiple' : '' ?>
		<?= $uf_required ? 'required' : '' ?>
		<?= $uf_preview ? 'data-preview="'.html_escape($uf_preview).'"' : '' ?>>
	<label for="<?= $uf_uid ?>" class="upload-label">
		<span class="upload-btn">
			<?= bloom_icon($uf_icon, 17) ?>
			<span><?= html_escape($uf_label) ?></span>
		</span>
		<span class="upload-name" data-upload-name>No file selected</span>
	</label>
	<?php if ($uf_hint): ?>
		<p class="hint"><?= $uf_hint ?></p>
	<?php endif; ?>
</div>
