import {
	activity,
	User
} from "@/config/api.js"
let app = getApp()
export default {
	data() {
		return {
			page_no: 1,
			pageSize: 10,
			collectList: [],
			status: "loadmore",
			loadText: {
				loadmore: "上拉加载更多",
				loading: "加载中",
				nomore: "已经到底了",
			},
			listActivityStaus: [], //状态
			listActivityType: [], //类型
			listChartPermission: [], //分类
			listChartPermissionInit:[],
			activityTypeIds: '', //活动类型id 多个用 , 隔开
			chartPermissionIds: '', //行业id 多个用 , 隔开
			chartPermissionName: '行业分类',
			activityTypeName: '活动类型',
			selectShow: false,
			isShowJurisdiction: false,
			isGain: true,
			signupType: '', //关注类型
			goFollow: false, //是否关注公众号
			signupStatus: 'Success', //4个不同的类型提示文案
			isShow: false, //弹框
			isCancelShow: false,
			idTypeCancel: {
				id: '',
				type: 1,
				cutId: '',
				state: '',
				isNum: false
			},

			isAreaCode: false,
			areaCode: {
				id: '',
				type: ''
			},
			isNeedAddCountryCode: false,

			isId: 0,
			activityIdAdd: '',
			// 历史搜索列表
			historySearchList: [],
			hasPermission: '', //是否有权限
			jurisdictionList: {
				SellerMobile: '',
				PopupMsg: '',
				OperationMode: '',
			},
			isShowhasPermission: false, //拨打销售电话
			applyForIsShow: false, //潜在用户
			flag: false,
			activityTimeList:[{Id: 1, IsChoose: false, StatusName: "今日活动"},{Id: 2, IsChoose: false, StatusName: "明日活动"}],
			activityTimeStatus:''
		}
	},
	methods: {
		closeTheWindow(type) {
			if (type == 'statusstate') {
				const state = this.activeState ? this.activeState.split(',').map(Number) : []
				this.listActivityStaus.forEach(item => {
					if (state.indexOf(item.Id) !== -1) {
						item.IsChoose = true
					} else {
						item.IsChoose = false
					}
				})
			} else if (type == 'genre') {
				const types = this.activityTypeIds ? this.activityTypeIds.split(',').map(Number) : []
				this.listActivityType.forEach(item => {
					if (types.includes(item.ActivityTypeId)) {
						item.IsChoose = true
					} else {
						item.IsChoose = false
					}
				})
			} else {
				const chart = this.chartPermissionIds ? this.chartPermissionIds.split(',').map(Number) : []
				this.listChartPermission.forEach(item => {
					if (chart.includes(item.ChartPermissionId)) {
						item.IsChoose = true
					} else {
						item.IsChoose = false
					}
				})
			}
		},
		wanttosignup(id) {
			this.activityIdAdd = id
			this.selectShow = true
		},

		countryCcode() {
			User.countryCcode().then(res => {
				if (res.Ret == 200) {
					this.isNeedAddCountryCode = res.Data.IsNeedAddCountryCode
				}
			})
		},
		//报名
		signupAdd(id, type) {
			let timer = null;
			if (this.flag) return
			clearTimeout(timer)
			this.flag = true
			this.$store.dispatch('checkHandle').then(res => {
				app.globalData.isAuth = res.IsAuth;
				app.globalData.isBind = res.IsBind;
			})
			if (app.globalData.isBind && !app.globalData.isAuth) {
				uni.navigateTo({
					url: '/pages/login/login'
				})
				return
			} else if (app.globalData.isAuth) {
				uni.navigateTo({
					url: '/pages/authGuide/authGuide'
				})
				return
			}
			this.selectShow = false
			if (id == 'id') {
				id = this.activityIdAdd
			}
			if (this.isNeedAddCountryCode) {
				this.isAreaCode = true
				this.areaCode = {
					id,
					type
				}
			} else {
				activity.signupAdd({
					ActivityId: id,
					SignupType: type,
				}).then(res => {
					if (res.Ret == 200) {
						this.hasPermission = res.Data.HasPermission
						if (this.hasPermission == 1) {
							const index = this.collectList.findIndex(item => item.ActivityId == id)
							this.collectList[index].SignupType = type
							if (res.Data.GoFollow && res.Data.SignupStatus !== 'Success' || !res.Data
								.GoFollow) {
								this.isShow = true
							} else {
								this.goFollow = res.Data.GoFollow
							}
							this.signupType = res.Data.SignupType
							this.signupStatus = res.Data.SignupStatus
							this.idTypeCancel = {
								cutId: res.Data.ActivityId,
								state: 1
							}
						} else if (this.hasPermission == 2) {
							this.jurisdictionList.SellerMobile = res.Data.SellerMobile
							this.jurisdictionList.PopupMsg = res.Data.PopupMsg
							this.isShowhasPermission = true
						} else if (this.hasPermission == 3 || this.hasPermission == 4) {
							this.jurisdictionList.PopupMsg = res.Data.PopupMsg
							this.applyForIsShow = true
						}

					}
				})
			}
			timer = setTimeout(() => {
				this.flag = false
			}, 500)
		},
		signupCancel(id, type, time) {
			const str = time.replace(/-/g, '/')
			const date = new Date(str)
			const times = date.getTime()
			const num = new Date().getTime()
			if (times - num <= 3600000) {
				uni.showModal({
					confirmText: '知道了',
					showCancel: false,
					confirmColor: '#3385FF',
					content: type == 1 ? '活动开始前1小时内无法取消预约外呼，请联系对口销售处理' : '活动开始前1小时内无法取消报名，请联系对口销售处理'
				})
				return
			}
			this.isCancelShow = true
			this.idTypeCancel = {
				id,
				type,
				cutId: '',
				state: 0,
				isNum: true
			}
		},
		//去往详情页面
		goDetail(item) {
			this.public('show', true, '/activityPages/activityDetail/activityDetail?id=', item.ActivityId)
		},
		//是否展示有权限的行业
		isJurisdiction() {
			this.$store.dispatch('checkHandle').then(res => {
				app.globalData.isAuth = res.IsAuth;
				app.globalData.isBind = res.IsBind;
				if ((!res.IsAuth) && (!res.IsBind)) { //已授权已绑定
					if (this.isShowJurisdiction) {
						this.isId = 2
					} else {
						this.isId = 1
					}
					this.getUserSearchContent('traverse')
				} else if (res.IsAuth) { //未授权
					uni.navigateTo({
						url: '/pages/authGuide/authGuide'
					})
				} else if (res.IsBind && !res.IsAuth) { //已授权未绑定
					uni.navigateTo({
						url: '/pages/login/login'
					})
				}
			})
		},
		/* 初始化判断token  */
		authentication() {
			this.$store.dispatch('checkHandle').then(res => {
				app.globalData.isAuth = res.IsAuth;
				app.globalData.isBind = res.IsBind;
			})
		},
		/* 新布局产业选项确定按钮 */
		replacementConfirm(type) {
			this.page_no = 1
			if (type == 'statusstate') {
				const arr = []
				const str = []
				this.listActivityStaus.forEach(key => {
					if (key.IsChoose) {
						arr.push(key.Id)
						str.push(key.StatusName)
					}
				})
				this.activeState = arr.join(',')
				const arrTwo = []
				const strTwo = []
				this.activityTimeList.forEach(key => {
					if (key.IsChoose) {
						arrTwo.push(key.Id)
						strTwo.push(key.StatusName)
					}
				})
				this.activityTimeStatus = arrTwo.join(',')
				if (strTwo.length > 0) {
					this.activeStateName = strTwo.join(',')
				}else{
					if (str.length == this.listActivityStaus.length) {
						this.activeStateName = '所有状态'
					} else if (str.length <= 0) {
						this.activeStateName = '活动状态'
					} else {
						this.activeStateName = str.join(',')
					}
				}
			} else if (type == 'industry') {
				const arr = []
				const str = []
				this.listChartPermission.forEach(key => {
					if (key.IsChoose) {
						arr.push(key.ChartPermissionId)
						str.push(key.PermissionName)
					}
				})
				if (str.length == this.listChartPermission.length) {
					this.chartPermissionName = '所有行业'
				} else if (str.length <= 0) {
					this.chartPermissionName = '行业分类'
				} else {
					this.chartPermissionName = str.join(',')
				}
				this.chartPermissionIds = arr.join(',')

			} else {
				const arr = []
				const str = []
				this.listActivityType.forEach(key => {
					if (key.IsChoose) {
						arr.push(key.ActivityTypeId)
						str.push(key.ActivityTypeName)
					}
				})
				if (str.length == this.listActivityType.length) {
					this.activityTypeName = '所有活动'
				} else if (str.length <= 0) {
					this.activityTypeName = '活动类型'
				} else {
					this.activityTypeName = str.join(',')
				}
				this.activityTypeIds = arr.join(',')
			}
			this.page_no = 1
			this.collectList = []
			this.getList()
			this.selectComponent(`#${type}`).toggle();
		},
		/* 新布局产业选项重置按钮 */
		replacementBtn(type) {
			if (type == 'statusstate') {
				this.listActivityStaus.forEach(item => item.IsChoose = false)
				this.activityTimeList.forEach(key => {key.IsChoose=false})
				this.listActivityStaus[0].IsChoose = true
				this.activeState = '1'
				this.activityTimeStatus=''
				this.activeStateName = '未开始'
			} else if (type == 'industry') {
				this.listChartPermission=this.listChartPermissionInit
				this.chartPermissionIds = ''
				this.isShowJurisdiction = false
				this.isId = 2
				// this.getUserSearchContent()
				this.listChartPermission.map(item => item.IsChoose = false)
				this.chartPermissionName = '行业分类'
			} else {
				this.listActivityType.forEach(item => item.IsChoose = false)
				this.activityTypeIds = ''
				this.activityTypeName = '活动类型'
			}
		},
		//状态的下拉选择
		overallBtn(type, item) {
			this.listActivityStaus.forEach(key => {
				if (key.Id == item.Id) {
					key.IsChoose = !key.IsChoose
				}
			})
		},
		// 行业的下拉选择
		overallClick(type, item) {
			if (this.isShowJurisdiction) return
			this.listChartPermission.forEach(key => {
				if (key.ChartPermissionId == item.ChartPermissionId) {
					key.IsChoose = !key.IsChoose
				}
			})
		},
		//类型的下拉选择
		overallActivity(type, item) {
			this.listActivityType.forEach(key => {
				if (key.ActivityTypeId == item.ActivityTypeId) {
					key.IsChoose = !key.IsChoose
				}
			})
		},
		init() {
			this.signupType = ''
			this.goFollow = false
			this.signupStatus = ''
			this.isShow = false
			this.isCancelShow = false
		},
		public(type, flag, purl, price = '') {
			this.$store.dispatch('checkHandle', type).then(res => {
				app.globalData.isAuth = res.IsAuth;
				app.globalData.isBind = res.IsBind;
				if ((!res.IsAuth) && (!res.IsBind)) { //已授权已绑定
					if (flag) {
						uni.navigateTo({
							url: purl + price,
						});
					}
				} else if (res.IsAuth) { //未授权
					uni.navigateTo({
						url: '/pages/authGuide/authGuide'
					})
				} else if (res.IsBind && !res.IsAuth) { //已授权未绑定
					uni.navigateTo({
						url: '/pages/login/login'
					})
				}
			})

		},
		//今日，明日 活动的选择
		timeListBtn(type,item){
			this.activityTimeList.forEach(key => {
				if (key.Id == item.Id) {
					key.IsChoose = !key.IsChoose
				}
			})
		}
	},
}
