var api_url_name_js_login_main = 'https://grab-api-stg.azurewebsites.net';  //Staging

//Production
if(client.applicationUrl == 'https://grab-taxi-prod.azurewebsites.net/'){
    api_url_name_js_login_main = 'https://grab-api-prod.azurewebsites.net'; //Production
}
// var api_url_name_js_login_main = 'https://grab-api-prod.azurewebsites.net'; //Production

var api_url_name_js_login = `${api_url_name_js_login_main}/V01`;
var regEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

var oBusyDialogWO = new sap.m.BusyDialog({});
jQuery.sap.require("sap.m.MessageBox");


var oBundle = "";
var browserLang = navigator.language || navigator.userLanguage;
var languageCode = browserLang.toLowerCase();

if (languageCode.includes("en")) {
    oBundle = jQuery.sap.resources({ url: "i18n/i18n_en.properties" });
    sessionStorage.setItem('languageCode', "EN");
} else {
    sessionStorage.setItem('languageCode', "EN");
    oBundle = jQuery.sap.resources({ url: "i18n/i18n_en.properties" });
}

// var hvi_remember = sessionStorage.getItem('oxmaint_remember');
// if(hvi_remember){
//     var emailUser = sessionStorage.getItem('oxmaint_email');
//      if(emailUser == null){
//         sessionStorage.removeItem('oxmaint_email');
//     }else{
//          sessionStorage.setItem('oxmaint_email', emailUser.toLowerCase());
//          window.open("portal.html", "_self");
//     }

// }else{
//     sessionStorage.removeItem('oxmaint_email');
// }
window.onload = function() {
      localization();
    };

function localization(){
 
}

 function firebaseLogin(type) {
    
        var userid = document.getElementById("email").value;
        var password = document.getElementById("password").value;

        if (userid.trim() == "") {
            sap.m.MessageBox.error(oBundle.getText("UserID_Invalid"));
            return ;
        }
        if (password.trim() == "") {
            sap.m.MessageBox.error(oBundle.getText("Password_Required"));
            return ;
        };


        if(type == "twoWheeler"){
            api_url_name_js_login = `${api_url_name_js_login_main}/Bike_V01`;
        }else{
            api_url_name_js_login = `${api_url_name_js_login_main}/V01`;
        }


        oBusyDialogWO.open();

        $.ajax({
            type: "POST",
            url: `${api_url_name_js_login}/Login.asmx/getTeamLogin`,
            data: {
                "user_id": userid,
                "password": password
            },
            success: function (results) { 
                
                if(results.length == 1 ){
  
                    // if(userid != results[0]['user_id']){
                    //     oBusyDialogWO.close();
                    //     sap.m.MessageBox.error(oBundle.getText("User ID mismatch! Please check your User ID and try again."));
                    //     return
                    // }
                    // if(password != results[0]['password']){
                    //     oBusyDialogWO.close();
                    //     sap.m.MessageBox.error(oBundle.getText("Password incorrect! Please check your password and try again."));
                    //     return
                    // }
                    var user_block = "No";
                    var user_access = results[0]['user_access'];
                    var master_email = results[0]['master_email'];
                    var employee_name = results[0]['user_name'];
                    var supplier_id = results[0]['supplier_id'];
                    var role = results[0]['role'];
                    var approval_level = results[0]['approval_level'];
                    var approval_amount = results[0]['approval_amount'];
                    var status = results[0]['status'];
                    // var assign_vehicle = results[0]['assign_vehicle'];
                    var notification_email = results[0]['email'];
                    // sessionStorage.setItem('oxmaint_location_id', results[0]['location_id']);
                    
                    if (status == "Inactive") {
                        oBusyDialogWO.close();
                        sap.m.MessageBox.error(oBundle.getText("Your account is inactive, please contact to email: tpi.is@grabtaxi.com"));
                    } else {
                        sessionStorage.setItem('oxmaint_account_access', user_access);
                        sessionStorage.setItem('stripe_customer_id', results[0]['customer_id']);
    
                        sessionStorage.setItem('oxmaint_wo_verification', results[0]['verification_flag']||"0");
                        sessionStorage.setItem('oxmaint_login_type', "2");
                        sessionStorage.setItem('oxmaint_employee_name', employee_name);
                        sessionStorage.setItem('oxmaint_email', master_email);
                        sessionStorage.setItem('oxmaint_userid', userid);
                        sessionStorage.setItem("oxmaint_remember", true);
                        sessionStorage.setItem("oxmaint_ws_group_number", results[0]['ws_group_number']);
                        sessionStorage.setItem("oxmaint_ws_group_name", results[0]['ws_group_name']);
                        sessionStorage.setItem("oxmaint_workshop_name", results[0]['workshop_name']);
                        sessionStorage.setItem("oxmaint_workshop_number", results[0]['workshop_number']);
                        sessionStorage.setItem("oxmaint_supplier_id", supplier_id);
                        sessionStorage.setItem('oxmaint_user_access', user_access);
                        sessionStorage.setItem('oxmaint_notification_email', notification_email);                     
                        sessionStorage.setItem('oxmaint_role', role);                     
                        sessionStorage.setItem('oxmaint_approval_level', approval_level);                     
                        sessionStorage.setItem('oxmaint_approval_amount', approval_amount);                     
                        sessionStorage.setItem('portal_type', results[0]['vehicle_type']);                     
    
    
                        window.open("portal.html", "_self");
                        oBusyDialogWO.close();
    
                    }
                }else if (results.length == 2) {
                    oBusyDialogWO.close();
                
                    sap.m.MessageBox.show(
                        "Please choose a portal to login:", {
                            // icon: sap.m.MessageBox.Icon.QUESTION,
                            title: "PRIME Portal Login",
                            actions: ["Car", "eMoto", sap.m.MessageBox.Action.CANCEL],
                            emphasizedAction: "Car",
                            onClose: function (sAction) {
                                var selectedVehicle;
                                if (sAction === "Car") {
                                    selectedVehicle = results.find(item => item.vehicle_type === "4W");
                                } else if (sAction === "eMoto") {
                                    selectedVehicle = results.find(item => item.vehicle_type === "2W");
                                } else {
                                    return; // User clicked Cancel, do nothing
                                }
                
                                if (selectedVehicle) {
                                    sessionStorage.setItem('oxmaint_account_access', selectedVehicle['user_access']);
                                    sessionStorage.setItem('stripe_customer_id', selectedVehicle['customer_id']);
                                    sessionStorage.setItem('oxmaint_login_type', "2");
                                    sessionStorage.setItem('oxmaint_employee_name', selectedVehicle['user_name']);
                                    sessionStorage.setItem('oxmaint_email', selectedVehicle['master_email']);
                                    sessionStorage.setItem('oxmaint_userid', userid);
                                    sessionStorage.setItem("oxmaint_remember", true);
                                    sessionStorage.setItem("oxmaint_ws_group_number", selectedVehicle['ws_group_number']);
                                    sessionStorage.setItem("oxmaint_ws_group_name", selectedVehicle['ws_group_name']);
                                    sessionStorage.setItem("oxmaint_workshop_name", selectedVehicle['workshop_name']);
                                    sessionStorage.setItem("oxmaint_workshop_number", selectedVehicle['workshop_number']);
                                    sessionStorage.setItem("oxmaint_supplier_id", selectedVehicle['supplier_id']);
                                    sessionStorage.setItem('oxmaint_user_access', selectedVehicle['user_access']);
                                    sessionStorage.setItem('oxmaint_notification_email', selectedVehicle['email']);
                                    sessionStorage.setItem('oxmaint_role', selectedVehicle['role']);
                                    sessionStorage.setItem('oxmaint_approval_level', selectedVehicle['approval_level']);
                                    sessionStorage.setItem('oxmaint_approval_amount', selectedVehicle['approval_amount']);
                                    sessionStorage.setItem('portal_type', selectedVehicle['vehicle_type']);
                
                                    window.open("portal.html", "_self");
                                }
                            }
                        }
                    );
                }else{
                    oBusyDialogWO.close();
                    sap.m.MessageBox.error(oBundle.getText("Your user id or password is incorrect. Please contact to email: tpi.is@grabtaxi.com"));
                }
               
            }
        });
}
