<commit_before>{% extends 'layout_semi_wide.html' %}

{% block head %}
  <script type='text/javascript'>
    // <![CDATA[

    var N_BLANKS = 0;

    var DELIVERY = {{delivery.id}};

    var PRODUCTS = [{% for pd in delivery.product_set.all %}
      { 'id': "{{pd.id|escapejs}}",
        'name': "{{pd.name|escapejs}}",
        'price': "{{pd.price|escapejs}}",
        'unit': "{{pd.unit|escapejs}}",
        'quantity_limit': "{{pd.quantity_limit|escapejs}}",
        'quantity_per_package': "{{pd.quantity_per_package|escapejs}}",
        'quantum': "{{pd.quantum|default_if_none:''|escapejs}}",
        'unit_weight': "{{pd.unit_weight|escapejs}}",
        'deleted': {% if pd.delivery.id == delivery.id %} false  {% else %} true {% endif %},
        'delivery': {{pd.delivery.id}} },
    {% endfor %}];

    /* If the "deleted" checkbox is checked, strike theline and disable product edition. */
    function reflect_deletion(prefix) {
      var deleted = $('#'+prefix+'-deleted').is(':checked');
      var inputs = $("#"+prefix+"-row input[type=number], #"+prefix+"-row input[type=text]")
      if( deleted) {
        $("#"+prefix+"-row").addClass('deleted');
        inputs.attr('disabled', 'disabled');
      } else {
        $("#"+prefix+"-row").removeClass('deleted');
        inputs.removeAttr('disabled');
      }
    }

    /* Keep unit names in sync between the input field and the text spans where it occurs. */
    function attach_closure(prefix) {
      var closure = function() {
        var content = $("#"+prefix+"-unit").attr('value');
        $("."+prefix+"-unit-mirror").text(content)
        if(content) {
          $("."+prefix+"-if-unit-mirror").show();
        } else {
          $("."+prefix+"-if-unit-mirror").hide();
        }
      }
      $("#"+prefix+"-unit").keyup(closure);
      closure();
    }

    /* Add rows of blank products at the end of the table */
    function add_row(prefix) {
      $("#products-table").append('\n\
        <tr id="%-row">\n\
            <td class="name"><input id="%-id" name="%-id" type="hidden" /><input id="%-name" maxlength="64" name="%-name" type="text" /></td>\n\
            <td class="price"><input id="%-price" name="%-price" step="0.01" min="0" type="number" /></td>\n\
            <td>€/</td>\n\
            <td class="unit"><input id="%-unit" maxlength="64" name="%-unit" type="text" /></td>\n\
            <td class="qpp"><input id="%-quantity_per_package" name="%-quantity_per_package" type="number" /></td>\n\
            <td class="unit-label">&nbsp;<span class="%-unit-mirror"></span><span class="%-if-unit-mirror">/ct</span></td>\n\
            {% if 0 %}<td class="quota"><input id="%-quantity_limit" name="%-quantity_limit" min="0" type="number"/></td>\n\
            <td class="unit-label">&nbsp;<span class="%-unit-mirror"></span></td>{% endif %}\n\
            <td class="quantum"><input id="%-quantum" name="%-quantum" min="0" type="number" step="0.001"/></td>\n\
            <td class="unit-label">&nbsp;<span class="%-unit-mirror"></span></td>\n\
            <td class="weight"><input id="%-unit_weight" name="%-unit_weight" min="0" step="0.1" type="number"/></td><td>kg</td>\n\
            <td class="deleted">\n\
              <input id="%-deleted" name="%-deleted" value="%-deleted" onchange="reflect_deletion(\'%\')" type="checkbox">\n\
            </td>\n\
        </tr>'.replace(/%/g, prefix));
    }

    /* Fill a product row with data from a JSON record */
    function fill_row(prefix, record) {
      $.each(record, function(k, v) {
        // console.log("set #"+prefix+"-"+k+" to "+v);
        $('#'+prefix+'-'+k).attr('value', v);
      });
      if( record['deleted']) {
        $("#"+prefix+"-deleted")[0].checked=true;
        reflect_deletion(prefix);
      }
    }

    /* Add rows of blank products at the end of the table */
    function add_blank_products() {
      var N_ADDED_ROWS = 3;
      var i;
      for(i=N_BLANKS; i<N_BLANKS+N_ADDED_ROWS; i++) {
        var prefix = "blank"+i;
        add_row(prefix);
        attach_closure(prefix);
      }
      N_BLANKS += N_ADDED_ROWS;
      $("#n_blanks").attr("value", N_BLANKS);
    }

    $(document).ready(function() {
      /* Generate and fill product rows */
      var product_ids = [];
      for(i=0; i<PRODUCTS.length; i++) {
        var record = PRODUCTS[i];
        var prefix = "pd"+record['id'];
        add_row(prefix);
        fill_row(prefix, record);
        attach_closure(prefix);
        product_ids.push(record['id'])
      }
      add_blank_products();
      $("#product_ids").attr("value", product_ids.toString());
    })
    // ]]>
  </script>
  <style type="text/css">
      td.name input { width: 200px; }
      td.unit input { width: 40px; }
      td.unit-label { text-align: left; }
      td.price input { width: 40px; text-align: right; }
      td.qpp input { width: 40px; text-align: right; }
      .quota, .qpp { padding-left: 20px; }
      td.quota input { width: 40px; }
      td.quantum input { width: 40px; }
      td.weight input { width: 40px; text-align: right; }
      tr.deleted { text-decoration: line-through; }
      tr.deleted input { text-decoration: line-through; }
  </style>
{% endblock %}

{% block content %}
<form method="post" action="">
    <table id="delivery-table">
        <tr>
          <th><label for="dv-name">Nom de la livraison :</label></th>
          <td><input id="dv-name" maxlength="64" name="dv-name" type="text" value="{{delivery.name|escapejs}}" /></td>
        </tr>
      <tr>
        <th><label for="dv-state">Commandes autorisées :</label></th>
        <td><select id="dv-state" name="dv-state">
          {% for key, name in delivery.STATE_CHOICES.items %}
          <option value="{{key}}" {% if delivery.state == key %}selected="selected"{% endif %}>{{name}}</option>
          {% endfor %}
        </select></td>
      </tr>
    </table>
    <div style="height: 20px;"></div>
    <table id="products-table">
        <tr>
            <th>Produit</th>
            <th>Prix</th>
            <th></th>
            <th>Unité</th>
            <th colspan="2" class="qpp">Par carton</th>
            {% if QUOTAS_ENABLED %}<th colspan="2" class="quota">Quota</th>{% endif %}
            <th colspan="2">Quantum</th>
            <th colspan="2">Poids</th>
            <th>Effacer ?</th>
        </tr>
    </table>
    <p>
      <button type='button' onclick="add_blank_products();">Ajouter des produits</button>
      <input type='submit' value="Sauvegarder" />
      <input type='hidden' name='dv-id' value='{{delivery.id}}' />
      <input type='hidden' name='n_blanks' id="n_blanks" value='0' />
      <input type='hidden' name='product_ids' id="product_ids" value='' /><commit_msg>allow to enter product descriptions<commit_after>{% extends 'layout_semi_wide.html' %}

{% block head %}
  <script type='text/javascript'>
    // <![CDATA[

    var N_BLANKS = 0;

    var DELIVERY = {{delivery.id}};

    var PRODUCTS = [{% for pd in delivery.product_set.all %}
      { 'id': "{{pd.id|escapejs}}",
        'name': "{{pd.name|escapejs}}",
        'price': "{{pd.price|escapejs}}",
        'unit': "{{pd.unit|escapejs}}",
        'quantity_limit': "{{pd.quantity_limit|escapejs}}",
        'quantity_per_package': "{{pd.quantity_per_package|escapejs}}",
        'quantum': "{{pd.quantum|default_if_none:''|escapejs}}",
        'unit_weight': "{{pd.unit_weight|escapejs}}",
        'deleted': {% if pd.delivery.id == delivery.id %} false  {% else %} true {% endif %},
        'described': false,
        'delivery': {{pd.delivery.id}} },
    {% endfor %}];

    /* If the "deleted" checkbox is checked, strike theline and disable product edition. */
    function reflect_deletion(prefix) {
      var deleted = $('#'+prefix+'-deleted').is(':checked');
      var inputs = $("#"+prefix+"-row input[type=number], #"+prefix+"-row input[type=text]");
      inputs = $.merge(inputs, $("#"+prefix+"-description textarea"));
      if( deleted) {
        $("#"+prefix+"-row").addClass('deleted');
        inputs.attr('disabled', 'disabled');
      } else {
        $("#"+prefix+"-row").removeClass('deleted');
        inputs.removeAttr('disabled');
      }
    }

    /* Enable/disable description editor */
    function reflect_description(prefix) {
      var described = $('#'+prefix+'-described').is(':checked');
      console.log("Change description of", prefix, "to", described);
      if( described) {
          $("#"+prefix+"-description").show();
          $("#"+prefix+"-description textarea").removeAttr("disabled");
      } else {
          $("#"+prefix+"-description").hide();
          $("#"+prefix+"-description textarea").attr("disabled", "disabled");
      }
    }

    /* Keep unit names in sync between the input field and the text spans where it occurs. */
    function attach_closure(prefix) {
      var closure = function() {
        var content = $("#"+prefix+"-unit").attr('value');
        $("."+prefix+"-unit-mirror").text(content)
        if(content) {
          $("."+prefix+"-if-unit-mirror").show();
        } else {
          $("."+prefix+"-if-unit-mirror").hide();
        }
      }
      $("#"+prefix+"-unit").keyup(closure);
      closure();
    }

    /* Add rows of blank products at the end of the table */
    function add_row(prefix) {
      $("#products-table").append('\n\
        <tr id="%-row">\n\
            <td class="name"><input id="%-id" name="%-id" type="hidden" /><input id="%-name" maxlength="64" name="%-name" type="text" /></td>\n\
            <td class="price"><input id="%-price" name="%-price" step="0.01" min="0" type="number" /></td>\n\
            <td>€/</td>\n\
            <td class="unit"><input id="%-unit" maxlength="64" name="%-unit" type="text" /></td>\n\
            <td class="qpp"><input id="%-quantity_per_package" name="%-quantity_per_package" type="number" /></td>\n\
            <td class="unit-label">&nbsp;<span class="%-unit-mirror"></span><span class="%-if-unit-mirror">/ct</span></td>\n\
            {% if 0 %}<td class="quota"><input id="%-quantity_limit" name="%-quantity_limit" min="0" type="number"/></td>\n\
            <td class="unit-label">&nbsp;<span class="%-unit-mirror"></span></td>{% endif %}\n\
            <td class="quantum"><input id="%-quantum" name="%-quantum" min="0" type="number" step="0.001"/></td>\n\
            <td class="unit-label">&nbsp;<span class="%-unit-mirror"></span></td>\n\
            <td class="weight"><input id="%-unit_weight" name="%-unit_weight" min="0" step="0.1" type="number"/></td><td>kg</td>\n\
            <td class="deleted">\n\
              <input id="%-deleted" name="%-deleted" value="%-deleted" onchange="reflect_deletion(\'%\')" type="checkbox">\n\
            </td>\n\
            <td class="described">\n\
              <input id="%-described" name="%-described" value="%-described" onchange="reflect_description(\'%\')" type="checkbox">\n\
            </td>\n\
        </tr>\n\
        <tr id="%-description"><td colspan="{%if QUOTAS_ENABLED%}14{%else%}12{%endif%}">\n\
        <textarea name="%-description" cols="5">TOTO</textarea>\n\
        </td></tr>'.replace(/%/g, prefix));
    }

    /* Fill a product row with data from a JSON record */
    function fill_row(prefix, record) {
      $.each(record, function(k, v) {
        // console.log("set #"+prefix+"-"+k+" to "+v);
        $('#'+prefix+'-'+k).attr('value', v);
      });
      if( record['deleted']) {
        $("#"+prefix+"-deleted")[0].checked=true;
        reflect_deletion(prefix);
      }
      if( record['described']) {
        $("#"+prefix+"-described")[0].checked=true;
      } else {
        reflect_description(prefix);
      }
    }

    /* Add rows of blank products at the end of the table */
    function add_blank_products() {
      var N_ADDED_ROWS = 3;
      var i;
      for(i=N_BLANKS; i<N_BLANKS+N_ADDED_ROWS; i++) {
        var prefix = "blank"+i;
        add_row(prefix);
        attach_closure(prefix);
        reflect_description(prefix);
      }
      N_BLANKS += N_ADDED_ROWS;
      $("#n_blanks").attr("value", N_BLANKS);
    }

    $(document).ready(function() {
      /* Generate and fill product rows */
      var product_ids = [];
      for(i=0; i<PRODUCTS.length; i++) {
        var record = PRODUCTS[i];
        var prefix = "pd"+record['id'];
        add_row(prefix);
        fill_row(prefix, record);
        attach_closure(prefix);
        product_ids.push(record['id'])
      }
      add_blank_products();
      $("#product_ids").attr("value", product_ids.toString());
    })
    // ]]>
  </script>
  <style type="text/css">
      td.name input { width: 200px; }
      td.unit input { width: 40px; }
      td.unit-label { text-align: left; }
      td.price input { width: 40px; text-align: right; }
      td.qpp input { width: 40px; text-align: right; }
      .quota, .qpp { padding-left: 20px; }
      td.quota input { width: 40px; }
      td.quantum input { width: 40px; }
      td.weight input { width: 40px; text-align: right; }
      tr.deleted { text-decoration: line-through; }
      tr.deleted input { text-decoration: line-through; }
      #products-table th.non-rotate { vertical-align: bottom; }
      #products-table textarea { width: 100%; }
  </style>
{% endblock %}

{% block content %}
<form method="post" action="">
    <table id="delivery-table">
        <tr>
          <th><label for="dv-name">Nom de la livraison :</label></th>
          <td><input id="dv-name" maxlength="64" name="dv-name" type="text" value="{{delivery.name|escapejs}}" /></td>
        </tr>
      <tr>
        <th><label for="dv-state">Commandes autorisées :</label></th>
        <td><select id="dv-state" name="dv-state">
          {% for key, name in delivery.STATE_CHOICES.items %}
          <option value="{{key}}" {% if delivery.state == key %}selected="selected"{% endif %}>{{name}}</option>
          {% endfor %}
        </select></td>
      </tr>
    </table>
    <div style="height: 20px;"></div>
    <table id="products-table">
        <tr>
            <th class="non-rotate">Produit</th>
            <th class="non-rotate">Prix</th>
            <th class="non-rotate"></th>
            <th class="non-rotate">Unité</th>
            <th colspan="2" class="qpp non-rotate">Par carton</th>
            {% if QUOTAS_ENABLED %}<th colspan="2" class="quota">Quota</th>{% endif %}
            <th colspan="2" class="non-rotate">Quantum</th>
            <th colspan="2" class="non-rotate">Poids</th>
            <th class="rotate"><div><div>Effacer ?</div></div></th>
            <th class="rotate"><div><div>Description ?</div></div></th>
        </tr>
        <tr><td>&nbsp;</td></tr>
    </table>
    <p>
      <button type='button' onclick="add_blank_products();">Ajouter des produits</button>
      <input type='submit' value="Sauvegarder" />
      <input type='hidden' name='dv-id' value='{{delivery.id}}' />
      <input type='hidden' name='n_blanks' id="n_blanks" value='0' />
      <input type='hidden' name='product_ids' id="product_ids" value='' />
{'fraction_non_alphanumeric': 0.21549427124966694, 'fraction_numerical': 0.0069944044764188645, 'mean_word_length': 2.6324703605129445, 'pattern_counts': {'":': 0, '<': 240, '<?xml version=': 0, '>': 236, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1075644', 'n_tokens_mistral': 5133, 'n_tokens_neox': 4887, 'n_words': 947}