"use strict";

var pwa = pwa || {};
//--##
pwa.userManager = pwa.userManager || {};

pwa.userManager.init = function() {
  $("#espaceMasonry")
    .html("");
  pwa.userProfile.ficheUser("0", "ADMIN");
};
pwa.userManager.userList = async function() {
  //check ADMIN
  if(pwa.MODE_ENV) {
    return [
      { uuid: "pcolzy", desc: "pcolzy" },
      { uuid: "user2", desc: "User2" }
    ];
  } else {
    const datauser = await axios.get(pwa.urlbackauth + "/login", {
      headers: pwa.state.data.headers
    });
    const lst = datauser.data.items.map(u => {
      return { uuid: u.uuid, desc: u.name };
    });
    return lst;
  }
};

pwa.userManager.ficheUser = function(iduser) {
  // Génere un form de user pre-rempli avec iduser
  let userForm = {
    includein: "#espaceMasonry",
    template: "#mainContentMasonry",
    idmasonry: "UserForm",
    destinationclass: "formUser",
    header: "Information du compte",
    formclass: "formUserProfil",
    nombreColonne: 6,
    commentaire: "",
    footer: ""
  };
  let datauser = pwa.state.data.user;
  if(iduser) {
    //On recupere les informations iduser
    // faire un get (iduser) datauser =
    userForm.button = [{
      action: "update",
      label: "Sauvegarder",
      msgok: "Votre sauvegarde a bien été effectuée en local",
      msgko: "Votre sauvegarde n'a pas pu être réalisée ",
      actionclick: "pwa.userManager.update()",
      idObject: pwa.state.data.user.uuid,
      objectSelected: "user"
    }];
  } else {
    userForm.button = [{
      action: "create",
      label: "Créer",
      msgok: "Utilisateur créer en local",
      msgko: "La création d'un utilisateur n'est pas possible ",
      actionclick: "pwa.userManager.create()",
      idObject: "0",
      objectSelected: "user"
    }];
  }
  userForm.main = pwa.forms.genereForm(pwa.state.app.ref.users, datauser);
  logger.info("button envoye", userForm.button);
  userForm.buttonup = Mustache.render($("#actionButton")
    .html(), {
      button: userForm.button
    });
  pwa.masonry.append(userForm, pwa.forms.actionForm);
};
