/*
 * 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.nfe.Controller;
import br.com.ctecinf.nfe.SEFAZConnection;
import br.com.ctecinf.swing.Image;
import br.com.ctecinf.swing.OptionPane;
import br.com.ctecinf.swing.PleaseWaitDialog;
import br.com.ctecinf.table.Table;
import br.com.ctecinf.table.TableModel;
import br.inf.portalfiscal.nfe.v400.inutilizacao.TRetInutNFe;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

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

    private static final String LABEL_INUTILIZAR = "Inutilizar";

    public QuebraSequenciaPanel() {
        super(new BorderLayout());
        initUI();
    }

    private void initUI() {

        try {

            TableModel model = Controller.getQuebraSequencia();

            Table table = new Table(model);

            add(new JScrollPane(table), BorderLayout.CENTER);

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

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

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

        add(pButton, BorderLayout.NORTH);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        new PleaseWaitDialog() {

            @Override
            public Object exec() throws Exception {
                return SEFAZConnection.enviarInutNFe(2, 5);
            }

            @Override
            public void end(Object result) {
                if (result != null) {
                    String msg = ((TRetInutNFe) result).getInfInut().getCStat() + ": " + ((TRetInutNFe) result).getInfInut().getXMotivo();
                    OptionPane.info(msg);
                }
            }

        }.start();
    }

}
