var G_BC_WS_SERVICE = "ws://localhost:6025";
var G_BC_PRINTER = "zebra";

var g_bc_cln_printer_ready = false;
var g_bc_cln_socket = new WebSocket(G_BC_WS_SERVICE);
g_bc_cln_socket.onmessage = function(msg) {

};
g_bc_cln_socket.onopen = function() {

};
g_bc_cln_socket.onerror = function(msg) {

};
function cln_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 one_print_barcode_pk(inp) {
   g_bc_cln_socket = new WebSocket(G_BC_WS_SERVICE);
   axios.get('/one-api/v1/su/barcode/pk',{
      params: {barcodeId: inp}
   })
   .then( function(resp) {
      if (resp.data.status == "OK") {
	 var cat_print_data_lab = "";
         for(var i=0; i < resp.data.rows.length ; i++) {
            var r = resp.data.rows[i];
            var print_data_lab =
            "CT~~CD,~CC^~CT~"+ "\n"
            +  "^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR5,5~SD15^JUS^LRN^CI0^XZ"+ "\n"
            +  "^XA"+ "\n"
            +  "^MMT"+ "\n"
            +  "^PW440"+ "\n"
            +  "^LL0240"+ "\n"
            +  "^LS0"+ "\n"
            +  "^FT106,219^A0I,17,19^FH\^FD"+ r.T_OrderHeaderDate  +"^FS"+ "\n"
            +  "^FT184,14^A0I,17,16^FH\^FD"+ r.T_OrderHeaderM_PatientAge  +"^FS"+ "\n"
            +  "^FT392,16^A0I,25,24^FH\^FD"+ r.T_SampleTypeName  +"^FS"+ "\n"
            +  "^FT395,208^A0I,28,21^FH\^FD"+ r.M_PatientName  +"^FS"+ "\n"
            +  "^FO75,50^BY2^BC,140,N,N,N,A^FD"+ r.T_BarcodeLabBarcode + "^FS^"+ "\n"
            //+  "^BY2,2,141^FT365,46^BCI,,N,N"+ "\n"
          //  +  "^FD"+ r.T_BarcodeLabBarcode + "^FS"+ "\n"
            +  "^FT396,29^A0R,31,31^FH\^FD"+ r.Full_T_OrderHeaderLabNumber + "^FS"+ "\n"
            +  "^PQ1,0,1,Y^XZ"+ "\n"
            ;
            console.log(print_data_lab);
            var data_print = {
               printer : G_BC_PRINTER,
               type : "zpl",
               data : print_data_lab
            };
		 cat_print_data_lab += print_data_lab;
            //g_bc_cln_socket.send(JSON.stringify(data_print));
         }
         var all_data_print = {
               printer : G_BC_PRINTER,
               type : "zpl",
               data : cat_print_data_lab
        };
	setTimeout(function() {
        g_bc_cln_socket.send(JSON.stringify(all_data_print));
	},500);
      } else {
         console.log("ERR", resp.data);
      }
   })
   .catch( function(e) {
      console.log('Err Get Barcode', e);
   })
}


function one_print_barcode_pk_2020(inp) {
   g_bc_cln_socket = new WebSocket(G_BC_WS_SERVICE);
   axios.get('/one-api/v1/su/barcode/pk',{
      params: {barcodeId: inp}
   })
   .then( function(resp) {
      if (resp.data.status == "OK") {
         for(var i=0; i < resp.data.rows.length ; i++) {
            var r = resp.data.rows[i];
            var print_data_lab =
            "CT~~CD,~CC^~CT~"+ "\n"
            +  "^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR2,2~SD15^JUS^LRN^CI0^XZ"+ "\n"
            +  "^XA"+ "\n"
            +  "^MMT"+ "\n"
            +  "^PW400"+ "\n"
            +  "^LL0240"+ "\n"
            +  "^LS0"+ "\n"

          +  "  ^CF0,25"+ "\n"
          +  "  ^FO35,20^FD"+ r.M_PatientName  +"^FS"+ "\n"

          +  "  ^CF0,20"+ "\n"
         +  "  ^FO35,197^FD"+ r.T_OrderHeaderM_PatientAge  +"^FS"+ "\n"


          +  "  ^CF0,20"+ "\n"
          +  "  ^FO35,220^FD"+ r.T_SampleTypeName  +"^FS"+ "\n"
          +  "  ^FO220,220^FD"+ r.T_OrderHeaderDate  +"^FS"+ "\n"

          +  "  ^CF0^A0B,30"+ "\n"
          +  "   ^FO2,80^FD"+ r.T_OrderHeaderLabNumber  +"^FS"+ "\n"

        //  +  "  ^BY2,2,120"+ "\n"
        //    +  "^FO25,80^BCN,,N,N^FD"+ r.T_BarcodeLabBarcode + "^FS"+ "\n"
          +  "^FO35,50^BY2^BC,140,N,N,N,A^FD"+ r.T_BarcodeLabBarcode + "^FS^"+ "\n"

            +  "^PQ1,0,1,Y^XZ"+ "\n";
            console.log(print_data_lab);
            var data_print = {
               printer : G_BC_PRINTER,
               type : "zpl",
               data : print_data_lab
            };
            g_bc_cln_socket.send(JSON.stringify(data_print));
         }
      } else {
         console.log("ERR", resp.data);
      }
   })
   .catch( function(e) {
      console.log('Err Get Barcode', e);
   })
}

function one_print_barcode_pk_bkpadhi(inp) {
   g_bc_cln_socket = new WebSocket(G_BC_WS_SERVICE);
   axios.get('/one-api/v1/su/barcode/pk',{
      params: {barcodeId: inp}
   })
   .then( function(resp) {
      if (resp.data.status == "OK") {
         for(var i=0; i < resp.data.rows.length ; i++) {
            var r = resp.data.rows[i];
            var print_data_lab = "CT~~CD,~CC^~CT~"+ "\n"
            +  "^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR2,2~SD15^JUS^LRN^CI0^XZ"+ "\n"
            +  "^XA"+ "\n"
            +  "^MMT"+ "\n"
            +  "^PW400"+ "\n"
            +  "^LL0240"+ "\n"
            +  "^LS0"+ "\n"
            +  "^FT20,220^A0B,23,24^FH\^FD"+ r.T_BarcodeLabBarcode +"^FS"+ "\n"
            +  "^FT50,220^A0N,23,24^FH\^FD"+ r.T_SampleTypeName +"^FS"+ "\n"
            +  "^FT50,50^A0N,28,28^FH\^FD"+ r.M_PatientName  +"^FS"+ "\n"
          //   +  "^BY1,2,120^FT44,188^BCN,,N,N"+ "\n"
      //    +  "^FD"+ r.T_BarcodeLabBarcode + "^FS"+ "\n"
          +  "^BY2,2,120"+ "\n"
          +  "^FO35,70^BCN,,N,N^FD"+ r.T_BarcodeLabBarcode + "^FS"+ "\n"

          //  +  "^FO40,70^BY1^BCN,100,N,N,N"+ "\n"
        //    +  "^FD"+ r.T_BarcodeLabBarcode + "^FS"+ "\n"
          //  +  "^FO44,70^BY2^B1N,N,100,N,N"+ "\n"
        //    +  "^FDL-191106003EL111^FS"+ "\n"
            +  "^PQ1,0,1,Y^XZ"+ "\n";
            console.log(print_data_lab);
            var data_print = {
               printer : G_BC_PRINTER,
               type : "zpl",
               data : print_data_lab
            };
            g_bc_cln_socket.send(JSON.stringify(data_print));
         }
      } else {
         console.log("ERR", resp.data);
      }
   })
   .catch( function(e) {
      console.log('Err Get Barcode', e);
   })
}


function one_print_barcode_so(inp) {
   g_bc_cln_socket = new WebSocket(G_BC_WS_SERVICE);
   axios.get('/one-api/v1/su/barcode/so',{
      params: {orderId: inp}
   })
   .then( function(resp) {
      if (resp.data.status == "OK") {
         var data= resp.data.rows[0];
         var print_data_so = "CT~~CD,~CC^~CT~"+ "\n"
         +  "^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR2,2~SD15^JUS^LRN^CI0^XZ"+ "\n"
         +  "^XA"+ "\n"
         +  "^MMT"+ "\n"
         +  "^PW400"+ "\n"
         +  "^LL0240"+ "\n"
         +  "^LS0"+ "\n"
         +  "^FT5,150^A0N,23,24^FH\^FD"+ data.sex_code +"/"+ data.datereg +"("+ data.age +")^FS"+ "\n"
         +  "^FT5,200^A0N,23,24^FH\^FD"+ data.city+"^FS"+ "\n"
         +  "^FT5,175^A0N,23,24^FH\^FD" + data.alamat + "^FS"+ "\n"
         +  "^FT3,125^A0N,28,28^FH\^FD"+ data.M_PatientName +"^FS"+ "\n"
         +  "^BY2,2,45^FT22,70^BCN,,Y,N"+ "\n"
         +  "^FD" + data.nolab + "^FS"+ "\n"
         +  "^FT5,225^A0N,23,24^FH\^FD"+ data.patienttype +"^FS"+ "\n"
         +  "^PQ1,0,1,Y^XZ"+ "\n"
         console.log(print_data_so);
         var data_print = {
            printer : G_BC_PRINTER,
            type : "zpl",
            data : print_data_so
         };
         g_bc_cln_socket.send(JSON.stringify(data_print));
      } else {
         console.log("ERR", resp.data);
      }
   })
   .catch( function(e) {
      console.log('Err Get Barcode', e);
   })
}

function one_print_barcode_so_group(inp) {
   g_bc_cln_socket = new WebSocket(G_BC_WS_SERVICE);
   axios.get('/one-api/v1/su/barcode/so_group',{
      params: {orderId: inp}
   })
   .then( function(resp) {
      if (resp.data.status == "OK") {
         var data = resp.data.rows;
         var print_data_so_group = "CT~~CD,~CC^~CT~"+ "\n"
         +  "^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR2,2~SD15^JUS^LRN^CI0^XZ"+ "\n"
         +  "^XA"+ "\n"
         +  "^MMT"+ "\n"
         +  "^PW400"+ "\n"
         +  "^LL0240"+ "\n"
         +  "^LS0"+ "\n"
         +  "^FT5,150^A0N,23,24^FH\^FD"+ data.sexcode +"/"+ data.datereg +"("+ data.age +")^FS"+ "\n"
         +  "^FT5,200^A0N,23,24^FH\^FD"+ data.city+"^FS"+ "\n"
         +  "^FT5,175^A0N,23,24^FH\^FD" + data.alamat + "^FS"+ "\n"
         +  "^FT3,125^A0N,28,28^FH\^FD"+  data.M_PatientName +"^FS"+ "\n"
         +  "^BY2,2,45^FT22,70^BCN,,Y,N"+ "\n"
         +  "^FD" + data.nolab + "^FS"+ "\n"
         +  "^FT5,225^A0N,23,24^FH\^FD"+ data.patienttype +"^FS"+ "\n"
         +  "^PQ1,0,1,Y^XZ"+ "\n"
         console.log(print_data_so_group);
         var data_print = {
            printer : G_BC_PRINTER,
            type : "zpl",
            data : print_data_so_group
         };
         g_bc_cln_socket.send(JSON.stringify(data_print));
      } else {
         console.log("ERR", resp.data);
      }
   })
   .catch( function(e) {
      console.log('Err Get Barcode', e);
   })
}

function one_print_barcode_amplop_so_group(inp) {
   g_bc_cln_socket = new WebSocket(G_BC_WS_SERVICE);
   axios.get('/one-api/v1/su/barcode/so_group',{
      params: {orderId: inp}
   })
   .then( function(resp) {
      if (resp.data.status == "OK") {
         var data = resp.data.rows;
         var print_data_amplop_so_group =   "CT~~CD,~CC^~CT~"+ "\n"
    +  "^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR2,2~SD15^JUS^LRN^CI0^XZ"+ "\n"
    +  "^XA"+ "\n"
    +  "^MMT"+ "\n"
    +  "^PW719"+ "\n"
    +  "^LL0480"+ "\n"
    +  "^LS0"+ "\n"
+  "^FT18,65^A0N,31,31^FH\^GB700,1,3^FS"+ "\n"
+  "^FT18,100^A0N,31,31^FH\^FDNO. ID / NO REG^FS"+ "\n"
+  "^FT18,130^A0N,31,31^FH\^FDTGL REG^FS"+ "\n"
+  "^FT18,160^A0N,31,31^FH\^FDNAMA^FS"+ "\n"
+  "^FT18,190^A0N,31,31^FH\^FDUMUR^FS"+ "\n"
+  "^FT18,220^A0N,31,31^FH\^FDALAMAT^FS"+ "\n"
+  "^FT18,290^A0N,31,31^FH\^FDPENGIRIM^FS"+ "\n"
+  "^FT18,320^A0N,31,31^FH\^FDPSN GRUP^FS"+ "\n"

+  "^FT18,60^A0N,31,31^FH\^FD"+  data.test +"^FS"+ "\n"
+  "^FT19,60^A0N,31,31^FH\^FD"+  data.test  +"^FS"+ "\n"
+  "^FT210,100^A0N,31,31^FH\^FD "+"  :  "+ data.noreg+"/"+ data.nolab+"^FS"+ "\n"
+  "^FT210,130^A0N,31,31^FH\^FD "+"  :  "+data.datereg+"^FS"+ "\n"
+  "^FT210,160^A0N,31,31^FH\^FD "+"  :  "+data.M_PatientName+"^FS"+ "\n"
+  "^FT210,160^A0N,31,31^FH\^FD "+"  :  "+data.M_PatientName+"^FS"+ "\n"
+  "^FT210,190^A0N,31,31^FH\^FD "+"  :  "+ data.age+"^FS"+ "\n"
+  "^FT210,220^A0N,31,31^FH\^FD "+"  :  "+ data.alamat+"^FS"+ "\n"
+  "^FT210,255^A0N,31,31^FH\^FD "+"     "+ data.city+"^FS"+ "\n"
+  "^FT210,290^A0N,31,31^FH\^FD "+"  :  "+ data.M_DoctorName+"^FS"+ "\n"
+  "^FT212,290^A0N,31,31^FH\^FD "+"  :  "+ data.M_DoctorName+"^FS"+ "\n"

+  "^FT210,325^A0N,31,31^FH\^FD "+"  :  "+ data.patienttype+"^FS"+ "\n"
 +  "^BY3,3,45^FT240,380^BCN,Y,N,N,N" + "\n"
+  "^FD" + data.nolab + "^FS"+ "\n"
    +  "^PQ1,0,1,Y^XZ"+ "\n";


         console.log(print_data_amplop_so_group);
         var data_print = {
            printer : G_BC_PRINTER,
            type : "zpl",
            data : print_data_amplop_so_group
         };
         g_bc_cln_socket.send(JSON.stringify(data_print));
      } else {
         console.log("ERR", resp.data);
      }
   })
   .catch( function(e) {
      console.log('Err Get Barcode', e);
   })
}

function one_print_barcode_amplop_so (inp) {
   g_bc_cln_socket = new WebSocket(G_BC_WS_SERVICE);
   axios.get('/one-api/v1/su/barcode/so',{
      params: {orderId: inp}
   })
   .then( function(resp) {
      if (resp.data.status == "OK") {
         var data = resp.data.rows[0];
         var print_data_amplop_so  =   "CT~~CD,~CC^~CT~"+ "\n"
    +  "^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR2,2~SD15^JUS^LRN^CI0^XZ"+ "\n"
    +  "^XA"+ "\n"
    +  "^MMT"+ "\n"
    +  "^PW719"+ "\n"
    +  "^LL0480"+ "\n"
    +  "^LS0"+ "\n"


+  "^FT18,65^A0N,31,31^FH\^GB700,1,3^FS"+ "\n"
+  "^FT18,100^A0N,31,31^FH\^FDNO. ID / NO REG^FS"+ "\n"
+  "^FT18,130^A0N,31,31^FH\^FDTGL REG^FS"+ "\n"
+  "^FT18,160^A0N,31,31^FH\^FDNAMA^FS"+ "\n"
+  "^FT18,190^A0N,31,31^FH\^FDUMUR^FS"+ "\n"
+  "^FT18,220^A0N,31,31^FH\^FDALAMAT^FS"+ "\n"
+  "^FT18,290^A0N,31,31^FH\^FDPENGIRIM^FS"+ "\n"
+  "^FT18,320^A0N,31,31^FH\^FDPSN GRUP^FS"+ "\n"

+  "^FT18,60^A0N,31,31^FH\^FD"+  data.test +"^FS"+ "\n"
+  "^FT19,60^A0N,31,31^FH\^FD"+  data.test  +"^FS"+ "\n"
+  "^FT210,100^A0N,31,31^FH\^FD "+"  :  "+ data.noreg+"/"+ data.nolab+"^FS"+ "\n"
+  "^FT210,130^A0N,31,31^FH\^FD "+"  :  "+data.datereg+"^FS"+ "\n"
+  "^FT210,160^A0N,31,31^FH\^FD "+"  :  "+data.M_PatientName+"^FS"+ "\n"
+  "^FT210,160^A0N,31,31^FH\^FD "+"  :  "+data.M_PatientName+"^FS"+ "\n"
+  "^FT210,190^A0N,31,31^FH\^FD "+"  :  "+ data.age+"^FS"+ "\n"
+  "^FT210,220^A0N,31,31^FH\^FD "+"  :  "+ data.alamat+"^FS"+ "\n"
+  "^FT210,255^A0N,31,31^FH\^FD "+"     "+ data.city+"^FS"+ "\n"
+  "^FT210,290^A0N,31,31^FH\^FD "+"  :  "+ data.M_DoctorName+"^FS"+ "\n"
+  "^FT212,290^A0N,31,31^FH\^FD "+"  :  "+ data.M_DoctorName+"^FS"+ "\n"



+  "^FT210,325^A0N,31,31^FH\^FD "+"  :  "+ data.patienttype+"^FS"+ "\n"
 +  "^BY4,3,45^FT200,390^BCN,Y,N,N,N" + "\n"
+  "^FD" + data.nolab + "^FS"+ "\n"
    +  "^PQ1,0,1,Y^XZ"+ "\n";

         console.log(print_data_amplop_so);
         var data_print = {
            printer : G_BC_PRINTER,
            type : "zpl",
            data : print_data_amplop_so
         };
         g_bc_cln_socket.send(JSON.stringify(data_print));
      } else {
         console.log("ERR", resp.data);
      }
   })
   .catch( function(e) {
      console.log('Err Get Barcode', e);
   })
}

function one_print_barcode_formulir(inp) {
   g_bc_cln_socket = new WebSocket(G_BC_WS_SERVICE);
   axios.get('/one-api/v1/su/barcode/formulir',{
      params: {orderId: inp}
   })
   .then( function(resp) {
      if (resp.data.status == "OK") {
         var data= resp.data.rows ;
         var print_data_formulir = "CT~~CD,~CC^~CT~"+ "\n"
         +  "^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR2,2~SD15^JUS^LRN^CI0^XZ"+ "\n"
         +  "^XA"+ "\n"
         +  "^MMT"+ "\n"
         +  "^PW400"+ "\n"
         +  "^LL0240"+ "\n"
         +  "^LS0"+ "\n"
         +  "^FT5,150^A0N,23,24^FH\^FD"+ data.sexcode +"/"+ data.datereg +"("+ data.age +")^FS"+ "\n"
         +  "^FT5,200^A0N,23,24^FH\^FD"+ data.city+"^FS"+ "\n"
         +  "^FT5,175^A0N,23,24^FH\^FD" + data.alamat + "^FS"+ "\n"
         +  "^FT3,125^A0N,28,28^FH\^FD"+ data.M_PatientName +"^FS"+ "\n"
         +  "^BY2,2,45^FT22,70^BCN,,Y,N"+ "\n"
         +  "^FD" + data.nolab + "^FS"+ "\n"
         +  "^FT5,225^A0N,23,24^FH\^FD"+ data.M_DoctorName +"^FS"+ "\n"
         +  "^FT5,275^A0N,23,24^FH\^FD"+ data.patienttype +"^FS"+ "\n"
         +  "^PQ1,0,1,Y^XZ"+ "\n"
         console.log(print_data_formulir);
         var data_print = {
            printer : G_BC_PRINTER,
            type : "zpl",
            data : print_data_formulir
         };
	setTimeout(function() {
        	g_bc_cln_socket.send(JSON.stringify(data_print));
	},500);
      } else {
         console.log("ERR", resp.data);
      }
   })
   .catch( function(e) {
      console.log('Err Get Barcode', e);
   })
}

window.one_print_barcode_pk= one_print_barcode_pk;
window.one_print_barcode_so = one_print_barcode_so;
window.one_print_barcode_so_group = one_print_barcode_so_group;
window.one_print_barcode_amplop_so_group = one_print_barcode_amplop_so_group;
window.one_print_barcode_amplop_so = one_print_barcode_amplop_so;
window.one_print_barcode_formulir = one_print_barcode_formulir;
