/*
 * JRealizarEmprestimo.java
 *
 * Created on 11 de Outubro de 2004, 13:59
 */

package locadora.ui;


import javax.swing.*;
import java.util.*;
import locadora.core.*;
import locadora.main.*;
import locadora.ui.*;
import locadora.factory.Factory;

/**
 *
 * @author  Rafael
 */
public class JEfetuarEmprestimo extends javax.swing.JInternalFrame implements IRecebeSelecao{

    Emprestimo emprestimo;
    JNumeroField tfValorTotal;
       
    /** Creates new form JRealizarEmprestimo */
    public JEfetuarEmprestimo() {
        initComponents();
        init();
        this.setSize(700,500);
        
        loadObject(new Emprestimo());
        
    }

   
    public void init()
    {
        tfValorTotal = new JNumeroField();        
        tfValorTotal.setTipoMonetario(true);
        tfValorTotal.setEnabled(false);
        getContentPane().add(tfValorTotal);
        tfValorTotal.setBounds(440, 264, 88, 19);
        tfValorTotal.updateUI();        
    }    
    /** 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.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jLabel1 = new javax.swing.JLabel();
        txtNomeCliente = new javax.swing.JTextField();
        btnProcurar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbFitasSelecionadas = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        txtDataAluguel = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDataDevolucao = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnAddFita = new javax.swing.JButton();
        btnRemoveFita = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        btnSalvar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        getContentPane().setLayout(null);

        setTitle("Efetuar Empr\u00e9stimo");
        jLabel1.setText("Nome do Cliente");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(24, 20, 104, 14);

        getContentPane().add(txtNomeCliente);
        txtNomeCliente.setBounds(24, 36, 280, 19);

        btnProcurar.setText("...");
        btnProcurar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProcurarActionPerformed(evt);
            }
        });

        getContentPane().add(btnProcurar);
        btnProcurar.setBounds(316, 34, 20, 23);

        jScrollPane1.setBorder(new javax.swing.border.TitledBorder("Fitas seleciondadas"));
        tbFitasSelecionadas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Título", "Preço"
            }
        ));
        jScrollPane1.setViewportView(tbFitasSelecionadas);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(24, 76, 524, 184);
        jScrollPane1.getAccessibleContext().setAccessibleName("Fitas Selecionadas");

        jLabel2.setText("Data do Aluguel");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(28, 294, 98, 14);

        getContentPane().add(txtDataAluguel);
        txtDataAluguel.setBounds(28, 310, 178, 19);

        jLabel3.setText("Data de Devolu\u00e7\u00e3o");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(28, 340, 120, 14);

        getContentPane().add(txtDataDevolucao);
        txtDataDevolucao.setBounds(28, 356, 178, 19);

        jLabel4.setText("Valor Total");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(330, 266, 90, 14);

        btnAddFita.setText("Adicionar Fitas");
        getContentPane().add(btnAddFita);
        btnAddFita.setBounds(266, 306, 132, 23);

        btnRemoveFita.setText("Remover Fitas");
        btnRemoveFita.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveFitaActionPerformed(evt);
            }
        });

        getContentPane().add(btnRemoveFita);
        btnRemoveFita.setBounds(408, 306, 130, 23);

        jPanel1.setLayout(null);

        jPanel1.setBorder(new javax.swing.border.EtchedBorder());
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        jPanel1.add(btnSalvar);
        btnSalvar.setBounds(12, 12, 104, 23);

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jPanel1.add(btnCancelar);
        btnCancelar.setBounds(124, 12, 108, 23);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(310, 388, 246, 48);

        pack();
    }//GEN-END:initComponents

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        emprestimo = (Emprestimo) getObject();
        Main.factory.save(emprestimo);
        this.setVisible(false);
        //Main.factory.close();
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnProcurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProcurarActionPerformed
        JListagem list;
        list = new JListagem(null,true);
        list.setReceptor(this);
        Factory fac = Main.factory;
        list.setObject(fac.getClientes());
        list.setVisible(true);
    }//GEN-LAST:event_btnProcurarActionPerformed

    private void btnRemoveFitaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveFitaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnRemoveFitaActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed
    
    public void loadTabela(Collection col)
    {
        
    }

     public void setSelecionado(Object obj)
    {
           emprestimo.setCliente((Cliente) obj);
           loadObject(emprestimo);
    }
     
    public void loadObject(Object obj)
    {
        //Main.factory.init();
        emprestimo = (Emprestimo) obj;        
       try{       
            if(emprestimo.getCliente()!=null)
                txtNomeCliente.setText(emprestimo.getCliente().getNome());
            else
                txtNomeCliente.setText("");
            if(emprestimo.getDataLocacao()!=null)
                txtDataAluguel.setText(emprestimo.getDataLocacao().toString());
            else
                txtDataAluguel.setText("");
            if(emprestimo.getDataDevolucao()!=null)
                txtDataDevolucao.setText(emprestimo.getDataDevolucao().toString());
            else
                txtDataDevolucao.setText("");        
           
            tfValorTotal.setValor(emprestimo.getValorAlguel());
       }
       catch(Exception e)
       {
           e.printStackTrace();
       }
     
        loadTabela(emprestimo.getVideoss());
        
    }
    
    public Object getObject()
    {
        try
        {
        emprestimo.setDataLocacao(new Date());
        emprestimo.setDataDevolucao(new Date());                    
        emprestimo.setValorAlguel(tfValorTotal.getValor());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return emprestimo;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddFita;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnProcurar;
    private javax.swing.JButton btnRemoveFita;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbFitasSelecionadas;
    private javax.swing.JTextField txtDataAluguel;
    private javax.swing.JTextField txtDataDevolucao;
    private javax.swing.JTextField txtNomeCliente;
    // End of variables declaration//GEN-END:variables
    
}
