package vehicle

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"wails-app/internal/dao"
	"wails-app/internal/model/common/response"
	"wails-app/internal/model/vehicle/request"
	"strconv"
)

type ShortlistApi struct{}

func (sa *ShortlistApi) QueryAllShortlists(c *gin.Context) {
	shortlists, err := shortlistService.QueryAllShortlists()
	if err != nil {
		response.FailWithMessage("查询失败", c)
		return
	}
	response.OkWithData(shortlists, c)
}

func (sa *ShortlistApi) QueryShortlistList(c *gin.Context) {
	var info request.ShortlistRequest
	err := c.ShouldBindJSON(&info)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	fmt.Println(info)
	list, total, err := shortlistService.QueryShortlistList(info)
	if err != nil {
		response.FailWithMessage("查询失败", c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     info.Page,
		PageSize: info.PageSize,
	}, "获取成功", c)
}

func (sa *ShortlistApi) CreateShortlist(c *gin.Context) {
	var shortlist dao.Shortlist
	err := c.ShouldBindJSON(&shortlist)
	if err != nil {
		response.FailWithMessage("参数错误"+err.Error(), c)
		return
	}
	err = shortlistService.CreateShortlist(shortlist)
	if err != nil {
		response.FailWithMessage("新增失败", c)
		return
	}
	response.OkWithMessage("新增成功", c)
}

func (sa *ShortlistApi) UpdateShortlist(c *gin.Context) {
	var shortlist dao.Shortlist
	err := c.ShouldBindJSON(&shortlist)
	if err != nil {
		response.FailWithMessage("参数错误"+err.Error(), c)
		return
	}
	err = shortlistService.UpdateShortlist(shortlist)
	if err != nil {
		response.FailWithMessage("修改失败", c)
		return
	}
	response.OkWithMessage("修改成功", c)
}

func (sa *ShortlistApi) DeleteShortlist(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		response.FailWithMessage("参数错误", c)
		return
	}
	err = shortlistService.DeleteShortlist(id)
	if err != nil {
		response.FailWithMessage("删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)
}
