window.one_money = one_money
window.one_token = one_token
window.one_float = one_float
window.do_print_ticket = do_print_ticket

window.BASE_URL = ""

window.BASE_URL_QUEUE = window.BASE_URL + ":9090/ticket/";
window.WS_SERVICE = "ws://localhost:6025";
window.PRINTER = "POS";



// isLoggedIn()

function one_money(inp) {
   return numeral(inp).format('0,000.00')
}

function one_token() {
   //sipe add null token redirect ke logout
   var tkn = localStorage.getItem('token')
   if (tkn == null ) one_logout("/one-ui/")
   return localStorage.getItem('token')
}
function one_logout(urllogout){
   window.localStorage.removeItem("token")
   window.localStorage.removeItem("user")
   window.location = urllogout
}
window.one_logout = one_logout

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

function isLoggedIn() {
   let s = localStorage.getItem("one_clinic_user")
   console.log(s)
   if (s == null) {
      window.location = "/one-ui/test/vuex/one-fo-clinic-login"
      return false
   }
   
   s = JSON.parse(s)
   if (window.location.href.match(/one-fo-clinic-login/))
      window.location = s.user.M_UserGroupDashboard
}

// Printing
window.zpl_socket = new WebSocket(WS_SERVICE);
window.zpl_socket.onmessage = function(msg) {
   console.log('message',msg);
};
window.zpl_socket.onopen= function(msg) {
   console.log('open',msg);

};
window.zpl_socket.onerror= function(msg) {
   console.log('error',msg);
};
window.printer_ready = false;

function svc_name(svc) {
   switch(svc) {
      case "UMUM":
         return "Pasien Umum";
      case "COM" :
         return "Pasien Rekanan";
      case "KLINIK" :
         return "Pasien Klinik";
      case "ONLINE" :
         return "Pasien Online";
      case "RES" :
         return "Pengambilan Hasil";
      case "APPDR" :
         return "Mobile Dokter";
      case "MEMBER" :
         return "Pasien Member";
      default :
         return "OTHER";
   }
}
function do_print_ticket(inp,fn_cb) {
   var set_center =String.fromCharCode(0x1b,0x61,0x01) ; 
   var set_font_size_big = String.fromCharCode(0x1b,0x21,0x10);
   var set_font_size = "";
   var set_font_size_large = String.fromCharCode(0x1d,0x21,0x21);
   var set_cutter =String.fromCharCode(0x1d, 0x56, 0x41, 0x03); 
   var reset_mode = String.fromCharCode(0x1b,0x40);
   var nomor_sebelumnya = inp.prev_number;
   var sisa_antrian = inp.remaining_queue;
   var hari = inp.queue_day;

   var fmt_data = reset_mode +
      set_font_size_big + 
      set_center + "PRAMITA LAB - Cik Di Tiro\n" +
      set_center + "\n" +
      set_center + svc_name(inp.serviceCode) + "\n" +
      "\n" + set_font_size_large + 
      set_center  + inp.number + "\n" +
      "\n" + reset_mode +
      set_center + "Nomor sebelumnya " + nomor_sebelumnya + "\n" +
   set_center + "Sisa Antrian " + sisa_antrian + "\n"  +
   set_center  + hari + " " +  inp.queue_date + " " + inp.queue_time + "\n" +
   set_center  + "Terima Kasih\n\n\n" ;

   fmt_data = fmt_data + set_cutter;
   var data_print = {
      printer : PRINTER,
      type : "zpl",
      data : fmt_data
   };
   window.zpl_socket.send(JSON.stringify(data_print));    
   fn_cb();
}
