package api

import (
	"box-cost/db/model"
	"box-cost/db/repo"
	"fmt"

	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

// func UpdateIsAckStatus(c *gin.Context, apictx *ApiSession) (interface{}, error) {
// products := []*model.ProductBill{}
// repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
// 	CollectName: repo.CollectionBillProduct,
// 	Query:       repo.Map{"isAck": nil},
// Project:     []string{"isAck"},
// }, &products)
// fmt.Println(len(products))

// for _, product := range products {
// 	fmt.Println(product.Id.Hex())
// 	fmt.Println(product.IsAck)
// 	notAck := false
// 	repo.RepoUpdateSetDocsProps(apictx.CreateRepoCtx(), &repo.DocFilterOptions{
// 		CollectName: repo.CollectionBillProduct,
// 		Query:       repo.Map{"isAck": nil},
// 	}, &model.ProductBill{IsAck: &notAck})
// }
// return products, nil

// produces := []*model.ProduceBill{}
// repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
// 	CollectName: repo.CollectionBillProduce,
// 	Query:       repo.Map{"isAck": nil},
// 	Project:     []string{"isAck"},
// }, &produces)

// for _, produce := range produces {
// 	fmt.Println(produce.Id.Hex())
// 	fmt.Println(produce.IsAck)
// 	notAck := false
// 	repo.RepoUpdateSetDocsProps(apictx.CreateRepoCtx(), &repo.DocFilterOptions{
// 		CollectName: repo.CollectionBillProduce,
// 		Query:       repo.Map{"isAck": nil},
// 	}, &model.ProduceBill{IsAck: &notAck})
// }
// return produces, nil

// purchases := []*model.PurchaseBill{}
// repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
// 	CollectName: repo.CollectionBillPurchase,
// 	Query:       repo.Map{"isAck": nil},
// Project:     []string{"isAck"},
// }, &purchases)

// for _, purchase := range purchases {
// 	fmt.Println(purchase.Id.Hex())
// 	fmt.Println(purchase.IsAck)
// 	notAck := false
// 	repo.RepoUpdateSetDocsProps(apictx.CreateRepoCtx(), &repo.DocFilterOptions{
// 		CollectName: repo.CollectionBillPurchase,
// 		Query:       repo.Map{"isAck": nil},
// 	}, &model.PurchaseBill{IsAck: &notAck})
// }
// return purchases, nil

// return nil, nil

// }

func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	// !如果数据模型tag omitempty存在 对某个字段设置零值的方法
	// !如果有omitempty存在,对应字段零值将被忽略
	// !该字段设置为引用类型
	// !创建该字段时设置默认值
	id, err := primitive.ObjectIDFromHex("")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(id.IsZero())

	return nil, nil
	// type RegCode struct {
	// 	Id    primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
	// 	Roles *[]string          `bson:"roles,omitempty" json:"roles"`
	// 	Key   string             `bson:"key,omitempty" json:"key"`
	// }
	// roles := []string{}

	// ?接收客户端传参数,更新
	// {"_id":"642e2e97243b5c5107d70204","roles":false}
	// form := RegCode{}
	// err := c.ShouldBindJSON(&form)
	// if err != nil {
	// 	return nil, err
	// }

	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), "reg-code", form.Id.Hex(), &RegCode{Roles: form.Roles})
	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), "reg-code", "642e2e97243b5c5107d70204", &RegCode{Roles: &roles})

}

func RemovePlanSyncBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	plans := []*model.ProductPlan{}
	err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionProductPlan,
		Project:     []string{"_id"},
	}, &plans)
	if err != nil {
		return nil, err
	}

	planIds := make([]primitive.ObjectID, 0)
	for _, plan := range plans {
		fmt.Println(plan.Id.Hex())
		planIds = append(planIds, plan.Id)
	}
	// Purchases := []*model.PurchaseBill{}
	// repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
	// 	CollectName: repo.CollectionBillPurchase,
	// 	Query:       repo.Map{"planId": bson.M{"$nin": planIds}},
	// 	Project:     []string{"planId"},
	// }, &Purchases)

	produces := []*model.ProduceBill{}
	repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionBillProduce,
		Query:       repo.Map{"planId": bson.M{"$nin": planIds}},
		Project:     []string{"planId", "serialNumber"},
	}, &produces)
	// products := []*model.ProductBill{}
	// repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
	// 	CollectName: repo.CollectionBillProduct,
	// 	Query:       repo.Map{"planId": bson.M{"$nin": planIds}},
	// 	Project:     []string{"planId"},
	// }, &products)

	// repo.RepoDeleteDocs(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, repo.Map{"planId": bson.M{"$nin": planIds}})
	// repo.RepoDeleteDocs(apictx.CreateRepoCtx(), repo.CollectionBillProduce, repo.Map{"planId": bson.M{"$nin": planIds}})
	// repo.RepoDeleteDocs(apictx.CreateRepoCtx(), repo.CollectionBillProduct, repo.Map{"planId": bson.M{"$nin": planIds}})

	return produces, nil
}