package dev_handlers

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

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

type MdBahanServices interface {
	GetListMdBahan(
		search string,
		stationID string,
		currentPage int,
		rowPerPage int) ([]models.Bahan, int, error)
	GetMdBahanBreadCrumb(title string) (models.BreadCrumbV1, error)
	GetListSampleStation(isFilter bool) ([]models.TmpSampleStation, error)
	AddMdBahan(code string, name string, stationid string) (bool, error)
	EditMdBahan(id string, code string, name string, stationid string) (bool, error)
	GetMdBahanByID(id string) (models.Bahan, error)
	DeleteMdBahan(id string) (bool, error)
}

func NewMdBahanHandler(us MdBahanServices) *MdBahanHandler {
	return &MdBahanHandler{
		MdBahanServices: us,
	}
}

type MdBahanHandler struct {
	MdBahanServices MdBahanServices
}

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

	dataMenu, err := services.GetMenu()
	paginationID := utils.GenerateRandomID("paginationID")
	modalAddID := utils.GenerateRandomID("modalAdd")
	modalAddBodyID := utils.GenerateRandomID("modalAddbody")
	modalEditID := utils.GenerateRandomID("modalEdit")
	modalEditBodyID := utils.GenerateRandomID("modalEditbody")
	modalDeleteID := utils.GenerateRandomID("modalDelete")
	modalDeleteBodyID := utils.GenerateRandomID("modalDeletebody")
	tableBahanID := utils.GenerateRandomID("BahanID")

	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR BREADCRUMB DEV",
			zap.Any("error", err),
		)
		return err
	}

	//get user login
	dataUser, err := services.GetUserLogin()

	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR BREADCRUMB DEV",
			zap.Any("error", err),
		)
		return err
	}
	dataBreadCrumb, err := lh.MdBahanServices.GetMdBahanBreadCrumb("Bahan")

	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR BREADCRUMB DEV",
			zap.Any("error", err),
		)
		return err
	}

	//Breadcrumb component
	breadcrumbComponent := breadcrumadmin.MainBreadcrumbAdminV1(dataBreadCrumb)
	// navbar menu
	navbarmenuComponent := navbarmenu.NavbarMenu(dataMenu)

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

	// sidebar
	sidebaruserprofileComponent := sidebaruserprofile.Navbaruserprofile(dataUser)
	bahanList, totalPage, err := lh.MdBahanServices.GetListMdBahan("", "0", 1, 5)
	defer logger.Sync()
	logger.Info("GET DATA BAHAN",
		zap.Any("data", bahanList),
		zap.Any("err", err),
		zap.Any("totalPage", totalPage),
	)
	stationListFilter, err := lh.MdBahanServices.GetListSampleStation(true)
	defer logger.Sync()
	logger.Info("GET STATION LIST Filter",
		zap.Any("err", err),
	)
	stationList, err := lh.MdBahanServices.GetListSampleStation(false)
	defer logger.Sync()
	logger.Info("GET STATION LIST ",
		zap.Any("err", err),
	)

	//Bahan Table
	// #dialogDeleteBodyID, #dialogDeleteID
	mdBahanTable := dev_mdbahanview.TableBahan(
		bahanList,
		tableBahanID,
		"/dev/md/bahan/openedit",
		"#"+modalEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/bahan/opendelete",
		"#"+modalDeleteBodyID,
		"outerHTML",
		"#dialogDeleteBodyID, #dialogDeleteID, #dialogEditBodyID, #dialogEditID")

	//FIlter table
	mdBahanFilter := dev_mdbahanview.MdBahanFilter(
		"/dev/md/bahan/filter",
		"input changed delay:500ms",
		"#filter-name, #filter-station, #tableID, #paginationID, #dialogAddID, #dialogAddBodyID, #dialogEditID, #dialogEditBodyID, #dialogDeleteBodyID, #dialogDeleteID, #currpage"+paginationID,
		"outerHTML",
		"#"+paginationID,
		stationListFilter,
		stationListFilter[0].T_SampleStationID,
		dev_mdbahanview.BeforeRequestContent(),
		dev_mdbahanview.AfterRequestContent(),
	)

	//Pagination

	mdbahanPagination := pagination.PaginationV3(
		totalPage, 1,
		"/dev/md/bahan/pagination",
		paginationID,
		"#filter-name, #filter-station, #tableID, #paginationID, #dialogAddID, #dialogAddBodyID, #dialogEditID, #dialogEditBodyID,  #dialogDeleteBodyID, #dialogDeleteID, #currpage"+paginationID,
		"#"+paginationID,
		"outerHTML",
		"",
		"",
		dev_mdbahanview.BeforeRequestContent(),
		dev_mdbahanview.AfterRequestContent(),
	)
	//Form Add
	modalAddBody := dev_mdbahanview.BodyFormBahan(
		"",
		models.CustomTextFieldv2Prm{Label: "Kode Bahan",
			Name:        "kodebahan",
			Placeholder: "Kode Bahan",
			Type:        "text"},
		models.CustomTextFieldv2Prm{Label: "Nama Bahan",
			Name:        "namabahan",
			Placeholder: "Nama Bahan",
			Type:        "text"},
		models.CustomDropdownV2Prm{
			Name:     "stationid",
			Label:    "Sample Station",
			ListItem: dev_mdbahanview.ListItemStation(stationList, 0),
		},
		modalAddBodyID,
		dev_mdbahanview.JsShowModal(""),
	)
	modalAddAction := dev_mdbahanview.ActionFormBahan("/dev/md/bahan/closeaddform", "#"+modalAddBodyID, "outerHTML", "#"+modalAddID)
	modalAddBtnCls := dev_mdbahanview.BtnCloseFormBahan("/dev/md/bahan/closeaddform", "#"+modalAddBodyID, "outerHTML", "#"+modalAddID)
	modalAddBahan := dev_mdbahanview.BahanForm(models.BahanFormComponent{
		IDComponent:  "",
		Link:         "/dev/md/bahan/add",
		HxTarget:     "#" + modalAddBodyID,
		HxSwap:       "outerHTML",
		HxInclude:    "#filter-name, #filter-station, #tableID, #paginationID, #dialogAddID, #dialogAddBodyID, #dialogEditID, #dialogEditBodyID, #dialogDeleteBodyID, #dialogDeleteID, #currpage" + paginationID,
		ModalID:      modalAddID,
		ModalTitle:   "New-Bahan",
		DialogBody:   modalAddBody,
		DialogAction: modalAddAction,
		ButtonCLose:  modalAddBtnCls,
	})

	//Form Edit
	modalEditBody := dev_mdbahanview.BodyFormBahan(
		"",
		models.CustomTextFieldv2Prm{Label: "Kode Bahan",
			Name:        "kodebahan",
			Placeholder: "Kode Bahan",
			Type:        "text"},
		models.CustomTextFieldv2Prm{Label: "Nama Bahan",
			Name:        "namabahan",
			Placeholder: "Nama Bahan",
			Type:        "text"},
		models.CustomDropdownV2Prm{
			Name:     "stationid",
			Label:    "Sample Station",
			ListItem: dev_mdbahanview.ListItemStation(stationList, 0),
		},
		modalEditBodyID,
		dev_mdbahanview.JsShowModal(""),
	)
	modalEditAction := dev_mdbahanview.ActionFormBahan("/dev/md/bahan/closeeditform", "#"+modalEditBodyID, "outerHTML", "#"+modalEditID)
	modalEditBtnCls := dev_mdbahanview.BtnCloseFormBahan("/dev/md/bahan/closeeditform", "#"+modalEditBodyID, "outerHTML", "#"+modalEditID)
	modalEditBahan := dev_mdbahanview.BahanForm(models.BahanFormComponent{
		IDComponent:  "",
		Link:         "/dev/md/bahan/edit",
		HxTarget:     "#" + modalEditBodyID,
		HxSwap:       "outerHTML",
		HxInclude:    "#filter-name, #filter-station, #tableID, #paginationID, #dialogEditID, #dialogEditBodyID, #dialogDeleteBodyID, #dialogDeleteID, #currpage" + paginationID,
		ModalID:      modalEditID,
		ModalTitle:   "Edit-Bahan",
		DialogBody:   modalEditBody,
		DialogAction: modalEditAction,
		ButtonCLose:  modalEditBtnCls,
	})

	//Form Delete
	modalDeleteBody := dev_mdbahanview.DeleteConfirmationBody(
		"",
		modalDeleteBodyID,
		"Apakah anda yakin menghapus bahan berikut ?",
		[]string{"Kode", "Nama Bahan", "Sample Station"},
		[]string{"", "", ""}, dev_mdbahanview.JsShowModal(""))
	modalDeleteAction := dev_mdbahanview.ActionFormBahanDelete("/dev/md/bahan/closedeleteform", "#"+modalDeleteBodyID, "outerHTML", "#"+modalDeleteID)
	modalDeleteBtnCls := dev_mdbahanview.BtnCloseFormBahan("/dev/md/bahan/closedeleteform", "#"+modalDeleteBodyID, "outerHTML", "#"+modalDeleteID)
	modalDeleteBahan := dev_mdbahanview.BahanForm(models.BahanFormComponent{
		IDComponent:  "",
		Link:         "/dev/md/bahan/delete",
		HxTarget:     "#" + modalDeleteBodyID,
		HxSwap:       "outerHTML",
		HxInclude:    "#filter-name, #filter-station, #tableID, #paginationID, #dialogEditID, #dialogEditBodyID, #dialogDeleteBodyID, #dialogDeleteID, #currpage" + paginationID,
		ModalID:      modalDeleteID,
		ModalTitle:   "Delete-Bahan",
		DialogBody:   modalDeleteBody,
		DialogAction: modalDeleteAction,
		ButtonCLose:  modalDeleteBtnCls,
	})

	mdBahanScreen := dev_mdbahanview.MdBahanScreen(
		tableBahanID,
		paginationID,
		modalAddID,
		modalAddBodyID,
		modalEditID,
		modalEditBodyID,
		modalDeleteID,
		modalDeleteBodyID,
		breadcrumbComponent,
		mdBahanTable,
		mdBahanFilter,
		mdbahanPagination,
		modalAddBahan,
		modalEditBahan,
		modalDeleteBahan)
	cssMdBahan := dev_mdbahanview.CssMdUserGroup()
	jsMdBahan := dev_mdbahanview.JsMdBahan()

	view := dev_mdbahanview.ShowMdBahanScreen(
		"Md Bahan",
		mdBahanScreen,
		cssMdBahan,
		jsMdBahan,
		navbarmenuComponent,
		navbaruserComponent,
		sidebaruserprofileComponent)
	return utils.View(c, view)
}

func (lh *MdBahanHandler) HandleFilterMdBahan(c echo.Context) error {
	logger, _ := zap.NewProduction()
	var retval []templ.Component
	filtername := c.QueryParam("filtername")
	filterstation := c.QueryParam("filterstation")
	dialogEditBodyID := c.QueryParam("dialogEditBodyID")
	dialogDeleteBodyID := c.QueryParam("dialogDeleteBodyID")
	tableID := c.QueryParam("tableID")
	paginationID := c.QueryParam("paginationID")
	bahanList, totalPage, err := lh.MdBahanServices.GetListMdBahan(filtername, filterstation, 1, 5)
	defer logger.Sync()
	logger.Info("GET DATA BAHAN",
		zap.Any("data", bahanList),
		zap.Any("err", err),
		zap.Any("totalPage", totalPage),
	)
	//Bahan Table

	mdBahanTable := dev_mdbahanview.TableBahan(
		bahanList,
		tableID,
		"/dev/md/bahan/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/bahan/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID")
	//Pagination

	mdbahanPagination := pagination.PaginationV3(
		totalPage, 1,
		"/dev/md/bahan/pagination",
		paginationID,
		"#filter-name, #filter-station, #tableID, #paginationID, #dialogAddID, #dialogAddBodyID, #dialogEditID, #dialogEditBodyID, #dialogDeleteBodyID, #dialogDeleteID, #currpage"+paginationID,
		"#"+paginationID,
		"outerHTML",
		"",
		"",
		dev_mdbahanview.BeforeRequestContent(),
		dev_mdbahanview.AfterRequestContent(),
	)
	retval = append(retval, mdBahanTable)
	retval = append(retval, mdbahanPagination)
	return utils.ViewMulti(c, retval)
}
func (lh *MdBahanHandler) HandlePaginationMdBahan(c echo.Context) error {
	logger, _ := zap.NewProduction()
	var retval []templ.Component
	pageparam := c.QueryParam("page")

	filtername := c.QueryParam("filtername")
	filterstation := c.QueryParam("filterstation")
	tableID := c.QueryParam("tableID")
	dialogEditBodyID := c.QueryParam("dialogEditBodyID")
	dialogDeleteBodyID := c.QueryParam("dialogDeleteBodyID")
	paginationID := c.QueryParam("paginationID")
	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
	}
	bahanList, totalPage, err := lh.MdBahanServices.GetListMdBahan(filtername, filterstation, page, 5)
	defer logger.Sync()
	logger.Info("GET DATA BAHAN",
		zap.Any("data", bahanList),
		zap.Any("err", err),
		zap.Any("totalPage", totalPage),
	)
	//Bahan Table

	mdBahanTable := dev_mdbahanview.TableBahan(
		bahanList,
		tableID,
		"/dev/md/bahan/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/bahan/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID")
	//Pagination

	mdbahanPagination := pagination.PaginationV3(
		totalPage, page,
		"/dev/md/bahan/pagination",
		paginationID,
		"#filter-name, #filter-station, #tableID, #paginationID, #dialogAddID, #dialogAddBodyID, #dialogEditID, #dialogEditBodyID, #dialogDeleteBodyID, #dialogDeleteID, #currpage"+paginationID,
		"#"+paginationID,
		"outerHTML",
		"",
		"",
		dev_mdbahanview.BeforeRequestContent(),
		dev_mdbahanview.AfterRequestContent(),
	)
	retval = append(retval, mdBahanTable)
	retval = append(retval, mdbahanPagination)
	return utils.ViewMulti(c, retval)
}
func (lh *MdBahanHandler) HandleCloseFormAddMdBahan(c echo.Context) error {
	logger, _ := zap.NewProduction()
	stationList, err := lh.MdBahanServices.GetListSampleStation(false)
	defer logger.Sync()
	logger.Info("GET DATA STATIOn",
		zap.Any("err", err),
	)
	dialogAddBodyID := c.FormValue("dialogAddBodyID")
	dialogAddID := c.FormValue("dialogAddID")

	modalAddBody := dev_mdbahanview.BodyFormBahan(
		"",
		models.CustomTextFieldv2Prm{Label: "Kode Bahan",
			Name:        "kodebahan",
			Placeholder: "Kode Bahan",
			Type:        "text"},
		models.CustomTextFieldv2Prm{Label: "Nama Bahan",
			Name:        "namabahan",
			Placeholder: "Nama Bahan",
			Type:        "text"},
		models.CustomDropdownV2Prm{
			Name:     "stationid",
			Label:    "Sample Station",
			ListItem: dev_mdbahanview.ListItemStation(stationList, 0),
		},
		dialogAddBodyID,
		dev_mdbahanview.JsHideModal("#"+dialogAddID),
	)
	return utils.View(c, modalAddBody)
}
func (lh *MdBahanHandler) HandleAddMdBahan(c echo.Context) error {
	logger, _ := zap.NewProduction()

	code := c.FormValue("kodebahan")
	name := c.FormValue("namabahan")
	stationIDPrm := c.FormValue("stationid")
	tableID := c.FormValue("tableID")
	paginationID := c.FormValue("paginationID")
	filtername := c.FormValue("filtername")
	filterstation := c.FormValue("filterstation")
	currPagePrm := c.FormValue("currpage" + paginationID)
	dialogAddBodyID := c.FormValue("dialogAddBodyID")
	// searchID := c.FormValue("searchID")
	dialogAddID := c.FormValue("dialogAddID")
	dialogEditBodyID := c.FormValue("dialogEditBodyID")
	dialogDeleteBodyID := c.FormValue("dialogDeleteBodyID")
	currPage, err := strconv.Atoi(currPagePrm)
	if err != nil {
		return err
	}
	stationID, err := strconv.Atoi(stationIDPrm)
	if err != nil {
		return err
	}
	formValidation := ""
	codeValidation := ""
	nameValidation := ""
	stationValidation := ""

	if strings.TrimSpace(code) == "" {
		formValidation = "Code bahan tidak boleh kosong"
		codeValidation = "Code bahan tidak boleh kosong"
		nameValidation = ""
		stationValidation = ""
	}
	if strings.TrimSpace(name) == "" {
		formValidation = "Nama bahan tidak boleh kosong"
		codeValidation = ""
		stationValidation = ""
		nameValidation = "Nama bahan tidak boleh kosong"
	}
	if stationID == 0 {
		formValidation = "Pilih salah satu station"
		codeValidation = ""
		nameValidation = ""
		stationValidation = "Pilih salah satu station"
	}
	if strings.TrimSpace(code) == "" && strings.TrimSpace(name) == "" {
		formValidation = "Code,nama, dan station bahan tidak boleh kosong"
		codeValidation = "Code bahan tidak boleh kosong"
		stationValidation = "Pilih salah satu station"
		nameValidation = "Nama bahan tidak boleh kosong"

	}
	stationList, err := lh.MdBahanServices.GetListSampleStation(false)
	defer logger.Sync()
	logger.Info("GET DATA STATIOn",
		zap.Any("err", err),
	)
	if formValidation != "" || codeValidation != "" || nameValidation != "" || stationValidation != "" {
		newForm := dev_mdbahanview.BodyFormBahan(
			"",
			models.CustomTextFieldv2Prm{Label: "Kode Bahan",
				Name:        "kodebahan",
				Placeholder: "Kode Bahan",
				Value:       code,
				ErrorMsg:    codeValidation,
				Type:        "text"},
			models.CustomTextFieldv2Prm{Label: "Nama Bahan",
				Name:        "namabahan",
				Placeholder: "Nama Bahan",
				ErrorMsg:    nameValidation,
				Value:       name,
				Type:        "text"},
			models.CustomDropdownV2Prm{
				Name:     "stationid",
				Label:    "Sample Station",
				ErrorMsg: stationValidation,
				ListItem: dev_mdbahanview.ListItemStation(stationList, stationID),
			},
			dialogAddBodyID,
			dev_mdbahanview.JsShowModal(""),
		)
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Warning", formValidation, "warning"), newForm})
	}
	_, err = lh.MdBahanServices.AddMdBahan(code, name, stationIDPrm)
	if err != nil {
		newForm := dev_mdbahanview.BodyFormBahan(
			"",
			models.CustomTextFieldv2Prm{Label: "Kode Bahan",
				Name:        "kodebahan",
				Placeholder: "Kode Bahan",
				Value:       code,
				Type:        "text"},
			models.CustomTextFieldv2Prm{Label: "Nama Bahan",
				Name:        "namabahan",
				Placeholder: "Nama Bahan",
				Value:       name,
				Type:        "text"},
			models.CustomDropdownV2Prm{
				Name:     "stationid",
				Label:    "Sample Station",
				ListItem: dev_mdbahanview.ListItemStation(stationList, int(stationID)),
			},
			dialogAddBodyID,
			dev_mdbahanview.JsShowModal(""),
		)
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Gagal", err.Error(), "danger"), newForm})
	}

	bahanList, totalPage, err := lh.MdBahanServices.GetListMdBahan(filtername, filterstation, currPage, 5)
	defer logger.Sync()
	logger.Info("GET DATA BAHAN",
		zap.Any("data", bahanList),
		zap.Any("err", err),
		zap.Any("totalPage", totalPage),
	)
	//Bahan Table
	mdBahanTable := dev_mdbahanview.TableBahan(
		bahanList,
		tableID,
		"/dev/md/bahan/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/bahan/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID")

	//Pagination

	mdbahanPagination := pagination.PaginationV3(
		totalPage, currPage,
		"/dev/md/bahan/pagination",
		paginationID,
		"#filter-name, #filter-station, #tableID, #paginationID, #dialogAddID, #dialogAddBodyID, #dialogEditID, #dialogEditBodyID,  #dialogDeleteBodyID, #dialogDeleteID, #currpage"+paginationID,
		"#"+paginationID,
		"outerHTML",
		"",
		"true",
		dev_mdbahanview.BeforeRequestContent(),
		dev_mdbahanview.AfterRequestContent(),
	)
	//Form Add
	modalAddBody := dev_mdbahanview.BodyFormBahan(
		"",
		models.CustomTextFieldv2Prm{Label: "Kode Bahan",
			Name:        "kodebahan",
			Placeholder: "Kode Bahan",
			Type:        "text"},
		models.CustomTextFieldv2Prm{Label: "Nama Bahan",
			Name:        "namabahan",
			Placeholder: "Nama Bahan",
			Type:        "text"},
		models.CustomDropdownV2Prm{
			Name:     "stationid",
			Label:    "Sample Station",
			ListItem: dev_mdbahanview.ListItemStation(stationList, 0),
		},
		dialogAddBodyID,
		dev_mdbahanview.JsHideModal("#"+dialogAddID),
	)

	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Success add bahan ", "success")
	retval := []templ.Component{toastSuccess, mdBahanTable, mdbahanPagination, modalAddBody}
	// time.Sleep(20 * time.Second)
	return utils.ViewMulti(c, retval)
}
func (lh *MdBahanHandler) HandleOpenEditMdBahan(c echo.Context) error {
	logger, _ := zap.NewProduction()

	id := c.QueryParam("id")
	dialogEditID := c.QueryParam("dialogEditID")
	dialogEditBodyID := c.QueryParam("dialogEditBodyID")
	stationList, err := lh.MdBahanServices.GetListSampleStation(false)
	defer logger.Sync()
	logger.Info("GET DATA STATION",
		zap.Any("err", err),
	)
	dataBahan, err := lh.MdBahanServices.GetMdBahanByID(id)
	if err != nil {
		newForm := dev_mdbahanview.BodyFormBahan(
			id,
			models.CustomTextFieldv2Prm{Label: "Kode Bahan",
				Name:        "kodebahan",
				Placeholder: "Kode Bahan",
				Type:        "text"},
			models.CustomTextFieldv2Prm{Label: "Nama Bahan",
				Name:        "namabahan",
				Placeholder: "Nama Bahan",
				Type:        "text"},
			models.CustomDropdownV2Prm{
				Name:     "stationid",
				Label:    "Sample Station",
				ListItem: dev_mdbahanview.ListItemStation(stationList, dataBahan.T_BahanT_SampleStationID),
			},
			dialogEditBodyID,
			dev_mdbahanview.JsHideModal(""),
		)
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Error", "Get data bahan error", "danger"), newForm})
	}
	newForm := dev_mdbahanview.BodyFormBahan(
		id,
		models.CustomTextFieldv2Prm{Label: "Kode Bahan",
			Name:        "kodebahan",
			Placeholder: "Kode Bahan",
			Value:       dataBahan.T_BahanCode,
			Type:        "text"},
		models.CustomTextFieldv2Prm{Label: "Nama Bahan",
			Name:        "namabahan",
			Placeholder: "Nama Bahan",
			Value:       dataBahan.T_BahanName,
			Type:        "text"},
		models.CustomDropdownV2Prm{
			Name:     "stationid",
			Label:    "Sample Station",
			ListItem: dev_mdbahanview.ListItemStation(stationList, dataBahan.T_BahanT_SampleStationID),
		},
		dialogEditBodyID,
		dev_mdbahanview.JsShowModal("#"+dialogEditID),
	)
	return utils.View(c, newForm)
}
func (lh *MdBahanHandler) HandleCloseFormEditMdBahan(c echo.Context) error {
	logger, _ := zap.NewProduction()
	stationList, err := lh.MdBahanServices.GetListSampleStation(false)
	dialogEditID := c.FormValue("dialogEditID")
	dialogEditBodyID := c.FormValue("dialogEditBodyID")
	defer logger.Sync()
	logger.Info("GET DATA STATION",
		zap.Any("err", err),
		zap.Any("dialogeditbodyid", dialogEditBodyID),
	)

	modalAddBody := dev_mdbahanview.BodyFormBahan(
		"",
		models.CustomTextFieldv2Prm{Label: "Kode Bahan",
			Name:        "kodebahan",
			Placeholder: "Kode Bahan",
			Type:        "text"},
		models.CustomTextFieldv2Prm{Label: "Nama Bahan",
			Name:        "namabahan",
			Placeholder: "Nama Bahan",
			Type:        "text"},
		models.CustomDropdownV2Prm{
			Name:     "stationid",
			Label:    "Sample Station",
			ListItem: dev_mdbahanview.ListItemStation(stationList, 0),
		},
		dialogEditBodyID,
		dev_mdbahanview.JsHideModal("#"+dialogEditID),
	)
	return utils.View(c, modalAddBody)
}
func (lh *MdBahanHandler) HandleEditMdBahan(c echo.Context) error {
	logger, _ := zap.NewProduction()

	code := c.FormValue("kodebahan")
	name := c.FormValue("namabahan")
	id := c.FormValue("bahanid")
	stationIDPrm := c.FormValue("stationid")
	tableID := c.FormValue("tableID")
	paginationID := c.FormValue("paginationID")
	filtername := c.FormValue("filtername")
	filterstation := c.FormValue("filterstation")
	currPagePrm := c.FormValue("currpage" + paginationID)
	// dialogAddBodyID := c.FormValue("dialogAddBodyID")
	// searchID := c.FormValue("searchID")
	dialogEditID := c.FormValue("dialogEditID")
	dialogEditBodyID := c.FormValue("dialogEditBodyID")
	dialogDeleteBodyID := c.FormValue("dialogDeleteBodyID")
	currPage, err := strconv.Atoi(currPagePrm)
	if err != nil {
		return err
	}
	stationID, err := strconv.Atoi(stationIDPrm)
	if err != nil {
		return err
	}
	formValidation := ""
	codeValidation := ""
	nameValidation := ""
	stationValidation := ""

	if strings.TrimSpace(code) == "" {
		formValidation = "Code bahan tidak boleh kosong"
		codeValidation = "Code bahan tidak boleh kosong"
		nameValidation = ""
		stationValidation = ""
	}
	if strings.TrimSpace(name) == "" {
		formValidation = "Nama bahan tidak boleh kosong"
		codeValidation = ""
		stationValidation = ""
		nameValidation = "Nama bahan tidak boleh kosong"
	}
	if stationID == 0 {
		formValidation = "Pilih salah satu station"
		codeValidation = ""
		nameValidation = ""
		stationValidation = "Pilih salah satu station"
	}
	if strings.TrimSpace(code) == "" && strings.TrimSpace(name) == "" {
		formValidation = "Code,nama, dan station bahan tidak boleh kosong"
		codeValidation = "Code bahan tidak boleh kosong"
		stationValidation = "Pilih salah satu station"
		nameValidation = "Nama bahan tidak boleh kosong"

	}
	stationList, err := lh.MdBahanServices.GetListSampleStation(false)
	defer logger.Sync()
	logger.Info("GET DATA STATIOn",
		zap.Any("err", err),
	)
	if formValidation != "" || codeValidation != "" || nameValidation != "" || stationValidation != "" {
		newForm := dev_mdbahanview.BodyFormBahan(
			id,
			models.CustomTextFieldv2Prm{Label: "Kode Bahan",
				Name:        "kodebahan",
				Placeholder: "Kode Bahan",
				Value:       code,
				ErrorMsg:    codeValidation,
				Type:        "text"},
			models.CustomTextFieldv2Prm{Label: "Nama Bahan",
				Name:        "namabahan",
				Placeholder: "Nama Bahan",
				ErrorMsg:    nameValidation,
				Value:       name,
				Type:        "text"},
			models.CustomDropdownV2Prm{
				Name:     "stationid",
				Label:    "Sample Station",
				ErrorMsg: stationValidation,
				ListItem: dev_mdbahanview.ListItemStation(stationList, stationID),
			},
			dialogEditBodyID,
			dev_mdbahanview.JsShowModal(""),
		)
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Warning", formValidation, "warning"), newForm})
	}
	_, err = lh.MdBahanServices.EditMdBahan(id, code, name, stationIDPrm)
	if err != nil {
		newForm := dev_mdbahanview.BodyFormBahan(
			id,
			models.CustomTextFieldv2Prm{Label: "Kode Bahan",
				Name:        "kodebahan",
				Placeholder: "Kode Bahan",
				Value:       code,
				Type:        "text"},
			models.CustomTextFieldv2Prm{Label: "Nama Bahan",
				Name:        "namabahan",
				Placeholder: "Nama Bahan",
				Value:       name,
				Type:        "text"},
			models.CustomDropdownV2Prm{
				Name:     "stationid",
				Label:    "Sample Station",
				ListItem: dev_mdbahanview.ListItemStation(stationList, int(stationID)),
			},
			dialogEditBodyID,
			dev_mdbahanview.JsShowModal(""),
		)
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Gagal", err.Error(), "danger"), newForm})
	}

	bahanList, totalPage, err := lh.MdBahanServices.GetListMdBahan(filtername, filterstation, currPage, 5)
	defer logger.Sync()
	logger.Info("GET DATA BAHAN",
		zap.Any("data", bahanList),
		zap.Any("err", err),
		zap.Any("totalPage", totalPage),
	)
	//Bahan Table
	mdBahanTable := dev_mdbahanview.TableBahan(
		bahanList,
		tableID,
		"/dev/md/bahan/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/bahan/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID")

	//Pagination

	mdbahanPagination := pagination.PaginationV3(
		totalPage, currPage,
		"/dev/md/bahan/pagination",
		paginationID,
		"#filter-name, #filter-station, #tableID, #paginationID, #dialogAddID, #dialogAddBodyID, #dialogEditID, #dialogEditBodyID, #dialogDeleteBodyID, #dialogDeleteID, #currpage"+paginationID,
		"#"+paginationID,
		"outerHTML",
		"",
		"true",
		dev_mdbahanview.BeforeRequestContent(),
		dev_mdbahanview.AfterRequestContent(),
	)
	//Form Add
	modalAddBody := dev_mdbahanview.BodyFormBahan(
		"",
		models.CustomTextFieldv2Prm{Label: "Kode Bahan",
			Name:        "kodebahan",
			Placeholder: "Kode Bahan",
			Type:        "text"},
		models.CustomTextFieldv2Prm{Label: "Nama Bahan",
			Name:        "namabahan",
			Placeholder: "Nama Bahan",
			Type:        "text"},
		models.CustomDropdownV2Prm{
			Name:     "stationid",
			Label:    "Sample Station",
			ListItem: dev_mdbahanview.ListItemStation(stationList, 0),
		},
		dialogEditBodyID,
		dev_mdbahanview.JsHideModal("#"+dialogEditID),
	)

	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Success edit bahan ", "success")
	retval := []templ.Component{toastSuccess, mdBahanTable, mdbahanPagination, modalAddBody}
	// time.Sleep(20 * time.Second)
	return utils.ViewMulti(c, retval)
}
func (lh *MdBahanHandler) HandleOpenDeleteMdBahan(c echo.Context) error {
	logger, _ := zap.NewProduction()

	id := c.QueryParam("id")
	dialogDeleteID := c.QueryParam("dialogDeleteID")
	dialogDeleteBodyID := c.QueryParam("dialogDeleteBodyID")

	dataBahan, err := lh.MdBahanServices.GetMdBahanByID(id)
	if err != nil {
		defer logger.Sync()
		logger.Info("GET DATA BAHAN",
			zap.Any("err", err),
		)
		newForm := dev_mdbahanview.DeleteConfirmationBody(
			"",
			dialogDeleteBodyID,
			"Apakah anda yakin menghapus bahan berikut ?",
			[]string{"Kode", "Nama Bahan", "Sample Station"},
			[]string{"", "", ""}, dev_mdbahanview.JsShowModal(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Error", "Get data bahan error", "danger"), newForm})
	}
	newForm := dev_mdbahanview.DeleteConfirmationBody(
		strconv.Itoa(dataBahan.T_BahanID),
		dialogDeleteBodyID,
		"Apakah anda yakin menghapus bahan berikut ?",
		[]string{"Kode", "Nama Bahan", "Sample Station"},
		[]string{dataBahan.T_BahanCode, dataBahan.T_BahanName, dataBahan.T_SampleStationName}, dev_mdbahanview.JsShowModal("#"+dialogDeleteID))
	return utils.View(c, newForm)
}
func (lh *MdBahanHandler) HandleCloseDeleteMdBahan(c echo.Context) error {

	dialogDeleteID := c.FormValue("dialogDeleteID")
	dialogDeleteBodyID := c.FormValue("dialogDeleteBodyID")
	newForm := dev_mdbahanview.DeleteConfirmationBody(
		"",
		dialogDeleteBodyID,
		"Apakah anda yakin menghapus bahan berikut ?",
		[]string{"Kode", "Nama Bahan", "Sample Station"},
		[]string{"", "", ""}, dev_mdbahanview.JsHideModal("#"+dialogDeleteID))
	return utils.View(c, newForm)
}

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

	id := c.FormValue("bahanid")

	tableID := c.FormValue("tableID")
	paginationID := c.FormValue("paginationID")
	filtername := c.FormValue("filtername")
	filterstation := c.FormValue("filterstation")
	currPagePrm := c.FormValue("currpage" + paginationID)
	// dialogAddBodyID := c.FormValue("dialogAddBodyID")
	// searchID := c.FormValue("searchID")
	dialogDeleteID := c.FormValue("dialogDeleteID")
	dialogEditBodyID := c.FormValue("dialogEditBodyID")
	dialogDeleteBodyID := c.FormValue("dialogDeleteBodyID")
	currPage, err := strconv.Atoi(currPagePrm)
	if err != nil {
		return err
	}

	_, err = lh.MdBahanServices.DeleteMdBahan(id)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERR DELETE MD BAHAN",
			zap.Any("err", err),
		)
		newForm := dev_mdbahanview.DeleteConfirmationBody(
			"",
			dialogDeleteBodyID,
			"Apakah anda yakin menghapus bahan berikut ?",
			[]string{"Kode", "Nama Bahan", "Sample Station"},
			[]string{"", "", ""}, dev_mdbahanview.JsShowModal(""))
		return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Error", "Get data bahan error", "danger"), newForm})
	}
	bahanList, totalPage, err := lh.MdBahanServices.GetListMdBahan(filtername, filterstation, currPage, 5)
	defer logger.Sync()
	logger.Info("GET DATA BAHAN",
		zap.Any("data", bahanList),
		zap.Any("err", err),
		zap.Any("totalPage", totalPage),
	)
	//Bahan Table
	mdBahanTable := dev_mdbahanview.TableBahan(
		bahanList,
		tableID,
		"/dev/md/bahan/openedit",
		"#"+dialogEditBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID",
		"/dev/md/bahan/opendelete",
		"#"+dialogDeleteBodyID,
		"outerHTML",
		"#dialogEditBodyID, #dialogEditID, #dialogDeleteBodyID, #dialogDeleteID")

	//Pagination

	mdbahanPagination := pagination.PaginationV3(
		totalPage, currPage,
		"/dev/md/bahan/pagination",
		paginationID,
		"#filter-name, #filter-station, #tableID, #paginationID, #dialogAddID, #dialogAddBodyID, #dialogEditID, #dialogEditBodyID, #dialogDeleteBodyID, #dialogDeleteID, #currpage"+paginationID,
		"#"+paginationID,
		"outerHTML",
		"",
		"true",
		dev_mdbahanview.BeforeRequestContent(),
		dev_mdbahanview.AfterRequestContent(),
	)

	toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil hapus bahan", "success")
	newForm := dev_mdbahanview.DeleteConfirmationBody(
		"",
		dialogDeleteBodyID,
		"Apakah anda yakin menghapus bahan berikut ?",
		[]string{"Kode", "Nama Bahan", "Sample Station"},
		[]string{"", "", ""}, dev_mdbahanview.JsHideModal("#"+dialogDeleteID))
	return utils.ViewMulti(c, []templ.Component{toastSuccess, mdBahanTable, mdbahanPagination, newForm})
}
