/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DlgAbout.java
 *
 * Created on 23 Jun 10, 19:03:08
 */

package bridging;

import fungsi.koneksiDB;
import fungsi.validasi;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import static javafx.concurrent.Worker.State.FAILED;
import javafx.embed.swing.JFXPanel;
import javafx.print.PageLayout;
import javafx.print.PageOrientation;
import javafx.print.Paper;
import javafx.print.Printer;
import javafx.print.PrinterJob;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.transform.Scale;
import javafx.scene.web.PopupFeatures;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Callback;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import keuangan.DlgBilingRalan;
import keuangan.DlgBilingRanap;
import laporan.DlgDiagnosaPenyakit;
import rekammedis.RMRiwayatPerawatan;

/**
 *
 * @author perpustakaan
 */
public class INACBGHybrid extends javax.swing.JDialog {
    private final JFXPanel jfxPanel = new JFXPanel();
    private WebEngine engine;
 
    private final JPanel panel = new JPanel(new BorderLayout());
    private final JLabel lblStatus = new JLabel();

    private final JTextField txtURL = new JTextField();
    private final JProgressBar progressBar = new JProgressBar();
    private final validasi Valid=new validasi();
    private final Connection koneksi=koneksiDB.condb();
    private PreparedStatement ps;
    private ResultSet rs;
    private String URL="";
                                    
    
    public INACBGHybrid(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        initComponents2();
    }
    
    private void initComponents2() {           
        txtURL.addActionListener((ActionEvent e) -> {
            loadURL(txtURL.getText());
        });
  
        progressBar.setPreferredSize(new Dimension(150, 18));
        progressBar.setStringPainted(true);
        
        panel.add(jfxPanel, BorderLayout.CENTER);
        
        internalFrame1.setLayout(new BorderLayout());
        internalFrame1.add(panel);        
    }
    
     private void createScene() {        
        Platform.runLater(new Runnable() {

            public void run() {
                WebView view = new WebView();
                
                engine = view.getEngine();
                engine.setJavaScriptEnabled(true);
                
                engine.setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() {
                    @Override
                    public WebEngine call(PopupFeatures p) {
                        Stage stage = new Stage(StageStyle.TRANSPARENT);
                        return view.getEngine();
                    }
                });
                
                engine.titleProperty().addListener((ObservableValue<? extends String> observable, String oldValue, final String newValue) -> {
                    SwingUtilities.invokeLater(() -> {
                        INACBGHybrid.this.setTitle(newValue);
                    });
                });
                
                
                engine.setOnStatusChanged((final WebEvent<String> event) -> {
                    SwingUtilities.invokeLater(() -> {
                        lblStatus.setText(event.getData());
                    });
                });
                
                
                engine.getLoadWorker().workDoneProperty().addListener((ObservableValue<? extends Number> observableValue, Number oldValue, final Number newValue) -> {
                    SwingUtilities.invokeLater(() -> {
                        progressBar.setValue(newValue.intValue());
                    });                                                   
                });
                
                engine.getLoadWorker().exceptionProperty().addListener((ObservableValue<? extends Throwable> o, Throwable old, final Throwable value) -> {
                    if (engine.getLoadWorker().getState() == FAILED) {
                        SwingUtilities.invokeLater(() -> {
                            JOptionPane.showMessageDialog(
                                    panel,
                                    (value != null) ?
                                            engine.getLocation() + "\n" + value.getMessage() :
                                            engine.getLocation() + "\nUnexpected Catatan.",
                                    "Loading Catatan...",
                                    JOptionPane.ERROR_MESSAGE);
                        });
                    }
                });
                
                
                engine.locationProperty().addListener((ObservableValue<? extends String> ov, String oldValue, final String newValue) -> {
                    SwingUtilities.invokeLater(() -> {
                        txtURL.setText(newValue);
                    });
                });
                
                engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
                    @Override
                    public void changed(ObservableValue ov, State oldState, State newState) {
                        if (newState == State.SUCCEEDED) {
                            try {
                                if(engine.getLocation().replaceAll("http://"+koneksiDB.HOSTHYBRIDWEB()+":"+koneksiDB.PORTWEB()+"/"+koneksiDB.HYBRIDWEB()+"/","").contains("inacbg/pages")){
                                    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                                    Valid.panggilUrl(engine.getLocation().replaceAll("http://"+koneksiDB.HOSTHYBRIDWEB()+":"+koneksiDB.PORTWEB()+"/"+koneksiDB.HYBRIDWEB()+"/",""));
                                    engine.executeScript("history.back()");
                                    setCursor(Cursor.getDefaultCursor());
                                }else if(engine.getLocation().replaceAll("http://"+koneksiDB.HOSTHYBRIDWEB()+":"+koneksiDB.PORTWEB()+"/"+koneksiDB.HYBRIDWEB()+"/","").contains("Keluar")){
                                    dispose();    
                                }else if(engine.getLocation().replaceAll("http://"+koneksiDB.HOSTHYBRIDWEB()+":"+koneksiDB.PORTWEB()+"/"+koneksiDB.HYBRIDWEB()+"/","").contains("InputDiagnosa")){
                                    URL=engine.getLocation().replaceAll("InputDiagnosa","no");
                                    ps=koneksi.prepareStatement("select temppanggilnorawat.no_rawat, reg_periksa.status_lanjut, reg_periksa.tgl_registrasi  "+
                                            "from temppanggilnorawat inner join reg_periksa on temppanggilnorawat.no_rawat=reg_periksa.no_rawat");
                                    try {
                                        rs=ps.executeQuery();
                                        if(rs.next()){
                                            DlgDiagnosaPenyakit diagnosa=new DlgDiagnosaPenyakit(null,false);
                                            diagnosa.addWindowListener(new WindowListener() {
                                                @Override
                                                public void windowOpened(WindowEvent e) {}
                                                @Override
                                                public void windowClosing(WindowEvent e) {}
                                                @Override
                                                public void windowClosed(WindowEvent e) {
                                                    loadURL(URL);
                                                }
                                                @Override
                                                public void windowIconified(WindowEvent e) {}
                                                @Override
                                                public void windowDeiconified(WindowEvent e) {}
                                                @Override
                                                public void windowActivated(WindowEvent e) {}
                                                @Override
                                                public void windowDeactivated(WindowEvent e) {}
                                            });
                                            diagnosa.setSize(internalFrame1.getWidth()-20,internalFrame1.getHeight()-20);
                                            diagnosa.setLocationRelativeTo(internalFrame1);
                                            diagnosa.isCek();
                                            diagnosa.setNoRm(rs.getString("no_rawat"),rs.getDate("tgl_registrasi"),rs.getDate("tgl_registrasi"),rs.getString("status_lanjut"));
                                            diagnosa.panelDiagnosa1.tampil();
                                            diagnosa.setVisible(true); 
                                        }
                                    } catch (Exception e) {
                                        System.out.println("Notif : "+e);
                                    } finally{
                                        if(rs!=null){
                                            rs.close();
                                        }
                                        if(ps!=null){
                                            ps.close();
                                        }
                                    }   
                                }else if(engine.getLocation().replaceAll("http://"+koneksiDB.HOSTHYBRIDWEB()+":"+koneksiDB.PORTWEB()+"/"+koneksiDB.HYBRIDWEB()+"/","").contains("InputCorona")){
                                    URL=engine.getLocation().replaceAll("InputCorona","no");
                                    ps=koneksi.prepareStatement("select temppanggilnorawat.no_rawat,reg_periksa.no_rkm_medis,pasien.nm_pasien "+
                                            "from temppanggilnorawat inner join reg_periksa on temppanggilnorawat.no_rawat=reg_periksa.no_rawat "+
                                            "inner join pasien on reg_periksa.no_rkm_medis=pasien.no_rkm_medis");
                                    try {
                                        rs=ps.executeQuery();
                                        if(rs.next()){
                                            INACBGPerawatanCorona corona=new INACBGPerawatanCorona(null,false);
                                            corona.addWindowListener(new WindowListener() {
                                                @Override
                                                public void windowOpened(WindowEvent e) {}
                                                @Override
                                                public void windowClosing(WindowEvent e) {}
                                                @Override
                                                public void windowClosed(WindowEvent e) {
                                                    loadURL(URL);
                                                }
                                                @Override
                                                public void windowIconified(WindowEvent e) {}
                                                @Override
                                                public void windowDeiconified(WindowEvent e) {}
                                                @Override
                                                public void windowActivated(WindowEvent e) {}
                                                @Override
                                                public void windowDeactivated(WindowEvent e) {}
                                            });
                                            corona.setSize(internalFrame1.getWidth()-20,internalFrame1.getHeight()-20);
                                            corona.setLocationRelativeTo(internalFrame1);
                                            corona.isCek();
                                            corona.setPasien(rs.getString("no_rawat"),rs.getString("no_rkm_medis"),rs.getString("nm_pasien"));
                                            corona.tampil();
                                            corona.setVisible(true); 
                                        }
                                    } catch (Exception e) {
                                        System.out.println("Notif : "+e);
                                    } finally{
                                        if(rs!=null){
                                            rs.close();
                                        }
                                        if(ps!=null){
                                            ps.close();
                                        }
                                    }   
                                }else if(engine.getLocation().replaceAll("http://"+koneksiDB.HOSTHYBRIDWEB()+":"+koneksiDB.PORTWEB()+"/"+koneksiDB.HYBRIDWEB()+"/","").contains("RiwayatPerawatan")){
                                    URL=engine.getLocation().replaceAll("RiwayatPerawatan","no");
                                    ps=koneksi.prepareStatement("select temppanggilnorawat.no_rawat,reg_periksa.no_rkm_medis,pasien.nm_pasien "+
                                            "from temppanggilnorawat inner join reg_periksa on temppanggilnorawat.no_rawat=reg_periksa.no_rawat "+
                                            "inner join pasien on reg_periksa.no_rkm_medis=pasien.no_rkm_medis");
                                    try {
                                        rs=ps.executeQuery();
                                        if(rs.next()){
                                            RMRiwayatPerawatan resume=new RMRiwayatPerawatan(null,false);
                                            resume.addWindowListener(new WindowListener() {
                                                @Override
                                                public void windowOpened(WindowEvent e) {}
                                                @Override
                                                public void windowClosing(WindowEvent e) {}
                                                @Override
                                                public void windowClosed(WindowEvent e) {
                                                    loadURL(URL);
                                                }
                                                @Override
                                                public void windowIconified(WindowEvent e) {}
                                                @Override
                                                public void windowDeiconified(WindowEvent e) {}
                                                @Override
                                                public void windowActivated(WindowEvent e) {}
                                                @Override
                                                public void windowDeactivated(WindowEvent e) {}
                                            });
                                            resume.setNoRawat(rs.getString("no_rawat"));
                                            resume.setNoRm(rs.getString("no_rkm_medis"),rs.getString("nm_pasien"));
                                            resume.setSize(internalFrame1.getWidth()-20,internalFrame1.getHeight()-20);
                                            resume.setLocationRelativeTo(internalFrame1);
                                            resume.setVisible(true);
                                        }
                                    } catch (Exception e) {
                                        System.out.println("Notif : "+e);
                                    } finally{
                                        if(rs!=null){
                                            rs.close();
                                        }
                                        if(ps!=null){
                                            ps.close();
                                        }
                                    }   
                                }else if(engine.getLocation().replaceAll("http://"+koneksiDB.HOSTHYBRIDWEB()+":"+koneksiDB.PORTWEB()+"/"+koneksiDB.HYBRIDWEB()+"/","").contains("DataBilling")){
                                    URL=engine.getLocation().replaceAll("DataBilling","no");
                                    ps=koneksi.prepareStatement("select temppanggilnorawat.no_rawat,reg_periksa.status_lanjut from temppanggilnorawat inner join reg_periksa on temppanggilnorawat.no_rawat=reg_periksa.no_rawat");
                                    try {
                                        rs=ps.executeQuery();
                                        if(rs.next()){
                                            if(rs.getString("status_lanjut").equals("Ralan")){
                                                DlgBilingRalan dlgbil=new DlgBilingRalan(null,false);
                                                dlgbil.addWindowListener(new WindowListener() {
                                                    @Override
                                                    public void windowOpened(WindowEvent e) {}
                                                    @Override
                                                    public void windowClosing(WindowEvent e) {}
                                                    @Override
                                                    public void windowClosed(WindowEvent e) {
                                                        loadURL(URL);
                                                    }
                                                    @Override
                                                    public void windowIconified(WindowEvent e) {}
                                                    @Override
                                                    public void windowDeiconified(WindowEvent e) {}
                                                    @Override
                                                    public void windowActivated(WindowEvent e) {}
                                                    @Override
                                                    public void windowDeactivated(WindowEvent e) {}
                                                });
                                                dlgbil.TNoRw.setText(rs.getString("no_rawat"));
                                                dlgbil.isCek();
                                                dlgbil.isRawat(); 
                                                dlgbil.setSize(internalFrame1.getWidth()-20,internalFrame1.getHeight()-20);
                                                dlgbil.setLocationRelativeTo(internalFrame1);
                                                dlgbil.setVisible(true);
                                            }else if(rs.getString("status_lanjut").equals("Ranap")){
                                                DlgBilingRanap billing=new DlgBilingRanap( null,false);
                                                billing.addWindowListener(new WindowListener() {
                                                    @Override
                                                    public void windowOpened(WindowEvent e) {}
                                                    @Override
                                                    public void windowClosing(WindowEvent e) {}
                                                    @Override
                                                    public void windowClosed(WindowEvent e) {
                                                        loadURL(URL);
                                                    }
                                                    @Override
                                                    public void windowIconified(WindowEvent e) {}
                                                    @Override
                                                    public void windowDeiconified(WindowEvent e) {}
                                                    @Override
                                                    public void windowActivated(WindowEvent e) {}
                                                    @Override
                                                    public void windowDeactivated(WindowEvent e) {}
                                                });
                                                billing.TNoRw.setText(rs.getString("no_rawat"));                   
                                                billing.isCek();  
                                                billing.isRawat();          
                                                billing.setSize(internalFrame1.getWidth()-20,internalFrame1.getHeight()-20);
                                                billing.setLocationRelativeTo(internalFrame1);
                                                billing.setVisible(true);
                                            }
                                        }
                                    } catch (Exception e) {
                                        System.out.println("Notif : "+e);
                                    } finally{
                                        if(rs!=null){
                                            rs.close();
                                        }
                                        if(ps!=null){
                                            ps.close();
                                        }
                                    }   
                                }
                            } catch (Exception ex) {
                                System.out.println("Notifikasi : "+ex);
                            }
                        } 
                    }
                });
                
                jfxPanel.setScene(new Scene(view));
            }
        });
    }
 
    public void loadURL(String url) {  
        try {
            createScene();
        } catch (Exception e) {
        }
        
        Platform.runLater(() -> {
            try {
                engine.load(url);
            }catch (Exception exception) {
                engine.load(url);
            }
        });        
    }    
    
    public void CloseScane(){
        Platform.setImplicitExit(false);
    }
    
    public void print(final Node node) {
        Printer printer = Printer.getDefaultPrinter();
        PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
        double scaleX = pageLayout.getPrintableWidth() / node.getBoundsInParent().getWidth();
        double scaleY = pageLayout.getPrintableHeight() / node.getBoundsInParent().getHeight();
        node.getTransforms().add(new Scale(scaleX, scaleY));

        PrinterJob job = PrinterJob.createPrinterJob();
        if (job != null) {
            boolean success = job.printPage(node);
            if (success) {
                job.endJob();
            }
        }
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        internalFrame1 = new widget.InternalFrame();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("::[ About Program ]::");
        setUndecorated(true);
        setResizable(false);
        addWindowStateListener(new java.awt.event.WindowStateListener() {
            public void windowStateChanged(java.awt.event.WindowEvent evt) {
                formWindowStateChanged(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        internalFrame1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(240, 245, 235)), "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(50, 70, 40))); // NOI18N
        internalFrame1.setName("internalFrame1"); // NOI18N
        internalFrame1.setLayout(new java.awt.BorderLayout());
        getContentPane().add(internalFrame1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        Platform.setImplicitExit(false);
    }//GEN-LAST:event_formWindowClosed

    private void formWindowStateChanged(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowStateChanged
        if(this.isActive()==false){
            Platform.setImplicitExit(false);
        }
    }//GEN-LAST:event_formWindowStateChanged

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(() -> {
            INACBGHybrid dialog = new INACBGHybrid(new javax.swing.JFrame(), true);
            dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                @Override
                public void windowClosing(java.awt.event.WindowEvent e) {
                    System.exit(0);
                }
            });
            dialog.setVisible(true);
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private widget.InternalFrame internalFrame1;
    // End of variables declaration//GEN-END:variables

    public void setJudul(String Judul){
        internalFrame1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(240, 245, 235)), Judul, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(50,50,50))); // NOI18N
    }
}
