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

import Controllers.CommandeClt.CommandeCltDialogConfirmationController;
import Controllers.Traitement.Adaptateur;
import Controllers.Traitement.MyWindow;
import Controllers.Traitement.ParametreSystem;
import Models.FactureFrs.FactureFrs;
import Models.Reglement.ChequeFrs;
import Models.Reglement.Reglement;
import Models.Reglement.TraiteFrs;
import Models.User.User;
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.scene.Node;
import javafx.scene.Scene;
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.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

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

    public Node nodeFxml ;
    
    @FXML public AnchorPane AnchorPrincipal ;
    @FXML public Button Enregister ;
    
    @FXML private AnchorPane AnchorDetailReglement;
    
    @FXML private Text TextTypePaiement;
    @FXML private Text TextModePaiement;
    @FXML private Text TextDateCreation;
    @FXML private Text TextNombreFact;
    @FXML private Text TextTotalReglement;
    @FXML private Text TextProfile;
    
    @FXML private Text TextCodeFrs;
    @FXML private Text TextNomFrs;
    @FXML private Text TextFormesFrs;
    @FXML private Text TextTele1Frs;
    @FXML private Text TextTele2Frs;
    @FXML private Text TextmailFrs;
    
    @FXML private GridPane GridPaneCheque;
    @FXML private GridPane GridPaneCarte;
    
    @FXML private Text TextCartNomBanque;
    @FXML private Text TextCartNumeroCart;
    @FXML private Text TextCartNumeroTransaction;
    @FXML private Text TextCartDateHeurTransaction;
    
    @FXML private Text TextChequeNomBanque;
    @FXML private Text TextChequeNumero;
    @FXML private Text TextChequeNomComple;
    @FXML private Text TextChequeDate;
    
    @FXML private TableView<ChequeFrs> TableViewListeCheque ;
    @FXML private TableColumn<ChequeFrs ,String> TabColDateCheque;
    @FXML private TableColumn<ChequeFrs ,String> TabColNumeroCheque;
    @FXML private TableColumn<ChequeFrs ,String> TabColBanqueCheque;
    @FXML private TableColumn<ChequeFrs ,String> TabColMontantCheque;
    
    @FXML private TableView<TraiteFrs> TableViewListeTraite ;
    @FXML private TableColumn<TraiteFrs ,String> TabColDateTraite;
    @FXML private TableColumn<TraiteFrs ,String> TabColMontantTraite;
    
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        //Cheque
        TabColNumeroCheque.setStyle( "-fx-alignment: CENTER;");
        TabColNumeroCheque.getStyleClass().add("Center");
        TabColNumeroCheque.setCellValueFactory(new PropertyValueFactory<ChequeFrs, String>("NumeroChequeFrs"));
        
        TabColBanqueCheque.setStyle( "-fx-alignment: CENTER;");
        TabColBanqueCheque.getStyleClass().add("Center");
        TabColBanqueCheque.setCellValueFactory(new PropertyValueFactory<ChequeFrs, String>("BanqueChequeFrs"));
        
        TabColMontantCheque.setStyle( "-fx-alignment: CENTER;");
        TabColMontantCheque.getStyleClass().add("Center");
        TabColMontantCheque.setCellValueFactory(new PropertyValueFactory<ChequeFrs, String>("MontantChequeFrs"));
        
        TabColDateCheque.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColDateCheque.getStyleClass().add("Center");
        TabColDateCheque.setCellValueFactory(new PropertyValueFactory<ChequeFrs, String>("DatePaiement"));
        
        
        TabColDateTraite.setStyle( "-fx-alignment: CENTER;");
        TabColDateTraite.getStyleClass().add("Center");
        TabColDateTraite.setCellValueFactory(new PropertyValueFactory<TraiteFrs, String>("DatePaiement"));
        
        TabColMontantTraite.setStyle( "-fx-alignment: CENTER;");
        TabColMontantTraite.getStyleClass().add("Center");
        TabColMontantTraite.setCellValueFactory(new PropertyValueFactory<TraiteFrs, String>("Montant"));
        
        Image coffeeImage = new Image(getClass().getResourceAsStream("/Public/icon/iconsave.png"));
        ImageView buttonGraphic = new ImageView();
        buttonGraphic.setImage(coffeeImage);
        Enregister.setGraphic(buttonGraphic);
    }    
    
    
    public void setReglement(Reglement reglement){
        
        TextModePaiement.setText(reglement.getModePaiementString());
        TextTypePaiement.setText(reglement.getTypePaiementString());
        
        if(reglement.getTypePaiement().equals("0")){
            if(reglement.getModePaiement().equals("0")){
                AnchorDetailReglement.setVisible(false);
            }else if(reglement.getModePaiement().equals("1")){
                GridPaneCheque.setVisible(true);
                TextChequeNomBanque.setText(reglement.getCheque().getBanqueChequeFrs());
                TextChequeNumero.setText(reglement.getCheque().getNumeroChequeFrs());
                TextChequeNomComple.setText(reglement.getCheque().getNomCompletChequeFrs());
                TextChequeDate.setText(reglement.getCheque().getDatePaiement());
            }else if(reglement.getModePaiement().equals("2")){
                GridPaneCarte.setVisible(true);
                TextCartNomBanque.setText(reglement.getCarte().getBanque());
                TextCartNumeroCart.setText(reglement.getCarte().getCodeCarte());
                TextCartNumeroTransaction.setText(reglement.getCarte().getNumeroTransaction());
                TextCartDateHeurTransaction.setText(Adaptateur.NormalDateFormat(reglement.getCarte().getDateTransaction())+" "+reglement.getCarte().getHeurTransaction());
            }
        }
        
        if(reglement.getTypePaiement().equals("1")){
            AnchorDetailReglement.setVisible(true);
            if(reglement.getListTraite() != null){
                TableViewListeTraite.setVisible(true);
                TableViewListeTraite.setItems(reglement.getListTraiteString());
            }else if(reglement.getListCheque() != null){
                TableViewListeCheque.setVisible(true);
                ObservableList<ChequeFrs>  NewListCheque =  FXCollections.observableArrayList();
                for(ChequeFrs Cheque : reglement.getListCheque()) {
                    String CodeChequeFrs = Cheque.getCodeChequeFrs();
                    String NumeroChequeFrs = Cheque.getNumeroChequeFrs();
                    String BanqueChequeFrs = Cheque.getBanqueChequeFrs();
                    String MontantChequeFrs = Adaptateur.StringDeleZero(Cheque.getMontantChequeFrs());
                    String NomCompletChequeFrs = Cheque.getNomCompletChequeFrs(); 
                    String DatePaiement = Adaptateur.NormalDateFormat(Cheque.getDatePaiement());
                    String DateCreation = Adaptateur.NormalDateTimeFormat(Cheque.getDateCreation());
                    NewListCheque.add(new ChequeFrs(CodeChequeFrs, NumeroChequeFrs, BanqueChequeFrs, MontantChequeFrs, NomCompletChequeFrs, DatePaiement, DateCreation));
                }
                TableViewListeCheque.setItems(NewListCheque);
            }
        }
        
       
        
        ObservableList<FactureFrs> ListFacture = reglement.getListFacture();
        float Somme = 0;
        for(FactureFrs facture : ListFacture ) {
            Somme += Adaptateur.StringToFloat(facture.getNetAPayer());
        }
        
        TextTotalReglement.setText(Adaptateur.FloatToStringEspaceCurrency(Somme));
        TextNombreFact.setText(ListFacture.size()+"");
        
        TextDateCreation.setText(Adaptateur.NormalDateFormat(reglement.getDateTimeCreation()));
        TextProfile.setText(User.nom+"  "+User.prenom);
        
        TextCodeFrs.setText(reglement.getFournisseur().getCode());
        TextNomFrs.setText(reglement.getFournisseur().getNom());
        TextFormesFrs.setText(reglement.getFournisseur().getFormes());
        TextTele1Frs.setText(reglement.getFournisseur().getTele1());
        TextTele2Frs.setText(reglement.getFournisseur().getTele2());
        TextmailFrs.setText(reglement.getFournisseur().getMail());
    }
    
    public  ReglementDialogConfirmationController(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Views/Reglement/ReglementDialogConfirmation.fxml"));
            fxmlLoader.setController(this);
            nodeFxml = (Node) fxmlLoader.load();
        } catch (IOException ex) {
            Logger.getLogger(CommandeCltDialogConfirmationController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @FXML
    private void ExitButtonAction(ActionEvent event) throws IOException {
        AnchorPrincipal.setVisible(false);
    }
    
    
    
    public void Show()
    {
        StackPane Sp = new StackPane();
            
        Scene scene = new Scene(Sp);
        
        Sp.setPrefSize((double)MyWindow.PrincipalContentWidth, (double)MyWindow.PrincipalContentHeight);
        
        Sp.getChildren().add(MyWindow.myParent);
        Sp.getChildren().add(nodeFxml);
        
        Stage stage = MyWindow.myStage;
        stage.setScene(scene);
        stage.show();
    }
}
