package user

import (
	"context"
	"fmt"
	"git.i2edu.net/i2/i2-bill-api/internal/utils"
	"net/http"
	"strconv"
	"time"

	"git.i2edu.net/i2/i2-bill-api/internal/svc"
	"git.i2edu.net/i2/i2-bill-api/internal/types"

	"git.i2edu.net/i2/go-zero/core/logx"
)

type PunchClockPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPunchClockPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) PunchClockPageLogic {
	return PunchClockPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PunchClockPageLogic) PunchClockPage(r *http.Request) (*types.Response, error) {
	// todo: add your logic here and delete this line
	r.ParseForm()
	create_time_sta := r.Form.Get("date_sta")
	create_time_end := r.Form.Get("date_end")
	page, _ := strconv.Atoi(r.Form.Get("page"))
	rows, _ := strconv.Atoi(r.Form.Get("rows"))
	sort := r.Form.Get("__sort__")
	userId := fmt.Sprintf("%d", l.svcCtx.GetUserIdByJwt(l.ctx))
	if create_time_end != "" {
		end, err := time.Parse("2006-01-02", create_time_end)
		if err != nil {
			return &types.Response{500, err.Error(), nil}, nil
		}
		create_time_end = end.Add(time.Second*24*60*60 - 1*time.Second).Format("2006-01-02 15:04:05")

	}
	if sort == "" {
		sort = "id desc"
	}
	paramMap_i_t := map[string]interface{}{
		"page":            page,
		"rows":            rows,
		"create_time_sta": create_time_sta,
		"create_time_end": create_time_end,
		"user_id":         userId,
		"__sort__":        sort,
	}

	res, err := utils.PageSearch(l.svcCtx.DB, "i2bill_acquirer_attendance_record", "page", "i2bill_acquirer_attendance_record", paramMap_i_t)
	if err != nil {
		return &types.Response{500, err.Error(), nil}, nil
	}
	return &types.Response{200, "", res}, nil
}
