var apx = apx || {};
apx.form = {};

/**
 * Data form schema
 *  {}
 *  
 * Will produce a html expected:
 * <div class="formmanager">
 *  <input data-name="keydata" data-format="checkJSON" value="defaultvalue" placeholder="" />
 *  .. any other data field
 *  <button class="getdata" onclick="apx.form.submit(this,cb)">save</button>
 * </div>
 *  
 */



apx.form.build = (elt,schema,data)=>{
    // Build in html a form with all prerequest and prefill by data into a DOM element elt  

};
apx.form.submit = (elt,cb)

/**
 * To install in html input onclick=apx.form.enter to simulate a senddata
 * @param {*} elt 
 * @param {*} event 
 */
apx.form.enter = (elt, event) => {
  if (event.keyCode === 13) {
    const getform = elt.closest(".contactform");
    event.preventDefault();
    getform.querySelector(".sendregister").click();
  }
};
//   apx.sendform(this,{route:'actions/contact',order:'registercontact',srckey:'teasingwebpage',mlist:'getinform'},fctregisteremail)
registerlist.check = {
  email:
    /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
  telephone: /^0[1-9][0-9]{8}$/,
  profil: /^(seeker|recruiter)$/,
};
registerlist.msg = {
  email: "Vérifier votre email ",
  telephone: "Verifier votre téléphone ",
  profil: "Choisir votre profil ",
  serverissue:
    "Désolé, un probléme empeche votre inscription, réessayer plus tard",
  register: "Vous êtes bien inscrits",
  alreadysent: "Vous avez déjà envoyé votre email ",
};
registerlist.send = async (elt) => {
  const formdata = {};
  // formdata.uuid = (localStorage.getItem('xuuid')) ? localStorage.getItem('xuuid'):"uuidunknown";
  const form = elt.closest(".contactform");
  form.querySelector(".answer").innerHTML = "";
  var valid = true;
  form.querySelectorAll("input,textarea,option:checked").forEach((e) => {
    if (!e.name) e.name = info.getAttribute("name");
    if (e.name == "email") formdata.typekey = "email";
    if (e.name == "telephone") formdata.typekey = "telephone";

    if (!e.type || e.type !== "radio" || (e.type == "radio" && e.checked)) {
      //check value
      if (
        registerlist.check[e.name] &&
        !registerlist.check[e.name].test(e.value)
      ) {
        form.querySelector(".answer").innerHTML +=
          registerlist.msg[e.name] + "  ";
        form.querySelector(".answer").classList.remove("d-none");
        valid = false;
      }
      if (!formdata[e.name]) {
        formdata[e.name] = e.value;
      } else {
        // array case multi input with same name
        formdata[e.name] = [formdata[e.name]];
        formdata[e.name].push(e.value);
      }
    }
  });
  formdata.tribe=apxtri.headers.xtribe
  if (!formdata.profil || formdata.profil == "") {
    // Pas de choix
    formdata.profil = "both";
    //form.querySelector(".answer").innerHTML = registerlist.msg.profil;
    //form.querySelector(".answer").classList.remove("d-none");
  }
  //add phil
  
  formdata.mlist += formdata.profil;
  console.log(formdata);
  if (
    valid &&
    formdata.profil &&
    formdata.srckey &&
    (formdata.email || formdata.telephone)
  ) {
    form.querySelector(".submitbtn").classList.add("d-none");
    form.querySelector(".loaderbtn").classList.remove("d-none");
    const datasent = await axios.post(
      "/api/apxtri/notifications/registeranonymous",
      formdata,
      { headers: apx.data.headers }
    );
    //console.log(datasent)
    if (datasent.data.status == 200) {
      form.querySelector(".answer").innerHTML = registerlist.msg.register;
      form.querySelector(".answer").classList.remove("d-none");
      form.querySelector(".loaderbtn").classList.add("d-none");
      elt.setAttribute(
        "onclick",
        `"alert('${registerlist.msg.alreadysent}');"`
      );
      //document.location.href = "thank-you.html";
    } else {
      form.querySelector(".answer").innerHTML = registerlist.msg.serverissue;
      form.querySelector(".answer").classList.remove("d-none");
    }
  } else {
    console.log(
      "Check your form it miss something profil or srckey or email or telephone"
    );
  }
};
