package sikd.sikd5.laporan;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import sikd.sikd5.filter.DateMarginFilter;
import sikd.sikd5.model.combo.PemdaCombo;
import sikd.sikd5.security.LoginMB;
import sikd.sikd5.sikd5bean.laporan.Lampiran1Apbd;
import sikd.sikd5.sikd5bean.tabel.Pemda;
import sikd.sikd5.sikd5bean.tabel.TahunAnggaran;
import sikd.sikd5.sikd5bean.tabel.mgr.TUserAccount;
import sikd.sikd5.sikd5ejb.session.LaporanSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.SettingsSessionBeanRemote;

/**
 *
 * @author soraturaes
 */
@ManagedBean(name = "laporanNeracaMBean")
@ViewScoped
public class LaporanNeracaMBean implements Serializable {

    protected DateMarginFilter dateMarginFilter;
    PemdaCombo pemdaCombo;
    String selectedJenisPosting;
    protected List<Lampiran1Apbd> datas;
    
    @EJB
    LaporanSessionBeanRemote laporanSession;
    
    @ManagedProperty(value="#{loginMB}")
    LoginMB loginMB;
    
    short maxTahun;
    @EJB
    SettingsSessionBeanRemote settingSession;
    
    @PostConstruct
    public void init() {
        try {
            List<TahunAnggaran> tahuns = settingSession.getTahuns();
            if(tahuns!=null && !tahuns.isEmpty()){
                maxTahun = tahuns.get(0).getTahun();
            }
            selectedJenisPosting="Semua Data";
            pemdaCombo = new PemdaCombo(true, false, getUpdateRefresh(), "Pemerintah Daerah");
            pemdaCombo.setAlign("center");
            dateMarginFilter = new DateMarginFilter();
            Date dd = new Date();            
            GregorianCalendar gc = (GregorianCalendar)Calendar.getInstance();
            gc.setTime(dd);
            short yy = (short) gc.get(Calendar.YEAR);

            pemdaCombo.setPemdas(loginMB.getPemdaByTahuns(yy));
            Pemda p = new Pemda(0, "", "", "", "Nasional", "", (short) 0);
            if (pemdaCombo.getPemdas() != null && !pemdaCombo.getPemdas().isEmpty()) {
                pemdaCombo.getPemdas().add(0, p);
                pemdaCombo.setSelectedPemda(pemdaCombo.getPemdas().get(0));
            } else {
                pemdaCombo.setPemdas(loginMB.getPemdaByTahuns(maxTahun));
                if( pemdaCombo.getPemdas() != null )
                    pemdaCombo.getPemdas().add(0, p);
                else
                    pemdaCombo.addPemda( p);
                pemdaCombo.setSelectedPemda(p);
            }

            TUserAccount uac = loginMB.getTuserAccount();
            if (uac != null && uac.getPemda() != null) {
                pemdaCombo.setSelectedPemda(uac.getPemda());
                pemdaCombo.setView(false);
            }

        } catch (Exception ex) {
            Logger.getLogger(LaporanNeracaMBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public void onRefresh() {
        if(datas!=null) datas.clear();
        else datas = new ArrayList();
        if (pemdaCombo.getSelectedPemda() != null && pemdaCombo.getSelectedPemda().getKodeSatker() != null) {
            try {
                DecimalFormat df = new DecimalFormat("#.##");
                Date d1 = dateMarginFilter.getDateStart();
                Date d2 = dateMarginFilter.getDateEnd();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String tgl1 = sdf.format(d1);
                String tgl2 = sdf.format(d2);
                if( pemdaCombo.getSelectedPemda().getId()==0 ){
                    if(selectedJenisPosting.trim().equals("Semua Data"))
                        datas = laporanSession.getLaporanNeracaNasional(tgl1, tgl2);
                    else
                        datas = laporanSession.getLaporanPostingedNeracaNasional(tgl1, tgl2);
                }
                else{
                    if(selectedJenisPosting.trim().equals("Semua Data"))
                        datas = laporanSession.getLaporanNeracaPemda(tgl1, tgl2, pemdaCombo.getSelectedPemda().getKodeSatker());
                    else
                        datas = laporanSession.getLaporanPostingedNeracaPemda(tgl1, tgl2, pemdaCombo.getSelectedPemda().getKodeSatker());
                }
                
            } catch (Exception ex) {
                Logger.getLogger(LaporanNeracaMBean.class.getName()).log(Level.SEVERE, null, ex);
                ex.printStackTrace();
            }
        }
        
    }
    
    public void onPemdaChange() {
        
    }

    public PemdaCombo getPemdaCombo() {
        return pemdaCombo;
    }

    public void setPemdaCombo(PemdaCombo pemdaCombo) {
        this.pemdaCombo = pemdaCombo;
    }

    public DateMarginFilter getDateMarginFilter() {
        return dateMarginFilter;
    }

    public void setDateMarginFilter(DateMarginFilter dateMarginFilter) {
        this.dateMarginFilter = dateMarginFilter;
    }

    public List<Lampiran1Apbd> getDatas() {
        return datas;
    }

    public void setDatas(List<Lampiran1Apbd> datas) {
        this.datas = datas;
    }

    public LoginMB getLoginMB() {
        return loginMB;
    }

    public void setLoginMB(LoginMB loginMB) {
        this.loginMB = loginMB;
    }
    public String getUpdateRefresh(){
        return "tabel";
    }

    public String getSelectedJenisPosting() {
        return selectedJenisPosting;
    }

    public void setSelectedJenisPosting(String selectedJenisPosting) {
        this.selectedJenisPosting = selectedJenisPosting;
    }
    
    

}
