/*
 * 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.FactureFrs;

import Controllers.Traitement.Adaptateur;
import Models.BonReceptionFrs.BonReceptionFrs;
import Models.FactureFrs.FactureFrs;
import Models.FactureFrs.FactureFrsDB;
import Models.Fournisseur.Fournisseur;
import Models.Produit.ListeProduit;
import Models.User.Profile;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;

/**
 * FXML Controller class
 *
 * @author Maher
 */
public class FactureFrsDetailController implements Initializable {

    @FXML private AnchorPane PaneFactureFrsDetail ;
    @FXML private AnchorPane AnchorPaneLoading;

    @FXML public AnchorPane PaneSucessFrs ;

    @FXML private Tab TabBonReception;
    
    @FXML private Text TextNumero;
    @FXML private Text TextDateReception;
    @FXML private Text TextTypeReglement;   	//type reglement: 0 comptant 1 facilité 
    @FXML private Text TextLocalReception;
    @FXML private Text TextEtatReglement;       //0 :payer 1 :en cour 3: non pays
    @FXML private Text TextProfile;
    @FXML private Text TextNomFrs;
    @FXML private Text TextSpecialiteFrs;
    @FXML private Text TextFormesFrs;
    @FXML private Text TextAdresseFrs;
    @FXML private Text TextTele1Frs;
    @FXML private Text TextTele2Frs;
    
    
    @FXML private Text TotalTVA ;
    @FXML private Text Remise ;
    @FXML private Text NetAPayer ;
    @FXML private Text Timbre ;
    @FXML private Text TotalHTNet ;
    
    @FXML public TableView<ListeProduit> TableViewListeProduit ;
    @FXML public TableColumn<ListeProduit ,String> TabColReference;
    @FXML public TableColumn<ListeProduit ,String> TabColDesignation;
    @FXML public TableColumn<ListeProduit ,String> TabColQuantite ;
    @FXML public TableColumn<ListeProduit ,String> TabColPrixHT;
    @FXML public TableColumn<ListeProduit ,String> TabColRemise;
    @FXML public TableColumn<ListeProduit ,String> TabColTotalHT;
    @FXML public TableColumn<ListeProduit ,String> TabColTVA;
    @FXML public TableColumn<ListeProduit ,String> TabColTotalTTC;

    @FXML public TableView<BonReceptionFrs> TableViewBonReception;
    @FXML public TableColumn<BonReceptionFrs ,String> TabColNumero;
    @FXML public TableColumn<BonReceptionFrs ,String> TabColDate;
    @FXML public TableColumn<BonReceptionFrs ,String> TabColHeur;
    @FXML public TableColumn<BonReceptionFrs ,String> TabColLocalReception;
    @FXML public TableColumn<BonReceptionFrs ,String> TabColTransporteur;
    @FXML public TableColumn<BonReceptionFrs ,String> TabColNetPayer;
    
    private Service<Void> ThreadFactureFrsDetail;
    
    FactureFrs Facture ;
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        TabColReference.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("reference"));
        TabColDesignation.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("designation"));

        TabColQuantite.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("quantite"));
        TabColQuantite.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColQuantite.getStyleClass().add("Center");

        TabColPrixHT.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("PrixHT"));
        TabColPrixHT.setStyle( "-fx-alignment: CENTER;");
        TabColPrixHT.getStyleClass().add("Center");

        TabColRemise.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("Remise"));
        TabColRemise.setStyle( "-fx-alignment: CENTER;");
        TabColRemise.getStyleClass().add("Center");
        
        TabColTotalHT.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("TotalHT"));
        TabColTotalHT.setStyle( "-fx-alignment: CENTER;");
        TabColTotalHT.getStyleClass().add("Center");

        TabColTVA.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("TVA"));
        TabColTVA.setStyle( "-fx-alignment: CENTER;");
        TabColTVA.getStyleClass().add("Center");

        TabColTotalTTC.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("TotalTTC"));
        TabColTotalTTC.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColTotalTTC.getStyleClass().add("Center");
        
        TabColNumero.setCellValueFactory(new PropertyValueFactory<BonReceptionFrs, String>("Numero"));
        
        TabColDate.setCellValueFactory(new PropertyValueFactory<BonReceptionFrs, String>("Date"));
        TabColDate.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColDate.getStyleClass().add("Center");
        
        TabColHeur.setCellValueFactory(new PropertyValueFactory<BonReceptionFrs, String>("Heur"));
        TabColHeur.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColHeur.getStyleClass().add("Center");
        
        
        TabColLocalReception.setCellValueFactory(new PropertyValueFactory<BonReceptionFrs, String>("Local"));
        TabColLocalReception.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColLocalReception.getStyleClass().add("Center");
        
        
        TabColTransporteur.setCellValueFactory(new PropertyValueFactory<BonReceptionFrs, String>("Transporteur"));
        TabColTransporteur.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColTransporteur.getStyleClass().add("Center");
        
        TabColNetPayer.setCellValueFactory(new PropertyValueFactory<BonReceptionFrs, String>("NetPayer"));
        TabColNetPayer.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColNetPayer.getStyleClass().add("Center");
    }

    
    public void setDataFactureDetailFrs(final String IdFactureFrs){
        ThreadFactureFrsDetail = new Service<Void>(){
            @Override
            protected Task<Void> createTask(){
                return new Task<Void>(){
                    @Override
                    protected Void call() throws Exception{
                        
                        Facture =  new FactureFrsDB().getFactureFrs(IdFactureFrs, null, null);  
                        
                        return null;
                    }
                };
            }
	};
        ThreadFactureFrsDetail.setOnSucceeded(new EventHandler<WorkerStateEvent>(){
            @Override
            public void handle(WorkerStateEvent event){
                TextNumero.setText(Facture.getNumero());
                TextDateReception.setText(Facture.getDateCreation()+" "+Facture.getHeurCreation());
                TextTypeReglement.setText(Facture.getTypeReglementString());
                TextLocalReception.setText(Facture.getLocal());
                TextEtatReglement.setText(Facture.getEtatReglementString());
                
                Profile profile = Facture.getProfile();
                TextProfile.setText(profile.getNom()+" "+profile.getPrenom());
                
                
                TotalTVA.setText(Adaptateur.FloatToStringEspaceCurrency(Facture.getFTotalTVA()));
                Remise.setText(Adaptateur.FloatToStringEspaceCurrency(Facture.getFRemise()));
                NetAPayer.setText(Adaptateur.FloatToStringEspaceCurrency(Facture.getFNetAPayer()));
                Timbre.setText(Adaptateur.FloatToStringEspaceCurrency(Facture.getFTimbre()));
                TotalHTNet.setText(Adaptateur.FloatToStringEspaceCurrency(Facture.getFTotalHorsTaxNet()));
                
                Fournisseur Frs = Facture.getFournisseur();
                TextNomFrs.setText(Frs.getNom());
                TextSpecialiteFrs.setText(Frs.getSpecialite());
                TextFormesFrs.setText(Frs.getFormes());
                TextAdresseFrs.setText(Frs.getAdresse());
                TextTele1Frs.setText(Frs.getTele1());
                TextTele2Frs.setText(Frs.getTele2());
                
                ObservableList<ListeProduit> ProduitList = Facture.getListProduit();
                TableViewListeProduit.setItems(ProduitList);
                TableViewListeProduit.refresh();
                
                if(Facture.getBonReception() == null){
                    TabBonReception.setText("");
                    TabBonReception.setDisable(true);
                }else{
                    ObservableList<BonReceptionFrs> ListBonReception = Facture.getListeBonReception();
                    TableViewBonReception.setItems(ListBonReception);
                    TableViewBonReception.refresh();
                }
                
                PaneFactureFrsDetail.setVisible(true);
                AnchorPaneLoading.setVisible(false);
                
            }
        });
        ThreadFactureFrsDetail.start();
    }
    
}
