{% extends "custom_field.tpl.html" %}
<!-- $Id: custom_field_date_box.tpl.html,v 1.1.2.2 2026/03/16 13:14:16 rtigero Exp $ -->

{% block field %}
<div id="div_{{ custom_field.NAME }}">
	{% for value in custom_field.VALUES %}
		<input type="date" name="{{ custom_field.NAME }}[]" id="{{ custom_field.NAME }}_val_{{ loop.counter0 }}" value="{{ value }}" onchange="" class="pmb-input" aria-describedby="pmb-input-{{ custom_field.NAME }}-desc-error">
		<button type="button" class="bouton" title="{{ msg.date_reset_btn_title }}" onclick='document.getElementById("{{ custom_field.NAME }}_val_{{ loop.counter0 }}").value = "";'>
			<span aria-hidden="true">{{ msg.custom_fields_raz_button }}</span>
		</button>
	{% endfor %}
	<input id="customfield_date_box_{{ custom_field.ID }}" type="hidden" name="customfield_date_box_{{ custom_field.ID }}" value="{{ custom_field.VALUES | length }}" class="pmb-input" aria-describedby="pmb-input-{{ custom_field.NAME }}-desc-error">
	{% if custom_field.REPEATABLE == "1" %}
		<button type="button" id="button_add_{{ custom_field.NAME }}_{{ custom_field.ID }}" name="" class="bouton" onclick="add_custom_date_box_('{{ custom_field.ID }}', '{{ custom_field.NAME }}')" aria-label="{{ msg.custom_fields_add_aria_label | printf custom_field.TITRE }}">{{ msg.custom_fields_add_button }}</button>
	{% endif %}
</div>
<script>

	if(typeof use_dojo_calendar == "undefined") {
		var use_dojo_calendar = 0;
	}
	{% if custom_field.REPEATABLE != '1' and custom_field.VALUES.0 == '' %}
		window.addEventListener('load', function() {
			document.getElementById("{{ custom_field.NAME }}_val_0").value = get_date_default_value();
		});
	{% endif %}

	function get_date_default_value()
	{
		let value = "";
		if("{{ custom_field.DEFAULT_TODAY }}" != "yes") {
			value = new Date().toISOString().split('T')[0];
		}
		return value;
	}
	{% if custom_field.REPEATABLE == "1" %}
		function add_custom_date_box_(field_id, field_name) {
			let value = get_date_default_value();
			var count = document.getElementById('customfield_date_box_'+field_id).value;

			var node_id = "div_{{ custom_field.NAME }}";
			var buttonAdd = document.getElementById('button_add_' + field_name + '_' + field_id);

			var val = get_input_date_js(field_name + '[]', field_name + '_val_' + count, value);
			document.getElementById(node_id).appendChild(val);

			var del = document.createElement('button');
			del.setAttribute('type', 'button');
			del.setAttribute('class', 'bouton');
			del.setAttribute('title', '{{ msg.date_reset_btn_title }}');
			del.setAttribute('onClick','document.getElementById("'+field_name + '_val_' + count+'").value = "";');
			del.innerHTML = '<span aria-hidden="true">{{ msg.custom_fields_raz_button }}</span>';

			document.getElementById(node_id).appendChild(document.createTextNode (' '));
			document.getElementById(node_id).appendChild(del);
			if (buttonAdd) document.getElementById(node_id).appendChild(buttonAdd);
			var br = document.createElement('br');
			document.getElementById(node_id).appendChild(br);
			document.getElementById('customfield_date_box_'+field_id).value = document.getElementById('customfield_date_box_'+field_id).value * 1 + 1;
		}
	{% endif %}
</script>
{% endblock %}