$("body").tooltip({
    selector:'[data-toggle="tooltip"]'
});

appendCircleDropList();
appendCustomerDropDownList();

function msgPanel(alertType, msg) {
    return `<p class="text-${alertType}"><b>${msg}</b></p>`;
}

function validateForm(formdata) {
  //https://stackoverflow.com/questions/3518504/regular-expression-for-matching-latitude-longitude-coordinates/18690202#18690202
  const latLongPattern = /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/;
  let feedbackMsg = "";
  if (!latLongPattern.test(formdata.latitude + ", " + formdata.longitude))
     feedbackMsg += "latitude or longitude is not valid.";
  return feedbackMsg;
}

const addSiteURL =
  ADD_SITE_BASE_URL+"addSiteWeb";
    
const addSiteForm = document.getElementById("addSiteForm");
const panel = document.getElementById("msgPanel");
addSiteForm.addEventListener("submit", function(event) {
  event.preventDefault();
  if(checkWheatherLogoutOrNot_InternetConnection()){
      postSiteDetails();
  }else{
      alertNoInternet_connection();
  }
 
});

function postSiteDetails(){
    var spinnerClassIcon=document.getElementById("addSiteSpinner");
    var formdata = {
        siteId: addSiteForm.siteId.value.trim(),
        siteName: addSiteForm.siteName.value.trim(),
        latitude: addSiteForm.latitude.value.trim(),
        longitude: addSiteForm.longitude.value.trim(),
        circle: addSiteForm.circle.value.trim(),
        district: addSiteForm.district.value.trim(),
        customerName: addSiteForm.customerName.value.trim()
      };
         formdata = JSON.parse(JSON.stringify(formdata).replace(/"\s+|\s+"/g, '"'));
      const feedbackMsg = validateForm(formdata);
      if (feedbackMsg === "") {
          spinnerClassIcon.classList.remove("d-none");
        postData(addSiteURL, formdata).then(data => {
          if (data.error) {
            panel.innerHTML = msgPanel("danger", data.message);
          } else {
            // addSiteForm.reset();
            panel.innerHTML = msgPanel("success", data.message);
            $("#addSiteSubmitBtn").hide();
            $("#addSiteForm input,#addSiteForm select").attr("disabled",true);
            $("#assignEmployeeModalPopBtn").removeClass('d-none');
          }
            spinnerClassIcon.classList.add("d-none");
        });
      } else {
          panel.innerHTML = msgPanel("danger", feedbackMsg);
      }
}

function appendCircleDropList(){
    var circleDropDownArray = getCircleDropDownList();// function call in constant.js
    let option_tag = '<option value="" disabled selected>Select Circle</option>';
    for (let index = 0; index < circleDropDownArray.length; index++) {
    	option_tag += '<option value="' + circleDropDownArray[index] + '">' + circleDropDownArray[index] + '</option>';
    }
    $("#circle").html(option_tag);
}
function appendCustomerDropDownList(){
    let customerDropDownArray=getCustomerDropDownList(); // function  call in constant.js
    let option_tag=`<option value="" disabled selected>Select Customer Name</option>`;
    customerDropDownArray.forEach(function(value){
        option_tag+=`<option value="${value}">${value}</option>`;
    });
    $("#customerName").html(option_tag);
}




