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) {
   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"
            +  "^FT25,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"
             +  "^BY2,2,120^FT44,188^BCN,,N,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) {
   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.M_CompanyName +"^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) {
   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) {
   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"
+  "^FT1,65^A0N,31,31^FH\^GB700,1,3^FS"+ "\n"
+  "^FT1,100^A0N,31,31^FH\^FDNO. ID / NO REG^FS"+ "\n"
+  "^FT1,130^A0N,31,31^FH\^FDTGL REG^FS"+ "\n"
+  "^FT1,160^A0N,31,31^FH\^FDNAMA^FS"+ "\n"
+  "^FT1,190^A0N,31,31^FH\^FDUMUR^FS"+ "\n"
+  "^FT1,220^A0N,31,31^FH\^FDALAMAT^FS"+ "\n"
+  "^FT1,300^A0N,31,31^FH\^FDPENGIRIM^FS"+ "\n"
+  "^FT1,330^A0N,31,31^FH\^FDPSN GRUP^FS"+ "\n"

+  "^FT1,60^A0N,31,31^FH\^FD"+  data.test +"^FS"+ "\n"
+  "^FT2,60^A0N,31,31^FH\^FD"+  data.test  +"^FS"+ "\n"
+  "^FT200,100^A0N,31,31^FH\^FD "+":  "+ data.nolab+"/"+ data.noreg+"^FS"+ "\n"
+  "^FT200,130^A0N,31,31^FH\^FD "+":  "+data.datereg+"^FS"+ "\n"
+  "^FT200,160^A0N,31,31^FH\^FD "+":  "+data.M_PatientName+"^FS"+ "\n"
+  "^FT202,160^A0N,31,31^FH\^FD "+":  "+data.M_PatientName+"^FS"+ "\n"
+  "^FT200,190^A0N,31,31^FH\^FD "+":  "+ data.age+"^FS"+ "\n"
+  "^FT200,220^A0N,31,31^FH\^FD "+":  "+ data.alamat+"^FS"+ "\n"
+  "^FT200,255^A0N,31,31^FH\^FD "+"   "+ data.city+"^FS"+ "\n"
+  "^FT200,300^A0N,31,31^FH\^FD "+":  "+ data.M_DoctorName+"^FS"+ "\n"
+  "^FT202,300^A0N,31,31^FH\^FD "+":  "+ data.M_DoctorName+"^FS"+ "\n"

+  "^FT200,330^A0N,31,31^FH\^FD "+":  "+ data.M_CompanyName+"^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_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) {
   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"
+  "^FT1,65^A0N,31,31^FH\^GB700,1,3^FS"+ "\n"
+  "^FT1,100^A0N,31,31^FH\^FDNO. ID / NO REG^FS"+ "\n"
+  "^FT1,130^A0N,31,31^FH\^FDTGL REG^FS"+ "\n"
+  "^FT1,160^A0N,31,31^FH\^FDNAMA^FS"+ "\n"
+  "^FT1,190^A0N,31,31^FH\^FDUMUR^FS"+ "\n"
+  "^FT1,220^A0N,31,31^FH\^FDALAMAT^FS"+ "\n"
+  "^FT1,300^A0N,31,31^FH\^FDPENGIRIM^FS"+ "\n"
+  "^FT1,330^A0N,31,31^FH\^FDPSN GRUP^FS"+ "\n"

+  "^FT1,60^A0N,31,31^FH\^FD"+  data.test +"^FS"+ "\n"
+  "^FT2,60^A0N,31,31^FH\^FD"+  data.test  +"^FS"+ "\n"
+  "^FT200,100^A0N,31,31^FH\^FD "+":  "+ data.nolab+"/"+ data.noreg+"^FS"+ "\n"
+  "^FT200,130^A0N,31,31^FH\^FD "+":  "+data.datereg+"^FS"+ "\n"
+  "^FT200,160^A0N,31,31^FH\^FD "+":  "+data.M_PatientName+"^FS"+ "\n"
+  "^FT202,160^A0N,31,31^FH\^FD "+":  "+data.M_PatientName+"^FS"+ "\n"
+  "^FT200,190^A0N,31,31^FH\^FD "+":  "+ data.age+"^FS"+ "\n"
+  "^FT200,220^A0N,31,31^FH\^FD "+":  "+ data.alamat+"^FS"+ "\n"
+  "^FT200,255^A0N,31,31^FH\^FD "+"   "+ data.city+"^FS"+ "\n"
+  "^FT200,300^A0N,31,31^FH\^FD "+":  "+ data.M_DoctorName+"^FS"+ "\n"
+  "^FT202,300^A0N,31,31^FH\^FD "+":  "+ data.M_DoctorName+"^FS"+ "\n"

+  "^FT200,330^A0N,31,31^FH\^FD "+":  "+ data.M_CompanyName+"^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);
   })
}

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;
