{{#restrict section="config" level="admin" block=true}}{{/restrict}}
{{:admin_header title="Gestion des matériels" current="module_equipment"}}

{{* barre de navigation *}}
{{:include file="/%s/_nav.html"|args:$module.name current="entrees"}}

{{* types d'entrées *}}
{{#foreach from=$module.config.inputNature item="elem"}}
        {{:assign var='types_entrees.' value="%s"|args:$elem.label}}
{{/foreach}}

{{* Traiter l'envoi du formulaire *}}
{{#form on="save"}}
        {{* récupérer les infos de la catégorie *}}
        {{#load type="category" key=$_POST.category assign="category"}}
        {{/load}}

        {{* enregistrer un nouveau matériel *}}
        {{:assign equipment_key=""|uuid}}
        {{:save
                key=$equipment_key
                validate_schema="/%s/equipment.schema.json"|args:$module.name
                type="equipment"
                category=$category.key
                designation=$_POST.designation|trim
        }}

        {{* Enregistrer le mouvement *}}
        {{:assign mvt_key=""|uuid}}
        {{:assign var="operation" from="types_entrees.%d|args:$_POST.type_operation}}

        {{:save
                key=$mvt_key
                validate_schema="/%s/mouvements/movement.schema.json"|args:$module.name
                type="movement"
                direction="entrée"
                inputNature=$operation
                amount=$_POST.quantite|intval
                equipment=$equipment_key
                date=$_POST.date|parse_date
                comment=$_POST.remarques|trim
        }}

        {{:redirect force="index.html?ok=1&msg=Entrée de « %s » enregistrée"|args:$_POST.designation}}
{{else}}
        {{:form_errors}}
{{/form}}

{{* lister les catégories disponibles *}}
{{#load type="category" assign="category" order="$$.name"}}
{{* :assign var="categories.%d"|args:$category.id value=$category.name *}}
{{:assign var="categories.%s"|args:$category.key value=$category.name}}
{{/load}}

{{* formulaire d'ajout de matériel *}}
<form method="post" action="">

  <fieldset class="entree">
    <legend>Ajouter une entrée d'un matériel non répertorié</legend>
    {{:input type="select" name="type_operation" label="Type" required=true options=$types_entrees}}
    {{:input type="date" name="date" label="Date" required=true default=$now|date_short}}
    {{:input type="number" name="quantite" label="Quantité" required=true default=1}}

    <fieldset>
      <legend>Matériel</legend>
      {{:input type="select" name="category" label="Catégorie" required=true options=$categories}}
      {{:input type="text" name="designation" label="Désignation" required=true}}
      {{:input type="textarea" name="remarques" label="Remarques" cols="40", rows="3" required=false}}
    </fieldset>
  </fieldset>

  <p class="submit">
    {{:button type="submit" name="save" label="Enregistrer" shape="right" class="main"}}
  </p>

</form>
{{:admin_footer}}