package mcu_corporate_handlers

import (
	"cpone/component/pagination"
	customtoastv2 "cpone/component/toastbootstrap"
	"cpone/models"
	"cpone/utils"
	corporate_mcudetail "cpone/views/corporate/mcu/mcutab"
	"strconv"
	"strings"

	"github.com/a-h/templ"
	"github.com/golang-jwt/jwt/v5"
	"github.com/labstack/echo/v4"
	"go.uber.org/zap"
)

type TabDaftarPesertaServices interface {
	GetListDaftarPesertaMCU(id string, currentpage int, rowperpage int) ([]models.ModelMcuDaftarPeserta, int, error)
	GenerataPasswordMCU(mgmMCUID string, host string) (models.GeneratePasswordResponse, error)
	GetAkunPeserta(patientID string) (models.AuthPatient, error)
	GetListReportPesertaV4(orderheaderID string, staffname string) ([]models.TabViewReportMcu, error)
	ResetPasswordPeserta(token string, newPassword string, email string, authuserid string) (models.ResponseStatus, error)
	DetailTestdanPaket(orderheaderID string) ([]models.TableTestPaket, error)
}

type TabDaftarPesertaHandlers struct {
	TabDaftarPesertaServices TabDaftarPesertaServices
}

func NewTabDaftarPesertaHandlers(tdp TabDaftarPesertaServices) *TabDaftarPesertaHandlers {
	return &TabDaftarPesertaHandlers{
		TabDaftarPesertaServices: tdp,
	}
}

func (tdp *TabDaftarPesertaHandlers) HandleShowTabDaftarPeserta(c echo.Context) error {
	logger, _ := zap.NewProduction()
	title := "Daftar Report"
	tableID := utils.GenerateRandomID("tableid")
	paginationID := utils.GenerateRandomID("paginationID")

	dialogReportID := utils.GenerateRandomID("dialogreportid")
	dialogReportBodyID := utils.GenerateRandomID("dialogreportbodyid")

	dialogGeneratePassID := utils.GenerateRandomID("generatepasswordID")
	dialogGeneratePassBodyID := utils.GenerateRandomID("generatepasswordBodyID")

	dialogAkunID := utils.GenerateRandomID("dialogakunID")
	dialogAkunBodyID := utils.GenerateRandomID("dialogakunbodyID")

	dialogDetailID := utils.GenerateRandomID("dialogdetailID")
	dialogDetailBodyID := utils.GenerateRandomID("dialogdetailBodyID")

	id := c.Param("id")
	logger.Info("Params", zap.Any("id", id))

	dataTable, totalPage, err := tdp.TabDaftarPesertaServices.GetListDaftarPesertaMCU(id, 1, 10)
	if err != nil {
		return err
	}

	table := corporate_mcudetail.TableDaftarPeserta(
		dataTable,
		tableID,
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/openreport",
		"#"+dialogReportBodyID,
		"#tableID, #paginationID, #dialogReportID, #dialogReportBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogAkunID, #dialogAkunBodyID",
		"outerHTML",
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/openviewakun",
		"#"+dialogAkunBodyID,
		"#tableID, #paginationID, #dialogReportID, #dialogReportBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogAkunID, #dialogAkunBodyID",
		"outerHTML",
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/opendetail",
		"#"+dialogDetailBodyID,
		"#tableID, #paginationID, #dialogReportID, #dialogReportBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogAkunID, #dialogAkunBodyID, #dialogDetailID, #dialogDetailBodyID",
		"outerHTML",
	)

	pagination := pagination.PaginationV3(
		totalPage,
		1,
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/changepage",
		paginationID,
		"#tableID, #paginationID, #loading-parent, #loading-child, #loading-spinner, #loadingcontent, #dialogReportID, #dialogReportBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogAkunID, #dialogAkunBodyID",
		"#contentlayout",
		"outerHTML", "", "",
		corporate_mcudetail.BeforeRequestContent(),
		corporate_mcudetail.AfterRequestContent(),
	)

	clayout := corporate_mcudetail.ContentLayout(
		table,
		pagination,
	)

	// modal dialog tab rpt
	tabData := []models.TabViewReportMcu{}
	tabview := corporate_mcudetail.TabReportView(tabData)

	bodymodal := corporate_mcudetail.ModalRptBody(
		dialogReportBodyID,
		tabview,
		corporate_mcudetail.JsShowModal(""),
	)

	closemodal := corporate_mcudetail.ModalClose(
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/closereport",
		"#"+dialogReportBodyID,
		"outerHTML",
		"#"+dialogReportID,
	)

	modaldialog := corporate_mcudetail.DialogReport(
		"generatepasswordform",
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/dialogreport",
		"#"+dialogReportBodyID,
		"outerHTML",
		"#tableID, #paginationID, #dialogAkunID, #dialogAkunBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogReportID, #dialogReportBodyID"+paginationID,
		dialogReportID,
		title,
		bodymodal,
		closemodal,
	)

	// dialog generate password
	modalGenPassBody := corporate_mcudetail.ModalGenPassBody(
		dialogGeneratePassBodyID,
		"Apakah anda yakin untuk men-generate password untuk seluruh karyawan",
		corporate_mcudetail.JsShowModal(""),
	)

	closeGenPass := corporate_mcudetail.ModalClose(
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/closegenpass",
		"#"+dialogGeneratePassBodyID,
		"outerHTML",
		"#"+dialogGeneratePassID,
	)

	actionGenPass := corporate_mcudetail.ModalGenPassAction(
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/closegenpass",
		"#"+dialogGeneratePassBodyID,
		"outerHTML",
		"#"+dialogGeneratePassID,
	)

	modalGenPass := corporate_mcudetail.DialogGeneratePass(
		"",
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/dialoggenpass",
		"#"+dialogGeneratePassBodyID,
		"outerHTML",
		"#tableID, #paginationID, #contentlayout, #dialogAkunID, #dialogAkunBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogReportID, #dialogReportBodyID, #currpage"+paginationID,
		dialogGeneratePassID,
		"Generate Password MCU",
		modalGenPassBody,
		closeGenPass,
		actionGenPass,
	)

	// dialog view akun
	dialogAkunBody := corporate_mcudetail.ModalViewAkunBody(
		dialogAkunBodyID,
		models.CustomTextFieldv2Prm{
			Label:       "ID Akun Peserta",
			Name:        "akunpesertaid",
			Placeholder: "ID Akun Peserta",
			Type:        "hidden",
			ID:          "akunpesertaidid",
		},
		models.CustomTextFieldv2Prm{
			Label: "Email",
			Name:  "akunpesertaemail",
			Type:  "text",
			ID:    "akunpesertaemailid",
		},
		models.CustomTextFieldv2Prm{
			Label: "Password baru",
			Name:  "akunpesertapassnew",
			Type:  "password",
			ID:    "akunpesertapassnewid",
		},
		models.CustomTextFieldv2Prm{
			Label: "Ulangi password baru",
			Name:  "akunpesertapasscek",
			Type:  "password",
			ID:    "akunpesertapasscekid",
		},
		corporate_mcudetail.JsShowModal(""),
	)

	dialogAkunClose := corporate_mcudetail.ModalClose(
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/closeviewakun",
		"#"+dialogAkunBodyID,
		"outerHTML",
		"#"+dialogAkunID,
	)

	dialogAkunAction := corporate_mcudetail.ModalGenPassAction(
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/closeviewakun",
		"#"+dialogAkunBodyID,
		"outerHTML",
		"#"+dialogAkunID,
	)

	dialogAkunView := corporate_mcudetail.DialogGeneratePass(
		"viewakunform",
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/viewakun",
		"#"+dialogAkunBodyID,
		"outerHTML",
		"#tableID, #paginationID, #dialogAkunID, #contentlayout, #dialogAkunBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogReportID, #dialogReportBodyID, #currpage"+paginationID,
		dialogAkunID,
		"Reset Password Akun Peserta",
		dialogAkunBody,
		dialogAkunClose,
		dialogAkunAction,
	)

	// dialog detali pemeriksaan

	detailData, err := tdp.TabDaftarPesertaServices.DetailTestdanPaket("")
	if err != nil {
		return err
	}

	detailTable := corporate_mcudetail.TableDetailMCU(detailData)

	dialodDetailBody := corporate_mcudetail.ModalRptBody(
		dialogDetailBodyID,
		detailTable,
		corporate_mcudetail.JsShowModal(""),
	)

	closeDetail := corporate_mcudetail.ModalClose(
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/closedetail",
		"#"+dialogDetailBodyID,
		"outerHTML",
		"#"+dialogDetailID,
	)

	modalDetail := corporate_mcudetail.DialogReport(
		"detailmcu",
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/dialogdetail",
		"#"+dialogDetailBodyID,
		"outerHTML",
		"#tableID, #paginationID, #dialogAkunID, #dialogAkunBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogReportID, #dialogReportBodyID"+paginationID,
		dialogDetailID,
		"Detail Pemeriksaan",
		dialodDetailBody,
		closeDetail,
	)

	content := corporate_mcudetail.TabDaftarPesertaScreen(
		tableID,
		paginationID,
		dialogReportID,
		dialogReportBodyID,
		dialogGeneratePassID,
		dialogGeneratePassBodyID,
		dialogAkunID,
		dialogAkunBodyID,
		dialogDetailID,
		dialogDetailBodyID,
		clayout,
		modaldialog,
		modalGenPass,
		dialogAkunView,
		modalDetail,
	)

	return utils.View(c, content)
}

func (tdp *TabDaftarPesertaHandlers) HandlePagination(c echo.Context) error {
	pageparam := c.QueryParam("page")
	tableID := c.QueryParam("tableID")
	paginationID := c.QueryParam("paginationID")

	dialogReportBodyID := c.QueryParam("dialogReportBodyID")
	dialogAkunBodyID := c.QueryParam("dialogAkunBodyID")
	dialogDetailBodyID := c.QueryParam("dialogDetailBodyID")

	id := c.Param("id")

	// var retval []templ.Component
	page, err := strconv.Atoi(pageparam)
	if err != nil {
		return err
	}

	tableData, totalPage, err := tdp.TabDaftarPesertaServices.GetListDaftarPesertaMCU(id, page, 10)
	if err != nil {
		return err
	}
	table := corporate_mcudetail.TableDaftarPeserta(
		tableData,
		tableID,
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/openreport",
		"#"+dialogReportBodyID,
		"#tableID, #paginationID, #dialogReportID, #dialogReportBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogAkunID, #dialogAkunBodyID",
		"outerHTML",
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/openviewakun",
		"#"+dialogAkunBodyID,
		"#tableID, #paginationID, #dialogReportID, #dialogReportBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogAkunID, #dialogAkunBodyID",
		"outerHTML",
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/opendetail",
		"#"+dialogDetailBodyID,
		"#tableID, #paginationID, #dialogReportID, #dialogReportBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogAkunID, #dialogAkunBodyID, #dialogDetailID, #dialogDetailBodyID",
		"outerHTML",
	)

	pagination := pagination.PaginationV3(
		totalPage,
		page,
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/changepage",
		paginationID,
		"#tableID, #paginationID, #loading-parent, #loading-child, #loading-spinner, #loadingcontent, #dialogDetailID, #dialogDetailBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogAkunID, #dialogAkunBodyID, #dialogReportID, #dialogReportBodyID, #currPage"+paginationID,
		"#contentlayout",
		"outerHTML", "", "",
		corporate_mcudetail.BeforeRequestContent(),
		corporate_mcudetail.AfterRequestContent(),
	)

	clayout := corporate_mcudetail.ContentLayout(table, pagination)
	return utils.View(c, clayout)
}

func (tdp *TabDaftarPesertaHandlers) HandleOpenReportDialog(c echo.Context) error {
	logger, _ := zap.NewProduction()

	userCok := c.Get("user").(*jwt.Token)
	claims := userCok.Claims.(jwt.MapClaims)
	staffname := claims["M_StaffName"].(string)

	idx := c.QueryParam("idx")
	dialogReportID := c.QueryParam("dialogReportID")
	dialogReportBodyID := c.QueryParam("dialogReportBodyID")
	logger.Info("params", zap.Any("idx", idx))

	tabdatav3, err := tdp.TabDaftarPesertaServices.GetListReportPesertaV4(idx, staffname)
	if err != nil {
		logger.Info("error", zap.Any("err", err))
		tab := corporate_mcudetail.TabReportView([]models.TabViewReportMcu{})
		mod := corporate_mcudetail.ModalRptBody(
			dialogReportBodyID,
			tab,
			corporate_mcudetail.JsShowModal(""),
		)
		return utils.View(c, mod)
	}

	logger.Info("resp new data", zap.Any("data", tabdatav3))

	tabcomponent := corporate_mcudetail.TabReportView(tabdatav3)

	modalbody := corporate_mcudetail.ModalRptBody(
		dialogReportBodyID,
		tabcomponent,
		corporate_mcudetail.JsShowModal("#"+dialogReportID),
	)

	return utils.View(c, modalbody)
}

func (tdp *TabDaftarPesertaHandlers) HandleCloseDialogGenPass(c echo.Context) error {
	logger, _ := zap.NewProduction()

	id := c.Param("id")
	dialogGenPassID := c.FormValue("dialogGenPassID")
	dialogGenPassBodyID := c.FormValue("dialogGenPassBodyID")
	logger.Info("param", zap.Any("id", id))

	modalDialogGenPass := corporate_mcudetail.ModalGenPassBody(
		dialogGenPassBodyID,
		"Apakah anda yakin untuk men-generate password untuk seluruh karyawan",
		corporate_mcudetail.JsHideModal("#"+dialogGenPassID),
	)

	return utils.View(c, modalDialogGenPass)
}

func (tdp *TabDaftarPesertaHandlers) HandleDialogGenPass(c echo.Context) error {
	// logger, _ := zap.NewProduction()
	host := c.Request().Host
	id := c.Param("id")

	dialogGenPassID := c.FormValue("dialogGenPassID")
	dialogGenPassBodyID := c.FormValue("dialogGenPassBodyID")
	dialogReportID := c.FormValue("dialogReportID")
	dialogReportBodyID := c.FormValue("dialogReportBodyID")
	dialogAkunID := c.FormValue("dialogAkunID")
	dialogAkunBodyID := c.FormValue("dialogAkunBodyID")

	tableID := c.FormValue("tableID")
	paginationID := c.FormValue("paginationID")
	pageparam := c.FormValue("currpage" + paginationID)

	// generate pasword
	resp, err := tdp.TabDaftarPesertaServices.GenerataPasswordMCU(id, host)
	if err != nil {
		modalDialogGenPass := corporate_mcudetail.ModalGenPassBody(
			dialogGenPassBodyID,
			"Apakah anda yakin untuk men-generate password untuk seluruh karyawan",
			corporate_mcudetail.JsShowModal(""),
		)
		toastwarning := customtoastv2.CustomToastV2Show("Warning", "Gagal generate password", "warning")
		retVal := []templ.Component{toastwarning, modalDialogGenPass}
		return utils.ViewMulti(c, retVal)
	}

	if resp.Status != "OK" {
		modalDialogGenPass := corporate_mcudetail.ModalGenPassBody(
			dialogGenPassBodyID,
			"Apakah anda yakin untuk men-generate password untuk seluruh karyawan",
			corporate_mcudetail.JsShowModal(""),
		)
		toastwarning := customtoastv2.CustomToastV2Show("Warning", "Gagal generate password MCU", "warning")
		retVal := []templ.Component{toastwarning, modalDialogGenPass}
		return utils.ViewMulti(c, retVal)
	}

	aa := "/corp/dashboard_pic/detail/" + id + "/tabdaftarpeserta/reload?page=" + pageparam
	bb := "&tableID=" + tableID
	cc := "&paginationID=" + paginationID
	dd := "&dialogReportID=" + dialogReportID
	ee := "&dialogReportBodyID=" + dialogReportBodyID
	ff := "&dialogGenPassID=" + dialogGenPassID
	gg := "&dialogGenPassBodyID=" + dialogGenPassBodyID
	hh := "&dialogAkunID=" + dialogAkunID
	ii := "&dialogAkunBodyID=" + dialogAkunBodyID
	jj := "&currpage" + paginationID + "=" + pageparam
	link := aa + bb + cc + dd + ee + ff + gg + hh + ii + jj

	modalDialogGenPass := corporate_mcudetail.ModalGenPassBody(
		dialogGenPassBodyID,
		"Apakah anda yakin untuk men-generate password untuk seluruh karyawan",
		// corporate_mcudetail.JsHideModal("#"+dialogGenPassID),
		corporate_mcudetail.JsGenPassReload("#"+dialogGenPassID, link),
	)

	message := "Success generate password.\n" + "resp.Message" + " password, telah dikirim melalui email peserta"
	toastSuccess := customtoastv2.CustomToastV2Show("Success", message, "success")
	retVal := []templ.Component{toastSuccess, modalDialogGenPass}
	return utils.ViewMulti(c, retVal)
}

func (tdp *TabDaftarPesertaHandlers) HandleOpenViewAkun(c echo.Context) error {
	logger, _ := zap.NewProduction()

	patientID := c.QueryParam("patid")
	dialogAkunID := c.QueryParam("dialogAkunID")
	dialogAkunBodyID := c.QueryParam("dialogAkunBodyID")
	logger.Info("params", zap.Any("patid", patientID))

	akun, err := tdp.TabDaftarPesertaServices.GetAkunPeserta(patientID)
	if err != nil {
		dialogAkunBody := corporate_mcudetail.ModalViewAkunBody(
			dialogAkunBodyID,
			models.CustomTextFieldv2Prm{
				Label:       "ID Akun Peserta",
				Name:        "akunpesertaid",
				Placeholder: "ID Akun Peserta",
				Type:        "hidden",
				ID:          "akunpesertaidid",
				Value:       patientID,
			},
			models.CustomTextFieldv2Prm{
				Label: "Email Akun",
				Name:  "akunpesertaemail",
				Type:  "text",
				ID:    "akunpesertaemailid",
			},
			models.CustomTextFieldv2Prm{
				Label: "Password baru",
				Name:  "akunpesertapassnew",
				Type:  "password",
				ID:    "akunpesertapassnewid",
			},
			models.CustomTextFieldv2Prm{
				Label: "Ulangi password baru",
				Name:  "akunpesertapasscek",
				Type:  "password",
				ID:    "akunpesertapasscekid",
			},
			corporate_mcudetail.JsShowModal("#"+dialogAkunID),
		)
		toastwarning := customtoastv2.CustomToastV2Show("Warning", "Gagal generate password MCU", "warning")
		retval := []templ.Component{toastwarning, dialogAkunBody}
		return utils.ViewMulti(c, retval)
	}

	dialogAkunBody := corporate_mcudetail.ModalViewAkunBody(
		dialogAkunBodyID,
		models.CustomTextFieldv2Prm{
			Label:       "ID Akun Peserta",
			Name:        "akunpesertaid",
			Placeholder: "ID Akun Peserta",
			Type:        "hidden",
			ID:          "akunpesertaidid",
			Value:       strconv.Itoa(akun.AuthPatientID),
		},
		models.CustomTextFieldv2Prm{
			Label: "Email Akun",
			Name:  "akunpesertaemail",
			Type:  "text",
			ID:    "akunpesertaemailid",
			Value: akun.AuthPatientEmail,
		},
		models.CustomTextFieldv2Prm{
			Label:       "Password baru",
			Name:        "akunpesertapassnew",
			Type:        "password",
			ID:          "akunpesertapassnewid",
			Placeholder: "Masukan password baru",
		},
		models.CustomTextFieldv2Prm{
			Label:       "Ulangi password baru",
			Name:        "akunpesertapasscek",
			Type:        "password",
			ID:          "akunpesertapasscekid",
			Placeholder: "Masukan ulang password baru",
		},
		corporate_mcudetail.JsShowModal("#"+dialogAkunID),
	)
	return utils.View(c, dialogAkunBody)
}

func (tdp *TabDaftarPesertaHandlers) HandleCloseViewAkun(c echo.Context) error {
	logger, _ := zap.NewProduction()

	patientID := c.FormValue("akunpesertaid")
	dialogAkunID := c.FormValue("dialogAkunID")
	dialogAkunBodyID := c.FormValue("dialogAkunBodyID")
	logger.Info("params", zap.Any("patid", patientID))

	dialogAkunBody := corporate_mcudetail.ModalViewAkunBody(
		dialogAkunBodyID,
		models.CustomTextFieldv2Prm{
			Label:       "ID Akun Peserta",
			Name:        "akunpesertaid",
			Placeholder: "ID Akun Peserta",
			Type:        "hidden",
			ID:          "akunpesertaidid",
		},
		models.CustomTextFieldv2Prm{
			Label: "Email Akun",
			Name:  "akunpesertaemail",
			Type:  "text",
			ID:    "akunpesertaemailid",
		},
		models.CustomTextFieldv2Prm{
			Label: "Password baru",
			Name:  "akunpesertapassnew",
			Type:  "password",
			ID:    "akunpesertapassnewid",
		},
		models.CustomTextFieldv2Prm{
			Label: "Ulangi password baru",
			Name:  "akunpesertapasscek",
			Type:  "password",
			ID:    "akunpesertapasscekid",
		},
		corporate_mcudetail.JsHideModal("#"+dialogAkunID),
	)
	return utils.View(c, dialogAkunBody)
}

func (tdp *TabDaftarPesertaHandlers) HandleAkun(c echo.Context) error {
	logger, _ := zap.NewProduction()

	userToken := c.Get("user").(*jwt.Token)
	token := userToken.Raw

	patientID := c.FormValue("akunpesertaid")
	dialogAkunID := c.FormValue("dialogAkunID")
	dialogAkunBodyID := c.FormValue("dialogAkunBodyID")
	logger.Info("params", zap.Any("patid", patientID))

	akunID := c.FormValue("akunpesertaid")
	akunEmail := c.FormValue("akunpesertaemail")
	oldPass := c.FormValue("akunpesertapassold")
	newPass := c.FormValue("akunpesertapassnew")
	newPasscek := c.FormValue("akunpesertapasscek")

	formValidation := ""
	newValidation := ""
	cekValidation := ""

	if strings.TrimSpace(newPass) == "" {
		formValidation = "Password tidak boleh kosong"
		newValidation = "Password tidak boleh kosong"
	}
	if strings.TrimSpace(newPasscek) == "" {
		formValidation = "Password tidak boleh kosong"
		cekValidation = "Password tidak boleh kosong"
	}
	if strings.TrimSpace(oldPass) == "" && strings.TrimSpace(newPass) == "" && strings.TrimSpace(newPasscek) == "" {
		formValidation = "Password lama dan baru tidak boleh kosong"
		newValidation = "Password baru tidak boleh kosong"
		cekValidation = "Password baru tidak boleh kosong"
	}
	if strings.TrimSpace(newPass) != strings.TrimSpace(newPasscek) {
		formValidation = "Password tidak sama"
		newValidation = "Password tidak sama"
		cekValidation = "Password tidak sama"
	}

	if formValidation != "" || newValidation != "" || cekValidation != "" {
		dialogAkunBody := corporate_mcudetail.ModalViewAkunBody(
			dialogAkunBodyID,
			models.CustomTextFieldv2Prm{
				Label:       "ID Akun Peserta",
				Name:        "akunpesertaid",
				Placeholder: "ID Akun Peserta",
				Type:        "hidden",
				ID:          "akunpesertaidid",
				Value:       akunID,
			},
			models.CustomTextFieldv2Prm{
				Label: "Email Akun",
				Name:  "akunpesertaemail",
				Type:  "text",
				ID:    "akunpesertaemailid",
				Value: akunEmail,
			},
			models.CustomTextFieldv2Prm{
				Label:       "Password baru",
				Name:        "akunpesertapassnew",
				Type:        "password",
				ID:          "akunpesertapassnewid",
				Placeholder: "Masukan password baru",
				ErrorMsg:    newValidation,
			},
			models.CustomTextFieldv2Prm{
				Label:       "Ulangi password baru",
				Name:        "akunpesertapasscek",
				Type:        "password",
				ID:          "akunpesertapasscekid",
				Placeholder: "Masukan ulang password baru",
				ErrorMsg:    cekValidation,
			},
			corporate_mcudetail.JsShowModal(""),
		)
		toastwarning := customtoastv2.CustomToastV2Show("Warning", formValidation, "warning")
		retVal := []templ.Component{toastwarning, dialogAkunBody}
		return utils.ViewMulti(c, retVal)
	}

	resp, err := tdp.TabDaftarPesertaServices.ResetPasswordPeserta(token, newPass, akunEmail, akunID)
	dialogAkunErr := corporate_mcudetail.ModalViewAkunBody(
		dialogAkunBodyID,
		models.CustomTextFieldv2Prm{
			Label:       "ID Akun Peserta",
			Name:        "akunpesertaid",
			Placeholder: "ID Akun Peserta",
			Type:        "hidden",
			ID:          "akunpesertaidid",
			Value:       akunID,
		},
		models.CustomTextFieldv2Prm{
			Label: "Email Akun",
			Name:  "akunpesertaemail",
			Type:  "text",
			ID:    "akunpesertaemailid",
			Value: akunEmail,
		},
		models.CustomTextFieldv2Prm{
			Label:       "Password baru",
			Name:        "akunpesertapassnew",
			Type:        "password",
			ID:          "akunpesertapassnewid",
			Placeholder: "Masukan password baru",
		},
		models.CustomTextFieldv2Prm{
			Label:       "Ulangi password baru",
			Name:        "akunpesertapasscek",
			Type:        "password",
			ID:          "akunpesertapasscekid",
			Placeholder: "Masukan ulang password baru",
		},
		corporate_mcudetail.JsHideModal(""),
	)

	if err != nil {
		toastwarning := customtoastv2.CustomToastV2Show("Warning", err.Error(), "warning")
		return utils.ViewMulti(c, []templ.Component{toastwarning, dialogAkunErr})
	}

	if resp.Status == "ERR" {
		toastwarning := customtoastv2.CustomToastV2Show("Warning", resp.Message, "warning")
		return utils.ViewMulti(c, []templ.Component{toastwarning, dialogAkunErr})
	}

	dialogAkunBody := corporate_mcudetail.ModalViewAkunBody(
		dialogAkunBodyID,
		models.CustomTextFieldv2Prm{
			Label:       "ID Akun Peserta",
			Name:        "akunpesertaid",
			Placeholder: "ID Akun Peserta",
			Type:        "hidden",
			ID:          "akunpesertaidid",
		},
		models.CustomTextFieldv2Prm{
			Label: "Email Akun",
			Name:  "akunpesertaemail",
			Type:  "text",
			ID:    "akunpesertaemailid",
		},
		models.CustomTextFieldv2Prm{
			Label:       "Password baru",
			Name:        "akunpesertapassnew",
			Type:        "password",
			ID:          "akunpesertapassnewid",
			Placeholder: "Masukan password baru",
		},
		models.CustomTextFieldv2Prm{
			Label:       "Ulangi password baru",
			Name:        "akunpesertapasscek",
			Type:        "password",
			ID:          "akunpesertapasscekid",
			Placeholder: "Masukan ulang password baru",
		},
		corporate_mcudetail.JsHideModal("#"+dialogAkunID),
	)
	toastwarning := customtoastv2.CustomToastV2Show("Success", "Berhasil reset password peserta: "+akunEmail, "success")
	retVal := []templ.Component{toastwarning, dialogAkunBody}
	return utils.ViewMulti(c, retVal)
}

func (tdp *TabDaftarPesertaHandlers) HandleContentReload(c echo.Context) error {
	id := c.Param("id")
	dialogReportBodyID := c.FormValue("dialogReportBodyID")
	dialogAkunBodyID := c.FormValue("dialogAkunBodyID")
	dialogDetailBodyID := c.FormValue("dialogDetailBodyID")

	tableID := c.FormValue("tableID")
	paginationID := c.FormValue("paginationID")
	pageparam := c.FormValue("currpage" + paginationID)
	currpage, err := strconv.Atoi(pageparam)
	if err != nil {
		return err
	}

	tableData, totalPage, err := tdp.TabDaftarPesertaServices.GetListDaftarPesertaMCU(id, currpage, 10)
	if err != nil {
		return err
	}

	table := corporate_mcudetail.TableDaftarPeserta(
		tableData,
		tableID,
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/openreport",
		"#"+dialogReportBodyID,
		"#tableID, #paginationID, #dialogReportID, #dialogReportBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogAkunID, #dialogAkunBodyID",
		"outerHTML",
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/openviewakun",
		"#"+dialogAkunBodyID,
		"#tableID, #paginationID, #dialogReportID, #dialogReportBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogAkunID, #dialogAkunBodyID",
		"outerHTML",
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/opendetail",
		"#"+dialogDetailBodyID,
		"#tableID, #paginationID, #dialogReportID, #dialogReportBodyID, #dialogGenPassID, #dialogGenPassBodyID, #dialogAkunID, #dialogAkunBodyID, #dialogDetailID, #dialogDetailBodyID",
		"outerHTML",
	)

	pagination := pagination.PaginationV3(
		totalPage,
		currpage,
		"/corp/dashboard_pic/detail/"+id+"/tabdaftarpeserta/changepage",
		paginationID,
		"#tableID, #paginationID, #loading-parent, #loading-child, #loading-spinner, #loadingcontent, #dialogGenPassID, #dialogGenPassBodyID, #dialogAkunID, #dialogAkunBodyID, #dialogReportID, #dialogReportBodyID, #dialogDetailID, #dialogDetailBodyID, #currPage"+paginationID,
		"#contentlayout",
		"outerHTML", "", "",
		corporate_mcudetail.BeforeRequestContent(),
		corporate_mcudetail.AfterRequestContent(),
	)

	clayout := corporate_mcudetail.ContentLayout(table, pagination)
	return utils.View(c, clayout)
}

func (tdp *TabDaftarPesertaHandlers) HandleOpenDetail(c echo.Context) error {
	idx := c.QueryParam("idx")
	dialogDetailID := c.QueryParam("dialogDetailID")
	dialogDetailBodyID := c.QueryParam("dialogDetailBodyID")

	detailData, err := tdp.TabDaftarPesertaServices.DetailTestdanPaket(idx)
	if err != nil {
		tab := corporate_mcudetail.TableDetailMCU([]models.TableTestPaket{})
		mod := corporate_mcudetail.ModalRptBody(
			dialogDetailBodyID,
			tab,
			corporate_mcudetail.JsShowModal("#"+dialogDetailID),
		)
		return utils.View(c, mod)
	}

	tablecomponent := corporate_mcudetail.TableDetailMCU(detailData)
	detailBody := corporate_mcudetail.ModalRptBody(
		dialogDetailBodyID,
		tablecomponent,
		corporate_mcudetail.JsShowModal("#"+dialogDetailID),
	)

	return utils.View(c, detailBody)
}
