package corporate_services

import (
	"cpone/db"
	"cpone/models"
	dbx "cpone/package/database"
	"fmt"
)

type McuDetailServices struct {
	McuDetailStore db.AppStore
}

func NewMcuDetailServices(uStore db.AppStore) *McuDetailServices {
	return &McuDetailServices{
		McuDetailStore: uStore,
	}
}

func (mcud *McuDetailServices) GetBreadcrumb(id string) (models.BreadCrumbV1, error) {
	var mcu models.DashboardPic

	breadcrumb := models.BreadCrumbV1{
		Title: "",
		Item: []models.ItemBreadCrumbV1{
			{
				Item: "Dashboard",
				Url:  "/corp/dashboard_pic",
			},
			{
				Item: "Employee Health Medical Analytic",
				Url:  "/corp/dashboard_pic",
			},
			{
				Item: "",
				Url:  "",
			},
		},
	}

	q := `
		SELECT
		Mgm_McuID,
		Mgm_McuLabel,
		Mgm_McuFlagRelasiBayarSendiri,
		Mgm_McuBisaTambahPemeriksaan,
		Mgm_McuCorporateID,
		Mgm_McuNumber,
		Mgm_McuNumberNational,
		Mgm_McuNote,
		DATE_FORMAT(Mgm_McuStartDate, '%d-%m-%Y') AS Mgm_McuStartDate,
		DATE_FORMAT(Mgm_McuEndDate, '%d-%m-%Y') AS Mgm_McuEndDate,
		Mgm_McuIsActive
		FROM mgm_mcu
		WHERE Mgm_McuIsActive = 'Y'
		AND Mgm_McuID = ?
	`
	if err := dbx.Handlex.Get(&mcu, q, id); err != nil {
		return breadcrumb, err
	}

	breadcrumb.Title = mcu.Mgm_McuLabel
	breadcrumb.Item[2].Item = mcu.Mgm_McuLabel

	return breadcrumb, nil
}

func (mcud *McuDetailServices) GetTabNameList(id string) ([]models.McuTabModels, error) {
	var ret []models.McuTabModels

	q := `
		SELECT
		Mcu_ImageGrafikGroupResult,
		Mcu_ImageGrafikGroupMenu,
		Mcu_ImageGrafikName,
		Mcu_ImageGrafikJsonRender
		FROM mgm_mcugrafik
		JOIN mcu_image_grafik ON Mcu_ImageGrafikID = Mgm_McuGrafikMcu_ImageGrafikID
		WHERE Mgm_McuGrafikMgm_McuID = ?
		AND Mgm_McuGrafikIsActive = 'Y'
		GROUP BY Mcu_ImageGrafikGroupMenu, Mcu_ImageGrafikGroupResult
		ORDER BY Mcu_ImageGrafikID
	`
	if err := dbx.Handlex.Select(&ret, q, id); err != nil {
		return ret, fmt.Errorf("error get data tab name: %v", err)
	}

	for i, d := range ret {
		switch d.Mcu_ImageGrafikGroupMenu {
		case "SUMMARY":
			if d.Mcu_ImageGrafikGroupResult == "LAB" {
				ret[i].TabID = "tabkelainanlab"
				ret[i].TabName = "Kelainan Lab"
			} else if d.Mcu_ImageGrafikGroupResult == "NONLAB" {
				ret[i].TabID = "tabkelainannonlab"
				ret[i].TabName = "Kelainan Non Lab"
			} else {
				ret[i].TabID = "tabkelainanfisik"
				ret[i].TabName = "Kelainan Fisik"
			}
		case "GLOBAL":
			ret[i].TabID = "tabkelainanglobal"
			ret[i].TabName = "Kelainan Global"
		case "PESERTA":
			ret[i].TabID = "tabkepesertaan"
			ret[i].TabName = "Peserta"
		}
	}

	return ret, nil
}
