/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers.Stock;

import Controllers.Traitement.Adaptateur;
import Models.Stock.BonEntree;
import Models.Stock.BondeEntrerDB;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.print.JobSettings;
import javafx.print.PageRange;
import javafx.print.PrinterJob;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;

/**
 * FXML Controller class
 * @author Maher Ben Tili
 */
public class BondeEntrerDetailController implements Initializable {
    
    @FXML private AnchorPane   BondeEntrerDetail;
    
    @FXML private Button RecreerBD ;
    
    @FXML private Text source ;
    @FXML private Text id ;
    @FXML private Text Textdate ;
    @FXML private Text heur ;
    @FXML private Text transporteur ;
    
    @FXML private TableView<ListProduitBon> TableViewListeProduitBE ;
    @FXML private TableColumn<ListProduitBon ,String> TabColBEReference;
    @FXML private TableColumn<ListProduitBon ,String> TabColBEDesignation;
    @FXML private TableColumn<ListProduitBon ,String> TabColBEMarque ;
    @FXML private TableColumn<ListProduitBon ,String> TabColBECategorie;
    @FXML private TableColumn<ListProduitBon ,String> TabColBEPrixTTC;
    @FXML private TableColumn<ListProduitBon ,String> TabColBEQuantiteTTC;
    @FXML private TableColumn<ListProduitBon ,String> TabColBETVA;
    
    
    public  static String id_bondentree ;
    
    BondeEntrerDB MyBonEntreeDB = new BondeEntrerDB();
    BonEntree MyBonEntree = new BonEntree();
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
        Image coffeeImage = new Image(getClass().getResourceAsStream("/Public/icon/icon-relogin.png"));
        ImageView buttonGraphic = new ImageView();
        buttonGraphic.setImage(coffeeImage);
        RecreerBD.setGraphic(buttonGraphic);
        
        TabColBEReference.setCellValueFactory(new PropertyValueFactory<ListProduitBon, String>("reference"));
        
        TabColBEDesignation.setCellValueFactory(new PropertyValueFactory<ListProduitBon, String>("designaton"));
        
        TabColBEMarque.setCellValueFactory(new PropertyValueFactory<ListProduitBon, String>("marque"));
        
        TabColBECategorie.setCellValueFactory(new PropertyValueFactory<ListProduitBon, String>("categorie"));
        
        TabColBEPrixTTC.setCellValueFactory(new PropertyValueFactory<ListProduitBon, String>("prixttc"));
        TabColBEPrixTTC.setStyle( "-fx-alignment: CENTER_RIGHT;");
        
        TabColBETVA.setCellValueFactory(new PropertyValueFactory<ListProduitBon, String>("tva"));
        TabColBETVA.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        
        TabColBEQuantiteTTC.setCellValueFactory(new PropertyValueFactory<ListProduitBon, String>("quantite"));
        TabColBEQuantiteTTC.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        
        
        MyBonEntree = MyBonEntreeDB.getBonEntree(id_bondentree);
        
        id.setText(MyBonEntree.getId());
        source.setText(MyBonEntree.getLocal_emetteur());
        Textdate.setText(Adaptateur.NormalDateFormat(MyBonEntree.getDate().toString()));
        heur.setText(MyBonEntree.getHeur().toString());
        transporteur.setText(MyBonEntree.getTransporteur());
        TableViewListeProduitBE.setItems(MyBonEntree.getListe_prod());
        
    }  

    @FXML
    private void PDFBonEntreButtonAction(ActionEvent event) throws IOException {
        try {
            ObservableList<ListProduitBon> ListProd = MyBonEntree.getListe_prod();
            PrinterJob jobExportPDF = PrinterJob.createPrinterJob();
            int SizelistMouvement = ListProd.size();
            int total = SizelistMouvement / 20 + 1 ;
            int page = 0;
            for (int i=0; i<SizelistMouvement; i+=20){
                ObservableList<ListProduitBon> listProdTemp = FXCollections.observableArrayList();
                for (int j=i; (j<(i+20)) && (j<SizelistMouvement); j++){
                    listProdTemp.add(ListProd.get(j));
                }
                FXMLLoader fxmlLoaderPrintBonEnter = new FXMLLoader(getClass().getResource("/Views/Stock/PrintBon.fxml"));
                final Node ParentFxmlPrintBonEnter = (Node)fxmlLoaderPrintBonEnter.load();
                BondeEnterPrintController PrintBonEnter = fxmlLoaderPrintBonEnter.getController();
                page++;
                PrintBonEnter.setDataBonEnter(listProdTemp, "Bon d'entrée dépuis ", MyBonEntree.getLocal_emetteur(), "Récepteur", MyBonEntree.getLocal_recepteur(),  MyBonEntree.getId(), MyBonEntree.getDate().toString(), MyBonEntree.getHeur().toString(), MyBonEntree.getTransporteur(), page+"/"+total);
                jobExportPDF.printPage(ParentFxmlPrintBonEnter);
            }
            jobExportPDF.endJob();
        } catch (IOException ex) {
            System.err.println("BondeEntrerDetailController PDFBonEntreButtonAction ! \n"+ex.getMessage());
        }
    }
    
    @FXML
    private void PrintBonEntreButtonAction(ActionEvent event) throws IOException {
        try {
            ObservableList<ListProduitBon> ListProd = MyBonEntree.getListe_prod();
            
            int SizelistMouvement = ListProd.size();
            int total = SizelistMouvement / 20 + 1 ;
            int page = 0;
            
            PrinterJob jobPrint = PrinterJob.createPrinterJob();  
            jobPrint.getJobSettings().setPageRanges(new PageRange(1, total));
            
            if (jobPrint.showPrintDialog(null)) {
                JobSettings jsPrint = jobPrint.getJobSettings();
                for (int i=0; i<SizelistMouvement; i+=20){
                    ObservableList<ListProduitBon> listProdTemp = FXCollections.observableArrayList();
                    for (int j=i; (j<(i+20)) && (j<SizelistMouvement); j++){
                        listProdTemp.add(ListProd.get(j));
                    }
                    FXMLLoader fxmlLoaderPrintBonEnter = new FXMLLoader(getClass().getResource("/Views/Stock/BondeEnterPrint.fxml"));
                    final Node ParentFxmlPrintBonEnter = (Node)fxmlLoaderPrintBonEnter.load();
                    BondeEnterPrintController PrintBonEnter = fxmlLoaderPrintBonEnter.getController();
                    page++;
                    PrintBonEnter.setDataBonEnter(listProdTemp, "Bon d'entrée dépuis ", MyBonEntree.getLocal_emetteur(), "Récepteur", MyBonEntree.getLocal_recepteur(),  MyBonEntree.getId(), MyBonEntree.getDate().toString(), MyBonEntree.getHeur().toString(), MyBonEntree.getTransporteur(), page+"/"+total);
                    jobPrint.printPage(ParentFxmlPrintBonEnter);
                }
                jobPrint.endJob();
            }
        } catch (IOException ex) {
            System.err.println("BondeEntrerDetailController PrintBonEntreButtonAction ! \n"+ex.getMessage());
        }
    }
    
    
    @FXML
    private void NewBonEnterButtonAction(ActionEvent event) throws IOException {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Views/Stock/BondeEntrer.fxml"));
            Parent root = (Parent)fxmlLoader.load();
            BondeEntrerDetail.getChildren().clear();
            BondeEntrerDetail.getChildren().add(root);
        } catch (IOException ex) {
            Logger.getLogger(BondeEntrerController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
    
}
