/*
 * 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 br.com.ctecinf.nfe.view;

import br.com.ctecinf.Daruma;
import br.com.ctecinf.Utils;
import br.com.ctecinf.nfe.Controller;
import br.com.ctecinf.swing.Image;
import br.com.ctecinf.swing.OptionPane;
import br.com.ctecinf.table.Table;
import br.com.ctecinf.table.TableModel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

/**
 *
 * @author Cássio Conceição
 * @since 29/05/2019
 * @version 201905
 * @see http://ctecinf.com.br/
 */
public class VendasPanel extends JPanel implements ActionListener {

    private static final String LABEL_IMPRIMIR_VENDAS = "Imprimir vendas";
    private static final String LABEL_IMPRIMIR_PRODUTOS = "Imprimir produtos";
    private Table tableVendas;
    private Table tableNCM;
    private final Calendar dt;

    public VendasPanel(Calendar date) {
        super(new BorderLayout());
        this.dt = date;
        initUI();
    }

    private void initUI() {

        TableModel modelVendas;
        TableModel modelNCM;

        double total = 0;

        try {

            modelVendas = Controller.getVendas(dt);

            for (int i = 0; i < modelVendas.getRowCount(); i++) {
                total += ((Number) modelVendas.getValueAt(i, 1)).doubleValue();
            }

            modelVendas.addRow(new Object[]{"TOTAL", total});

            modelNCM = Controller.getSaidaNCM(dt);

        } catch (Exception ex) {
            OptionPane.error(ex);
            return;
        }

        tableVendas = new Table(modelVendas);
        tableNCM = new Table(modelNCM);

        JPanel pVendas = new JPanel(new BorderLayout());
        pVendas.setBorder(BorderFactory.createTitledBorder("Vendas"));
        pVendas.add(new JScrollPane(tableVendas));

        JPanel pNCM = new JPanel(new BorderLayout());
        pNCM.setBorder(BorderFactory.createTitledBorder("Produtos"));
        pNCM.add(new JScrollPane(tableNCM));

        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        split.setDividerLocation(400);
        split.add(pVendas);
        split.add(pNCM);

        add(split, BorderLayout.CENTER);

        JPanel pButton = new JPanel(new FlowLayout(FlowLayout.RIGHT));

        JButton button = new JButton(LABEL_IMPRIMIR_VENDAS, Image.parse(Image.PRINT, 24));
        button.addActionListener(this);
        pButton.add(button);

        button = new JButton(LABEL_IMPRIMIR_PRODUTOS, Image.parse(Image.PRINT, 24));
        button.addActionListener(this);
        pButton.add(button);
        
        add(pButton, BorderLayout.NORTH);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        switch (e.getActionCommand()) {
            case LABEL_IMPRIMIR_VENDAS:

                try {

                    Daruma p = new Daruma();

                    p.iniciarCond();
                    p.tamanhoFonte(4);
                    p.alinharCentro();
                    p.negrito("Vendas " + Utils.ascii(dt.getDisplayName(Calendar.MONTH, Calendar.LONG, new Locale("pt", "BR")), false) + "/" + dt.get(Calendar.YEAR));
                    p.tamanhoFonte(1);

                    p.saltarLinhas(2);

                    for (int i = 0; i < tableVendas.getRowCount(); i++) {

                        Object desc = tableVendas.getValueAt(i, 0);
                        Object vl = tableVendas.getValueAt(i, 1);

                        p.novaLinha();
                        p.alinharEsquerda();
                        p.iniciarCond();
                        p.texto(Utils.leftPad2(desc, 40, ' '));
                        p.texto(Utils.leftPad2(Utils.decimalFormat2Digits().format(vl), 12, ' '));
                        p.novaLinha();

                        if (i + 2 == tableVendas.getRowCount()) {
                            p.linhaCorte();
                        }
                    }

                    p.saltarLinhas(5);

                    p.end();

                } catch (Exception ex) {
                    OptionPane.error(ex);
                }

                break;

            case LABEL_IMPRIMIR_PRODUTOS:

                try {

                    Daruma p = new Daruma();

                    p.iniciarCond();
                    p.tamanhoFonte(4);
                    p.alinharCentro();
                    p.negrito("NCM " + Utils.ascii(dt.getDisplayName(Calendar.MONTH, Calendar.LONG, new Locale("pt", "BR")), false) + "/" + dt.get(Calendar.YEAR));
                    p.tamanhoFonte(1);

                    p.saltarLinhas(2);

                    for (int i = 0; i < tableNCM.getRowCount(); i++) {

                        Object desc = tableNCM.getValueAt(i, 0);
                        Object vl = tableNCM.getValueAt(i, 1);
                        Object und = tableNCM.getValueAt(i, 2);

                        p.novaLinha();
                        p.alinharEsquerda();
                        p.iniciarCond();
                        p.texto(Utils.leftPad2(desc, 38, ' '));
                        p.texto(Utils.leftPad2(Utils.decimalFormat2Digits().format(vl), 7,' '));
                        p.texto(Utils.leftPad2(und, 7, ' '));
                        p.novaLinha();
                    }

                    p.saltarLinhas(5);

                    p.end();

                } catch (Exception ex) {
                    OptionPane.error(ex);
                }

                break;
        }
    }

}
