/*
 * 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 sikd.ikonsol.ctrl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.PrimeFaces;
import sikd.ikonsol.ejb.session.HeaderResiprokalEjbHome;
import sikd.ikonsol.ejb.session.JenisBasEjbHome;
import sikd.ikonsol.ejb.session.JenisLaporanEjbHome;
import sikd.ikonsol.ejb.session.JenisTransferEjbHome;
import sikd.ikonsol.model.HeaderResiprokal;
import sikd.ikonsol.model.JenisBas;
import sikd.ikonsol.model.JenisLaporan;
import sikd.ikonsol.model.JenisTransfer;

/**
 *
 * @author Imam Bhoim
 */
@ViewScoped
@Named(value = "headerResiprokalBeanCtrl")
public class HeaderResiprokalBeanCtrl implements Serializable {

    HeaderResiprokal headres;
    HeaderResiprokal obj;
    private List<HeaderResiprokal> list;

    private List<JenisLaporan> listjlapor;
    private List<JenisTransfer> listjtransfer;

    private List<JenisBas> listjbas;

    private int idbas = 0;

    @EJB
    HeaderResiprokalEjbHome headerResiprokalEjbHome;

    @EJB
    JenisLaporanEjbHome jenisLaporanEjbHome;

    @EJB
    JenisTransferEjbHome jenisTransferEjbHome;

    @EJB
    JenisBasEjbHome jenisBasEjbHome;

    @PostConstruct
    public void init() {

        //list = headerResiprokalEjbHome.findAll();
        list = headerResiprokalEjbHome.findAllByIdbas(idbas);
        listjbas = jenisBasEjbHome.findAll();
    }

    public void add() {

        headres = headerResiprokalEjbHome.createNew();
//        listjlapor = headerResiprokalEjbHome.findAllJLaporan();
//        System.out.println(listjlapor.size() + " AAA");
//        listjtransfer = headerResiprokalEjbHome.findAllJtransfer();
//        System.out.println(listjtransfer.size() + " BBB ");

        listjlapor = jenisLaporanEjbHome.findAll();
        listjtransfer = jenisTransferEjbHome.findAll();

    }

    public String save() {

        if (idbas == 0) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Info", "Pilih Jenis ID Bas");
            PrimeFaces.current().dialog().showMessageDynamic(message);
        } else {

            headres.setIdbas(idbas);

            System.out.println("SIMPAN 1");
            System.out.println(headres.getId_jenislaporan());
            System.out.println(headres.getId_jenistransfer());

            if (headres.getNamaopsinilai().equalsIgnoreCase("Nilai Original")) {
                headres.setOpsinilai(1);

            } else if (headres.getNamaopsinilai().equalsIgnoreCase("Nilai Provinsi")) {
                headres.setOpsinilai(2);
            } else if (headres.getNamaopsinilai().equalsIgnoreCase("Nilai Terkecil")) {
                headres.setOpsinilai(3);
                System.out.println("SIMPAN nilai terkecil");
            } else {
                //obj.setOpsinilai(1);
                System.out.println("SIMPAN TESSS");
            }

            System.out.println("SIMPAN 2");
            // simpan
            System.out.println(headres.getId_jenislaporan() + " " + headres.getId_jenistransfer() + " "
                    + headres.getOpsinilai());
            headerResiprokalEjbHome.save(headres);
            System.out.println("SIMPAN 3");

            PrimeFaces.current().executeScript("PF('wg_entryform').hide()");
            PrimeFaces.current().ajax().update(":master:dtheaderres");

            //list = headerResiprokalEjbHome.findAll();
            list = headerResiprokalEjbHome.findAllByIdbas(idbas);
        }
        //this.list = service.findAll();
        return "index?faces-redirect=true";

    }

    public String update() {

        headerResiprokalEjbHome.update(obj);

        PrimeFaces.current().executeScript("PF('wg_entryform_ubah').hide()");
        PrimeFaces.current().ajax().update(":master:dtheaderres");

        //list = headerResiprokalEjbHome.findAll();
        list = headerResiprokalEjbHome.findAllByIdbas(idbas);
        // this.list = service.findAll();
        return "index?faces-redirect=true";
    }

    public void edit(int id) {
        //obj = service.findByID(Integer.parseInt(id));
        obj = headerResiprokalEjbHome.findByID(id);

        //listjlapor = headerResiprokalEjbHome.findAllJLaporan();
        //listjtransfer = headerResiprokalEjbHome.findAllJtransfer();
        if (obj.getOpsinilai() == 1) {
            obj.setNamaopsinilai("Nilai Original");
        } else if (obj.getOpsinilai() == 2) {
            obj.setNamaopsinilai("Nilai Provinsi");
        } else if (obj.getOpsinilai() == 3) {
            obj.setNamaopsinilai("Nilai Terkecil");
        } else {
            //obj.setNamahubungan("-");
        }

        //listjlapor = jenisLaporanEjbHome.findByIdList(obj.getId_jenislaporan());
        //listjtransfer = jenisTransferEjbHome.findByIdList(obj.getId_jenistransfer());
        listjlapor = jenisLaporanEjbHome.findAll();
        listjtransfer = jenisTransferEjbHome.findAll();
    }

    public void delete(int id) {
        //obj = service.findByID(Integer.parseInt(id));
        //obj = periodeEjbHome.findByKode(kode);
        headerResiprokalEjbHome.delete(id);
        PrimeFaces.current().ajax().update(":master:dtheaderres");
        //list = headerResiprokalEjbHome.findAll();
        list = headerResiprokalEjbHome.findAllByIdbas(idbas);
    }

    public void listKode(String kode) {

    }

    public void reload() {
        //list = tbEjbHome.findAllByTahunKode(tahun, kdPemda);
        list = headerResiprokalEjbHome.findAllByIdbas(idbas);

//System.out.println("CXCX th : "+tahun+" pmd "+kdPemda);
        //listPemda = pemdaEjbHome.findAll();
    }

    public Map<String, String> opsiNilai() {
        Map<String, String> map = new HashMap<String, String>();

        HashMap<String, List<Integer>> wordFreqMap = new HashMap<>();

        int noOfStudents = 20;
        map.put("Nilai Original", "Nilai Original");
        map.put("Nilai Provinsi", "Nilai Provinsi");
        map.put("Nilai Terkecil", "Nilai Terkecil");

        return map;
    }

    public HeaderResiprokal getHeadres() {
        return headres;
    }

    public void setHeadres(HeaderResiprokal headres) {
        this.headres = headres;
    }

    public List<HeaderResiprokal> getList() {
        return list;
    }

    public void setList(List<HeaderResiprokal> list) {
        this.list = list;
    }

    public HeaderResiprokal getObj() {
        return obj;
    }

    public void setObj(HeaderResiprokal obj) {
        this.obj = obj;
    }

    public HeaderResiprokalEjbHome getHeaderResiprokalEjbHome() {
        return headerResiprokalEjbHome;
    }

    public void setHeaderResiprokalEjbHome(HeaderResiprokalEjbHome headerResiprokalEjbHome) {
        this.headerResiprokalEjbHome = headerResiprokalEjbHome;
    }

    public JenisLaporanEjbHome getJenisLaporanEjbHome() {
        return jenisLaporanEjbHome;
    }

    public void setJenisLaporanEjbHome(JenisLaporanEjbHome jenisLaporanEjbHome) {
        this.jenisLaporanEjbHome = jenisLaporanEjbHome;
    }

    public JenisTransferEjbHome getJenisTransferEjbHome() {
        return jenisTransferEjbHome;
    }

    public void setJenisTransferEjbHome(JenisTransferEjbHome jenisTransferEjbHome) {
        this.jenisTransferEjbHome = jenisTransferEjbHome;
    }

    public List<JenisLaporan> getListjlapor() {
        return listjlapor;
    }

    public void setListjlapor(List<JenisLaporan> listjlapor) {
        this.listjlapor = listjlapor;
    }

    public List<JenisTransfer> getListjtransfer() {
        return listjtransfer;
    }

    public void setListjtransfer(List<JenisTransfer> listjtransfer) {
        this.listjtransfer = listjtransfer;
    }

    public List<JenisBas> getListjbas() {
        return listjbas;
    }

    public void setListjbas(List<JenisBas> listjbas) {
        this.listjbas = listjbas;
    }

    public int getIdbas() {
        return idbas;
    }

    public void setIdbas(int idbas) {
        this.idbas = idbas;
    }

}
