
<form class="form-" id="modif_dmde" name="modif_dmde" method="post" action="./empr.php?tab=request&amp;lvl=list_dmde&amp;sub=save_demande">
    <h1>{{ msg.demandes_creation }}  : {{ demande.titre_demande }}</h1>
    <input type="hidden" id="act" name="act" value="save">
	<input type="hidden" id="iddemande" name="iddemande" value="{{ demande.id_demande }}">
	<input type="hidden" id="idempr" name="idempr" value="{{ id_empr }}">
	<input type="hidden" id="iduser" name="iduser" value="{{ demande.num_user }}">
	<div class="form-contenu">
        <div class="row">
            <div class="colonne3">
                <label class="etiquette" for="id_theme">{{ msg.demandes_theme }}</label>
            </div>
            <div class="colonne-suite">
                <select id="id_theme" name="id_theme" required onchange="update_types(event.target.value)">
                    {% for theme in themes %}
                        <option value="{{ theme.id_theme }}" {% if theme.id_theme == demande.theme_demande %}selected{% endif %}>{{ theme.libelle_theme }}</option>
                    {% endfor %}
                </select>
            </div>
        </div>
        <div class="row">
            <div class="colonne3">
                <label class="etiquette" for="id_type">{{ msg.demandes_type }}</label>
            </div>
            <div class="colonne-suite">
                <select id="id_type" name="id_type" onchange="update_pperso(event)" required>
                    {% for type in types %}
                        <option value="{{ type.id_type }}" {% if type.id_type == demande.type_demande %}selected{% endif %}>{{ type.libelle_type }}</option>
                    {% endfor %}
                </select>
            </div>
        </div>
        <div class="row">
            <div class="colonne3">
                <label class="etiquette" for="etat">{{ msg.demandes_etat }}</label>
            </div>
            <div class="colonne-suite">
                {% for val in demande.liste_etat %}
                    {% if demande.etat_demande ==  0 and val.default %}
                        <input name="etat" disabled value="{{ val.comment }}" />
                        <input type="hidden" id="idetat" name="idetat" value="{{ val.id }}" class="">
                    {% else %}
                        {% if demande.etat_demande == val.id %}
                            <input name="etat" disabled value="{{ val.comment }}" />
                            <input type="hidden" id="idetat" name="idetat" value="{{ demande.etat_demande }}" class="">
                        {% endif %}
                    {% endif %}
                {% endfor %}
            </div>
        </div>
        <div class="row">
            <div class="colonne3">
                <label class="etiquette" for="titre">{{ msg.demandes_titre }}</label>
            </div>
            <div class="colonne-suite">
                <input type="text" id="titre" name="titre" value="{{ demande.titre_demande }}" class="saisie-50em" required>
            </div>
        </div>
        <div class="row">
            <div class="colonne3">
                <label class="etiquette" for="sujet">{{ msg.demandes_sujet }}</label>
            </div>
            <div class="colonne-suite">
                <textarea id="sujet" name="sujet" class="" cols="55" rows="4">{{ demande.sujet_demande }}</textarea>
            </div>
        </div>
        <div class="row">
            <div class="colonne3">
                <label class="etiquette" for="date_debut">{{ msg.demandes_date_dmde }}</label>
            </div>
            <div class="colonne-suite">
                <input type="date" id="date_debut" name="date_debut" value="{{ demande.date_demande }}" class="saisie-15em" required>
            </div>
        </div>
        <div class="row">
            <div class="colonne3">
                <label class="etiquette" for="date_prevue">{{ msg.demandes_date_prevue }}</label>
            </div>
            <div class="colonne-suite">
                <input type="date" id="date_prevue" name="date_prevue" value="{{ demande.date_prevue }}" class="saisie-15em" required>
            </div>
        </div>
        <div class="row">
            <div class="colonne3">
                <label class="etiquette" for="date_fin">{{ msg.demandes_date_butoir }}</label>
            </div>
            <div class="colonne-suite">
                <input type="date" id="date_fin" name="date_fin" value="{{ demande.deadline_demande }}" class="saisie-15em" required>
            </div>
        </div>
        <div class="demandes_pperso">
            <!--{% for pperso in pperso %}
                <div class="row">
                    <div class="colonne3">
                        <label class="etiquette" for="{{ pperso.NAME }}">{{ pperso.TITRE }}</label>
                    </div>
                    <div class="colonne-suite">
                        {{ pperso.AFF }}
                    </div>
                </div>
            {% endfor %}-->
            {{ pperso }}
        </div>

        {% if linked_record %}
            <div id="linked_record">
                <label class="etiquette" for="demandes_linked_record">{{ msg.demandes_linked_record }}</label>
                <a href='{{ linked_record.permalink }}' title='{{ linked_record.tit1 }}' id='demandes_linked_record'>{{ linked_record.tit1 }}</a>
                <input type='hidden' name='linked_record_id' value='{{ demande.num_linked_notice }}' />
            </div>
        {% endif %}

        <div class="row">
            <div class="left">
                <a href="./empr.php?tab=request&amp;lvl=list_dmde">{{ msg.76 }}</a>
                <input type="submit" class="bouton" value="{{ msg.77 }}">
            </div>
        </div>
    </div>
</form>

<script>
	function test_form_demand(form) {
		if(form.titre.value.length == 0){
			alert("{{ msg.demandes_create_ko }}");
			return false;
	    }
		var deb = dijit.byId('date_debut').get('value');
		var end = dijit.byId('date_fin').get('value');
		if(!deb || !end){
			alert("{{ msg.demandes_create_no_date }}");
			return false;
	    }
 		var date_debut = dojo.date.stamp.toISOString(deb, {selector: 'date'});
 		var date_fin = dojo.date.stamp.toISOString(end, {selector: 'date'});

	    if(date_debut > date_fin){
	    	alert("{{ msg.demandes_date_ko }}");
	    	return false;
	    }
		return true;

	}

	function update_pperso(event) {
	    let idType = parseInt(event.target.value);

		let url='./ajax.php?module=ajax&categ=demandes&quoifaire=get_pperso_form&id_demande={{ demande.id_demande }}&type_demande=' + idType;
		let xhr_object=  new http_request();
		xhr_object.request(url);

		//On passe par une classe car dojo on a plusieurs fois les memes id a cause des popups dojo
		var ppersoDivs = event.target.form.getElementsByClassName('demandes_pperso');
		for(let div of ppersoDivs) {
			div.innerHTML = xhr_object.get_text();
		}
	}
	
	function update_types(idTheme) {
        idTheme = parseInt(idTheme);

        let url='./ajax.php?module=ajax&categ=demandes&quoifaire=get_allowed_types&id_theme=' + idTheme;
        let xhr_object=  new http_request();
        xhr_object.request(url);
        let allowed_types = '';
        if('' != xhr_object.get_text()) {
            allowed_types = JSON.parse(xhr_object.get_text());
        }
        let type_selector = document.getElementById('id_type');
        for (let option of type_selector.options) {
            if( ('' == allowed_types) || (1 == allowed_types[option.value])) {
                option.disabled = false;
            } else {
                option.disabled = true;
            }
            if(allowed_types.default == option.value) {
                option.selected = true;
            }
        }
        if('' == allowed_types) {
            type_selector.options[0].selected = true;
        }
    }

    window.onload = function() {
        let idTheme = document.getElementById('id_theme').selectedOptions[0].value;
        update_types(idTheme);
    }
</script>
