package supplier

import (
	"errors"
	"fonchain-fiee/api/supplier"
	"fonchain-fiee/pkg/service"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

func GetSupplier(c *gin.Context) {
	req := &supplier.GetSupplierRequest{}
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.SupplierProvider.GetSupplier(c, req)
	if err != nil {
		service.Error(c, err)
		return
	}

	service.Success(c, res)
	return
}
func CreateSupplier(c *gin.Context) {
	req := &supplier.CreateSupplierRequest{}
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.SupplierProvider.CreateSupplier(c, req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
	return
}

func UpdateSupplier(c *gin.Context) {
	req := &supplier.UpdateSupplierRequest{}
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	info, err := service.SupplierProvider.GetSupplier(c, &supplier.GetSupplierRequest{Id: req.Id})
	if err != nil {
		service.Error(c, err)
		return
	}
	if info.Status == 3 {
		service.Error(c, errors.New("审核中不可修改")) //todo 修改:审核中并且存在审核人 不可修改
		return
	}
	if req.Status != 1 && req.Status != 2 {
		service.Error(c, errors.New("审批状态错误")) //todo 只允许前端暂存或提交审核申请
	}
	res, err := service.SupplierProvider.UpdateSupplier(c, req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
	return
}

func GetSupplierList(c *gin.Context) {
	req := &supplier.GetSupplierListRequest{}
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.SupplierProvider.GetSupplierList(c, req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
	return
}

func CreateOrganizeDictionary(c *gin.Context) {
	req := &supplier.CreateOrganizeDictionaryRequest{}
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.SupplierProvider.CreateOrganizeDictionary(c, req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
	return
}

func GetOrganizeDictionaryList(c *gin.Context) {
	req := &supplier.GetOrganizeDictionaryListRequest{}
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.SupplierProvider.GetOrganizeDictionaryList(c, req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
	return
}

func GetCountryRegionList(c *gin.Context) {
	req := &supplier.GetCountryRegionListRequest{}
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.SupplierProvider.GetCountryRegionList(c, req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
	return
}

func CreateImportRecord(c *gin.Context) {
	req := &supplier.CreateImportRecordRequest{}
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.SupplierProvider.CreateImportRecord(c, req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
	return
}

func GetImportRecordInfo(c *gin.Context) {
	req := &supplier.GetImportRecordRequest{}
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		service.Error(c, err)
		return
	}
	res, err := service.SupplierProvider.GetImportRecordInfo(c, req)
	if err != nil {
		service.Error(c, err)
		return
	}
	service.Success(c, res)
	return
}
