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_mdnatunitview "cpone/views/dev/mdnatunit"
	"fmt"
	"strconv"
	"strings"

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

type MdNatUnitServices interface {
	GetListMdNatUnit(search string, currentPage int, rowPerPage int) ([]models.NatUnit, int, error)
	GetMdNatUnitBreadCrumb(title string) (models.BreadCrumbV1, error)
	AddMdNatUnit(code string, name string) (models.NatUnit, error)
	GetMdNatUnitByID(id string) (models.NatUnit, error)
	EditMdNatUnit(id string, code string, name string) (models.NatUnit, error)
	DeleteMdNatUnit(id string) (models.NatUnit, error)
}

func NeWMdNatUnitHandler(nu MdNatUnitServices) *MdNatUnitHandler {
	return &MdNatUnitHandler{
		MdNatUnitServices: nu,
	}
}

type MdNatUnitHandler struct {
	MdNatUnitServices MdNatUnitServices
}

func (nh *MdNatUnitHandler) HandleShowMdNatUnitScreen(c echo.Context) error {
	logger, _ := zap.NewProduction()

	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 Unit"

	dataBreadCrumb, err := nh.MdNatUnitServices.GetMdNatUnitBreadCrumb(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
	dataNatUnit, totalPage, err := nh.MdNatUnitServices.GetListMdNatUnit("", 1, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET NAT UNTI",
			zap.Any("error", err),
		)
		fmt.Println(dataNatUnit)
		return err
	}

	tableComponent := dev_mdnatunitview.TableNatUnit(dataNatUnit,
		tableID,
		"/dev/md/natunit/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/natunit/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
	)

	//filter nat unit component
	natUnitFilterComponent := customtextfieldsearch.MainCustomTextFieldSearchV3(searchID,
		"search",
		"Cari Kode/Nama",
		"text",
		"/dev/md/natunit/filter",
		"input changed delay:500ms, search",
		"#"+paginationID, "#loadingcontent", "", "outerHTML", "#tableID, #paginationID, #searchID, #dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		dev_mdnatunitview.BeforeRequestContent(),
		dev_mdnatunitview.AfterRequestContent())

	//Pagination
	natUnitPaginationComponent := pagination.PaginationV3(
		totalPage,
		1,
		"/dev/md/natunit/changepage",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"#"+paginationID,
		"outerHTML", "", "",
		dev_mdnatunitview.BeforeRequestContent(),
		dev_mdnatunitview.AfterRequestContent(),
	)

	// modal add form
	dialogAddBodyCmp := dev_mdnatunitview.BodyFormNatUnit(
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitcode",
			Placeholder: "Kode Unit",
			Type:        "text", ID: "natunitinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Unit",
			Name:        "natunitname",
			Placeholder: "Nama Unit",
			Type:        "text", ID: "natunitinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitid",
			Placeholder: "Kode Unit",
			Type:        "hidden", ID: "natunitinputid"},
		dialogAddBodyID, dev_mdnatunitview.JsHideModal(""),
	)

	modalActionAddCmp := dev_mdnatunitview.ActionFormNatUnit(
		"/dev/md/natunit/closeaddform",
		"#"+dialogAddBodyID,
		"outerHTML",
		"#"+dialogAddID,
	)

	btnCloseModalAdd := dev_mdnatunitview.BtnCloseFormNatUnit(
		"/dev/md/natunit/closeaddform",
		"#"+dialogAddBodyID,
		"outerHTML",
		"#"+dialogAddID,
	)

	modalAddFormComponent := dev_mdnatunitview.NatUnitForm(
		models.NatUnitFormComponent{
			IDComponent:  "formnatunit",
			Link:         "/dev/md/natunit/add",
			HxTarget:     "#" + dialogAddBodyID,
			HxSwap:       "outerHTML",
			HxInclude:    "#tableID, #paginationID, #searchID, #dialogAddBodyID, #dialogAddID, #dialogDeleteBodyID, #dialogEditBodyID, #" + searchID + ", #currpage" + paginationID,
			ModalID:      dialogAddID,
			ModalTitle:   "New - Unit",
			DialogBody:   dialogAddBodyCmp,
			DialogAction: modalActionAddCmp,
			ButtonCLose:  btnCloseModalAdd,
		},
	)

	// modal edit form
	dialogEditBodyCmp := dev_mdnatunitview.BodyFormNatUnit(
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitcode",
			Placeholder: "Kode Unit",
			Type:        "text", ID: "natunitinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Unit",
			Name:        "natunitname",
			Placeholder: "Nama Unit",
			Type:        "text", ID: "natunitinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitid",
			Placeholder: "Kode Unit",
			Type:        "hidden", ID: "natunitinputid"},
		dialogEditBodyID, dev_mdnatunitview.JsHideModal(""),
	)

	modalActionEditCmp := dev_mdnatunitview.ActionFormNatUnit(
		"/dev/md/natunit/closeeditform",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#"+dialogEditID,
	)

	btnCloseModalEdit := dev_mdnatunitview.BtnCloseFormNatUnit(
		"/dev/md/natunit/closeeditform",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#"+dialogEditID,
	)

	modalEditFormComponent := dev_mdnatunitview.NatUnitForm(
		models.NatUnitFormComponent{
			IDComponent:  "formnatunit",
			Link:         "dev/md/natunit/edit",
			HxTarget:     "#" + dialogEditBodyID,
			HxSwap:       "outerHTML",
			HxInclude:    "#tableID, #paginationID, #searchID, #dialogDeleteBodyID, #dialogEditBodyID, #dialogEditID, #" + searchID + ", #currpage" + paginationID,
			ModalID:      dialogEditID,
			ModalTitle:   "New - Unit",
			DialogBody:   dialogEditBodyCmp,
			DialogAction: modalActionEditCmp,
			ButtonCLose:  btnCloseModalEdit,
		},
	)

	// modal delete form
	dialogDeleteBodyCmp := dev_mdnatunitview.BodyFormNatUnit(
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitcode",
			Placeholder: "Kode Unit",
			Type:        "text", ID: "natunitinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Unit",
			Name:        "natunitname",
			Placeholder: "Nama Unit",
			Type:        "text", ID: "natunitinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitid",
			Placeholder: "Kode Unit",
			Type:        "hidden", ID: "natunitinputid"},
		dialogDeleteBodyID, dev_mdnatunitview.JsHideModal(""))
	modalActionDeleteCmp := dev_mdnatunitview.ActionFormNatUnit(
		"/dev/md/natunit/closedeleteform",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#"+dialogDeleteID)
	btnCloaseModalDelete := dev_mdnatunitview.BtnCloseFormNatUnit(
		"/dev/md/natunit/closedeleteform",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#"+dialogDeleteID)
	modalDeleteFormComponent := dev_mdnatunitview.NatUnitForm(
		models.NatUnitFormComponent{
			IDComponent:  "formnatunit",
			Link:         "/dev/md/natunit/delete",
			HxTarget:     "#" + dialogDeleteBodyID,
			HxSwap:       "outerHTML",
			HxInclude:    "#tableID, #paginationID, #searchID, #dialogDeleteBodyID, #dialogDeleteID, #dialogEditBodyID, #" + searchID + ", #currpage" + paginationID,
			ModalID:      dialogDeleteID,
			ModalTitle:   "New - Unit",
			DialogBody:   dialogDeleteBodyCmp,
			DialogAction: modalActionDeleteCmp,
			ButtonCLose:  btnCloaseModalDelete,
		},
	)

	// content, css, js
	content := dev_mdnatunitview.MdNatUnitScreen(
		tableID,
		paginationID,
		searchID,
		dialogAddID,
		dialogAddBodyID,
		dialogEditID,
		dialogEditBodyID,
		dialogDeleteID,
		dialogDeleteBodyID,
		breadcrumbComponent,
		tableComponent,
		natUnitFilterComponent,
		natUnitPaginationComponent,
		modalAddFormComponent,
		modalEditFormComponent,
		modalDeleteFormComponent,
	)
	css := dev_mdnatunitview.CssMdNatUnit()
	js := dev_mdnatunitview.JsMdNatUnit()

	view := dev_mdnatunitview.ShowMdNatUnitScreen(
		title,
		content,
		css,
		js,
		navbarmenuComponent,
		navbaruserComponent,
		sidebaruserprofileComponent,
	)

	return utils.View(c, view)
}

func (nh *MdNatUnitHandler) HandleFilterMdNatUnit(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()

	dataNatUnit, totalpage, err := nh.MdNatUnitServices.GetListMdNatUnit(search, 1, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET NAT UNTI",
			zap.Any("error", err),
		)
		fmt.Println(dataNatUnit)
		return err
	}
	tableComponent := dev_mdnatunitview.TableNatUnit(dataNatUnit,
		tableID,
		"/dev/md/natunit/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/natunit/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID")
	natUnitPaginationComponent := pagination.PaginationV3(
		totalpage,
		1,
		"/dev/md/natunit/changepage",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"#"+paginationID,
		"outerHTML", "", "",
		dev_mdnatunitview.BeforeRequestContent(),
		dev_mdnatunitview.AfterRequestContent(),
	)
	retval = append(retval, tableComponent)
	retval = append(retval, natUnitPaginationComponent)
	return utils.ViewMulti(c, retval)
}

func (nh *MdNatUnitHandler) HandlerChangePageMdNatUnit(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
	}

	dataNatUnit, totalpage, err := nh.MdNatUnitServices.GetListMdNatUnit(search, page, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET NAT UNIT",
			zap.Any("error", err),
		)
		fmt.Println(dataNatUnit)
		return err
	}
	tableComponent := dev_mdnatunitview.TableNatUnit(dataNatUnit,
		tableID,
		"/dev/md/natunit/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/natunit/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
	)
	natUnitPaginationComponent := pagination.PaginationV3(
		totalpage,
		page,
		"/dev/md/natunit/changepage",
		paginationID,
		"#tableID, #searchID, #"+searchID+", #paginationID, #dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"#"+paginationID,
		"outerHTML", "", "",
		dev_mdnatunitview.BeforeRequestContent(),
		dev_mdnatunitview.AfterRequestContent(),
	)
	retval = append(retval, tableComponent)
	retval = append(retval, natUnitPaginationComponent)
	return utils.ViewMulti(c, retval)
}

func (nh *MdNatUnitHandler) HandlerCloseFormAdd(c echo.Context) error {
	dialogAddBodyID := c.FormValue("dialogAddBodyID")
	newForm := dev_mdnatunitview.BodyFormNatUnit(
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitcode",
			Placeholder: "Kode Unit",
			Type:        "text", ID: "natunitinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Unit",
			Name:        "natunitname",
			Placeholder: "Nama Unit",
			Type:        "text", ID: "natunitinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitid",
			Placeholder: "Kode Unit",
			Type:        "hidden", ID: "natunitinputid"},
		dialogAddBodyID, dev_mdnatunitview.JsHideModal(""))
	return utils.View(c, newForm)
}

func (nh *MdNatUnitHandler) HandlerAddNatUnit(c echo.Context) error {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	// prm form addnatunit
	code := c.FormValue("natunitcode")
	name := c.FormValue("natunitname")
	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 UNIT 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 unit tidak boleh kosong"
		codeValidation = "Code unit tidak boleh kosong"
		nameValidation = ""
	}
	if strings.TrimSpace(name) == "" {
		formValidation = "Nama unit tidak boleh kosong"
		codeValidation = ""
		nameValidation = "Nama unit tidak boleh kosong"
	}
	if strings.TrimSpace(code) == "" && strings.TrimSpace(name) == "" {
		formValidation = "Code dan nama unit tidak boleh kosong"
		codeValidation = "Code unit tidak boleh kosong"
		nameValidation = "Nama unit tidak boleh kosong"

	}

	if formValidation != "" || codeValidation != "" || nameValidation != "" {
		newForm := dev_mdnatunitview.BodyFormNatUnit(
			models.CustomTextFieldv2Prm{
				Label:       "Kode Unit",
				Name:        "natunitcode",
				Placeholder: "Kode Unit",
				ErrorMsg:    codeValidation,
				Value:       code,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama Unit",
				Name:        "natunitname",
				Placeholder: "Nama Unit",
				ErrorMsg:    nameValidation,
				Value:       name,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Kode Unit",
				Name:        "natunitid",
				Placeholder: "Kode Unit",
				Type:        "hidden", ID: "natunitinputid"},
			dialogAddBodyID, dev_mdnatunitview.JsHideModal(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Warning", formValidation, "warning"), newForm})
	}

	// add nat unit service
	dataNatUnit, err := nh.MdNatUnitServices.AddMdNatUnit(code, name)
	if err != nil {
		logger.Info("Error",
			zap.Any("Error", err),
			zap.Any("code", code),
			zap.Any("name", name),
		)
		newForm := dev_mdnatunitview.BodyFormNatUnit(
			models.CustomTextFieldv2Prm{
				Label:       "Kode Unit",
				Name:        "natunitcode",
				Placeholder: "Kode Unit",
				ErrorMsg:    codeValidation,
				Value:       code,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama Unit",
				Name:        "natunitname",
				Placeholder: "Nama Unit",
				ErrorMsg:    nameValidation,
				Value:       name,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Kode Unit",
				Name:        "natunitid",
				Placeholder: "Kode Unit",
				Type:        "hidden", ID: "natunitinputid"},
			dialogAddBodyID, dev_mdnatunitview.JsHideModal(""))
		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Error", err.Error(), "danger"),
			newForm})
	}
	logger.Info("ADD UNIT",
		zap.Any("return", dataNatUnit),
		zap.Any("code", code),
		zap.Any("name", name),
	)

	// get list nat unit for table
	dataNatUnitList, totalPage, err := nh.MdNatUnitServices.GetListMdNatUnit(search, currPage, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET UNIT",
			zap.Any("error", err),
		)
		fmt.Println(dataNatUnit)
		return err
	}

	// table component
	tableComponent := dev_mdnatunitview.TableNatUnit(
		dataNatUnitList,
		tableID,
		"/dev/md/natunit/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/natunit/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
	)

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

	newForm := dev_mdnatunitview.BodyFormNatUnit(
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitcode",
			Placeholder: "Kode Unit",
			ErrorMsg:    codeValidation,
			Type:        "text"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Unit",
			Name:        "natunitname",
			Placeholder: "Nama Unit",
			ErrorMsg:    nameValidation,
			Type:        "text"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitid",
			Placeholder: "Kode Unit",
			Type:        "hidden", ID: "natunitinputid"},
		dialogAddBodyID, dev_mdnatunitview.JsHideModal("#"+dialogAddID))
	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil insert unit "+name, "success")

	return utils.ViewMulti(c, []templ.Component{
		toastSuccess,
		tableComponent,
		natUnitPaginationComponent,
		newForm,
	})
}

func (nh *MdNatUnitHandler) HandlerOpenEditForm(c echo.Context) error {
	id := c.QueryParam("id")
	dialogEditID := c.QueryParam("dialogEditID")
	dialogEditBodyID := c.QueryParam("dialogEditBodyID")

	dataNatUnit, err := nh.MdNatUnitServices.GetMdNatUnitByID(id)
	if err != nil {
		newForm := dev_mdnatunitview.BodyFormNatUnit(
			models.CustomTextFieldv2Prm{
				Label:       "Kode Unit",
				Name:        "natunitcode",
				Placeholder: "Kode Unit",
				Type:        "text", ID: "natunitinputname"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama Unit",
				Name:        "natunitname",
				Placeholder: "Nama Unit",
				Type:        "text", ID: "natunitinputname"},
			models.CustomTextFieldv2Prm{
				Label:       "Kode Unit",
				Name:        "natunitid",
				Placeholder: "Kode Unit",
				Type:        "hidden", ID: "natunitinputid"},
			dialogEditBodyID, dev_mdnatunitview.JsHideModal(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Error", "Get unit error", "danger"), newForm})
	}
	newForm := dev_mdnatunitview.BodyFormNatUnit(
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitcode",
			Placeholder: "Kode Unit",
			Value:       dataNatUnit.Nat_UnitCode,
			Type:        "text",
			ID:          "natunitinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitname",
			Placeholder: "Kode Unit",
			Value:       dataNatUnit.Nat_UnitName,
			Type:        "text",
			ID:          "natunitinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitid",
			Placeholder: "Kode Unit",
			Value:       strconv.Itoa(dataNatUnit.Nat_UnitID),
			Type:        "hidden",
			ID:          "natunitinputid"},

		dialogEditBodyID, dev_mdnatunitview.JsShowModal("#"+dialogEditID))

	return utils.View(c, newForm)
}

func (nh *MdNatUnitHandler) HandlerCloseFormEdit(c echo.Context) error {
	dialogEditBodyID := c.FormValue("dialogEditBodyID")
	newForm := dev_mdnatunitview.BodyFormNatUnit(
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitcode",
			Placeholder: "Kode Unit",
			Type:        "text", ID: "natunitinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Unit",
			Name:        "natunitname",
			Placeholder: "Nama Unit",
			Type:        "text", ID: "natunitinputname"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitid",
			Placeholder: "Kode Unit",
			Type:        "hidden", ID: "natunitinputid"},
		dialogEditBodyID, dev_mdnatunitview.JsHideModal(""))
	return utils.View(c, newForm)
}

func (nh *MdNatUnitHandler) HandlerEditNatUnit(c echo.Context) error {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	// prm form addnatunit
	code := c.FormValue("natunitcode")
	name := c.FormValue("natunitname")
	id := c.FormValue("natunitid")
	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 NAT UNIT 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 unit tidak boleh kosong"
		codeValidation = "Code unit tidak boleh kosong"
		nameValidation = ""
	}
	if strings.TrimSpace(name) == "" {
		formValidation = "Nama unit tidak boleh kosong"
		codeValidation = ""
		nameValidation = "Nama unit tidak boleh kosong"
	}
	if strings.TrimSpace(code) == "" && strings.TrimSpace(name) == "" {
		formValidation = "Code dan nama unit tidak boleh kosong"
		codeValidation = "Code unit tidak boleh kosong"
		nameValidation = "Nama unit tidak boleh kosong"

	}

	if formValidation != "" || codeValidation != "" || nameValidation != "" {
		newForm := dev_mdnatunitview.BodyFormNatUnit(
			models.CustomTextFieldv2Prm{
				Label:       "Kode Unit",
				Name:        "natunitcode",
				Placeholder: "Kode Unit",
				ErrorMsg:    codeValidation,
				Value:       code,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama Unit",
				Name:        "natunitname",
				Placeholder: "Nama Unit",
				ErrorMsg:    nameValidation,
				Value:       name,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Kode Unit",
				Name:        "natunitid",
				Placeholder: "Kode Unit",
				Type:        "hidden", ID: "natunitinputid"},
			dialogEditBodyID, dev_mdnatunitview.JsHideModal(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Warning", formValidation, "warning"), newForm})
	}

	// edit nat unit service
	dataNatUnit, err := nh.MdNatUnitServices.EditMdNatUnit(id, code, name)
	if err != nil {
		logger.Info("Error",
			zap.Any("Error", err),
			zap.Any("code", code),
			zap.Any("name", name),
		)
		newForm := dev_mdnatunitview.BodyFormNatUnit(
			models.CustomTextFieldv2Prm{
				Label:       "Kode Unit",
				Name:        "natunitcode",
				Placeholder: "Kode Unit",
				ErrorMsg:    codeValidation,
				Value:       code,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Nama Unit",
				Name:        "natunitname",
				Placeholder: "Nama Unit",
				ErrorMsg:    nameValidation,
				Value:       name,
				Type:        "text"},
			models.CustomTextFieldv2Prm{
				Label:       "Kode Unit",
				Name:        "natunitid",
				Placeholder: "Kode Unit",
				Type:        "hidden", ID: "natunitinputid"},
			dialogEditBodyID, dev_mdnatunitview.JsHideModal(""))
		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Error", err.Error(), "danger"), newForm})
	}

	logger.Info("ADD NAT UNIT",
		zap.Any("return", dataNatUnit),
		zap.Any("code", code),
		zap.Any("name", name),
	)

	// get list nat unit for table
	dataNatUnitList, totalPage, err := nh.MdNatUnitServices.GetListMdNatUnit(search, currPage, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET NAT UNIT",
			zap.Any("error", err),
		)
		fmt.Println(dataNatUnit)
		return err
	}

	// table component
	tableComponent := dev_mdnatunitview.TableNatUnit(
		dataNatUnitList,
		tableID,
		"/dev/md/natunit/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/natunit/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
	)

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

	var retVal []templ.Component
	newForm := dev_mdnatunitview.BodyFormNatUnit(
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitcode",
			Placeholder: "Kode Unit",
			ErrorMsg:    codeValidation,
			Type:        "text"},
		models.CustomTextFieldv2Prm{
			Label:       "Nama Unit",
			Name:        "natunitname",
			Placeholder: "Nama Unit",
			ErrorMsg:    nameValidation,
			Type:        "text"},
		models.CustomTextFieldv2Prm{
			Label:       "Kode Unit",
			Name:        "natunitid",
			Placeholder: "Kode Unit",
			Type:        "hidden", ID: "natunitinputid"},
		dialogEditBodyID, dev_mdnatunitview.JsHideModal("#"+dialogEditID))
	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil edit Unit "+name, "success")
	retVal = append(retVal, toastSuccess)
	retVal = append(retVal, tableComponent)
	retVal = append(retVal, natUnitPaginationComponent)
	retVal = append(retVal, newForm)
	return utils.ViewMulti(c, retVal)
}

func (nh *MdNatUnitHandler) HandlerOpenDeleteForm(c echo.Context) error {
	id := c.QueryParam("id")
	dialogDeleteID := c.QueryParam("dialogDeleteID")
	dialogDeleteBodyID := c.QueryParam("dialogDeleteBodyID")

	dataNatUnit, err := nh.MdNatUnitServices.GetMdNatUnitByID(id)
	if err != nil {
		newForm := dev_mdnatunitview.DeleteConfirmationBody(
			models.CustomTextFieldv2Prm{Name: "natunitid", Type: "hidden", Value: strconv.Itoa(dataNatUnit.Nat_UnitID)},
			dialogDeleteBodyID,
			"Apakah anda yakin menghapus unit berikut ?",
			[]string{"Kode", "Unit"},
			[]string{"", ""},
			dev_mdnatunitview.JsShowModal(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Error", "Get unit error", "danger"), newForm})
	}
	newForm := dev_mdnatunitview.DeleteConfirmationBody(
		models.CustomTextFieldv2Prm{Name: "natunitid", Type: "hidden", Value: strconv.Itoa(dataNatUnit.Nat_UnitID)},
		dialogDeleteBodyID,
		"Apakah anda yakin menghapus unit berikut ?",
		[]string{"Kode", "Unit"},
		[]string{dataNatUnit.Nat_UnitCode, dataNatUnit.Nat_UnitName},
		dev_mdnatunitview.JsShowModal("#"+dialogDeleteID))
	return utils.View(c, newForm)
}

func (nh *MdNatUnitHandler) HandlerCloseFormDelete(c echo.Context) error {
	dialogDeleteBodyID := c.FormValue("dialogDeleteBodyID")
	newForm := dev_mdnatunitview.DeleteConfirmationBody(
		models.CustomTextFieldv2Prm{Name: "natunitid", Type: "hidden", Value: ""},
		dialogDeleteBodyID,
		"Apakah anda yakin menghapus unit berikut ?",
		[]string{"Kode", "Unit"},
		[]string{"", ""},
		dev_mdnatunitview.JsShowModal(""))
	return utils.View(c, newForm)
}

func (nh *MdNatUnitHandler) HandlerDeleteNatUnit(c echo.Context) error {
	logger, _ := zap.NewProduction()
	defer logger.Sync()

	// prm form addnatunit
	id := c.FormValue("natunitid")
	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 NAT UNIT PARAM",

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

	// edit nat unit service
	dataNatUnit, err := nh.MdNatUnitServices.DeleteMdNatUnit(id)
	if err != nil {
		logger.Info("Error",
			zap.Any("Error", err),
		)
		newForm := dev_mdnatunitview.DeleteConfirmationBody(
			models.CustomTextFieldv2Prm{Name: "natunitid", Type: "hidden", Value: ""},
			dialogDeleteBodyID,
			"Apakah anda yakin menghapus unit berikut ?",
			[]string{"Kode", "Unit"},
			[]string{"", ""},
			dev_mdnatunitview.JsShowModal(""))
		return utils.ViewMulti(c, []templ.Component{
			customtoastv2.CustomToastV2Show("Error", err.Error(), "danger"),
			newForm})
	}
	logger.Info("ADD Unit",
		zap.Any("return", dataNatUnit),
	)

	// get list nat unit fo table
	dataNatUnitList, totalPage, err := nh.MdNatUnitServices.GetListMdNatUnit(search, currPage, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET Unit",
			zap.Any("error", err),
		)
		fmt.Println(dataNatUnit)
		return err
	}
	if len(dataNatUnitList) == 0 && totalPage > 1 {
		currPage = currPage - 1
		dataNatUnitList, _, err = nh.MdNatUnitServices.GetListMdNatUnit(search, currPage, 5)
		if err != nil {
			defer logger.Sync()
			logger.Info("ERROR GET UNIT",
				zap.Any("error", err),
			)
			return err
		}
	}

	// table component
	tableComponent := dev_mdnatunitview.TableNatUnit(
		dataNatUnitList,
		tableID,
		"/dev/md/natunit/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/natunit/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
	)

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

	var retVal []templ.Component
	newForm := dev_mdnatunitview.DeleteConfirmationBody(
		models.CustomTextFieldv2Prm{Name: "natunitid", Type: "hidden", Value: ""},
		dialogDeleteBodyID,
		"Apakah anda yakin menghapus unit berikut ?",
		[]string{"Kode", "Unit"},
		[]string{"", ""},
		dev_mdnatunitview.JsHideModal("#"+dialogDeleteID))
	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil delete Unit ", "success")
	retVal = append(retVal, toastSuccess)
	retVal = append(retVal, tableComponent)
	retVal = append(retVal, natUnitPaginationComponent)
	retVal = append(retVal, newForm)
	return utils.ViewMulti(c, retVal)
}
