
itemFields['TACHE'] = ['libelle','parent','rank','client','intervenant','debut','fin'];

datagrid_hooks['taches'] = function() {
  let element = document.querySelector("#datagrid");
  let contenu = "";
  let nbLignes = 1;
  let parentLevel=getContexteValeur('parent_level');

  if (parentLevel == 0) parentLevel=1;

  sortItemsByClientParentDates();
  
  contenu += "<table>";
  contenu += " <thead>";
  contenu += "   <tr>";
  contenu += '    <th colspan="4" onchange="updateValue(event)"><input id="newitem_input" type="text" placeholder="Nouvel item" style="width: 98%;outline: none"></th>';
  contenu += "   </tr>";
  contenu += "   <tr>";
  // contenu += "    <th><center>Id</center></th>";
  contenu += '    <th style="width:50px;text-align:center"><span onclick="openFiltrePopup(\'client\')">Client</span></th>';
  contenu += '    <th style="max-width:150px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis"><span onclick="openFiltrePopup(\'libelle\')">Libellé</span></th>';
  contenu += '    <th style="width:100px;text-align:center"><span onclick="openFiltrePopup(\'debut\')">Début</span></th>';
  contenu += '    <th style="width:100px;text-align:center"><span onclick="openFiltrePopup(\'fin\')">Fin</span></th>';
  // contenu += "    <th><center>État</center></th>";
  contenu += "   </tr>";
  contenu += "  </thead>";

  contenu += " <tbody>";

  items.forEach((element) => {
    let item = getItemFromElement(element);
    let texte = getNonNullTextValeur(item.libelle);

    if (item.status == 3) texte = "<strike>" + texte + "</strike>";
    if (item.status == 2) texte = "<strike>" + texte + "</strike>";

    // On n'affiche pas la racine
    if (item["idlignes"] == 1) return true;
    // On n'affiche que ce qui est marqué comme visible
    if (!item["visible"]) return true;

    contenu += '   <tr class="js-open-modal-trigger" data-modal-info="type='+item.type+':id=' + getNonNullTextValeur(item.idlignes) + '">';
    //contenu += "    <td><center>" + getNonNullTextValeur(item.idlignes) + "</center></td>";
    contenu += '    <td style="width:50px;text-align:center">' + getNonNullTextValeur(item.client) + "</td>";
    contenu += '    <td style="max-width:150px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis">' + setLevelPadding(item["level"]-parentLevel) + texte + "</td>";
    contenu += '    <td style="width:100px;text-align:center">' + formatLocaleDateFromStringDate(item.debut) + "</td>";
    contenu += '    <td style="width:100px;text-align:center">' + formatLocaleDateFromStringDate(item.fin) + "</td>";
    //contenu += "    <td>" + getStatusLibelleFromId(item.status) + "</td>";
    contenu += "   </tr>";

    nbLignes += 1;
  });
  contenu += "  </tbody>";

  contenu += " <tfoot>";
  contenu += "   <tr>";
  contenu += '    <th colspan="4">'+setLevelPadding(2)+''+(nbLignes-1)+' tâches</th>';
  contenu += "   </tr>";
  contenu += "  </foot>";
  contenu += "</table>";

  element.innerHTML = contenu;

  // Ne marche pas , ça enlève le stick du header du tableau ...
  // document.getElementById('newitem_input').focus({preventScroll: true});
}

// on affiche ces éléments comme des tâches
datagrid_hooks['absences'] = datagrid_hooks['taches'];
datagrid_hooks['releases'] = datagrid_hooks['taches'];
datagrid_hooks['feries'] = datagrid_hooks['taches'];
datagrid_hooks['rdv'] = datagrid_hooks['taches'];


function taskSave(idlignes) {
  let bodyJSON = JSON.stringify(getCurrentTASK(idlignes));
  return itemAPI(idlignes, bodyJSON, itemInfo);
}

item_save_hooks['TACHE'] = taskSave;

function getCurrentTASK(idlignes)
{
  let result = new Object();

  if (idlignes > 1) {
    result['idlignes'] = idlignes;
  } else {
    result['type']=getContexteValeur('type_courant');
  }

  itemFields['TACHE'].forEach(attribut => {
    result[attribut] = documentGetElementById('item_'+attribut);
  });
  
  if (result['parent']=='') result['parent']=1;
  if (result['client']=='') result['client']==getTrigrammeCollaborateur(result['intervenant']);
  if (result['libelle']=='') result['libelle']='xxxxxxxxxxxx';
  if (result['intervevant']=='') result['intervenant']=getContexteValeur('intervenant');;
  if (result['debut']=='') result['debut']=0;
  if (result['fin']=='') result['fin']=0;
  if (result['status']=='') result['status']=0;

  return result;
}