package dev_handlers

import (
	breadcrumadmin "cpone/component/breadcrumbadmin"
	customtextfieldsearch "cpone/component/customtextfieldsearch"
	navbarmenu "cpone/component/navbar"
	"cpone/component/pagination"
	sidebaruserprofile "cpone/component/sidebar_user_profile"
	customtoastv2 "cpone/component/toastbootstrap"
	"cpone/models"
	"cpone/services"
	"cpone/utils"
	dev_mdusergroupview "cpone/views/dev/mdusergroup"
	"strconv"
	"strings"

	"fmt"

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

type MdUserGroupServices interface {
	GetListMdUserGroup(search string, currentPage int, rowPerPage int) ([]models.UserGroupV1, int, error)
	GetMdUserGroupByID(id string) (models.UserGroupV1, error)
	AddMdUserGroup(code string, name string) (models.UserGroupV1, error)
	EditMdUsergroup(id string, code string, name string) (models.UserGroupV1, error)
	DeleteMdUserGroup(id string) (models.UserGroupV1, error)
	GetMdUserGroupBreadCrumb(title string) (models.BreadCrumbV1, error)
}

func NewMdUserGroupHandler(us MdUserGroupServices) *MdUserGroupHandler {
	return &MdUserGroupHandler{
		MdUserGroupServices: us,
	}
}

type MdUserGroupHandler struct {
	MdUserGroupServices MdUserGroupServices
}

func (lh *MdUserGroupHandler) HandleShowMdUserGroupScreen(c echo.Context) error {
	logger, _ := zap.NewProduction()
	//get sidebarmenu
	tableID := utils.GenerateRandomID("tablebody")
	paginationID := utils.GenerateRandomID("paginationid")
	searchID := utils.GenerateRandomID("searchid")
	dialogAddID := utils.GenerateRandomID("dialogaddID")
	dialogAddBodyID := utils.GenerateRandomID("dialogaddbodyID")
	dialogEditID := utils.GenerateRandomID("dialogEditID")
	dialogEditBodyID := utils.GenerateRandomID("dialogEditbodyID")
	dialogDeleteID := utils.GenerateRandomID("dialogDeleteID")
	dialogDeleteBodyID := utils.GenerateRandomID("dialogDeletebodyID")
	dataMenu, err := services.GetMenu()
	fmt.Println(dataMenu)
	fmt.Println(err)
	if err != nil {
		fmt.Println(dataMenu)
		return err
	}

	//get user login
	dataUser, err := services.GetUserLogin()
	fmt.Println(dataUser)
	fmt.Println(err)
	if err != nil {
		fmt.Println(dataUser)
		return err
	}
	defer logger.Sync()
	logger.Info("LOAD USER DEV",
		zap.Any("data", dataUser),
	)

	title := "Master User Group"

	dataBreadCrumb, err := lh.MdUserGroupServices.GetMdUserGroupBreadCrumb(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),
	)
	//Breadcrumb component
	breadcrumbComponent := breadcrumadmin.MainBreadcrumbAdminV1(dataBreadCrumb)
	// navbar menu
	navbarmenuComponent := navbarmenu.NavbarMenu(dataMenu)

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

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

	//Table component
	dataUserGroup, totalPage, err := lh.MdUserGroupServices.GetListMdUserGroup("", 1, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET USER GROUP",
			zap.Any("error", err),
		)
		fmt.Println(dataUserGroup)
		return err
	}

	tableComponent := dev_mdusergroupview.TableUserGroup(dataUserGroup,
		tableID,
		"/dev/md/usergroupv2/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/usergroupv2/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
	)

	//filter user group component
	usergroupFilterComponent := customtextfieldsearch.MainCustomTextFieldSearchV3(searchID,
		"search",
		"Cari Kode/Nama",
		"text",
		"/dev/md/usergroupv2/filter",
		"input changed delay:500ms, search",
		"#"+paginationID, "#loadingcontent", "", "outerHTML", "#tableID, #paginationID, #searchID,#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		dev_mdusergroupview.BeforeRequestContent(),
		dev_mdusergroupview.AfterRequestContent())
	//Pagination
	userGroupPaginationComponent := pagination.PaginationV3(
		totalPage,
		1,
		"/dev/md/usergroupv2/changepage",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"#"+paginationID,
		"outerHTML", "", "",
		dev_mdusergroupview.BeforeRequestContent(),
		dev_mdusergroupview.AfterRequestContent(),
	)

	//modal add form
	dialogAddBodyCmp := dev_mdusergroupview.BodyFormUserGroup(
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupcode",
			Placeholder: "Kode user group",
			Type:        "text", ID: "usergroupinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama User Group",
			Name:        "usergroupname",
			Placeholder: "Nama user group",
			Type:        "text", ID: "usergroupinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupid",
			Placeholder: "Kode user group",
			Type:        "hidden", ID: "usergroupinputid"},
		dialogAddBodyID, dev_mdusergroupview.JsHideModal(""))
	modalActioAddCmp := dev_mdusergroupview.ActionFormUserGroup(
		"/dev/md/usergroupv2/closeaddform",
		"#"+dialogAddBodyID,
		"outerHTML",
		"#"+dialogAddID)
	btnCloaseModalAdd := dev_mdusergroupview.BtnCloseFormUserGroup(
		"/dev/md/usergroupv2/closeaddform",
		"#"+dialogAddBodyID,
		"outerHTML",
		"#"+dialogAddID)
	modalAddFormComponent := dev_mdusergroupview.UserGroupForm(
		models.UserGroupFormComponent{
			IDComponent:  "formusergroup",
			Link:         "/dev/md/usergroupv2/add",
			HxTarget:     "#" + dialogAddBodyID,
			HxSwap:       "outerHTML",
			HxInclude:    "#tableID, #paginationID, #searchID, #dialogAddBodyID, #dialogAddID, #dialogDeleteBodyID, #dialogEditBodyID, #" + searchID + ", #currpage" + paginationID,
			ModalID:      dialogAddID,
			ModalTitle:   "New-User Group",
			DialogBody:   dialogAddBodyCmp,
			DialogAction: modalActioAddCmp,
			ButtonCLose:  btnCloaseModalAdd,
		},
	)

	//modal edit form
	dialogEditBodyCmp := dev_mdusergroupview.BodyFormUserGroup(
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupcode",
			Placeholder: "Kode user group",
			Type:        "text", ID: "usergroupinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama User Group",
			Name:        "usergroupname",
			Placeholder: "Nama user group",
			Type:        "text", ID: "usergroupinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupid",
			Placeholder: "Kode user group",
			Type:        "hidden", ID: "usergroupinputid"},
		dialogEditBodyID, dev_mdusergroupview.JsHideModal(""))
	modalActioEditCmp := dev_mdusergroupview.ActionFormUserGroup(
		"/dev/md/usergroupv2/closeeditform",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#"+dialogEditID)
	btnCloaseModalEdit := dev_mdusergroupview.BtnCloseFormUserGroup(
		"/dev/md/usergroupv2/closeeditform",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#"+dialogEditID)
	modalEditFormComponent := dev_mdusergroupview.UserGroupForm(
		models.UserGroupFormComponent{
			IDComponent:  "formusergroup",
			Link:         "/dev/md/usergroupv2/edit",
			HxTarget:     "#" + dialogEditBodyID,
			HxSwap:       "outerHTML",
			HxInclude:    "#tableID, #paginationID, #searchID, #dialogDeleteBodyID, #dialogEditBodyID, #dialogEditID, #" + searchID + ", #currpage" + paginationID,
			ModalID:      dialogEditID,
			ModalTitle:   "New-User Group",
			DialogBody:   dialogEditBodyCmp,
			DialogAction: modalActioEditCmp,
			ButtonCLose:  btnCloaseModalEdit,
		},
	)
	//modal delete form
	dialogDeleteBodyCmp := dev_mdusergroupview.BodyFormUserGroup(
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupcode",
			Placeholder: "Kode user group",
			Type:        "text", ID: "usergroupinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama User Group",
			Name:        "usergroupname",
			Placeholder: "Nama user group",
			Type:        "text", ID: "usergroupinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupid",
			Placeholder: "Kode user group",
			Type:        "hidden", ID: "usergroupinputid"},
		dialogDeleteBodyID, dev_mdusergroupview.JsHideModal(""))
	modalActioDeleteCmp := dev_mdusergroupview.ActionFormUserGroupDelete(
		"/dev/md/usergroupv2/closedeleteform",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#"+dialogDeleteID)
	btnCloaseModalDelete := dev_mdusergroupview.BtnCloseFormUserGroup(
		"/dev/md/usergroupv2/closedeleteform",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#"+dialogDeleteID)
	modalDeleteFormComponent := dev_mdusergroupview.UserGroupForm(
		models.UserGroupFormComponent{
			IDComponent:  "formusergroup",
			Link:         "/dev/md/usergroupv2/delete",
			HxTarget:     "#" + dialogDeleteBodyID,
			HxSwap:       "outerHTML",
			HxInclude:    "#tableID, #paginationID, #searchID, #dialogDeleteBodyID, #dialogDeleteID, #dialogEditBodyID, #" + searchID + ", #currpage" + paginationID,
			ModalID:      dialogDeleteID,
			ModalTitle:   "New-User Group",
			DialogBody:   dialogDeleteBodyCmp,
			DialogAction: modalActioDeleteCmp,
			ButtonCLose:  btnCloaseModalDelete,
		},
	)

	//content js & css
	content := dev_mdusergroupview.MdUserGroupScreen(
		tableID,
		paginationID,
		searchID,
		dialogAddID,
		dialogAddBodyID,
		dialogEditID,
		dialogEditBodyID,
		dialogDeleteID,
		dialogDeleteBodyID,
		breadcrumbComponent,
		tableComponent,
		usergroupFilterComponent,
		userGroupPaginationComponent,
		modalAddFormComponent,
		modalEditFormComponent,
		modalDeleteFormComponent)
	css := dev_mdusergroupview.CssMdUserGroup()
	js := dev_mdusergroupview.JsMdUserGroup()

	view := dev_mdusergroupview.ShowMdUserGroupScreen(title,
		content,
		css,
		js,
		navbarmenuComponent,
		navbaruserComponent,
		sidebaruserprofileComponent)
	return utils.View(c, view)
}
func (lh *MdUserGroupHandler) HandleFilterMdUserGroup(c echo.Context) error {
	search := c.QueryParam("search")
	searchID := c.QueryParam("searchID")
	tableID := c.QueryParam("tableID")
	paginationID := c.QueryParam("paginationID")
	dialogEditBodyID := c.QueryParam("dialogEditBodyID")
	dialogDeleteBodyID := c.QueryParam("dialogDeleteBodyID")
	var retval []templ.Component
	logger, _ := zap.NewProduction()

	dataUserGroup, totalpage, err := lh.MdUserGroupServices.GetListMdUserGroup(search, 1, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET USER GROUP",
			zap.Any("error", err),
		)
		fmt.Println(dataUserGroup)
		return err
	}
	tableComponent := dev_mdusergroupview.TableUserGroup(dataUserGroup,
		tableID,
		"/dev/md/usergroupv2/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/usergroupv2/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID")
	userGroupPaginationComponent := pagination.PaginationV3(
		totalpage,
		1,
		"/dev/md/usergroupv2/changepage",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID,  #dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"#"+paginationID,
		"outerHTML", "", "",
		dev_mdusergroupview.BeforeRequestContent(),
		dev_mdusergroupview.AfterRequestContent(),
	)
	retval = append(retval, tableComponent)
	retval = append(retval, userGroupPaginationComponent)
	return utils.ViewMulti(c, retval)
}
func (lh *MdUserGroupHandler) HandleChangePageMdUserGroup(c echo.Context) error {
	search := c.QueryParam("search")
	pageparam := c.QueryParam("page")
	tableID := c.QueryParam("tableID")
	searchID := c.QueryParam("searchID")
	paginationID := c.QueryParam("paginationID")
	dialogEditBodyID := c.QueryParam("dialogEditBodyID")
	dialogDeleteBodyID := c.QueryParam("dialogDeleteBodyID")
	var retval []templ.Component
	logger, _ := zap.NewProduction()
	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
	}
	dataUserGroup, totalpage, err := lh.MdUserGroupServices.GetListMdUserGroup(search, page, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET USER GROUP",
			zap.Any("error", err),
		)
		fmt.Println(dataUserGroup)
		return err
	}
	tableComponent := dev_mdusergroupview.TableUserGroup(dataUserGroup,
		tableID,
		"/dev/md/usergroupv2/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/usergroupv2/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
	)
	userGroupPaginationComponent := pagination.PaginationV3(
		totalpage,
		page,
		"/dev/md/usergroupv2/changepage",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"#"+paginationID,
		"outerHTML", "", "",
		dev_mdusergroupview.BeforeRequestContent(),
		dev_mdusergroupview.AfterRequestContent(),
	)
	retval = append(retval, tableComponent)
	retval = append(retval, userGroupPaginationComponent)
	return utils.ViewMulti(c, retval)
}
func (lh *MdUserGroupHandler) HandleCloseFormAdd(c echo.Context) error {
	dialogAddBodyID := c.FormValue("dialogAddBodyID")
	newForm := dev_mdusergroupview.BodyFormUserGroup(
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupcode",
			Placeholder: "Kode user group",
			Type:        "text", ID: "usergroupinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama User Group",
			Name:        "usergroupname",
			Placeholder: "Nama user group",
			Type:        "text", ID: "usergroupinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupid",
			Placeholder: "Kode user group",
			Type:        "hidden", ID: "usergroupinputid"},
		dialogAddBodyID, dev_mdusergroupview.JsHideModal(""))
	return utils.View(c, newForm)
}
func (lh *MdUserGroupHandler) HandleAddUserGroup(c echo.Context) error {

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

	//prm form addusergroup
	code := c.FormValue("usergroupcode")
	name := c.FormValue("usergroupname")
	tableID := c.FormValue("tableID")
	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")
	dialogEditBodyID := c.FormValue("dialogEditBodyID")
	dialogDeleteBodyID := c.FormValue("dialogDeleteBodyID")

	currPage, err := strconv.Atoi(currPagePrm)
	if err != nil {
		return err
	}
	logger.Info("ADD USER GROUP PARAM",
		zap.Any("code", code),
		zap.Any("nama", name),
		zap.Any("tableid", tableID),
		zap.Any("search", search),
		zap.Any("currpage", currPage),
	)
	formValidation := ""
	codeValidation := ""
	nameValidation := ""
	if strings.TrimSpace(code) == "" {
		formValidation = "Code user group tidak boleh kosong"
		codeValidation = "Code user group tidak boleh kosong"
		nameValidation = ""
	}
	if strings.TrimSpace(name) == "" {
		formValidation = "Nama user group tidak boleh kosong"
		codeValidation = ""
		nameValidation = "Nama user group tidak boleh kosong"
	}
	if strings.TrimSpace(code) == "" && strings.TrimSpace(name) == "" {
		formValidation = "Code dan nama user group tidak boleh kosong"
		codeValidation = "Code user group tidak boleh kosong"
		nameValidation = "Nama user group tidak boleh kosong"

	}

	if formValidation != "" || codeValidation != "" || nameValidation != "" {
		newForm := dev_mdusergroupview.BodyFormUserGroup(
			models.CustomTextFieldv2Prm{
				Label:       "Kode User Group",
				Name:        "usergroupcode",
				Placeholder: "Kode user group",
				ErrorMsg:    codeValidation,
				Value:       code,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama User Group",
				Name:        "usergroupname",
				Placeholder: "Nama user group",
				ErrorMsg:    nameValidation,
				Value:       name,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Kode User Group",
				Name:        "usergroupid",
				Placeholder: "Kode user group",
				Type:        "hidden", ID: "usergroupinputid"},
			dialogAddBodyID, dev_mdusergroupview.JsHideModal(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Warning", formValidation, "warning"), newForm})
	}
	//Add user group service
	dataUserGroup, err := lh.MdUserGroupServices.AddMdUserGroup(code, name)
	if err != nil {
		logger.Info("Error",
			zap.Any("Error", err),
			zap.Any("code", code),
			zap.Any("name", name),
		)
		newForm := dev_mdusergroupview.BodyFormUserGroup(
			models.CustomTextFieldv2Prm{
				Label:       "Kode User Group",
				Name:        "usergroupcode",
				Placeholder: "Kode user group",
				ErrorMsg:    codeValidation,
				Value:       code,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama User Group",
				Name:        "usergroupname",
				Placeholder: "Nama user group",
				ErrorMsg:    nameValidation,
				Value:       name,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Kode User Group",
				Name:        "usergroupid",
				Placeholder: "Kode user group",
				Type:        "hidden", ID: "usergroupinputid"},
			dialogAddBodyID, dev_mdusergroupview.JsHideModal(""))
		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Error", err.Error(), "danger"),
			newForm})

	}
	logger.Info("ADD USER GROUP",
		zap.Any("return", dataUserGroup),
		zap.Any("code", code),
		zap.Any("name", name),
	)
	//get list user group for table
	dataUserGroupList, totalPage, err := lh.MdUserGroupServices.GetListMdUserGroup(search, currPage, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET USER GROUP",
			zap.Any("error", err),
		)
		fmt.Println(dataUserGroup)
		return err
	}
	//table component
	tableComponent := dev_mdusergroupview.TableUserGroup(dataUserGroupList,
		tableID,
		"/dev/md/usergroupv2/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/usergroupv2/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID")

	//pagination component
	//Pagination
	userGroupPaginationComponent := pagination.PaginationV3(
		totalPage,
		currPage,
		"/dev/md/usergroupv2/changepage",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"#"+paginationID,
		"outerHTML", "", "true",
		dev_mdusergroupview.BeforeRequestContent(),
		dev_mdusergroupview.AfterRequestContent(),
	)

	newForm := dev_mdusergroupview.BodyFormUserGroup(
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupcode",
			Placeholder: "Kode user group",
			ErrorMsg:    codeValidation,
			Type:        "text"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama User Group",
			Name:        "usergroupname",
			Placeholder: "Nama user group",
			ErrorMsg:    nameValidation,
			Type:        "text"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupid",
			Placeholder: "Kode user group",
			Type:        "hidden", ID: "usergroupinputid"},
		dialogAddBodyID, dev_mdusergroupview.JsHideModal("#"+dialogAddID))
	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil insert User group "+name, "success")

	return utils.ViewMulti(c, []templ.Component{
		toastSuccess,
		tableComponent,
		userGroupPaginationComponent,
		newForm})
}
func (lh *MdUserGroupHandler) HandleOpenEditForm(c echo.Context) error {
	id := c.QueryParam("id")
	dialogEditID := c.QueryParam("dialogEditID")
	dialogEditBodyID := c.QueryParam("dialogEditBodyID")

	dataUserGroup, err := lh.MdUserGroupServices.GetMdUserGroupByID(id)
	if err != nil {
		newForm := dev_mdusergroupview.BodyFormUserGroup(
			models.CustomTextFieldv2Prm{
				Label:       "Kode User Group",
				Name:        "usergroupcode",
				Placeholder: "Kode user group",
				Type:        "text", ID: "usergroupinputname"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama User Group",
				Name:        "usergroupname",
				Placeholder: "Nama user group",
				Type:        "text", ID: "usergroupinputname"},
			models.CustomTextFieldv2Prm{
				Label:       "Kode User Group",
				Name:        "usergroupid",
				Placeholder: "Kode user group",
				Type:        "hidden", ID: "usergroupinputid"},
			dialogEditBodyID, dev_mdusergroupview.JsHideModal(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Error", "Get user group error", "danger"), newForm})
	}
	newForm := dev_mdusergroupview.BodyFormUserGroup(
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupcode",
			Placeholder: "Kode user group",
			Value:       dataUserGroup.M_UserGroupCode,
			Type:        "text", ID: "usergroupinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama User Group",
			Name:        "usergroupname",
			Placeholder: "Nama user group",
			Value:       dataUserGroup.M_UserGroupName,
			Type:        "text", ID: "usergroupinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupid",
			Placeholder: "Kode user group",
			Value:       strconv.Itoa(dataUserGroup.M_UserGroupID),
			Type:        "hidden", ID: "usergroupinputid"},

		dialogEditBodyID, dev_mdusergroupview.JsShowModal("#"+dialogEditID))
	return utils.View(c, newForm)
}
func (lh *MdUserGroupHandler) HandleCloseFormEdit(c echo.Context) error {
	dialogEditBodyID := c.FormValue("dialogEditBodyID")
	newForm := dev_mdusergroupview.BodyFormUserGroup(
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupcode",
			Placeholder: "Kode user group",
			Type:        "text", ID: "usergroupinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama User Group",
			Name:        "usergroupname",
			Placeholder: "Nama user group",
			Type:        "text", ID: "usergroupinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupid",
			Placeholder: "Kode user group",
			Type:        "hidden", ID: "usergroupinputid"},
		dialogEditBodyID, dev_mdusergroupview.JsHideModal(""))
	return utils.View(c, newForm)
}
func (lh *MdUserGroupHandler) HandleEditUserGroup(c echo.Context) error {

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

	//prm form addusergroup
	code := c.FormValue("usergroupcode")
	name := c.FormValue("usergroupname")
	id := c.FormValue("usergroupid")
	tableID := c.FormValue("tableID")
	paginationID := c.FormValue("paginationID")
	searchID := c.FormValue("searchID")
	search := c.FormValue("search")
	currPagePrm := c.FormValue("currpage" + paginationID)
	// dialogAddBodyID := c.FormValue("dialogAddBodyID")
	dialogEditID := c.FormValue("dialogEditID")
	dialogEditBodyID := c.FormValue("dialogEditBodyID")
	dialogDeleteBodyID := c.FormValue("dialogDeleteBodyID")

	currPage, err := strconv.Atoi(currPagePrm)
	if err != nil {
		return err
	}
	logger.Info("ADD USER GROUP PARAM",
		zap.Any("code", code),
		zap.Any("nama", name),
		zap.Any("tableid", tableID),
		zap.Any("search", search),
		zap.Any("currpage", currPage),
	)
	formValidation := ""
	codeValidation := ""
	nameValidation := ""
	if strings.TrimSpace(code) == "" {
		formValidation = "Code user group tidak boleh kosong"
		codeValidation = "Code user group tidak boleh kosong"
		nameValidation = ""
	}
	if strings.TrimSpace(name) == "" {
		formValidation = "Nama user group tidak boleh kosong"
		codeValidation = ""
		nameValidation = "Nama user group tidak boleh kosong"
	}
	if strings.TrimSpace(code) == "" && strings.TrimSpace(name) == "" {
		formValidation = "Code dan nama user group tidak boleh kosong"
		codeValidation = "Code user group tidak boleh kosong"
		nameValidation = "Nama user group tidak boleh kosong"

	}

	if formValidation != "" || codeValidation != "" || nameValidation != "" {
		newForm := dev_mdusergroupview.BodyFormUserGroup(
			models.CustomTextFieldv2Prm{
				Label:       "Kode User Group",
				Name:        "usergroupcode",
				Placeholder: "Kode user group",
				ErrorMsg:    codeValidation,
				Value:       code,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama User Group",
				Name:        "usergroupname",
				Placeholder: "Nama user group",
				ErrorMsg:    nameValidation,
				Value:       name,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Kode User Group",
				Name:        "usergroupid",
				Placeholder: "Kode user group",
				Type:        "hidden", ID: "usergroupinputid"},
			dialogEditBodyID, dev_mdusergroupview.JsHideModal(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Warning", formValidation, "warning"), newForm})
	}
	//edit user group service
	dataUserGroup, err := lh.MdUserGroupServices.EditMdUsergroup(id, code, name)
	if err != nil {
		logger.Info("Error",
			zap.Any("Error", err),
			zap.Any("code", code),
			zap.Any("name", name),
		)
		newForm := dev_mdusergroupview.BodyFormUserGroup(
			models.CustomTextFieldv2Prm{
				Label:       "Kode User Group",
				Name:        "usergroupcode",
				Placeholder: "Kode user group",
				ErrorMsg:    codeValidation,
				Value:       code,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama User Group",
				Name:        "usergroupname",
				Placeholder: "Nama user group",
				ErrorMsg:    nameValidation,
				Value:       name,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Kode User Group",
				Name:        "usergroupid",
				Placeholder: "Kode user group",
				Type:        "hidden", ID: "usergroupinputid"},
			dialogEditBodyID, dev_mdusergroupview.JsHideModal(""))
		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Error", err.Error(), "danger"),
			newForm})

	}
	logger.Info("ADD USER GROUP",
		zap.Any("return", dataUserGroup),
		zap.Any("code", code),
		zap.Any("name", name),
	)
	//get list user group for table
	dataUserGroupList, totalPage, err := lh.MdUserGroupServices.GetListMdUserGroup(search, currPage, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET USER GROUP",
			zap.Any("error", err),
		)
		fmt.Println(dataUserGroup)
		return err
	}
	//table component
	tableComponent := dev_mdusergroupview.TableUserGroup(dataUserGroupList,
		tableID,
		"/dev/md/usergroupv2/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/usergroupv2/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID")

	//pagination component
	//Pagination
	userGroupPaginationComponent := pagination.PaginationV3(
		totalPage,
		currPage,
		"/dev/md/usergroupv2/changepage",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"#"+paginationID,
		"outerHTML", "", "true",
		dev_mdusergroupview.BeforeRequestContent(),
		dev_mdusergroupview.AfterRequestContent(),
	)
	var retVal []templ.Component
	newForm := dev_mdusergroupview.BodyFormUserGroup(
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupcode",
			Placeholder: "Kode user group",
			ErrorMsg:    codeValidation,
			Type:        "text"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama User Group",
			Name:        "usergroupname",
			Placeholder: "Nama user group",
			ErrorMsg:    nameValidation,
			Type:        "text"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode User Group",
			Name:        "usergroupid",
			Placeholder: "Kode user group",
			Type:        "hidden", ID: "usergroupinputid"},
		dialogEditBodyID, dev_mdusergroupview.JsHideModal("#"+dialogEditID))
	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil edit User group "+name, "success")
	retVal = append(retVal, toastSuccess)
	retVal = append(retVal, tableComponent)
	retVal = append(retVal, userGroupPaginationComponent)
	retVal = append(retVal, newForm)
	return utils.ViewMulti(c, retVal)
}
func (lh *MdUserGroupHandler) HandleOpenDeleteForm(c echo.Context) error {
	id := c.QueryParam("id")
	dialogDeleteID := c.QueryParam("dialogDeleteID")
	dialogDeleteBodyID := c.QueryParam("dialogDeleteBodyID")

	dataUserGroup, err := lh.MdUserGroupServices.GetMdUserGroupByID(id)
	if err != nil {
		newForm := dev_mdusergroupview.DeleteConfirmationBody(
			models.CustomTextFieldv2Prm{Name: "usergroupid", Type: "hidden", Value: strconv.Itoa(dataUserGroup.M_UserGroupID)},
			dialogDeleteBodyID,
			"Apakah anda yakin menghapus user group berikut ?",
			[]string{"Kode", "User Group"},
			[]string{"", ""},
			dev_mdusergroupview.JsShowModal(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Error", "Get user group error", "danger"), newForm})
	}
	newForm := dev_mdusergroupview.DeleteConfirmationBody(
		models.CustomTextFieldv2Prm{Name: "usergroupid", Type: "hidden", Value: strconv.Itoa(dataUserGroup.M_UserGroupID)},
		dialogDeleteBodyID,
		"Apakah anda yakin menghapus user group berikut ?",
		[]string{"Kode", "User Group"},
		[]string{dataUserGroup.M_UserGroupCode, dataUserGroup.M_UserGroupName},
		dev_mdusergroupview.JsShowModal("#"+dialogDeleteID))
	return utils.View(c, newForm)
}
func (lh *MdUserGroupHandler) HandleCloseFormDelete(c echo.Context) error {
	dialogDeleteBodyID := c.FormValue("dialogDeleteBodyID")
	newForm := dev_mdusergroupview.DeleteConfirmationBody(
		models.CustomTextFieldv2Prm{Name: "usergroupid", Type: "hidden", Value: ""},
		dialogDeleteBodyID,
		"Apakah anda yakin menghapus user group berikut ?",
		[]string{"Kode", "User Group"},
		[]string{"", ""},
		dev_mdusergroupview.JsShowModal(""))
	return utils.View(c, newForm)
}
func (lh *MdUserGroupHandler) HandleDeleteUserGroup(c echo.Context) error {

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

	//prm form addusergroup

	id := c.FormValue("usergroupid")
	tableID := c.FormValue("tableID")
	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")
	dialogEditBodyID := c.FormValue("dialogEditBodyID")
	dialogDeleteBodyID := c.FormValue("dialogDeleteBodyID")

	currPage, err := strconv.Atoi(currPagePrm)
	if err != nil {
		return err
	}
	logger.Info("ADD USER GROUP PARAM",

		zap.Any("tableid", tableID),
		zap.Any("search", search),
		zap.Any("currpage", currPage),
	)

	//edit user group service
	dataUserGroup, err := lh.MdUserGroupServices.DeleteMdUserGroup(id)
	if err != nil {
		logger.Info("Error",
			zap.Any("Error", err),
		)
		newForm := dev_mdusergroupview.DeleteConfirmationBody(
			models.CustomTextFieldv2Prm{Name: "usergroupid", Type: "hidden", Value: ""},
			dialogDeleteBodyID,
			"Apakah anda yakin menghapus user group berikut ?",
			[]string{"Kode", "User Group"},
			[]string{"", ""},
			dev_mdusergroupview.JsShowModal(""))
		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Error", err.Error(), "danger"),
			newForm})

	}
	logger.Info("ADD USER GROUP",
		zap.Any("return", dataUserGroup),
	)
	//get list user group for table
	dataUserGroupList, totalPage, err := lh.MdUserGroupServices.GetListMdUserGroup(search, currPage, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET USER GROUP",
			zap.Any("error", err),
		)
		fmt.Println(dataUserGroup)
		return err
	}
	if len(dataUserGroupList) == 0 && totalPage > 1 {
		currPage = currPage - 1
		dataUserGroupList, _, err = lh.MdUserGroupServices.GetListMdUserGroup(search, currPage, 5)
		if err != nil {
			defer logger.Sync()
			logger.Info("ERROR GET USER GROUP",
				zap.Any("error", err),
			)
			return err
		}
	}
	//table component
	tableComponent := dev_mdusergroupview.TableUserGroup(dataUserGroupList,
		tableID,
		"/dev/md/usergroupv2/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/usergroupv2/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID")

	//pagination component
	//Pagination
	userGroupPaginationComponent := pagination.PaginationV3(
		totalPage,
		currPage,
		"/dev/md/usergroupv2/changepage",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"#"+paginationID,
		"outerHTML", "", "true",
		dev_mdusergroupview.BeforeRequestContent(),
		dev_mdusergroupview.AfterRequestContent(),
	)
	var retVal []templ.Component
	newForm := dev_mdusergroupview.DeleteConfirmationBody(
		models.CustomTextFieldv2Prm{Name: "usergroupid", Type: "hidden", Value: ""},
		dialogDeleteBodyID,
		"Apakah anda yakin menghapus user group berikut ?",
		[]string{"Kode", "User Group"},
		[]string{"", ""},
		dev_mdusergroupview.JsHideModal("#"+dialogDeleteID))
	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil delete User group ", "success")
	retVal = append(retVal, toastSuccess)
	retVal = append(retVal, tableComponent)
	retVal = append(retVal, userGroupPaginationComponent)
	retVal = append(retVal, newForm)
	return utils.ViewMulti(c, retVal)
}
