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结束
	// 可以去除勾选的共创项目承接对象列表
	UndertakerTypesUncheckedAvailable []int `json:"undertakerTypesUncheckedAvailable"`
	//	 承接人
	Applicants []struct {
		UserId     int64 `json:"userId,string"`
		UserBaseId int64 `json:"userBaseId,string"` // 用户基本id
		Org        struct {
			OrgId   int64  `json:"orgId,string"` // 组织机构ID
			OrgName string `json:"orgName"`      // 组织名称
		} `json:"org"` // 用户所属组织机构
		Department struct {
			DepartmentId     int64  `json:"departmentId,string"` // 部门ID,
			DepartmentName   string `json:"departmentName"`      // 部门名称
			DepartmentNumber string `json:"departmentNumber"`    // 部门编码
		} `json:"department"` // 用户所属的部门
		UserInfo struct {
			UserAvatar  string `json:"userAvatar"`  // 用户头像
			UserEmail   string `json:"userEmail"`   // 用户邮箱
			UserName    string `json:"userName"`    // 共创人员姓名
			UserPhone   string `json:"userPhone"`   // 用户手机号
			UserAccount string `json:"userAccount"` // 用户账号,
			UserCode    string `json:"userCode"`    //	用户编码
		} `json:"userInfo,omitempty"`
	} `json:"applicants"`
}

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,
		UndertakerTypesUncheckedAvailable: param.UndertakerTypesUncheckedAvailable,
		Applicants:                        param.Applicants,
	}

	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
}