package dto

import (
	"strconv"
	"time"

	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
)

type CooperationProjectInfo struct {
	Company struct {
		CompanyId   int    `json:"companyId,string,"`
		CompanyLogo string `json:"companyLogo"`
		CompanyName string `json:"companyName"`
	} `json:"company"` //项目归属企业
	Org struct {
		OrgId   int    `json:"orgId,string"`
		OrgName string `json:"orgName"`
	} `json:"org"` //项目创建时的组织
	CooperationMode struct {
		CooperationModeId     int    `json:"cooperationModeId,string"`
		CooperationModeName   string `json:"cooperationModeName"`
		CooperationModeNumber string `json:"cooperationModeNumber"`
	} `json:"cooperationMode"` //项目模式
	CooperationProjectDescription string              `json:"cooperationProjectDescription"`
	Images                        []domain.Attachment `json:"images"` //图片附件
	CooperationProjectId          int                 `json:"cooperationProjectId,string,"`
	CooperationProjectName        string              `json:"cooperationProjectName"`
	CooperationProjectNumber      string              `json:"cooperationProjectNumber"`
	CooperationProjectPublishTime time.Time           `json:"cooperationProjectPublishTime"`
	CreatedAt                     time.Time           `json:"createdAt"`
	CooperationProjectSponsor     struct {
		UserId    int    `json:"userId,string"`
		UsersName string `json:"userName"`
		UsersCode string `json:"userCode"`
		UserInfo  struct {
			UsersName string `json:"userName"`
			UsersCode string `json:"userCode"`
			Phone     string `json:"phone"`
			UsersId   int    `json:"userId,string"`
		} `json:"userInfo"`
	} `json:"cooperationProjectSponsor"` //项目发起人
	CooperationProjectUndertakerType []int `json:"cooperationProjectUndertakerType"` //共创合约承接对象,1员工,2共创用户,3公开
	Department                       struct {
		DepartmentNumber string `json:"departmentNumber"`
		DepartmentId     int    `json:"departmentId,string,"`
		DepartmentName   string `json:"departmentName"`
	} `json:"department"` //项目发起部门
	Status int `json:"status"` // 共创项目状态,1招标中,2结束
}

func ToCooperationProjectInfo(param *allied_creation_cooperation.CooperationProject) *CooperationProjectInfo {
	data := CooperationProjectInfo{
		Company:                          param.Company,
		Org:                              param.Org,
		CooperationMode:                  param.CooperationMode,
		CooperationProjectDescription:    param.CooperationProjectDescription,
		CooperationProjectId:             param.CooperationProjectId,
		CooperationProjectName:           param.CooperationProjectName,
		CooperationProjectNumber:         param.CooperationProjectNumber,
		CooperationProjectPublishTime:    param.CooperationProjectPublishTime,
		CreatedAt:                        param.CreatedAt,
		CooperationProjectUndertakerType: param.CooperationProjectUndertakerType,
		Department:                       param.Department,
		Status:                           param.Status,
		Images:                           param.Attachment,
	}

	data.CooperationProjectSponsor.UsersCode = param.CooperationProjectSponsor.UserInfo.UsersCode
	data.CooperationProjectSponsor.UsersName = param.CooperationProjectSponsor.UserInfo.UsersName
	data.CooperationProjectSponsor.UserId = param.CooperationProjectSponsor.UsersId
	data.CooperationProjectSponsor.UserInfo.Phone = param.CooperationProjectSponsor.UserInfo.UserPhone
	data.CooperationProjectSponsor.UserInfo.UsersId = param.CooperationProjectSponsor.UsersId
	data.CooperationProjectSponsor.UserInfo.UsersName = param.CooperationProjectSponsor.UserInfo.UsersName
	data.CooperationProjectSponsor.UserInfo.UsersCode = param.CooperationProjectSponsor.UserInfo.UsersCode
	return &data
}

type CooperationApplicationItem struct {
	CooperationApplicationApplicant struct {
		Company    domain.CompanyData `json:"company"`
		Department domain.Department  `json:"department"`
		Org        domain.Org         `json:"org"`
		UserBaseID int                `json:"userBaseId,string"`
		UserID     int                `json:"userId,string"`
		UserName   string             `json:"userName"`
		UserCode   string             `json:"userCode"`
		UserInfo   domain.UserInfo    `json:"userInfo"`
		UserType   int                `json:"userType"`
	} `json:"cooperationApplicationApplicant"` //申请人
	CooperationApplicationDescription string              `json:"cooperationApplicationDescription"` //描述
	CooperationApplicationID          int                 `json:"cooperationApplicationId,string"`   //申请id
	CooperationApplicationStatus      int                 `json:"cooperationApplicationStatus"`      //状态
	CooperationApplyTime              int                 `json:"cooperationApplyTime"`              //申请时间
	CooperationApplicationAttachment  []domain.Attachment `json:"cooperationApplicationAttachment"`  //附件
	CooperationApplicationVerifyTime  int                 `json:"cooperationApplicationVerifyTime"`
}

func ToCooperationApplicationItem(param *allied_creation_cooperation.CooperationApplication) *CooperationApplicationItem {
	applicationID, _ := strconv.Atoi(param.CooperationApplicationID)
	data := CooperationApplicationItem{
		CooperationApplicationDescription: param.CooperationApplicationDescription,
		CooperationApplicationID:          applicationID,
		CooperationApplicationStatus:      param.CooperationApplicationStatus,
		CooperationApplyTime:              param.CooperationApplyTime,
		CooperationApplicationAttachment:  param.CooperationApplicationAttachment,
		CooperationApplicationVerifyTime:  param.CooperationApplicationVerifyTime,
	}
	data.CooperationApplicationApplicant.Company.CompanyID = param.Company.CompanyID
	data.CooperationApplicationApplicant.Company.CompanyName = param.Company.CompanyName
	data.CooperationApplicationApplicant.Department.DepartmentId = int(param.CooperationApplicationApplicant.Department.DepartmentId)
	data.CooperationApplicationApplicant.Department.DepartmentName = param.CooperationApplicationApplicant.Department.DepartmentName
	//data.CooperationApplicationApplicant.Department.DepartmentNumber = param.CooperationApplicationApplicant.Department.DepartmentNumber
	data.CooperationApplicationApplicant.Org.OrgID = param.Org.OrgID
	data.CooperationApplicationApplicant.Org.OrgName = param.Org.OrgName
	data.CooperationApplicationApplicant.UserID = param.CooperationApplicationApplicant.UserID
	data.CooperationApplicationApplicant.UserName = param.CooperationApplicationApplicant.UserInfo.UserName
	data.CooperationApplicationApplicant.UserCode = param.CooperationApplicationApplicant.UserInfo.UserCode
	//data.CooperationApplicationApplicant.UserType = param.CooperationApplicationApplicant.
	data.CooperationApplicationApplicant.UserInfo.Phone = param.CooperationApplicationApplicant.UserInfo.UserPhone
	data.CooperationApplicationApplicant.UserInfo.UsersName = param.CooperationApplicationApplicant.UserInfo.UserName
	data.CooperationApplicationApplicant.UserInfo.UsersId = param.CooperationApplicationApplicant.UserID
	return &data
	//return nil
}

type CooperationProjectSearchItem struct {
	allied_creation_cooperation.DataCooperationProjectSearchItem
	AuthFlag bool `json:"authFlag"` //当前用户是否可以编辑
}

func ToCooperationProjectSearchItem(items []allied_creation_cooperation.DataCooperationProjectSearchItem, operatorOrgId int) []CooperationProjectSearchItem {
	listData := []CooperationProjectSearchItem{}
	operatorOrgIdStr := strconv.Itoa(operatorOrgId)
	for i := range items {
		listItem := CooperationProjectSearchItem{
			DataCooperationProjectSearchItem: items[i],
			AuthFlag:                         operatorOrgIdStr == items[i].Org.OrgID,
		}
		listData = append(listData, listItem)
	}

	return listData
}