package mcu_corporate_handlers

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

	"github.com/labstack/echo/v4"
	"go.uber.org/zap"
)

type TabKeuanganServices interface {
	GetTotalOrderKeuangan(mcuID string) (int, error)
	GetTotalCardData(mcuID string) (models.TotalBillCard, error)
	GetListingDataKeuangan(mcuID string, currentpage int, rowperpage int) ([]models.KeuanganModel, int, error)
}

type TabKeuanganHandler struct {
	TabKeuanganServices TabKeuanganServices
}

func NewTabKeuanganHandler(tk TabKeuanganServices) *TabKeuanganHandler {
	return &TabKeuanganHandler{
		TabKeuanganServices: tk,
	}
}

func (tk *TabKeuanganHandler) HandleShowTabKeuangan(c echo.Context) error {
	logger, _ := zap.NewProduction()
	logger.Info("handler keuangan")
	tableID := utils.GenerateRandomID("tableID")
	paginationID := utils.GenerateRandomID("paginationID")

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

	dataTable, totalPage, err := tk.TabKeuanganServices.GetListingDataKeuangan(id, 1, 10)
	if err != nil {
		defer logger.Sync()
		logger.Info("error get list data keuangan", zap.Any("error", err))
		return err
	}

	tablecompo := corporate_mcudetail.TableKeuangan(dataTable, tableID)

	pagination := pagination.PaginationV3(
		totalPage,
		1,
		"/corp/dashboard_pic/detail/"+id+"/tabkeuangan/changepage",
		paginationID,
		"#tableID, #paginationID, #loading-parent, #loading-child, #loading-spinner, #loadingcontent",
		"#tabdanpagi",
		"outerHTML", "", "",
		corporate_mcudetail.BeforeRequestContent(),
		corporate_mcudetail.AfterRequestContent(),
	)

	totalOrder, err := tk.TabKeuanganServices.GetTotalOrderKeuangan(id)
	if err != nil {
		defer logger.Sync()
		logger.Info("error get total order keuangan", zap.Any("error", err))
		return err
	}

	totalBill, err := tk.TabKeuanganServices.GetTotalCardData(id)
	if err != nil {
		defer logger.Sync()
		logger.Info("error get total bill keuangan", zap.Any("error", err))
		return err
	}

	totalOrderF := utils.FormatRibuan(int(totalOrder))
	totalTagihan := utils.FormatRibuan(int(totalBill.TotalTagihan))
	totalPelunasan := utils.FormatRibuan(int(totalBill.TotalPelunasan))
	totalHutang := utils.FormatRibuan(int(totalBill.TotalHutang))

	content := corporate_mcudetail.TabKeuanganScreen(
		tableID,
		paginationID,
		corporate_mcudetail.KeuanganCard(
			totalOrderF,
			totalTagihan,
			totalPelunasan,
			totalHutang,
		),
		corporate_mcudetail.TableAndPagination(
			tablecompo,
			pagination,
		),
	)

	return utils.View(c, content)
}

func (tk *TabKeuanganHandler) HandleChangePage(c echo.Context) error {
	pageparam := c.QueryParam("page")
	tableID := c.QueryParam("tableID")
	paginationID := c.QueryParam("paginationID")

	id := c.Param("id")

	page, err := strconv.Atoi(pageparam)
	if err != nil {
		return err
	}

	tableData, totalPage, err := tk.TabKeuanganServices.GetListingDataKeuangan(id, page, 10)
	if err != nil {
		return err
	}

	tablecomp := corporate_mcudetail.TableKeuangan(tableData, tableID)
	pagination := pagination.PaginationV3(
		totalPage,
		page,
		"/corp/dashboard_pic/detail/"+id+"/tabkeuangan/changepage",
		paginationID,
		"#tableID, #paginationID, #loading-parent, #loading-child, #loading-spinner, #loadingcontent",
		"#tabdanpagi",
		"outerHTML", "", "",
		corporate_mcudetail.BeforeRequestContent(),
		corporate_mcudetail.AfterRequestContent(),
	)

	tabdanpagi := corporate_mcudetail.TableAndPagination(tablecomp, pagination)
	return utils.View(c, tabdanpagi)
}
