package mcu_corporate_services

import (
	"bytes"
	"cpone/db"
	"cpone/models"
	"cpone/package/config"
	dbx "cpone/package/database"
	"encoding/json"
	"fmt"
	"io"
	"math"
	"net/http"
	"net/url"
	"strconv"

	"go.uber.org/zap"
)

type TabDaftarPesertaServices struct {
	TabDaftarPesertaStore db.AppStore
}

func NewTabDaftarPesertaServices(uStore db.AppStore) *TabDaftarPesertaServices {
	return &TabDaftarPesertaServices{
		TabDaftarPesertaStore: uStore,
	}
}

func (tdps *TabDaftarPesertaServices) GetListDaftarPesertaMCU(id string, currentpage int, rowperpage int) ([]models.ModelMcuDaftarPeserta, int, error) {
	var listDaftarPeserta []models.ModelMcuDaftarPeserta
	var totalData int
	offset := (currentpage - 1) * rowperpage

	querytotal := `SELECT COUNT(*)
		FROM t_orderheader
		JOIN m_patient ON T_OrderHeaderM_PatientID = M_PatientID
		AND M_PatientIsActive = 'Y'
		WHERE T_OrderHeaderIsActive = 'Y' AND T_OrderHeaderMgm_McuID = ?`

	if err := dbx.Handlex.Get(&totalData, querytotal, id); err != nil {
		return nil, 0, fmt.Errorf("error querying database: %v", err)
	}

	query := `
		SELECT *,
		GROUP_CONCAT(test_paket) as list_paket
		FROM (
			SELECT 
			T_OrderHeaderID,
			T_OrderHeaderDate,
			IFNULL(T_OrderHeaderLabNumber, 0) AS T_OrderHeaderLabNumber,
			T_OrderHeaderM_PatientAge,
			M_PatientID,
			M_PatientNoReg,
			M_PatientName,
			M_PatientGender,
			CASE 
				WHEN M_PatientGender = 'male' THEN 'Laki - Laki'
				WHEN M_PatientGender = 'female' THEN 'Perempuan'
			ELSE ' '
			END AS jenisKelamin,
			M_PatientDOB,
			FLOOR(DATEDIFF(CURDATE(), M_PatientDOB)/ 365.25) AS age,
			IFNULL(authPatientEmail, "none") AS authPatientEmail,
			IFNULL(authPatientIsActive, "N") AS authPatientIsActive,
			CASE
				WHEN T_OrderDetailOrderIsPacket = 'Y' THEN T_OrderDetailOrderT_PacketName
				ELSE T_OrderDetailOrderT_TestName
			END AS test_paket
			FROM t_orderheader
			JOIN m_patient ON T_OrderHeaderM_PatientID = M_PatientID AND M_PatientIsActive = 'Y'
			JOIN t_orderdetailorder ON T_OrderDetailOrderT_OrderHeaderID = T_OrderHeaderID 
			AND T_OrderDetailOrderIsActive = 'Y'
			LEFT JOIN auth_patient ON M_PatientID = authPatientM_PatientID AND  authPatientIsActive = 'Y'
			WHERE T_OrderHeaderIsActive = 'Y' AND T_OrderHeaderMgm_McuID = ?
		) x
		GROUP BY T_OrderHeaderID
		ORDER BY T_OrderHeaderLabNumber ASC
		LIMIT ? OFFSET ?
	`

	if err := dbx.Handlex.Select(&listDaftarPeserta, query, id, rowperpage, offset); err != nil {
		return nil, 0, fmt.Errorf("error querying database: %v", err)
	}
	totalPage := int(math.Ceil(float64(totalData) / float64(rowperpage)))

	return listDaftarPeserta, totalPage, nil
}

func (tdps *TabDaftarPesertaServices) DetailTestdanPaket(orderheaderID string) ([]models.TableTestPaket, error) {
	var listPaket []models.DetailPaketMCU
	var listTest []models.DetailTestMCU
	var ret []models.TableTestPaket

	// get list test name
	qr := `
		SELECT
		T_OrderDetailOrderT_TestID,
		T_OrderDetailOrderT_TestName
		FROM t_orderdetailorder
		WHERE T_OrderDetailOrderT_OrderHeaderID = ?
		AND T_OrderDetailOrderIsActive = 'Y'
		AND T_OrderDetailOrderIsPacket = 'N'
	`
	if err := dbx.Handlex.Select(&listTest, qr, orderheaderID); err != nil {
		return ret, err
	}

	// get detail paket
	q := `
		SELECT
		T_OrderDetailOrderT_PacketID,
		T_OrderDetailOrderT_PacketName,
		T_TestName
		FROM t_orderdetailorder
		JOIN t_packetdetail ON T_PacketDetailT_PacketID = T_OrderDetailOrderT_PacketID AND T_PacketDetailIsActive = 'Y'
		JOIN t_test ON T_TestName = T_PacketDetailT_TestID AND T_TestIsActive = 'Y'
		WHERE T_OrderDetailOrderT_OrderHeaderID = ? AND T_OrderDetailOrderIsActive = 'Y'
	`
	if err := dbx.Handlex.Select(&listPaket, q, orderheaderID); err != nil {
		return ret, err
	}

	nomor := 1
	for _, d := range listTest {
		ret = append(ret, models.TableTestPaket{
			Nomor:     nomor,
			NamaTest:  d.T_OrderDetailOrderT_TestName,
			NamaPaket: d.T_OrderDetailOrderT_TestName,
		})
		nomor++
	}

	for _, d := range listPaket {
		ret = append(ret, models.TableTestPaket{
			Nomor:     nomor,
			NamaTest:  d.T_TestName,
			NamaPaket: d.T_OrderDetailOrderT_PacketName,
		})
		nomor++
	}

	return ret, nil
}

func (tdps *TabDaftarPesertaServices) GenerataPasswordMCU(mgmMCUID string, host string) (models.GeneratePasswordResponse, error) {
	logger, _ := zap.NewProduction()
	var ret models.GeneratePasswordResponse
	uri := "https://" + config.Data.Get("servername") + "/one-api/tools/auth_patient/generate/" + mgmMCUID

	payload := []byte{}
	resp, err := http.Post(uri, "application/json", bytes.NewBuffer(payload))
	if err != nil {
		return ret, fmt.Errorf("error request generate password: %v", err)
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return ret, fmt.Errorf("error read body respoonse")
	}

	if err := json.Unmarshal(body, &ret); err != nil {
		return ret, fmt.Errorf("error unmarshal json respoonse")
	}
	defer logger.Sync()
	logger.Info("response", zap.Any("data", ret))

	return ret, nil
}

func (tdps *TabDaftarPesertaServices) GetAkunPeserta(patientID string) (models.AuthPatient, error) {
	// logger, _ := zap.NewProduction()
	var ret models.AuthPatient

	q := `
	SELECT
	authPatientID,
	authPatientM_PatientID,
	authPatientEmail,
	authPatientPassword,
	authPatientIsActive
	FROM auth_patient
	WHERE authPatientM_PatientID = ?
	AND authPatientIsActive = 'Y'
	`
	if err := dbx.Handlex.Get(&ret, q, patientID); err != nil {
		return ret, fmt.Errorf("error get akun peserta")
	}
	// logger.Info("response", zap.Any("data", ret))

	return ret, nil
}

func (tdps *TabDaftarPesertaServices) ResetPasswordPeserta(token string, newPassword string, email string, authuserid string) (models.ResponseStatus, error) {
	var ret models.ResponseStatus

	uri := "https://" + config.Data.Get("servername") + "/one-api/mockup/system/menu/reset_password_pat"

	params := url.Values{}
	params.Add("new", newPassword)
	params.Add("email", email)
	params.Add("authuserid", authuserid)
	params.Add("token", token)

	req, err := http.PostForm(uri, params)
	if err != nil {
		ret = models.ResponseStatus{
			Status:  "ERR",
			Message: "error request change password",
		}
		return ret, err
	}
	defer req.Body.Close()

	resp, err := io.ReadAll(req.Body)
	if err != nil {
		ret = models.ResponseStatus{
			Status:  "ERR",
			Message: "error read response",
		}
		return ret, err
	}

	err = json.Unmarshal(resp, &ret)
	if err != nil {
		ret = models.ResponseStatus{
			Status:  "ERR",
			Message: "error unmarshal response",
		}
		return ret, err
	}

	return ret, nil
}

func (tdps *TabDaftarPesertaServices) GetListReportPesertaV2(orderheaderID string, host string) ([]models.TabViewReportMcu, error) {
	var returnTabRpt []models.TabViewReportMcu
	var SoResultData []models.SoResultEntryReportModel

	q := `
	SELECT
	So_ResultEntryID,
	So_ResultEntryT_OrderHeaderID,
	So_ResultEntryNonlab_TemplateID,
	So_ResultEntryNonlab_TemplateName,
	DATE_FORMAT(NOW(), '%y%m%d%H%i%s') AS Datetime
	FROM so_resultentry
	WHERE So_ResultEntryT_OrderHeaderID = ?
	AND So_ResultEntryIsActive = 'Y'
	ORDER BY So_ResultEntryNonlab_TemplateID
	`
	if err := dbx.Handlex.Select(&SoResultData, q, orderheaderID); err != nil {
		return returnTabRpt, fmt.Errorf("error get so result data non lab: %v", err)
	}

	mapSoResult := map[string]int{}
	for _, so := range SoResultData {
		switch so.So_ResultEntryNonlab_TemplateName {
		case "ECG":
			mapSoResult["Elektromedik"] = so.So_ResultEntryID
		case "Thorax PA":
			mapSoResult["Radiologi"] = so.So_ResultEntryID
		case "Fisik Umum", "Fisik Umum K3":
			mapSoResult["Fisik"] = so.So_ResultEntryID
		default:
			mapSoResult[so.So_ResultEntryNonlab_TemplateName] = so.So_ResultEntryID
		}
	}

	var GroupResultOrder []models.GroupResultOrderModel
	q = `
	SELECT
	gr.Group_ResultID,
	gr.Group_ResultName,
	gr.Group_ResultResumeMcu,
	gr.Group_ResultUrl,
	gr.Group_ResultIsOrderHeader,
	gro.Group_ResultOrderOrder,
	DATE_FORMAT(NOW(), '%y%m%d%H%i%s') AS Datetime
	FROM t_orderdetail todtl
	JOIN t_test tt ON todtl.T_OrderDetailT_TestID = tt.T_TestID
	AND tt.T_TestIsActive = 'Y'
	JOIN group_resultdetail grd ON tt.T_TestID = grd.Group_ResultDetailT_TestID
	AND grd.Group_ResultDetailIsActive = 'Y'
	JOIN group_result gr ON grd.Group_ResultDetailGroup_ResultID = gr.Group_ResultID
	AND gr.Group_ResultIsActive = 'Y'
	JOIN group_resultorder gro ON gro.Group_ResultOrderGroup_ResultID = gr.Group_ResultID
	AND Group_ResultOrderIsActive = 'Y'
	WHERE todtl.T_OrderDetailT_OrderHeaderID = ?
	AND todtl.T_OrderDetailIsActive = 'Y'
	GROUP BY gr.Group_ResultID
	ORDER BY gro.Group_ResultOrderOrder
	`
	if err := dbx.Handlex.Select(&GroupResultOrder, q, orderheaderID); err != nil {
		return returnTabRpt, fmt.Errorf("error get group result order: %v", err)
	}

	for _, d := range GroupResultOrder {
		var urlMap map[string]string
		if err := json.Unmarshal([]byte(d.Group_ResultUrl), &urlMap); err != nil {
			return returnTabRpt, fmt.Errorf("error parsing json: %v", err)
		}

		if d.Group_ResultIsOrderHeader == "N" {
			if d.Group_ResultResumeMcu == "FISIK" {
				soResultID := strconv.Itoa(mapSoResult["Fisik"])
				for key, value := range urlMap {
					returnTabRpt = append(returnTabRpt, models.TabViewReportMcu{
						Name: key,
						Link: value + soResultID + "&tm=" + d.Datetime,
					})
				}
			}
			if d.Group_ResultResumeMcu == "NONLAB" {
				soResultID := strconv.Itoa(mapSoResult[d.Group_ResultName])
				for key, value := range urlMap {
					returnTabRpt = append(returnTabRpt, models.TabViewReportMcu{
						Name: key,
						Link: value + soResultID + "&tm=" + d.Datetime,
					})
				}
			}
		} else {
			for _, value := range urlMap {
				returnTabRpt = append(returnTabRpt, models.TabViewReportMcu{
					Name: d.Group_ResultName,
					Link: value + orderheaderID + "&tm=" + d.Datetime,
				})
			}
		}
	}

	return returnTabRpt, nil
}

func (tdps *TabDaftarPesertaServices) GetListReportPesertaV3(orderheaderID string, staffname string) ([]models.TabViewReportMcu, error) {
	var returnTab []models.TabViewReportMcu
	var GroupResultOrder []models.GroupResultOrderModel
	var SoResultData []models.SoResultEntryReportModel

	q := `
	SELECT
	gr.Group_ResultID,
	gr.Group_ResultName,
	gr.Group_ResultResumeMcu,
	gr.Group_ResultUrl,
	gr.Group_ResultIsOrderHeader,
	gro.Group_ResultOrderOrder,
	DATE_FORMAT(NOW(), '%y%m%d%H%i%s') AS Datetime
	FROM t_orderdetail todtl
	JOIN t_test tt ON todtl.T_OrderDetailT_TestID = tt.T_TestID
	AND tt.T_TestIsActive = 'Y'
	JOIN group_resultdetail grd ON tt.T_TestID = grd.Group_ResultDetailT_TestID
	AND grd.Group_ResultDetailIsActive = 'Y'
	JOIN group_result gr ON grd.Group_ResultDetailGroup_ResultID = gr.Group_ResultID
	AND gr.Group_ResultIsActive = 'Y'
	JOIN group_resultorder gro ON gro.Group_ResultOrderGroup_ResultID = gr.Group_ResultID
	AND Group_ResultOrderIsActive = 'Y'
	WHERE todtl.T_OrderDetailT_OrderHeaderID = ?
	AND todtl.T_OrderDetailIsActive = 'Y'
	GROUP BY gr.Group_ResultID
	ORDER BY gro.Group_ResultOrderOrder
	`
	if err := dbx.Handlex.Select(&GroupResultOrder, q, orderheaderID); err != nil {
		return returnTab, fmt.Errorf("error get group result order: %v", err)
	}

	q = `
	SELECT
	sre.So_ResultEntryID,
	sre.So_ResultEntryT_OrderHeaderID,
	sre.So_ResultEntryT_OrderDetailID,
	sre.So_ResultEntryNonlab_TemplateID,
	sre.So_ResultEntryNonlab_TemplateName
	FROM t_orderdetail tdet
	JOIN t_test tt ON tdet.T_OrderDetailT_TestID = tt.T_TestID
	AND tt.T_TestIsActive = 'Y'
	JOIN so_resultentry sre ON sre.So_ResultEntryT_OrderDetailID = tdet.T_OrderDetailID
	AND sre.So_ResultEntryIsActive = 'Y'
	WHERE tdet.T_OrderDetailT_OrderHeaderID = ?
	AND tdet.T_OrderDetailIsActive = 'Y'	
	`
	if err := dbx.Handlex.Select(&SoResultData, q, orderheaderID); err != nil {
		return returnTab, fmt.Errorf("error get so result data non lab: %v", err)
	}

	mapSoResultData := map[string]int{}
	for _, so := range SoResultData {
		switch so.So_ResultEntryNonlab_TemplateName {
		case "ECG":
			mapSoResultData["Elektromedik"] = so.So_ResultEntryID
		case "Thorax PA":
			mapSoResultData["Rontgen"] = so.So_ResultEntryID
		case "Fisik Umum", "Fisik Umum K3":
			mapSoResultData["FISIK"] = so.So_ResultEntryID
		default:
			mapSoResultData[so.So_ResultEntryNonlab_TemplateName] = so.So_ResultEntryID
		}
	}

	for _, group := range GroupResultOrder {
		var urlMap map[string]string
		if err := json.Unmarshal([]byte(group.Group_ResultUrl), &urlMap); err != nil {
			return returnTab, fmt.Errorf("error parsing json: %v", err)
		}

		switch group.Group_ResultResumeMcu {
		case "FISIK":
			soResultID := strconv.Itoa(mapSoResultData["FISIK"])
			if soResultID != "0" {
				for key, value := range urlMap {
					returnTab = append(returnTab, models.TabViewReportMcu{
						Name: key,
						Link: value + "&username=" + staffname + "&PID=" + soResultID + "&tm=" + group.Datetime,
					})
				}
			}
		case "NONLAB":
			soResultID := strconv.Itoa(mapSoResultData[group.Group_ResultName])
			if soResultID != "0" {
				for _, value := range urlMap {
					returnTab = append(returnTab, models.TabViewReportMcu{
						Name: group.Group_ResultName,
						Link: value + "&username=" + staffname + "&PID=" + soResultID + "&tm=" + group.Datetime,
					})
				}
			}
		case "LAB":
			for _, value := range urlMap {
				returnTab = append(returnTab, models.TabViewReportMcu{
					Name: group.Group_ResultName,
					Link: value + "&username=" + staffname + "&PID=" + orderheaderID + "&tm=" + group.Datetime,
				})
			}
		default:
			for _, value := range urlMap {
				returnTab = append(returnTab, models.TabViewReportMcu{
					Name: group.Group_ResultName,
					Link: value + "&username=" + staffname + "&PID=" + orderheaderID + "&tm=" + group.Datetime,
				})
			}
		}
	}

	return returnTab, nil
}

func (tdps *TabDaftarPesertaServices) GetListReportPesertaV4(orderheaderID string, staffname string) ([]models.TabViewReportMcu, error) {
	var returnTab []models.TabViewReportMcu

	var profile_patient models.PatientMCU
	lab_rpt := ""

	sql := `
		SELECT
		toh.T_OrderHeaderID,
		toh.T_OrderHeaderLabNumber,
		mp.M_PatientID,
		mp.M_PatientName,
		DATE_FORMAT(NOW(), '%y%m%d%H%i%s') AS Datetime
		FROM t_orderheader toh
		JOIN m_patient mp ON toh.T_OrderHeaderM_PatientID = mp.M_PatientID 
		AND mp.M_PatientIsActive = 'Y'
		WHERE toh.T_OrderHeaderID = ? AND toh.T_OrderHeaderIsActive = 'Y'
	`
	if err := dbx.Handlex.Get(&profile_patient, sql, orderheaderID); err != nil {
		return returnTab, fmt.Errorf("error get group result order: %v", err)
	}
	if profile_patient.T_OrderHeaderLabNumber != "" {
		lab_rpt = "/birt/frameset?__report=report/one/lab/rpt_test_portal.rptdesign&__format=pdf&PID=" + profile_patient.T_OrderHeaderID + "&tm=" + profile_patient.Datetime + "&username=" + staffname
	}

	var so_data_rpt []models.SoRptData
	fisik := ""
	rontgen := ""
	ekg := ""
	audiometri := ""
	spirometri := ""
	treadmill := ""
	papsmear := ""

	sql_rpt := `
		SELECT DISTINCT group_resultName as rpt_name, 
		IFNULL(So_ResultEntryID,0) as so_id, 
		T_OrderDetailT_OrderHeaderID as order_id,
		DATE_FORMAT(NOW(), '%y%m%d%H%i%s') AS Datetime
		FROM t_orderdetail
		JOIN t_test ON T_OrderDetailT_TestID = T_TestID
		JOIN group_resultdetail ON Group_ResultDetailT_TestID = T_TestID 
		JOIN group_result ON Group_ResultID = Group_ResultDetailGroup_ResultID AND 
		Group_ResultFlagPerTest = 'N' AND Group_ResultName <> 'LAB' AND Group_ResultFlagNonLab = 'N'
		LEFT JOIN so_resultentry ON So_ResultEntryT_OrderDetailID = T_OrderDetailID AND So_ResultEntryIsActive = 'Y'
		WHERE
		T_OrderDetailT_OrderHeaderID = ? AND Group_ResultName <> 'Tanpa Hasil'
		UNION
		SELECT DISTINCT group_resultName as rpt_name, 
		IFNULL(So_ResultEntryID,0) as so_id, 
		T_OrderDetailT_OrderHeaderID as order_id,
		DATE_FORMAT(NOW(), '%y%m%d%H%i%s') AS Datetime
		FROM t_orderdetail
		JOIN t_test ON T_OrderDetailT_TestID = T_TestID
		JOIN t_samplingso ON T_SamplingSoT_OrderHeaderID = T_OrderDetailT_OrderHeaderID  AND
		T_SamplingSoT_TestID = T_TestID
		JOIN group_resultdetail ON Group_ResultDetailT_TestID = T_TestID 
		JOIN group_result ON Group_ResultID = Group_ResultDetailGroup_ResultID AND 
		Group_ResultFlagPerTest = 'N' AND Group_ResultName <> 'LAB' AND Group_ResultFlagNonLab = 'Y'
		LEFT JOIN so_resultentry ON So_ResultEntryT_OrderDetailID = T_OrderDetailID AND So_ResultEntryIsActive = 'Y'
		WHERE
		T_OrderDetailT_OrderHeaderID = ? AND Group_ResultName <> 'Tanpa Hasil'
		UNION
		SELECT DISTINCT T_TestName as rpt_name, 
		IFNULL(So_ResultEntryID,0) as so_id, 
		T_OrderDetailT_OrderHeaderID as order_id,
		DATE_FORMAT(NOW(), '%y%m%d%H%i%s') AS Datetime
		FROM t_orderdetail
		JOIN t_test ON T_OrderDetailT_TestID = T_TestID AND T_TestName NOT IN ('Visus Mata','Buta Warna','Pemeriksaan Lapang Pandang')
		JOIN t_samplingso ON T_SamplingSoT_OrderHeaderID = T_OrderDetailT_OrderHeaderID  AND
		T_SamplingSoT_TestID = T_TestID                
		JOIN group_resultdetail ON Group_ResultDetailT_TestID = T_TestID
		JOIN group_result ON Group_ResultID = Group_ResultDetailGroup_ResultID AND 
		Group_ResultFlagPerTest = 'Y'  AND Group_ResultFlagNonLab = 'Y'
		LEFT JOIN so_resultentry ON So_ResultEntryT_OrderDetailID = T_OrderDetailID AND So_ResultEntryIsActive = 'Y'
		WHERE
		T_OrderDetailT_OrderHeaderID = ? AND Group_ResultName <> 'Tanpa Hasil'
	`
	if err := dbx.Handlex.Select(&so_data_rpt, sql_rpt, orderheaderID, orderheaderID, orderheaderID); err != nil {
		return returnTab, fmt.Errorf("error get data so_id: %v", err)
	}

	for _, d := range so_data_rpt {
		switch d.Rpt_name {
		case "Pemeriksaan Fisik":
			fisik = "/birt/frameset?__report=report/one/mcu/mcu_fisik_history_v2_portal.rptdesign&__format=pdf&PType=fisik&PLang=1&username=" + staffname + "&PID=" + d.So_id + "&tm=" + d.Datetime
		case "Rontgen":
			rontgen = "/birt/frameset?__report=report/one/lab/rpt_hasil_so_xray_portal.rptdesign&__format=pdf&username=" + staffname + "&PID=" + d.So_id + "&tm=" + d.Datetime
		case "Audiometri":
			audiometri = "/birt/frameset?__report=report/one/lab/rpt_hasil_so_audiometri_portal.rptdesign&__format=pdf&username=" + staffname + "&PID=" + d.So_id + "&tm=" + d.Datetime
		case "Spirometri":
			spirometri = "/birt/frameset?__report=report/one/lab/rpt_hasil_so_spirometri_portal.rptdesign&__format=pdf&username=" + staffname + "&PID=" + d.So_id + "&tm=" + d.Datetime
		case "ECG":
			ekg = "/birt/frameset?__report=report/one/lab/rpt_hasil_so_ecg_portal.rptdesign&__format=pdf&username=" + staffname + "&PID=" + d.So_id + "&tm=" + d.Datetime
		case "Treadmill":
			treadmill = "/birt/frameset?__report=report/one/lab/rpt_hasil_so_treadmill_portal.rptdesign&__format=pdf&username=" + staffname + "&PID=" + d.So_id + "&tm=" + d.Datetime
		case "Papsmear":
			papsmear = "/birt/frameset?__report=report/one/lab/rpt_test_papsmear_portal.rptdesign&__format=pdf&username=" + staffname + "&PID=" + d.So_id + "&tm=" + d.Datetime
		}
	}

	var ResumeData models.ResumeMCU
	kesimpulan := ""
	// cover := ""

	sql_resume := `
		SELECT
		Mcu_ResumeID,
		DATE_FORMAT(NOW(), '%y%m%d%H%i%s') AS Datetime
		FROM mcu_resume
		WHERE Mcu_ResumeT_OrderHeaderID = ? 
		AND Mcu_ResumeIsActive = 'Y' AND Mcu_ResumeStatus = 'VAL'
	`
	if err := dbx.Handlex.Get(&ResumeData, sql_resume, orderheaderID); err != nil {
		return returnTab, fmt.Errorf("error get data resume id: %v", err)
	}

	if ResumeData.Mcu_ResumeID != "" {
		// cover = "/birt/frameset?__report=report/one/mcu/rpt_mcu_resume_cover_portal.rptdesign&__format=pdf&username=" + staffname + "&PID=" + ResumeData.Mcu_ResumeID + "&tm=" + ResumeData.Datetime
		kesimpulan = "/birt/frameset?__report=report/one/mcu/rpt_mcu_saran_kesimpulan_v2_portal.rptdesign&__format=pdf&username=" + staffname + "&PID=" + ResumeData.Mcu_ResumeID + "&tm=" + ResumeData.Datetime
	}

	if fisik != "" {
		returnTab = append(returnTab, models.TabViewReportMcu{Name: "Fisik", Link: fisik})
	}
	if rontgen != "" {
		returnTab = append(returnTab, models.TabViewReportMcu{Name: "Rontgen", Link: rontgen})
	}
	if ekg != "" {
		returnTab = append(returnTab, models.TabViewReportMcu{Name: "Elektromedik", Link: ekg})
	}
	if audiometri != "" {
		returnTab = append(returnTab, models.TabViewReportMcu{Name: "Audiometri", Link: audiometri})
	}
	if spirometri != "" {
		returnTab = append(returnTab, models.TabViewReportMcu{Name: "Spirometri", Link: spirometri})
	}
	if treadmill != "" {
		returnTab = append(returnTab, models.TabViewReportMcu{Name: "Treadmill", Link: treadmill})
	}
	if kesimpulan != "" {
		returnTab = append(returnTab, models.TabViewReportMcu{Name: "Kesimpulan", Link: kesimpulan})
	}
	if lab_rpt != "" {
		returnTab = append(returnTab, models.TabViewReportMcu{Name: "Lab", Link: lab_rpt})
	}
	if papsmear != "" {
		returnTab = append(returnTab, models.TabViewReportMcu{Name: "Papsmear", Link: papsmear})
	}

	return returnTab, nil
}
