package dev_handlers

import (
	breadcrumadmin "cpone/component/breadcrumbadmin"
	"cpone/component/customtextfieldsearch"
	navbarmenu "cpone/component/navbar"
	"cpone/component/pagination"
	sidebaruserprofile "cpone/component/sidebar_user_profile"
	customtoastv2 "cpone/component/toastbootstrap"
	"cpone/models"
	globalservices "cpone/services"
	"cpone/utils"
	masterkelainan "cpone/views/dev/mdpolakelainan"
	"fmt"
	"strconv"
	"strings"

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

type MasterKelainanService interface {
	GetListBreadCrumb(title string) (models.BreadCrumbV1, error)
	GetListMasterKelainan(search string, currentPage int, rowPerPage int) ([]models.Kelainan, int, error)
	GetKelainanByID(id string) (models.Kelainan, error)
	AddKelainan(name string) (models.Kelainan, error)
	DeleteKelainan(id string) (models.Kelainan, error)
	UpdateFlagStatusKelainan(id string, status string) (models.Kelainan, error)
	GetListMasterKelainanDetail(id string, searchdetail string, currentPage int, rowPerPage int) ([]models.KelainanDetail, int, error)
	AddKelainanDetail(name string, nameclassification string, selectedid string) (models.KelainanDetail, error)
	GetKelainanDetailByID(id string) (models.KelainanDetail, error)
	DeleteKelainanDetail(id string) (models.KelainanDetail, error)
}

func NewMasterKelainanHandler(us MasterKelainanService) *MasterKelainanHandler {
	return &MasterKelainanHandler{
		MasterKelainanService: us,
	}
}

type MasterKelainanHandler struct {
	MasterKelainanService MasterKelainanService
}

func (uh *MasterKelainanHandler) View(c echo.Context, cmp templ.Component) error {
	c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)

	return cmp.Render(c.Request().Context(), c.Response().Writer)
}

func (lh *MasterKelainanHandler) HandlerShowMasterKelainan(c echo.Context) error {

	logger, _ := zap.NewProduction()
	//get sidebarmenu
	id := c.QueryParam("id")
	tableDetailID := utils.GenerateRandomID("tablebodydetail")
	paginationDetailID := utils.GenerateRandomID("paginationdetailid")
	searchDetailID := utils.GenerateRandomID("searchdetailid")
	dialogDetailAddID := utils.GenerateRandomID("dialogdetailaddid")
	dialogDetailAddBodyID := utils.GenerateRandomID("dialogdetailaddbodyid")
	dialogDetailDeleteID := utils.GenerateRandomID("dialogDetailDeleteID")
	dialogDetailDeleteBodyID := utils.GenerateRandomID("dialogDetailDeletebodyID")

	// println("cek id param", id)

	dataMenu, err := globalservices.GetMenu()
	fmt.Println(dataMenu)
	fmt.Println(err)
	if err != nil {
		fmt.Println(dataMenu)
		return err
	}

	//get user login
	dataUser, err := globalservices.GetUserLogin()
	fmt.Println(dataUser)
	fmt.Println(err)
	if err != nil {
		fmt.Println(dataUser)
		return err
	}

	dataKelainan, totalpage, err := lh.MasterKelainanService.GetListMasterKelainan("", 1, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET KELAINAN",
			zap.Any("error", err),
		)
		fmt.Println(dataKelainan)
		return err
	}

	defer logger.Sync()
	logger.Info("LOAD KELAINAN",
		zap.Any("data", dataKelainan),
	)

	dataKelainanDetail, totalPageDetail, err := lh.MasterKelainanService.GetListMasterKelainanDetail(id, "", 1, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET KELAINAN DETAIL",
			zap.Any("error", err),
		)
		return err
	}

	defer logger.Sync()
	logger.Info("LOAD KELAINAN DETAIL",
		zap.Any("data detail", dataKelainanDetail),
	)

	tableID := utils.GenerateRandomID("tablebody")
	paginationID := utils.GenerateRandomID("paginationid")
	searchID := utils.GenerateRandomID("searchid")
	dialogAddID := utils.GenerateRandomID("dialogaddID")
	dialogAddBodyID := utils.GenerateRandomID("dialogaddbodyID")
	dialogDeleteID := utils.GenerateRandomID("dialogDeleteID")
	dialogDeleteBodyID := utils.GenerateRandomID("dialogDeletebodyID")

	title := "Master Pola Kelainan"

	dataBreadCrumb, err := lh.MasterKelainanService.GetListBreadCrumb(title)
	fmt.Println(dataBreadCrumb)
	fmt.Println(err)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR BREADCRUMB DEV",
			zap.Any("error", err),
		)
		fmt.Println(dataBreadCrumb)
		return err
	}

	defer logger.Sync()
	logger.Info("LOAD BREADCRUMB DEV",
		zap.Any("data", dataBreadCrumb),
	)

	// breadcrumadmin
	breadcrumbComponent := breadcrumadmin.MainBreadcrumbAdminV1(dataBreadCrumb)

	// navbar menu
	navbarmenuComponent := navbarmenu.NavbarMenu(dataMenu)

	// navbar user
	navbaruserComponent := navbarmenu.Navbar(dataUser)

	// sidebar
	sidebaruserprofileComponent := sidebaruserprofile.Navbaruserprofile(dataUser)

	// component modal add kelainan
	dialogAddBodyCmp := masterkelainan.BodyFormKelainan(
		models.CustomTextFieldv2Prm{
			Label:       "Nama Grup Kelainan",
			Name:        "kelainanname",
			Placeholder: "Nama Grup Kelainan",
			Type:        "text", ID: "kelainaninputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Grup Kelainan",
			Name:        "kelainanid",
			Placeholder: "Nama Grup Kelainan",
			Type:        "hidden", ID: "kelainaninputid"},
		dialogAddBodyID, masterkelainan.JsHideModal(""))
	dialogAddActionCmp := masterkelainan.ActionFormKelainan(
		"/dev/md/kelainan/closeaddform",
		"#"+dialogAddBodyID,
		"outerHTML",
		"#"+dialogAddID)
	dialogAddCloseCmp := masterkelainan.BtnCloseFormKelainan(
		"/dev/md/kelainan/closeaddform",
		"#"+dialogAddBodyID,
		"outerHTML",
		"#"+dialogAddID)
	modaladdcomponent := masterkelainan.KelainanForm(models.KelainanFormComponent{
		IDComponent:  "formkelainan",
		Link:         "/dev/md/kelainan/add",
		HxTarget:     "#" + dialogAddBodyID,
		HxSwap:       "outerHTML",
		HxInclude:    "#tableID, #paginationID, #searchID, #dialogAddBodyID, #dialogAddID, #" + searchID + ", #currpage" + paginationID + ",#dialogDeleteBodyID, #dialogDeleteID, #dialogEditID",
		ModalID:      dialogAddID,
		ModalTitle:   "New - Grup Kelainan",
		DialogBody:   dialogAddBodyCmp,
		DialogAction: dialogAddActionCmp,
		ButtonCLose:  dialogAddCloseCmp,
	})

	// component table listing kelainan
	contentKelainanComponent := masterkelainan.TableKelainan(
		dataKelainan, tableID,
		"/dev/md/kelainan/flagaktif",
		"outerHTML",
		"/dev/md/kelainan/flagnonaktif",
		"outerHTML",
		"/dev/md/kelainan/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogDeleteBodyID, #dialogDeleteID, #tableDetailID",
		"/dev/md/kelainan/selected",
		"#"+tableDetailID,
	)

	// filter kelainan component
	kelainanFilterComponent := customtextfieldsearch.MainCustomTextFieldSearchV3(searchID,
		"search",
		"Cari Grup Kelainan",
		"text",
		"/dev/md/searchkelainan",
		"input changed delay:500ms, search",
		"#"+paginationID, "#loadingcontent", "", "outerHTML", "#tableDetailID, #tableID, #paginationID, #searchID, #dialogDeleteBodyID, #dialogDeleteID",
		masterkelainan.BeforeRequestContent(),
		masterkelainan.AfterRequestContent())

	// pagination kelainan component
	kelainanPaginationComponent := pagination.PaginationV3(
		totalpage,
		1,
		"/dev/md/kelainanpagination",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogDeleteBodyID, #dialogDeleteID, #tableDetailID",
		"#"+paginationID,
		"outerHTML", "", "",
		masterkelainan.BeforeRequestContent(),
		masterkelainan.AfterRequestContent(),
	)

	// modal delete form
	dialogDeleteBodyComponent := masterkelainan.BodyFormKelainan(
		models.CustomTextFieldv2Prm{
			Label:       "Nama Grup Kelainan",
			Name:        "kelainanname",
			Placeholder: "Nama Grup Kelainan",
			Type:        "text", ID: "kelainaninputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Grup Kelainan",
			Name:        "kelainanid",
			Placeholder: "Nama Grup Kelainan",
			Type:        "hidden", ID: "kelainaninputid"},
		dialogDeleteBodyID, masterkelainan.JsHideModal(""))
	modalActioDeleteComponent := masterkelainan.ActionFormKelainanDelete(
		"/dev/md/kelainan/closedeleteform",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#"+dialogDeleteID)
	btnCloaseModalDelete := masterkelainan.BtnCloseFormKelainan(
		"/dev/md/kelainan/closedeleteform",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#"+dialogDeleteID)
	modalDeleteFormComponent := masterkelainan.KelainanForm(
		models.KelainanFormComponent{
			IDComponent:  "formkelainan",
			Link:         "/dev/md/kelainan/delete",
			HxTarget:     "#" + dialogDeleteBodyID,
			HxSwap:       "outerHTML",
			HxInclude:    "#tableID, #paginationID, #searchID, #dialogDeleteBodyID, #dialogDeleteID, #" + searchID + ", #currpage" + paginationID,
			ModalID:      dialogDeleteID,
			ModalTitle:   "Konfirmasi",
			DialogBody:   dialogDeleteBodyComponent,
			DialogAction: modalActioDeleteComponent,
			ButtonCLose:  btnCloaseModalDelete,
		},
	)

	defer logger.Sync()
	logger.Info("CEK HandlerShowMdKelainanDetailPagination",
		zap.Any("dialogDetailDeleteBodyID", dialogDetailDeleteBodyID),
	)

	// component listing table listing kelainan detail
	contentKelainanDetailCmp := masterkelainan.TableKelainanDetail(
		dataKelainanDetail, tableDetailID,
		"/dev/md/kelainandetail/opendelete",
		"#"+dialogDetailDeleteBodyID,
		"outerHTML",
		"#dialogDetailDeleteBodyID, #dialogDetailDeleteID, #tableDetailID",
	)

	// filter kelainan detail component
	kelainanDetailFilterCmp := customtextfieldsearch.MainCustomTextFieldSearchV3(searchDetailID,
		"searchdetail",
		"Cari Kelainan/Klasifikasi",
		"text",
		"/dev/md/searchkelainandetail",
		"input changed delay:500ms, searchdetail",
		"#"+paginationDetailID, "#loadingcontentdetail", "", "none", "#tableDetailID, #paginationDetailID, #searchDetailID, #selectedID, #dialogDetailDeleteBodyID, #dialogDetailDeleteID",
		masterkelainan.BeforeRequestContentDetail(),
		masterkelainan.AfterRequestContentDetail())

	// pagination kelainan detail component
	kelainanDetailPaginationCmp := pagination.PaginationV3(
		totalPageDetail,
		1,
		"/dev/md/kelainandetailpagination",
		paginationDetailID,
		"#tableDetailID, #searchDetailID, #dialogDetailDeleteBodyID, #dialogDetailDeleteID, #"+searchDetailID+", #paginationDetailID",
		"#"+paginationDetailID,
		"outerHTML", "", "",
		masterkelainan.BeforeRequestContentDetail(),
		masterkelainan.AfterRequestContentDetail(),
	)

	// component modal add kelainan detail
	dialogDetailAddBodyCmp := masterkelainan.BodyFormKelainanDetail(
		models.CustomTextFieldv2Prm{
			Label:       "Nama Kelainan",
			Name:        "kelainandetailid",
			Placeholder: "Nama Kelainan",
			Type:        "hidden", ID: "kelainandetailid"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Kelainan",
			Name:        "kelainandetailname",
			Placeholder: "Nama Kelainan",
			Type:        "text", ID: "kelainandetailinputname"},
		models.SwitchclassPrm{
			Label:   "Dengan Klasifikasi",
			Type:    "checkbox",
			Checked: "true ",
			Name:    "select",
			ID:      "switchname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Klasifikasi",
			Name:        "classificationname",
			Placeholder: "Nama Klasifikasi",
			Type:        "text", ID: "classificationinputname"},

		dialogDetailAddBodyID, masterkelainan.JsHideModalDetail(""))
	dialogDetailAddActionCmp := masterkelainan.ActionFormKelainanDetail(
		"/dev/md/kelainandetail/closeaddform",
		"#"+dialogDetailAddBodyID,
		"outerHTML",
		"#"+dialogDetailAddID)
	dialogDetailAddCloseCmp := masterkelainan.BtnCloseFormKelainanDetail(
		"/dev/md/kelainandetail/closeaddform",
		"#"+dialogDetailAddBodyID,
		"outerHTML",
		"#"+dialogDetailAddID)
	modaldetailaddcomponent := masterkelainan.KelainanDetailForm(models.KelainanDetailFormComponent{
		IDComponent:  "formkelainandetail",
		Link:         "/dev/md/kelainandetail/add",
		HxTarget:     "#" + dialogDetailAddBodyID,
		HxSwap:       "outerHTML",
		HxInclude:    "#selectedID, #tableDetailID, #paginationDetailID, #searchDetailID, #dialogDetailAddBodyID, #dialogDetailAddID, #selectedID, #dialogDetailDeleteBodyID, #dialogDetailDeleteID, #" + searchDetailID + ", #currpage" + paginationDetailID,
		ModalID:      dialogDetailAddID,
		ModalTitle:   "New - Kelainan",
		DialogBody:   dialogDetailAddBodyCmp,
		DialogAction: dialogDetailAddActionCmp,
		ButtonCLose:  dialogDetailAddCloseCmp,
	})

	// modal delete kelainan detail form
	dialogDetailDeleteCmp := masterkelainan.BodyFormKelainanDetail(
		models.CustomTextFieldv2Prm{
			Label:       "Nama Kelainan",
			Name:        "kelainandetailid",
			Placeholder: "Nama Kelainan",
			Type:        "hidden", ID: "kelainandetailid"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Kelainan",
			Name:        "kelainandetailname",
			Placeholder: "Nama Kelainan",
			Type:        "text", ID: "kelainandetailinputname"},
		models.SwitchclassPrm{
			Label:   "Dengan Klasifikasi",
			Type:    "checkbox",
			Checked: "true ",
			Name:    "select",
			ID:      "switchname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Klasifikasi",
			Name:        "classificationname",
			Placeholder: "Nama Klasifikasi",
			Type:        "text", ID: "classificationinputname"},
		dialogDetailDeleteBodyID, masterkelainan.JsHideModalDetail(""))
	modalActionDetailDeleteCmp := masterkelainan.ActionFormKelainanDelete(
		"/dev/md/kelainandetail/closedeleteform",
		"#"+dialogDetailDeleteBodyID,
		"outerHTML",
		"#"+dialogDetailDeleteID)
	btnCloseModalDeleteDetail := masterkelainan.BtnCloseFormKelainanDetail(
		"/dev/md/kelainandetail/closedeleteform",
		"#"+dialogDetailDeleteBodyID,
		"outerHTML",
		"#"+dialogDetailDeleteID)
	modalDetailDeleteFormCmp := masterkelainan.KelainanDetailForm(
		models.KelainanDetailFormComponent{
			IDComponent:  "formkelainandetail",
			Link:         "/dev/md/kelainandetail/delete",
			HxTarget:     "#" + dialogDetailDeleteBodyID,
			HxSwap:       "outerHTML",
			HxInclude:    "#tableDetailID, #paginationDetailID, #searchDetail, #dialogDetailDeleteBodyID, #dialogDetailDeleteID, #" + searchDetailID + ", #currpage" + paginationDetailID,
			ModalID:      dialogDetailDeleteID,
			ModalTitle:   "Konfirmasi",
			DialogBody:   dialogDetailDeleteCmp,
			DialogAction: modalActionDetailDeleteCmp,
			ButtonCLose:  btnCloseModalDeleteDetail,
		},
	)

	// content, css, js
	content := masterkelainan.ContentMasterKelainan(
		tableID, paginationID, searchID, dialogAddID, dialogAddBodyID,
		dialogDeleteID,
		dialogDeleteBodyID,
		tableDetailID,
		paginationDetailID,
		searchDetailID,
		dialogDetailAddID,
		dialogDetailAddBodyID,
		dialogDetailDeleteID,
		dialogDetailDeleteBodyID,
		breadcrumbComponent,
		contentKelainanComponent,
		kelainanFilterComponent,
		kelainanPaginationComponent,
		modaladdcomponent,
		modalDeleteFormComponent,
		contentKelainanDetailCmp,
		kelainanDetailFilterCmp,
		kelainanDetailPaginationCmp,
		modaldetailaddcomponent,
		modalDetailDeleteFormCmp)
	css := masterkelainan.CssKelainan()
	js := masterkelainan.JsMdKelainan()

	// show kelainan screen
	view := masterkelainan.ShowKelainan(
		title,
		content,
		css,
		js,
		navbarmenuComponent,
		navbaruserComponent,
		sidebaruserprofileComponent)

	return utils.View(c, view)
}

func (lh *MasterKelainanHandler) HandlerShowMasterKelainanSearch(c echo.Context) error {
	search := c.QueryParam("search")
	searchID := c.QueryParam("searchID")
	tableID := c.QueryParam("tableID")
	tableDetailID := c.QueryParam("tableDetailID")
	paginationID := c.QueryParam("paginationID")
	dialogDeleteBodyID := c.QueryParam("dialogDeleteBodyID")
	logger, _ := zap.NewProduction()

	dataKelainan, totalpage, err := lh.MasterKelainanService.GetListMasterKelainan(search, 1, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET KELAINAN",
			zap.Any("error", err),
		)
		fmt.Println(dataKelainan)
		return err
	}

	contentKelainanComponent := masterkelainan.TableKelainan(
		dataKelainan, tableID,
		"/dev/md/kelainan/flagaktif",
		"outerHTML",
		"/dev/md/kelainan/flagnonaktif",
		"outerHTML",
		"/dev/md/kelainan/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogDeleteBodyID, #dialogDeleteID, #tableDetailID",
		"/dev/md/kelainan/selected",
		"#"+tableDetailID,
	)

	kelainanPaginationComponent := pagination.PaginationV3(totalpage,
		1,
		"/dev/md/kelainanpagination",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogDeleteBodyID, #dialogDeleteID, #tableDetailID",
		"#"+paginationID, "outerHTML", "", "",
		masterkelainan.BeforeRequestContent(),
		masterkelainan.AfterRequestContent(),
	)

	retval := []templ.Component{contentKelainanComponent, kelainanPaginationComponent}

	return utils.ViewMulti(c, retval)
}

func (lh *MasterKelainanHandler) HandlerShowMasterKelainanPagination(c echo.Context) error {
	logger, _ := zap.NewProduction()

	search := c.QueryParam("search")
	pageparam := c.QueryParam("page")
	tableID := c.QueryParam("tableID")
	tableDetailID := c.QueryParam("tableDetailID")
	searchID := c.QueryParam("searchID")
	paginationID := c.QueryParam("paginationID")
	dialogDeleteBodyID := c.QueryParam("dialogDeleteBodyID")
	page, err := strconv.Atoi(pageparam)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR CONVERT PAGE PARAM",
			zap.Any("page", page),
			zap.Any("error ", err),
		)
		return err
	}

	dataKelainan, totalpage, err := lh.MasterKelainanService.GetListMasterKelainan(search, page, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET KELAINAN",
			zap.Any("error", err),
		)
		fmt.Println(dataKelainan)
		return err
	}

	contentKelainanComponent := masterkelainan.TableKelainan(
		dataKelainan, tableID,
		"/dev/md/kelainan/flagaktif",
		"outerHTML",
		"/dev/md/kelainan/flagnonaktif",
		"outerHTML",
		"/dev/md/kelainan/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogDeleteBodyID, #dialogDeleteID, #tableDetailID",
		"/dev/md/kelainan/selected",
		"#"+tableDetailID,
	)

	kelainanPaginationComponent := pagination.PaginationV3(totalpage,
		page,
		"/dev/md/kelainanpagination",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogDeleteBodyID, #dialogDeleteID, #tableDetailID",
		"#"+paginationID, "outerHTML", "", "",
		masterkelainan.BeforeRequestContent(),
		masterkelainan.AfterRequestContent(),
	)

	retval := []templ.Component{contentKelainanComponent, kelainanPaginationComponent}

	return utils.ViewMulti(c, retval)
}

func (lh *MasterKelainanHandler) AddKelainan(c echo.Context) error {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	name := c.FormValue("kelainanname")
	tableID := c.FormValue("tableID")
	tableDetailID := c.FormValue("tableDetailID")
	paginationID := c.FormValue("paginationID")
	searchID := c.FormValue("searchID")
	search := c.FormValue("search")
	currPagePrm := c.FormValue("currpage" + paginationID)
	dialogAddBodyID := c.FormValue("dialogAddBodyID")
	dialogAddID := c.FormValue("dialogAddID")
	dialogDeleteBodyID := c.FormValue("dialogDeleteBodyID")

	currPage, err := strconv.Atoi(currPagePrm)

	if err != nil {
		return err
	}
	logger.Info("ADD KELAINAN",
		zap.Any("nama", name),
		zap.Any("tableid", tableID),
		zap.Any("search", search),
		zap.Any("currpage", currPage),
	)

	// form validation
	nameValidation := ""
	if strings.TrimSpace(name) == "" {
		nameValidation = "Nama grup kelainan tidak boleh kosong"
	}
	if nameValidation != "" {
		newForm := masterkelainan.BodyFormKelainan(
			models.CustomTextFieldv2Prm{
				Label:       "Nama Grup Kelainan",
				Name:        "kelainanname",
				Placeholder: "Nama Grup Kelainan",
				Type:        "text",
				Value:       name,
				ID:          "kelainaninputname",
				ErrorMsg:    nameValidation},
			models.CustomTextFieldv2Prm{Label: "Nama Grup Kelainan",
				Name:        "kelainanid",
				Placeholder: "Nama Grup Kelainan",
				Type:        "hidden",
				ID:          "kelainaninputid"},
			dialogAddBodyID, masterkelainan.JsHideModal(""))

		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Warning", nameValidation, "warning"), newForm})

	}

	// Add kelainan service
	dataKelainan, err := lh.MasterKelainanService.AddKelainan(name)

	if err != nil {
		logger.Info("Error",
			zap.Any("Error", err),
			zap.Any("name", name),
		)

		newForm := masterkelainan.BodyFormKelainan(
			models.CustomTextFieldv2Prm{
				Label:       "Nama Grup Kelainan",
				Name:        "kelainanname",
				Placeholder: "Nama Grup Kelainan",
				Type:        "text",
				Value:       name,
				ID:          "kelainaninputname",
			},
			models.CustomTextFieldv2Prm{Label: "Nama Grup Kelainan",
				Name:        "kelainanid",
				Placeholder: "Nama Grup Kelainan",
				Type:        "hidden",
				ID:          "kelainaninputid"},
			dialogAddBodyID, masterkelainan.JsHideModal(""))
		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Error", err.Error(), "danger"), newForm})
	}
	logger.Info("ADD KELAINAN",
		zap.Any("reutn", dataKelainan),
		zap.Any("name", name),
	)

	// Get data table current page
	dataKelainanList, totalpage, err := lh.MasterKelainanService.GetListMasterKelainan(search, currPage, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET KELAINAN",
			zap.Any("error", err),
		)
		return err
	}

	// New add form
	newForm := masterkelainan.BodyFormKelainan(
		models.CustomTextFieldv2Prm{
			Label:       "Nama Grup Kelainan",
			Name:        "kelainanname",
			Placeholder: "Nama Grup Kelainan",
			Type:        "text",
			Value:       "",
			ID:          "kelainaninputname",
		},
		models.CustomTextFieldv2Prm{Label: "Nama Grup Kelainan",
			Name:        "kelainanid",
			Placeholder: "Nama Grup Kelainan",
			Type:        "hidden",
			ID:          "kelainaninputid"},
		dialogAddBodyID, masterkelainan.JsHideModal("#"+dialogAddID))

	// New table listing
	contentKelainanComponent := masterkelainan.TableKelainan(
		dataKelainanList, tableID,
		"/dev/md/kelainan/flagaktif",
		"outerHTML",
		"/dev/md/kelainan/flagnonaktif",
		"outerHTML",
		"/dev/md/kelainan/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogDeleteBodyID, #dialogDeleteID, #tableDetailID",
		"/dev/md/kelainan/selected",
		"#"+tableDetailID,
	)

	// New pagination
	kelainanPaginationComponent := pagination.PaginationV3(totalpage,
		currPage,
		"/dev/md/kelainanpagination",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogDeleteBodyID, #dialogDeleteID, #tableDetailID",
		"#"+paginationID, "outerHTML", "", "true",
		masterkelainan.BeforeRequestContent(),
		masterkelainan.AfterRequestContent(),
	)

	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Success add kelainan "+name, "success")

	retval := []templ.Component{toastSuccess, contentKelainanComponent, kelainanPaginationComponent, newForm}

	return utils.ViewMulti(c, retval)
}

func (lh *MasterKelainanHandler) HandleCloseFormAdd(c echo.Context) error {
	dialogAddBodyID := c.FormValue("dialogAddBodyID")
	newForm := masterkelainan.BodyFormKelainan(
		models.CustomTextFieldv2Prm{
			Label:       "Nama Grup Kelainan",
			Name:        "kelainanname",
			Placeholder: "Nama Grup Kelainan",
			Type:        "text",
			ID:          "kelainaninputname"},
		models.CustomTextFieldv2Prm{Label: "Nama Grup Kelainan",
			Name:        "kelainanid",
			Placeholder: "Nama Grup Kelainan",
			Type:        "hidden",
			ID:          "kelainaninputid"},
		dialogAddBodyID, masterkelainan.JsHideModal(""))
	return utils.View(c, newForm)
}

func (lh *MasterKelainanHandler) HandleOpenDeleteForm(c echo.Context) error {
	id := c.QueryParam("id")
	dialogDeleteID := c.QueryParam("dialogDeleteID")
	dialogDeleteBodyID := c.QueryParam("dialogDeleteBodyID")

	dataKelainan, err := lh.MasterKelainanService.GetKelainanByID(id)
	if err != nil {
		newForm := masterkelainan.DeleteConfirmationBody(
			models.CustomTextFieldv2Prm{Name: "kelainanid", Type: "hidden", Value: strconv.Itoa(dataKelainan.Mcu_KelainanGroupID)},
			dialogDeleteBodyID,
			"Apakah anda yakin menghapus grup kelainan berikut ?",
			[]string{"Kelainan Grup"},
			[]string{""},
			masterkelainan.JsShowModal(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Error", "Get kelainan grup error", "danger"), newForm})
	}
	newForm := masterkelainan.DeleteConfirmationBody(
		models.CustomTextFieldv2Prm{Name: "kelainanid", Type: "hidden", Value: strconv.Itoa(dataKelainan.Mcu_KelainanGroupID)},
		dialogDeleteBodyID,
		"Apakah anda yakin menghapus grup kelainan berikut ?",
		[]string{"Nama Grup"},
		[]string{dataKelainan.Mcu_KelainanGroupName},
		masterkelainan.JsShowModal("#"+dialogDeleteID))
	return utils.View(c, newForm)
}

func (lh *MasterKelainanHandler) HandleCloseFormDelete(c echo.Context) error {
	dialogDeleteBodyID := c.FormValue("dialogDeleteBodyID")
	newForm := masterkelainan.DeleteConfirmationBody(
		models.CustomTextFieldv2Prm{Name: "kelainanid", Type: "hidden", Value: ""},
		dialogDeleteBodyID,
		"Apakah anda yakin menghapus grup kelainan berikut ?",
		[]string{"Grup Kelainan"},
		[]string{""},
		masterkelainan.JsShowModal(""))
	return utils.View(c, newForm)
}

func (lh *MasterKelainanHandler) HandleDeleteKelainan(c echo.Context) error {

	logger, _ := zap.NewProduction()
	defer logger.Sync()

	//prm form kelainan
	id := c.FormValue("kelainanid")
	tableID := c.FormValue("tableID")
	tableDetailID := c.FormValue("tableDetailID")
	paginationID := c.FormValue("paginationID")
	searchID := c.FormValue("searchID")
	search := c.FormValue("search")
	currPagePrm := c.FormValue("currpage" + paginationID)
	// dialogAddBodyID := c.FormValue("dialogAddBodyID")
	dialogDeleteID := c.FormValue("dialogDeleteID")
	dialogDeleteBodyID := c.FormValue("dialogDeleteBodyID")

	currPage, err := strconv.Atoi(currPagePrm)
	if err != nil {
		return err
	}
	logger.Info("ADD KELAINAN PARAM",
		zap.Any("tableid", tableID),
		zap.Any("search", search),
		zap.Any("currpage", currPage),
	)

	//edit user group service
	dataKelainan, err := lh.MasterKelainanService.DeleteKelainan(id)
	if err != nil {
		logger.Info("Error",
			zap.Any("Error", err),
		)
		newForm := masterkelainan.DeleteConfirmationBody(
			models.CustomTextFieldv2Prm{Name: "kelainanid", Type: "hidden", Value: ""},
			dialogDeleteBodyID,
			"Apakah anda yakin menghapus grup kelaian berikut ?",
			[]string{"Nama Grup"},
			[]string{""},
			masterkelainan.JsShowModal(""))
		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Error", err.Error(), "danger"),
			newForm})

	}
	logger.Info("ADD KELAINAN",
		zap.Any("return", dataKelainan),
	)
	//get list user group for table
	dataKelainanList, totalPage, err := lh.MasterKelainanService.GetListMasterKelainan(search, currPage, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET KELAINAN",
			zap.Any("error", err),
		)
		fmt.Println(dataKelainan)
		return err
	}
	if len(dataKelainanList) == 0 && totalPage > 1 {
		currPage = currPage - 1
		dataKelainanList, _, err = lh.MasterKelainanService.GetListMasterKelainan(search, currPage, 5)
		if err != nil {
			defer logger.Sync()
			logger.Info("ERROR GET KELAINAN",
				zap.Any("error", err),
			)
			return err
		}
	}
	//table component
	tableComponent := masterkelainan.TableKelainan(dataKelainanList,
		tableID,
		"/dev/md/kelainan/flagaktif",
		"outerHTML",
		"/dev/md/kelainan/flagnonaktif",
		"outerHTML",
		"/dev/md/kelainan/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogDeleteBodyID, #dialogDeleteID, #tableDetailID",
		"/dev/md/kelainan/selected",
		"#"+tableDetailID,
	)

	//pagination component
	kelainanPaginationComponent := pagination.PaginationV3(
		totalPage,
		currPage,
		"/dev/md/kelainanpagination",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogDeleteBodyID, #dialogDeleteID, #tableDetailID",
		"#"+paginationID,
		"outerHTML", "", "true",
		masterkelainan.BeforeRequestContent(),
		masterkelainan.AfterRequestContent(),
	)

	newForm := masterkelainan.DeleteConfirmationBody(
		models.CustomTextFieldv2Prm{Name: "kelainanid", Type: "hidden", Value: ""},
		dialogDeleteBodyID,
		"Apakah anda yakin menghapus grup kelainan berikut ?",
		[]string{"Grup Kelainan"},
		[]string{""},
		masterkelainan.JsHideModal("#"+dialogDeleteID))
	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil delete Grup Kelainan ", "success")

	retVal := []templ.Component{toastSuccess, tableComponent, kelainanPaginationComponent, newForm}

	return utils.ViewMulti(c, retVal)
}

func (lh *MasterKelainanHandler) HandlerUpdateStatusKelainanAktif(c echo.Context) error {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	id := c.QueryParam("id")
	selectedStatus := c.QueryParam("selectedStatus")

	logger.Info("CEK kelainan id",
		zap.Any("kelainan id", id),
		zap.Any("selectedStatus", selectedStatus))

	dataKelainan, err := lh.MasterKelainanService.UpdateFlagStatusKelainan(id, selectedStatus)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR UPDATE STATUS KELAINAN",
			zap.Any("error", err),
		)
		fmt.Println(dataKelainan)
		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Error", err.Error(), "danger")})
	}

	if selectedStatus == "Y" {
		buttonCmp := masterkelainan.CustomButtonTidak(
			models.ButtonPrm{
				ID:        "non" + id,
				HxGet:     "/dev/md/kelainan/flagaktif?id=" + id + "&selectedStatus=N",
				HxTarget:  "#non" + id,
				HxSwap:    "outerHTML",
				HxInclude: "#paginationID, #dialogDeleteBodyID, #dialogDeleteID, #tableID, #non" + id,
				Label:     "Tidak",
				Class:     "col-12 col-sm-12 col-md-12 col-lg-5 col-xl-5 col-xxl-5 btn btn-light-danger mb-2",
			},
		)

		toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil update status Grup Kelainan", "success")

		return utils.ViewMulti(c, []templ.Component{toastSuccess, buttonCmp})
	} else {
		buttonCmp := masterkelainan.CustomButtonTidak(
			models.ButtonPrm{
				ID:        "active" + id,
				HxGet:     "/dev/md/kelainan/flagaktif?id=" + id + "&selectedStatus=Y",
				HxTarget:  "#active" + id,
				HxSwap:    "outerHTML",
				HxInclude: "#paginationID, #dialogDeleteBodyID, #dialogDeleteID, #tableID, #active" + id,
				Label:     "Aktif",
				Class:     "col-12 col-sm-12 col-md-12 col-lg-5 col-xl-5 col-xxl-5 btn btn-light-tosca mb-2",
			},
		)

		toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil update status Grup Kelainan", "success")

		return utils.ViewMulti(c, []templ.Component{toastSuccess, buttonCmp})
	}
}

func (lh *MasterKelainanHandler) HandlerUpdateStatusKelainanNonAktif(c echo.Context) error {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	id := c.QueryParam("id")
	selectedStatus := c.QueryParam("selectedStatus")

	logger.Info("CEK kelainan id",
		zap.Any("kelainan id", id),
		zap.Any("selectedStatus", selectedStatus))

	dataKelainan, err := lh.MasterKelainanService.UpdateFlagStatusKelainan(id, selectedStatus)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR UPDATE STATUS KELAINAN",
			zap.Any("error", err),
		)
		fmt.Println(dataKelainan)
		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Error", err.Error(), "danger")})
	}

	if selectedStatus == "N" {
		buttonCmp := masterkelainan.CustomButtonTidak(
			models.ButtonPrm{
				ID:        "active" + id,
				HxGet:     "/dev/md/kelainan/flagnonaktif?id=" + id + "&selectedStatus=Y",
				HxTarget:  "#active" + id,
				HxSwap:    "outerHTML",
				HxInclude: "#paginationID, #dialogDeleteBodyID, #dialogDeleteID, #tableID, #active" + id,
				Label:     "Aktif",
				Class:     "col-12 col-sm-12 col-md-12 col-lg-5 col-xl-5 col-xxl-5 btn btn-light-tosca mb-2",
			},
		)

		toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil update status Grup Kelainan", "success")

		return utils.ViewMulti(c, []templ.Component{toastSuccess, buttonCmp})
	} else {
		buttonCmp := masterkelainan.CustomButtonTidak(
			models.ButtonPrm{
				ID:        "non" + id,
				HxGet:     "/dev/md/kelainan/flagnonaktif?id=" + id + "&selectedStatus=N",
				HxTarget:  "#non" + id,
				HxSwap:    "outerHTML",
				HxInclude: "#paginationID, #dialogDeleteBodyID, #dialogDeleteID, #tableID, #non" + id,
				Label:     "Tidak",
				Class:     "col-12 col-sm-12 col-md-12 col-lg-5 col-xl-5 col-xxl-5 btn btn-light-danger mb-2",
			},
		)

		toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil update status Grup Kelainan", "success")

		return utils.ViewMulti(c, []templ.Component{toastSuccess, buttonCmp})
	}
}

// master kelainan detail
func (lh *MasterKelainanHandler) HandlerShowMasterKelainanDetail(c echo.Context) error {
	logger, _ := zap.NewProduction()
	id := c.QueryParam("id")

	dataKelainanDetail, totalPageDetail, err := lh.MasterKelainanService.GetListMasterKelainanDetail(id, "", 1, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET KELAINAN DETAIL",
			zap.Any("error", err),
		)
		return err
	}

	defer logger.Sync()
	logger.Info("LOAD KELAINAN DETAIL",
		zap.Any("data detail", dataKelainanDetail),
	)

	tableDetailID := c.QueryParam("tableDetailID")
	paginationDetailID := c.QueryParam("paginationDetailID")
	searchDetailID := c.QueryParam("searchDetailID")
	// dialogDetailDeleteID := c.QueryParam("dialogDetailDeleteID")
	dialogDetailDeleteBodyID := c.QueryParam("dialogDetailDeleteBodyID")

	logger.Info("tabel paginationDetailID",
		zap.Any("tabel paginationDetailID", paginationDetailID))

	// component selected id
	selectid := masterkelainan.CustomTextFieldSelected(
		models.CustomTextFieldSelected{
			Name:  "selectedID",
			Type:  "hidden",
			Value: id,
			ID:    "selectedID",
		},
	)

	// component listing table listing kelainan detail
	contentKelainanDetailCmp := masterkelainan.TableKelainanDetail(
		dataKelainanDetail, tableDetailID,
		"/dev/md/kelainandetail/opendelete",
		"#"+dialogDetailDeleteBodyID,
		"outerHTML",
		"#dialogDetailDeleteBodyID, #dialogDetailDeleteID, #tableDetailID",
	)

	// pagination kelainan detail component
	kelainanDetailPaginationCmp := pagination.PaginationV3(
		totalPageDetail,
		1,
		"/dev/md/kelainandetailpagination",
		paginationDetailID,
		"#selectedID, #tableDetailID, #searchDetailID, #dialogDetailDeleteBodyID, #dialogDetailDeleteID, #"+searchDetailID+", #paginationDetailID",
		"#"+paginationDetailID,
		"outerHTML", "", "true",
		masterkelainan.BeforeRequestContentDetail(),
		masterkelainan.AfterRequestContentDetail(),
	)

	return utils.ViewMulti(c, []templ.Component{selectid, contentKelainanDetailCmp, kelainanDetailPaginationCmp})
}

func (lh *MasterKelainanHandler) HandlerShowMdKelainanDetailSearch(c echo.Context) error {
	logger, _ := zap.NewProduction()

	selectedID := c.QueryParam("selectedID")
	searchdetail := c.QueryParam("searchdetail")
	searchDetailID := c.QueryParam("searchDetailID")
	tableDetailID := c.QueryParam("tableDetailID")
	paginationDetailID := c.QueryParam("paginationDetailID")
	dialogDetailDeleteBodyID := c.QueryParam("dialogDetailDeleteBodyID")

	dataKelainanDetail, totalpagedetail, err := lh.MasterKelainanService.GetListMasterKelainanDetail(selectedID, searchdetail, 1, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET KELAINAN DETAIL",
			zap.Any("error", err),
		)
		return err
	}

	logger.Info("dataKelainanDetail",
		zap.Any("cek id", selectedID),
		zap.Any("dataKelainanDetail", dataKelainanDetail))

	contentKelainanDetailCmp := masterkelainan.TableKelainanDetail(
		dataKelainanDetail,
		tableDetailID,
		"/dev/md/kelainandetail/opendelete",
		"#"+dialogDetailDeleteBodyID,
		"outerHTML",
		"#dialogDetailDeleteBodyID, #dialogDetailDeleteID, #tableDetailID",
	)

	kelainanDetailPaginationCmp := pagination.PaginationV3(
		totalpagedetail,
		1,
		"/dev/md/kelainandetailpagination",
		paginationDetailID,
		"#selectedID, #tableDetailID, #searchDetailID, #dialogDetailDeleteBodyID, #dialogDetailDeleteID, #"+searchDetailID+", #paginationDetailID",
		"#"+paginationDetailID, "outerHTML", "", "true",
		masterkelainan.BeforeRequestContentDetail(),
		masterkelainan.AfterRequestContentDetail(),
	)

	retVal := []templ.Component{contentKelainanDetailCmp, kelainanDetailPaginationCmp}

	return utils.ViewMulti(c, retVal)
}

func (lh *MasterKelainanHandler) HandlerShowMdKelainanDetailPagination(c echo.Context) error {
	logger, _ := zap.NewProduction()

	selectedID := c.QueryParam("selectedID")
	searchdetail := c.QueryParam("searchdetail")
	pageparamdetail := c.QueryParam("page")
	searchDetailID := c.QueryParam("searchDetailID")
	tableDetailID := c.QueryParam("tableDetailID")
	paginationDetailID := c.QueryParam("paginationDetailID")
	dialogDetailDeleteBodyID := c.QueryParam("dialogDetailDeleteBodyID")
	// dialogDetailDeleteID := c.QueryParam("dialogDetailDeleteID")

	// defer logger.Sync()
	// logger.Info("CEK HandlerShowMdKelainanDetailPagination",
	// 	zap.Any("dialogDetailDeleteBodyID", dialogDetailDeleteBodyID),
	// )

	page, err := strconv.Atoi(pageparamdetail)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR CONVERT PAGE PARAM DETAIL",
			zap.Any("page", page),
			zap.Any("error", err),
		)
		return err
	}

	dataKelainanDetail, totalpgaedetail, err := lh.MasterKelainanService.GetListMasterKelainanDetail(selectedID, searchdetail, page, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET KELAINAN DETAIL",
			zap.Any("error", err),
		)
		return err
	}

	contentKelainanDetailCmp := masterkelainan.TableKelainanDetail(
		dataKelainanDetail,
		tableDetailID,
		"/dev/md/kelainandetail/opendelete",
		"#"+dialogDetailDeleteBodyID,
		"outerHTML",
		"#dialogDetailDeleteBodyID, #dialogDetailDeleteID, #tableDetailID",
	)

	kelainanPaginationCmp := pagination.PaginationV3(totalpgaedetail,
		page,
		"/dev/md/kelainandetailpagination",
		paginationDetailID,
		"#selectedID, #tableDetailID, #searchDetailID, #dialogDetailDeleteBodyID, #dialogDetailDeleteID, #"+searchDetailID+", #paginationDetailID",
		"#"+paginationDetailID, "outerHTML", "", "",
		masterkelainan.BeforeRequestContentDetail(),
		masterkelainan.AfterRequestContentDetail(),
	)

	retval := []templ.Component{contentKelainanDetailCmp, kelainanPaginationCmp}

	return utils.ViewMulti(c, retval)
}

func (lh *MasterKelainanHandler) HandlerAddKelainanDetail(c echo.Context) error {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	selectedID := c.FormValue("selectedID")
	nameKelainanDetail := c.FormValue("kelainandetailname")
	nameClassification := c.FormValue("classificationname")
	// tableID := c.FormValue("tableID")
	tableDetailID := c.FormValue("tableDetailID")
	paginationDetailID := c.FormValue("paginationDetailID")
	searchDetailID := c.FormValue("searchDetailID")
	searchdetail := c.FormValue("searchdetail")
	currPagePrmDetail := c.FormValue("currpage" + paginationDetailID)
	dialogDetailAddBodyID := c.FormValue("dialogDetailAddBodyID")
	dialogDetailAddID := c.FormValue("dialogDetailAddID")
	dialogDetailDeleteBodyID := c.FormValue("dialogDetailDeleteBodyID")

	currPage, err := strconv.Atoi(currPagePrmDetail)

	if err != nil {
		return err
	}
	logger.Info("ADD KELAINAN DETAIL",
		zap.Any("nama", nameKelainanDetail),
		zap.Any("nama klasifikasi", nameClassification),
		zap.Any("tabelid", tableDetailID),
		zap.Any("search", searchdetail),
		zap.Any("currpage", currPage),
	)

	kelainanGrupID, _ := strconv.Atoi(selectedID)
	// form validation
	formValidation := ""
	nameValidation := ""
	kelainanGrupValidation := ""
	if strings.TrimSpace(nameKelainanDetail) == "" {
		formValidation = "Nama kelainan tidak boleh kosong"
		nameValidation = "Nama kelainan tidak boleh kosong"
		kelainanGrupValidation = ""
	}
	if strings.TrimSpace(selectedID) == "" || kelainanGrupID == 0 {
		formValidation = "Pilih kelainan grup dulu"
		nameValidation = ""
		kelainanGrupValidation = "Pilih kelainan grup dulu"
	}
	if strings.TrimSpace(nameKelainanDetail) == "" && (strings.TrimSpace(selectedID) == "" || kelainanGrupID == 0) {
		formValidation = "Nama kelainan dan Kelainan Grup tidak boleh kosong"
		nameValidation = "Nama kelainan tidak boleh kosong"
		kelainanGrupValidation = "Pilih kelainan grup dulu"
	}
	if formValidation != "" || nameValidation != "" || kelainanGrupValidation != "" {
		newForm := masterkelainan.BodyFormKelainanDetail(
			models.CustomTextFieldv2Prm{Label: "Nama Kelainan",
				Name:        "kelainandetailid",
				Placeholder: "Nama Kelainan",
				Type:        "hidden",
				ID:          "kelainandetailid"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama Kelainan",
				Name:        "kelainandetailname",
				Placeholder: "Nama Kelainan",
				Type:        "text",
				Value:       nameKelainanDetail,
				ID:          "kelainandetailinputname",
				ErrorMsg:    nameValidation},
			models.SwitchclassPrm{
				Label:   "Dengan Klasifikasi",
				Type:    "checkbox",
				Checked: "true ",
				Name:    "select",
				ID:      "switchname"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama Klasifikasi",
				Name:        "classificationname",
				Placeholder: "Nama Klasifikasi",
				Type:        "text",
				Value:       nameClassification, ID: "classificationinputname"},
			dialogDetailAddBodyID, masterkelainan.JsHideModalDetail(""))

		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Warning", formValidation, "warning"), newForm})
	}

	// add kelainan detail service
	dataKelainanDetail, err := lh.MasterKelainanService.AddKelainanDetail(nameKelainanDetail, nameClassification, selectedID)

	if err != nil {
		logger.Info("Error",
			zap.Any("Error", err),
			zap.Any("nama", nameKelainanDetail),
			zap.Any("nama klasifikasi", nameClassification),
			zap.Any("selectedid", selectedID),
		)

		newForm := masterkelainan.BodyFormKelainanDetail(
			models.CustomTextFieldv2Prm{
				Label:       "Nama Kelainan",
				Name:        "kelainandetailid",
				Placeholder: "Nama Kelainan",
				Type:        "hidden", ID: "kelainandetailid"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama Kelainan",
				Name:        "kelainandetailname",
				Placeholder: "Nama Kelainan",
				Type:        "text", ID: "kelainandetailinputname"},
			models.SwitchclassPrm{
				Label:   "Dengan Klasifikasi",
				Type:    "checkbox",
				Checked: "true ",
				Name:    "select",
				ID:      "switchname"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama Klasifikasi",
				Name:        "classificationname",
				Placeholder: "Nama Klasifikasi",
				Type:        "text", ID: "classificationinputname"},
			dialogDetailAddBodyID, masterkelainan.JsHideModalDetail(""))
		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Error", err.Error(), "danger"), newForm})
	}
	logger.Info("PARAMETER LIST",
		zap.Any("selectedid", selectedID),
		zap.Any("searchdetail", searchdetail),
		zap.Any("currPage", currPage),
		zap.Any("dataKelainanDetail", dataKelainanDetail),
	)

	// get data table current page
	dataKelainanDetailList, totalpage, err := lh.MasterKelainanService.GetListMasterKelainanDetail(selectedID, searchdetail, currPage, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET KELAINAN DETAIL",
			zap.Any("error", err),
		)
	}

	logger.Info("list kelainan detail add",
		zap.Any("return", dataKelainanDetailList),
	)

	// new add form
	newForm := masterkelainan.BodyFormKelainanDetail(
		models.CustomTextFieldv2Prm{
			Label:       "Nama Kelainan",
			Name:        "kelainandetailid",
			Placeholder: "Nama Kelainan",
			Type:        "hidden", ID: "kelainandetailid"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Kelainan",
			Name:        "kelainandetailname",
			Placeholder: "Nama Kelainan",
			Type:        "text",
			Value:       "",
			ID:          "kelainandetailinputname"},
		models.SwitchclassPrm{
			Label:   "Dengan Klasifikasi",
			Type:    "checkbox",
			Checked: "true ",
			Name:    "select",
			ID:      "switchname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Klasifikasi",
			Name:        "classificationname",
			Placeholder: "Nama Klasifikasi",
			Type:        "text",
			Value:       "",
			ID:          "classificationinputname"},
		dialogDetailAddBodyID, masterkelainan.JsHideModalDetail("#"+dialogDetailAddID))

	// new table listing
	contentKelainanDetailComponent := masterkelainan.TableKelainanDetail(
		dataKelainanDetailList, tableDetailID,
		"/dev/md/kelainandetail/opendelete",
		"#"+dialogDetailDeleteBodyID,
		"outerHTML",
		"#dialogDetailDeleteBodyID, #dialogDetailDeleteID, #tableDetailID",
	)

	// new pagination
	kelainanDetailPaginationCmp := pagination.PaginationV3(totalpage,
		currPage,
		"/dev/md/kelainandetailpagination",
		paginationDetailID,
		"#tableDetailID, #searchDetailID, #dialogDetailDeleteBodyID, #dialogDetailDeleteID, #"+searchDetailID+", #paginationDetailID",
		"#"+paginationDetailID, "outerHTML", "", "true",
		masterkelainan.BeforeRequestContentDetail(),
		masterkelainan.AfterRequestContentDetail(),
	)

	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Success add kelainan "+nameKelainanDetail, "success")

	retVal := []templ.Component{toastSuccess, contentKelainanDetailComponent, kelainanDetailPaginationCmp, newForm}

	return utils.ViewMulti(c, retVal)
}

func (lh *MasterKelainanHandler) HandlerCloseFormAddDetail(c echo.Context) error {
	dialogDetailAddBodyID := c.FormValue("dialogDetailAddBodyID")

	logger, _ := zap.NewProduction()
	defer logger.Sync()
	logger.Info("CEK dialogDetailAddBodyID CLOSE",
		zap.Any("dialogDetailAddBodyID", dialogDetailAddBodyID),
	)

	newForm := masterkelainan.BodyFormKelainanDetail(
		models.CustomTextFieldv2Prm{
			Label:       "Nama Kelainan",
			Name:        "kelainandetailid",
			Placeholder: "Nama Kelainan",
			Type:        "hidden", ID: "kelainandetailid"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Kelainan",
			Name:        "kelainandetailname",
			Placeholder: "Nama Kelainan",
			Type:        "text", ID: "kelainandetailinputname"},
		models.SwitchclassPrm{
			Label:   "Dengan Klasifikasi",
			Type:    "checkbox",
			Checked: "true ",
			Name:    "select",
			ID:      "switchname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Klasifikasi",
			Name:        "classificationname",
			Placeholder: "Nama Klasifikasi",
			Type:        "text", ID: "classificationinputname"},
		dialogDetailAddBodyID, masterkelainan.JsHideModalDetail(""))
	return utils.View(c, newForm)
}

func (lh *MasterKelainanHandler) HandlerOpenDeleteFormDetail(c echo.Context) error {
	id := c.QueryParam("id")
	dialogDetailDeleteID := c.QueryParam("dialogDetailDeleteID")
	dialogDetailDeleteBodyID := c.QueryParam("dialogDetailDeleteBodyID")

	logger, _ := zap.NewProduction()
	defer logger.Sync()
	logger.Info("CEK dialogDetailDeleteBodyID",
		zap.Any("dialogDetailDeleteBodyID", dialogDetailDeleteBodyID),
	)

	dataKelainanDetail, err := lh.MasterKelainanService.GetKelainanDetailByID(id)

	if err != nil {
		newForm := masterkelainan.DeleteConfirmationBodyDetail(
			models.CustomTextFieldv2Prm{Name: "kelainandetailid", Type: "hidden", Value: strconv.Itoa(dataKelainanDetail.Mcu_KelainanID)},
			dialogDetailDeleteBodyID,
			"Apakah anda yakin menghapus kelainan berikut ?",
			[]string{"Nama Kelainan", "Nama Klasifikasi"},
			[]string{"", ""},
			masterkelainan.JsShowModalDetail(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Error", "Get kelainan error", "danger"), newForm})
	}
	newForm := masterkelainan.DeleteConfirmationBodyDetail(
		models.CustomTextFieldv2Prm{Name: "kelainandetailid", Type: "hidden", Value: strconv.Itoa(dataKelainanDetail.Mcu_KelainanID)},
		dialogDetailDeleteBodyID,
		"Apakah anda yakin menghapus kelainan berikut ?",
		[]string{"Nama Kelainan", "Nama Klasifikasi"},
		[]string{dataKelainanDetail.Mcu_KelainanName, dataKelainanDetail.Mcu_KelainanClasification},
		masterkelainan.JsShowModalDetail("#"+dialogDetailDeleteID))

	return utils.View(c, newForm)
}

func (lh *MasterKelainanHandler) HandlerCloseDeleteFormDetail(c echo.Context) error {
	dialogDetailDeleteBodyID := c.FormValue("dialogDetailDeleteBodyID")
	newForm := masterkelainan.DeleteConfirmationBodyDetail(
		models.CustomTextFieldv2Prm{Name: "kelainandetailid", Type: "hidden", Value: ""},
		dialogDetailDeleteBodyID,
		"Apakah anda yakin menghapus kelainan berikut ?",
		[]string{"Nama Kelainan", "Nama Klasifikasi"},
		[]string{"", ""},
		masterkelainan.JsShowModalDetail(""))
	return utils.View(c, newForm)
}

func (lh *MasterKelainanHandler) HandlerDeleteKelainanDetail(c echo.Context) error {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	// prm form kelainan
	id := c.FormValue("kelainandetailid")
	tableDetailID := c.FormValue("tableDetailID")
	paginationDetailID := c.FormValue("paginationDetailID")
	searchDetailID := c.FormValue("searchDetailID")
	searchdetail := c.FormValue("searchdetail")
	currPagePrm := c.FormValue("currpage" + paginationDetailID)
	dialogDetailDeleteID := c.FormValue("dialogDetailDeleteID")
	dialogDetailDeleteBodyID := c.FormValue("dialogDetailDeleteBodyID")

	currPage, err := strconv.Atoi(currPagePrm)
	if err != nil {
		return err
	}

	logger.Info("PARAMETER LIST",
		zap.Any("selectedid", id),
		zap.Any("searchdetail", searchdetail),
		zap.Any("currPage", currPage),
	)

	dataKelainanDetail, err := lh.MasterKelainanService.DeleteKelainanDetail(id)
	if err != nil {
		logger.Info("Error",
			zap.Any("Error", err),
		)
		newForm := masterkelainan.DeleteConfirmationBodyDetail(
			models.CustomTextFieldv2Prm{Name: "kelainandetailid", Type: "hidden", Value: ""},
			dialogDetailDeleteBodyID,
			"Apakah anda yakin menghapus kelainan berikut ?",
			[]string{"Nama Kelainan", "Nama Klasifikasi"},
			[]string{"", ""},
			masterkelainan.JsShowModalDetail(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Error", err.Error(), "danger"), newForm})
	}
	logger.Info("Kelainan",
		zap.Any("return", dataKelainanDetail),
	)

	//get list kelainan for table
	// kelainanDetailList, totalpage, err := lh.MasterKelainanService.GetListMasterKelainanDetail(id, searchdetail, currPage, 5)
	kelainanDetailList, totalpage, err := lh.MasterKelainanService.GetListMasterKelainanDetail(id, searchdetail, currPage, 5)
	logger.Info("list kelainan detail delete",
		zap.Any("return", kelainanDetailList),
	)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET KELAINAN",
			zap.Any("error", err),
		)
		fmt.Println(dataKelainanDetail)
		return err
	}
	if len(kelainanDetailList) == 0 && totalpage > 1 {
		currPage = currPage - 1
		kelainanDetailList, _, err = lh.MasterKelainanService.GetListMasterKelainanDetail(id, searchdetail, currPage, 5)
		if err != nil {
			defer logger.Sync()
			logger.Info("ERROR GET KELAINAN",
				zap.Any("error", err),
			)
			return err
		}
	}

	tablecomponent := masterkelainan.TableKelainanDetail(kelainanDetailList,
		tableDetailID,
		"/dev/md/kelainandetail/opendelete",
		"#"+dialogDetailDeleteBodyID,
		"outerHTML",
		"#dialogDetailDeleteBodyID, #dialogDetailDeleteID, #tableDetailID",
	)

	kelainanDetailPaginationCmp := pagination.PaginationV3(
		totalpage,
		currPage,
		"/dev/md/kelainandetailpagination",
		paginationDetailID,
		"#tableDetailID, #searchDetailID, #"+searchDetailID+", #paginationDetailID, #dialogDetailDeleteBodyID, #dialogDetailDeleteID",
		"#"+paginationDetailID,
		"outerHTML", "", "true",
		masterkelainan.BeforeRequestContentDetail(),
		masterkelainan.AfterRequestContentDetail(),
	)

	newForm := masterkelainan.DeleteConfirmationBodyDetail(
		models.CustomTextFieldv2Prm{Name: "kelainandetailid", Type: "hidden", Value: ""},
		dialogDetailDeleteBodyID,
		"Apakah anda yakin menghapus kelainan berikut ?",
		[]string{"Nama Kelainan", "Nama Klasifikasi"},
		[]string{""},
		masterkelainan.JsHideModalDetail("#"+dialogDetailDeleteID))
	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil delete Kelainan ", "success")

	retVal := []templ.Component{toastSuccess, tablecomponent, kelainanDetailPaginationCmp, newForm}

	return utils.ViewMulti(c, retVal)
}
