package hdl

import (
	"applet/app/db"
	"applet/app/db/model"
	"applet/app/e"
	"applet/app/store/svc"
	"github.com/gin-gonic/gin"
)

// Login 登陆
// @Summary      登陆
// @Tags         登陆
// @Description  登入
// @Accept       json
// @Produce      json
// @Param        req 	body      md.FastLoginRequestBody          true   "请求参数"
// @Success      200   {object}   md.LoginResponse				""
// @Failure      400   {object}   md.Response              	"具体错误"
// @Router       /api/v1/communityTeam/store/login [post]
func Login(c *gin.Context) {
	svc.Login(c)
}
func LoginSendSms(c *gin.Context) {
	svc.LoginSendSms(c)
}
func Sms(c *gin.Context) {
	svc.Sms(c)
}
func LoginFastIn(c *gin.Context) {
	svc.LoginFastIn(c)
}

type resultItem struct {
	Id   string `json:"id" example:"440100000000"`
	Name string `json:"name" example:"city"`
}

func GetRegionChildNode(c *gin.Context) {
	parent, has := c.GetQuery("parent")
	if !has {
		e.OutErr(c, e.ERR_INVALID_ARGS)
		return
	}
	id, has := c.GetQuery("id")
	if parent != "root" && !has {
		e.OutErr(c, e.ERR_INVALID_ARGS)
		return
	}

	var nodeList []interface{}
	var err error

	switch parent {
	case "root":
		var provinceList []*model.Province
		err = db.Db.Find(&provinceList)
		for _, item := range provinceList {
			nodeList = append(nodeList, resultItem{
				Id:   item.Id,
				Name: item.Name,
			})
		}
	case "province":
		var cityList []*model.City
		err = db.Db.Where("province_id=?", id).Find(&cityList)
		for _, item := range cityList {
			nodeList = append(nodeList, resultItem{
				Id:   item.Id,
				Name: item.Name,
			})
		}
	case "city":
		var countyList []*model.County
		err = db.Db.Where("city_id=?", id).Find(&countyList)
		for _, item := range countyList {
			nodeList = append(nodeList, resultItem{
				Id:   item.Id,
				Name: item.Name,
			})
		}
	default:
		e.OutErr(c, e.ERR_INVALID_ARGS)
		return
	}
	if err != nil {
		e.OutErr(c, e.ERR_DB_ORM, err)
		return
	}
	if len(nodeList) == 0 {
		e.OutSuc(c, []interface{}{}, nil)
		return
	}
	e.OutSuc(c, nodeList, nil)
}
