package message

import (
	"spaceofcheng/services/models"
	"spaceofcheng/services/models/message"
	"spaceofcheng/services/models/model"
	"spaceofcheng/services/service"
	"spaceofcheng/services/utils"
	"strings"

	"github.com/astaxie/beego"
)

// GetCmsCasesList 获取项目列表
func (s *MessageServ) GetCmsCasesList(name, caseids string, page int, pageSize int) (map[string]interface{}, error) {
	if pageSize == 0 {
		pageSize = service.PAGENUM
	}
	if page == 0 {
		page = 1
	}
	infos, err := s.dao.GetCmsCaseList(name, caseids, page, pageSize)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	total, err := s.dao.GetCmsCaseListCount(name, caseids)
	if err != nil {
		beego.Error(err)
		return nil, err
	}

	return map[string]interface{}{
		"list":     infos,
		"pagesize": pageSize,
		"pagenum":  total,
		"page":     page,
	}, err
}

// GetCmsCaseByOrg 获取项目列表
func (s *MessageServ) GetCmsCaseByOrg(orgid string) ([]message.CmsCase, error) {
	list, err := s.dao.GetCmsCaseByOrg(orgid)
	if err != nil {
		return nil, err
	}
	for i, c := range list {
		imgs, err := s.dao.GetCmsCaseImgs(c.CmsCaseId)
		if err != nil {
			return nil, err
		}
		list[i].CmsCaseImgs = imgs
	}
	return list, err
}

// GetCmsCasesByID 获取項目信息
func (s *MessageServ) GetCmsCasesByID(cmscaseid string) (*message.CmsCase, error) {
	var info = &message.CmsCase{}
	var err error
	info, err = s.dao.GetCmsCaseByID(cmscaseid)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	var course []model.TaCourse
	if info.IsAllCourse == 1 {
		course, err = s.dao.GetCourseByCaseID(info.CaseId)
	} else {
		course, err = s.dao.GetCoursesByCmsCase(info.CmsCaseId)
	}
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	info.Courses = course

	imgs, err := s.dao.GetCmsCaseImgs(info.CmsCaseId)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	info.CmsCaseImgs = imgs
	return info, nil
}

// SaveCmsCase 保存项目信息
func (s *MessageServ) SaveCmsCase(cmscase model.TaCmsCase, courseids, imgs, detailimgs string) (*model.TaCmsCase, error) {
	var newInfo *model.TaCmsCase
	var err error
	if cmscase.Name == "" {
		return nil, utils.LogError("项目名称不能为空！")
	}
	if cmscase.CaseId == "" {
		return nil, utils.LogError("案场不能为空")
	}
	if imgs == "" {
		return nil, utils.LogError("图片不能为空")
	}
	if detailimgs == "" {
		return nil, utils.LogError("详情图片不能为空")
	}
	if cmscase.DetailContent == "" {
		return nil, utils.LogError("专题介绍不能为空")
	}

	// if cmscase.Title == "" {
	// 	return nil, utils.LogError("项目标题不能为空！")
	// }

	if cmscase.CmsCaseId == "" {
		newInfo, err = s.dao.AddCmsCase(cmscase)
	} else {
		err = s.dao.UpdateCmsCase(cmscase)
		newInfo = &cmscase
	}
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	err = s.dao.DelCmsCaseCourse(newInfo.CmsCaseId)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	if newInfo.IsAllCourse == 0 {
		err = s.dao.SaveCmsCaseCourse(newInfo.CmsCaseId, courseids)
		if err != nil {
			beego.Error(err)
			return nil, err
		}
	}
	err = s.dao.DelCmsCaseImg(newInfo.CmsCaseId)
	if err != nil {
		beego.Error(err)
		return nil, err
	}

	if imgs != "" {
		arrimg := strings.Split(imgs, ",")
		for index, img := range arrimg {
			CmsImg := model.TaCmsCaseImage{
				ImageType:    models.IMAGETYPE_COVER,
				Sort:         (index + 1),
				CaseImageUrl: img,
				CmsCaseId:    newInfo.CmsCaseId,
			}
			err = s.dao.SaveCmsCaseImg(CmsImg)
			if err != nil {
				beego.Error(err)
				return nil, err
			}
		}
	}
	if detailimgs != "" {
		arrdetailimg := strings.Split(detailimgs, ",")
		for index, img := range arrdetailimg {
			CmsImg := model.TaCmsCaseImage{
				ImageType:    models.IMAGETYPE_DETAIL,
				Sort:         (index + 1),
				CaseImageUrl: img,
				CmsCaseId:    newInfo.CmsCaseId,
			}
			err = s.dao.SaveCmsCaseImg(CmsImg)
			if err != nil {
				beego.Error(err)
				return nil, err
			}
		}
	}
	return newInfo, err
}

// DelCmsCase 刪除项目信息
func (s *MessageServ) DelCmsCase(cmscaseid string) error {
	err := s.dao.DelCmsCase(cmscaseid)
	return err
}

// CmsCaseShow 前台展示
func (s *MessageServ) CmsCaseShow(cmscaseid string) error {
	err := s.dao.CmsCaseShow(cmscaseid)
	return err
}

// CmsCaseHide 前台隐藏
func (s *MessageServ) CmsCaseHide(cmscaseid string) error {
	err := s.dao.CmsCaseHide(cmscaseid)
	return err
}
