window.BASE_URL = ""

function one_money(nilai, format) {
  // return numeral(inp).format(format ? format : '0,000')
  /*if(!inp)
  inp = 0
  var number_string = inp.toString(),
			split   		= number_string.split(','),
			sisa     		= split[0].length % 3,
			rupiah     		= split[0].substr(0, sisa),
			ribuan     		= split[0].substr(sisa).match(/\d{3}/gi);
 
			// tambahkan titik jika yang di input sudah menjadi angka ribuan
			if(ribuan){
				separator = sisa ? '.' : '';
				rupiah += separator + ribuan.join('.');
			}
 
			rupiah = split[1] != undefined ? rupiah + ',' + split[1] : rupiah;
			return rupiah*/
    
            // Jika tidak ada nilai
            if (!nilai) return '';
            
            try {
                // Ubah ke string
                let strNilai = String(nilai);
                
                // Jika berisi titik desimal
                if (strNilai.includes('.')) {
                    // Split bagian utama dan desimal
                    let bagian = strNilai.split('.');
                    let utama = bagian[0];
                    let desimal = bagian[1];
                    
                    // Format bagian utama
                    utama = utama.replace(/\B(?=(\d{3})+(?!\d))/g, '.');
                    
                    // Kembalikan gabungan
                    return utama + ',' + desimal;
                } else {
                    // Tidak ada desimal, hanya format bagian utama
                    return strNilai.replace(/\B(?=(\d{3})+(?!\d))/g, '.');
                }
            } catch (error) {
                console.error("Error formatting:", error);
                return nilai;
            }
        
 


}
window.one_money = one_money

function one_token() {
   return localStorage.getItem('token')
}
window.one_token = one_token

function one_user() {
   return JSON.parse(localStorage.getItem('user'))
}
window.one_user = one_user

function one_float(inp) {
   try {
      let val = parseFloat(inp)
      if (isNaN(val)) return 0.0
      return val
   } catch(e) {
      return 0.0
   }
}
window.one_float = one_float

function one_logout(urllogout) {
   window.urllogout = urllogout   

   let logout = async function (token) {
      try {
          var resp = await axios.post(BASE_URL + '/one-api/v1/system/auth/logout', 
          { token: token });
          if (resp.status != 200) {
              return {
                  status: "ERR",
                  message: resp.statusText
              };
          }
          let data = resp.data;
          return data;
      } catch (e) {
          return {
              status: "ERR",
              message: e.message
          };
      }
   }
  
   let x = async () => {
      try {
         let resp = await logout(window.one_token())
         if (resp.status != "OK") {
             alert('error')
         } else {
            window.localStorage.removeItem("token")
            window.localStorage.removeItem("user")
            location.replace(window.urllogout)
         }
      } catch (e) {
         context.commit("update_login_status", 3)
         context.commit("update_login_error_message", e.message)
      }
   }

   x()
   

   // window.location = urllogout
}
window.one_logout = one_logout

var one_moment = function(d) {
   return moment(d,"YYYY-MM-DD hh:mm:ss").fromNow()
}
window.oneMoment = one_moment
window.one_moment = one_moment

// NavBar APIs
// async function one_get_menu() {
//    try {
//       var resp = await axios.post(BASE_URL + '/one-api/mockup/system/menu/get_menu');
//       if (resp.status != 200) {
//          return {
//             status: "ERR",
//             message: resp.statusText
//          };
//       }
//       let data = resp.data;
//       return data;
//    } catch(e) {
//       return {
//          status: "ERR",
//          message: e.message
//       };
//    }
// }
// window.one_get_menu = one_get_menu


async function xno(input) {
  try {
     var resp = await axios.get(BASE_URL + '/one-api/v1/su/xno/show/' + input )
     return resp.data
  } catch(e) {
     return input;
  } 
}
window.xno = xno
