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

import Models.ChequeClt.ChequeCltDB;
import Models.ChequeClt.ChequeCltGestionListe;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import com.gluonhq.charm.glisten.control.ProgressBar;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyEvent;
import javafx.util.Callback;


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

    @FXML private ProgressBar ProgressBarVerser;
    @FXML private CheckBox CheckboxSelectionner;
    @FXML private Button ButtonVerserSelection;
    
    @FXML private Label LabelNbrSelectionner ;
    
    @FXML private DatePicker DatePickerDateDebut;
    @FXML private DatePicker DatePickerDateFin;
    
    @FXML private TableView<ChequeCltGestionListe> TableViewChequeCltVerser ;
    
    @FXML private TableColumn<ChequeCltGestionListe ,Boolean> TabColSelectionner;
    @FXML private TableColumn<ChequeCltGestionListe ,String> TabColNumeroCheque;
    @FXML private TableColumn<ChequeCltGestionListe ,String> TabColBanque;
    @FXML private TableColumn<ChequeCltGestionListe ,String> TabColDateCheque;
    @FXML private TableColumn<ChequeCltGestionListe ,String> TabColMontant;
    @FXML private TableColumn<ChequeCltGestionListe ,String> TabColTypeClt;
    @FXML private TableColumn<ChequeCltGestionListe ,String> TabColNomClt;
    @FXML private TableColumn<ChequeCltGestionListe ,String> TabColPrenomClt;
    
    ChequeCltDB ChequeDB= new ChequeCltDB();
    ObservableList<ChequeCltGestionListe> DataListChequeClt;
    
    private String StrDateDebut;
    private String StrDateFin;
    private Integer NbrSelect;
    private Integer NbrCount;
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
        DatePickerDateDebut.setValue(LocalDate.now());
        DatePickerDateFin.setValue(LocalDate.now());
        
        StrDateDebut = DatePickerDateDebut.getValue().toString();
        StrDateFin = DatePickerDateFin.getValue().toString();
        
        GetChequeCltAvercer();
        
        CheckboxSelectionner.setOnAction(new EventHandler<ActionEvent>(){
           @Override
           public void handle(ActionEvent t) {
                NbrSelect = 0;
                ButtonVerserSelection.setDisable(true);
                ButtonVerserSelection.setOpacity(0.82);
                if(CheckboxSelectionner.isSelected()){
                    for(ChequeCltGestionListe ListChequeClt : DataListChequeClt){
                        ListChequeClt.setSelected(true);
                        NbrSelect++;
                    }
                } else {
                   for(ChequeCltGestionListe ListChequeClt : DataListChequeClt){
                       ListChequeClt.setSelected(false);
                   }
                }
                if(NbrSelect>0){
                    ButtonVerserSelection.setDisable(false);
                    ButtonVerserSelection.setOpacity(1);
                }
               TableViewChequeCltVerser.setItems(DataListChequeClt);
               LabelNbrSelectionner.setText(NbrSelect+"/"+NbrCount);
               TabColSelectionner.setVisible(false);
               TabColSelectionner.setVisible(true);
               
           }
        });
        
        TabColNumeroCheque.setCellValueFactory(new PropertyValueFactory<ChequeCltGestionListe, String>("NumeroCheque"));
        TabColNumeroCheque.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColNumeroCheque.getStyleClass().add("Center");
        
        TabColBanque.setCellValueFactory(new PropertyValueFactory<ChequeCltGestionListe, String>("Banque"));
        TabColBanque.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColBanque.getStyleClass().add("Center");
        
        TabColDateCheque.setCellValueFactory(new PropertyValueFactory<ChequeCltGestionListe, String>("DateCheque"));
        TabColDateCheque.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColDateCheque.getStyleClass().add("Center");
        
        TabColMontant.setCellValueFactory(new PropertyValueFactory<ChequeCltGestionListe, String>("Montant"));
        TabColMontant.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColMontant.getStyleClass().add("Center");
        
        TabColTypeClt.setCellValueFactory(new PropertyValueFactory<ChequeCltGestionListe, String>("TypeClt"));
        TabColTypeClt.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColTypeClt.getStyleClass().add("Center");
        
        
        TabColNomClt.setCellValueFactory(new PropertyValueFactory<ChequeCltGestionListe, String>("NomClt"));
        TabColNomClt.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColNomClt.getStyleClass().add("Center");
        
        TabColPrenomClt.setCellValueFactory(new PropertyValueFactory<ChequeCltGestionListe, String>("PrenomClt"));
        TabColPrenomClt.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColPrenomClt.getStyleClass().add("Center");
        
        TabColSelectionner.setCellValueFactory(new PropertyValueFactory<ChequeCltGestionListe, Boolean>("Selected"));
        TabColSelectionner.setStyle("-fx-alignment: CENTER;");
        TabColSelectionner.getStyleClass().add("Center");
        TabColSelectionner.setCellFactory(new Callback<TableColumn<ChequeCltGestionListe,Boolean>,TableCell<ChequeCltGestionListe,Boolean>>(){        
            @Override
            public TableCell<ChequeCltGestionListe, Boolean> call(TableColumn<ChequeCltGestionListe, Boolean> param) {                
                TableCell<ChequeCltGestionListe, Boolean> cell = new TableCell<ChequeCltGestionListe, Boolean>(){
                    @Override
                    public void updateItem(Boolean item, boolean empty) {
                        if(item!=null){
                            final CheckBox Check= new CheckBox();
                            Check.setSelected(item);
                            Check.setOnAction(new EventHandler<ActionEvent>(){
                            @Override
                            public void handle(ActionEvent t) {
                                ChequeCltGestionListe DataListChequeClt = ((ChequeCltGestionListe)getTableRow().getItem());
                                if(Check.isSelected()){
                                    DataListChequeClt.setSelected(true);
                                    NbrSelect++;
                                } else {
                                    DataListChequeClt.setSelected(true);
                                    NbrSelect--;
                                }
                                LabelNbrSelectionner.setText(NbrSelect+"/"+NbrCount);
                                if(NbrSelect>0){
                                    ButtonVerserSelection.setDisable(false);
                                    ButtonVerserSelection.setOpacity(1);
                                }else{
                                    ButtonVerserSelection.setDisable(true);
                                    ButtonVerserSelection.setOpacity(0.82);
                                }
                            }
                        });
                            setGraphic(Check);
                        }else{
                            setGraphic(null);
                        }
                    }
                };                           
                return cell;
            }
        });
        
        
        
        DatePickerDateDebut.setOnAction(new EventHandler<ActionEvent>() {
            @Override 
            public void handle(ActionEvent actionEvent) {
            try{ 
                StrDateDebut = DatePickerDateDebut.getConverter().fromString(DatePickerDateDebut.getEditor().getText()).toString();
                GetChequeCltAvercer();
            }catch(Exception ex){}
            
            }
        });
        DatePickerDateDebut.setOnKeyReleased(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent ke) {
            try{
                StrDateDebut = DatePickerDateDebut.getConverter().fromString(DatePickerDateDebut.getEditor().getText()).toString();
                GetChequeCltAvercer();
            }catch(Exception ex){}
        }
        });
        
       
        DatePickerDateFin.setOnAction(new EventHandler<ActionEvent>(){
            @Override 
            public void handle(ActionEvent actionEvent){
                try{ 
                    StrDateFin = DatePickerDateFin.getConverter().fromString(DatePickerDateFin.getEditor().getText()).toString();
                    GetChequeCltAvercer();
                }catch(Exception ex){}
            }
        });
        DatePickerDateFin.setOnKeyReleased(new EventHandler<KeyEvent>(){
            @Override
            public void handle(KeyEvent ke){
            try{
                StrDateFin = DatePickerDateFin.getConverter().fromString(DatePickerDateFin.getEditor().getText()).toString();
                GetChequeCltAvercer();
            }catch(Exception ex){}
        }
        });
        
        ButtonVerserSelection.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                List<String> ArrayChequeClt = new ArrayList<>();
                List<String> ArrayChequePaiementTraite =  new ArrayList<>(); //traite_paiement_cheque_clt
                for(ChequeCltGestionListe ListChequeClt : DataListChequeClt){
                    if( (ListChequeClt.getSelected()) && (ListChequeClt.getSource() == 0) ){
                        ArrayChequeClt.add(ListChequeClt.getIdCheque());
                    }
                    if( (ListChequeClt.getSelected()) && (ListChequeClt.getSource() == 1) ){
                        ArrayChequePaiementTraite.add(ListChequeClt.getIdCheque());
                    }
                }
                Integer NbrUpdateChequeClt = ChequeDB.UpdateChequeCltVerser(ArrayChequeClt, ArrayChequePaiementTraite);
                if(NbrUpdateChequeClt>0){
                    GetChequeCltAvercer();
                }
            }
        });
        
    }    
    
    
    public void GetChequeCltAvercer(){
        
        ProgressBarVerser.setVisible(true);
        
        // Create a Runnable
        Runnable task = new Runnable(){
            public void run(){
                Platform.runLater(new Runnable(){
                    @Override
		    public void run(){
                        DataListChequeClt = ChequeDB.GetDataChequeAverser(StrDateDebut, StrDateFin);
                        TableViewChequeCltVerser.setItems(DataListChequeClt);
                        NbrCount = DataListChequeClt.size();
                        NbrSelect = 0;
                        LabelNbrSelectionner.setText(NbrSelect+"/"+NbrCount);
                        ProgressBarVerser.setVisible(false);
                    }
		});	
            }
	};
        Thread backgroundThread = new Thread(task);// Run the task in a background thread
	backgroundThread.setDaemon(true);// Terminate the running thread if the application exits
	backgroundThread.start();// Start the thread
    }
    
    
    
    
    
    
}
