{extends file="views/layout.tpl"}

{block name="h2"}Ajouter un projet{/block}
{block name="p"}Ajouter un projet à votre portfolio{/block}


{block name="content"}
<section class="container mt-4">
   {if isset($smarty.session.user)}
    <form class="m-2" method="POST" enctype="multipart/form-data" onsubmit="return confirm('Êtes-vous certain de vouloir poster votre projet ?')">
        
        <div class="mb-3">
            <label class="form-label">Titre</label>
            <input type="text" name="title" class="form-control {if (isset($arrError['title'])) } is-invalid {/if} " value="{if $objProject}{$objProject->getTitle()}{/if}" required>
        </div>

        <div class="mb-3">
            <label class="form-label">Description (courte)</label>
            <textarea name="description" class="form-control {if (isset($arrError['descProject'])) } is-invalid {/if} " rows="2">{if $objProject}{$objProject->getDescription()}{/if}</textarea>
        </div>

        <div class="mb-3">
            <label class="form-label">Texte complet du Portfolio</label>
            <textarea name="content" class="form-control {if (isset($arrError['content'])) } is-invalid {/if}" rows="5">{if $objProject}{$objProject->getContent()}{/if}</textarea>
        </div>

        <div class="col-6 mb-3">
            <fieldset class="border p-2 rounded">
                <legend class="form-label h6">Catégorie</legend>
                <select class="form-select" id="category" name="category">
				<option value="0" {if $objProject->getCategory() == 0}selected{/if}>Toutes les catégories</option>
				{foreach $arrCategory as $arrDetCategory}
				<option value="{$arrDetCategory['category_id']}" 
					{if $objProject->getCategory() == $arrDetCategory['category_id']}selected{/if}>
					{$arrDetCategory['category_name']}
				</option>
				{/foreach}
				</select>
            </fieldset>
        </div>

        <div class="mb-3">
            <label class="h5">Miniature de votre projet</label>
            <input name="thumbnail" class="form-control" type="file">
			{if $objProject && $objProject->getId()}
				<label class="h5">Miniature actuelle :</label>
				<img src="{$smarty.env.IMG_PROJECT_PATH}{$objProject->getThumbnail()}" alt="Miniature">
			{/if}
        </div>

        <div class="mb-3">
			<label class="form-label h5">Galerie de photos (Max 20)</label>
			{assign var="nbActuel" value=$arrImages|@count}
			{assign var="disponible" value=20 - $nbActuel}

			{if $disponible > 0}
				<p class="small text-muted">Il vous reste <strong>{$disponible}</strong> emplacement(s) libre(s).</p>
				<input type="file" name="imageProject[]" multiple class="form-control" accept="image/*">
			{else}
				<div class="alert alert-warning p-2">
					<i class="fas fa-exclamation-triangle"></i> Quota de 20 photos atteint.
				</div>
				{* On désactive l'input si le quota est plein *}
				<input type="file" disabled class="form-control">
			{/if}
		</div>
		
		{* Affichage de Images du projet (s'il y en a)*}
		<div class="row mt-4">
			<label class="h5">Galerie du projet</label>
			{foreach $arrImages as $image}
				<div class="col-md-3 mb-3 text-center">
					<div class="card shadow-sm h-100 border-{if $image.image_status == 'approuvé'}success{elseif $image.image_status == 'refusé'}danger{else}warning{/if}">
						<img src="{$smarty.env.IMG_PROJECT_PATH}{$image.image_name}" 
							 class="card-img-top img-thumbnail" 
							 alt="{$image.image_alt}" 
							 style="height: 150px; object-fit: cover;">
						
						<div class="card-body p-2">
							<span class="badge {if $image.image_status == 'approuvé'}bg-success{elseif $image.image_status == 'refusé'}bg-danger{else}bg-warning text-dark{/if}">
								{*Permet de remplacer certains character par d'autre*}
								{$image.image_status|replace:'_':' '}
							</span>
							
							<div class="mt-2 d-flex flex-column gap-1">
								<a href="index.php?ctrl=project&action=delete_image&id_img={$image.image_id}" 
								   class="btn btn-sm btn-danger" 
								   onclick="return confirm('Supprimer définitivement cette image ?')">Supprimer</a>
							</div>
						</div>
					</div>
				</div>
			{foreachelse}
				<p class="text-muted italic">Aucune photo dans la galerie pour le moment.</p>
			{/foreach}
		</div>

        <div class="mt-4">
            <button type="submit" class="btn btn-primary btn-md" name="sendProject">Envoyer</button>
        </div>
    </form>
	{else}
		<div class="alert alert-danger mt-5">
			<p class="mb-0">Vous devez vous connecter pour accéder à cette fonctionnalité</p>
		</div>
	{/if}
</section>
{/block}