import { roadshowInterence } from '@/api/api.js';
import moment from 'moment';

export default {
	data() {
		return {
			dataLoading: false,
			default_tab: '周度统计表',
			select_date: '',
			staticTabs: [ '周度统计表','月度统计表','近1个月','近3个月','近6个月' ],
			tableTheadColumns: this.$route.path === '/sellerStatisticSeller' 
				? [
					`本周(${this.getWeekOrMonthDate(0)})`,
					`上一周(${this.getWeekOrMonthDate(1)})`,
					`上两周(${this.getWeekOrMonthDate(2)})`,
					`上三周(${this.getWeekOrMonthDate(3)})`,
					`上四周(${this.getWeekOrMonthDate(4)})`,
					`上五周(${this.getWeekOrMonthDate(5)})`]
				: ['上一周','本周','下一周'],//动态表头配置
			
			dialogForm:{},
			isShowDia: false,
			diaTitle: '路演详情',
			datalist:[],//表格数据
			totalGroupArr: [],//总合计列表

			departmentTabs:[{label:'ficc',val:'ficc'},{label:'权益',val:'权益'}],
			departmentAct:'ficc'
		}
	},
	computed:{
		departmentTheadColumns(){
			return this.departmentAct == 'ficc' ?  ['试用路演','正式路演','公开会议'] :  ['路演','沙龙']
		}
	},
	methods: {

		/* 切换顶部tab */
		changeTabHandle(tab,type) {
			// if(tab === this.default_tab) return
			$('.table-body-wrapper')[0].scrollTop = 0;
			this.default_tab = tab;
			switch(tab) {
				case '周度统计表':
					this.tableTheadColumns = this.$route.path === '/sellerStatisticSeller' 
						? [
							`本周(${this.getWeekOrMonthDate(0)})`,
							`上一周(${this.getWeekOrMonthDate(1)})`,
							`上两周(${this.getWeekOrMonthDate(2)})`,
							`上三周(${this.getWeekOrMonthDate(3)})`,
							`上四周(${this.getWeekOrMonthDate(4)})`,
							`上五周(${this.getWeekOrMonthDate(5)})`]
						: ['上一周','本周','下一周'];
					break;
				case '月度统计表':
					this.tableTheadColumns = this.$route.path === '/sellerStatisticSeller' 
						? [
							this.getWeekOrMonthDate(0,'month'),
							this.getWeekOrMonthDate(1,'month'),
							this.getWeekOrMonthDate(2,'month'),
							this.getWeekOrMonthDate(3,'month'),
							this.getWeekOrMonthDate(4,'month'),
							this.getWeekOrMonthDate(5,'month')]
						: [
								this.getWeekOrMonthDate(0,'month'),
								this.getWeekOrMonthDate(1,'month'),
								this.getWeekOrMonthDate(2,'month'),
								this.getWeekOrMonthDate(3,'month'),
							];
					break;
				default:
					this.tableTheadColumns = this.departmentTheadColumns;
					break;
			}

			let typeObj = {
				'近1个月': 1,
				'近3个月': 3,
				'近6个月': 6,
			};
			typeObj[tab] ? this.filterDate(typeObj[tab]) : this.filterDate(0);

			this.getTableData();
			
		},

		/* 处理数据结构 便于页面渲染 userid 时间用于弹窗获取列表*/
		filterTableData(data,userid= '') {
			let list = this.departmentAct == 'ficc' ? data.map(item => ([
				{
					key: '试用路演',
					value: item.TryOutNum,
					startDate: item.StartDate,
					endDate: item.EndDate,
					userid
				},
				{
					key: '正式路演',
					value: item.FormalNum,
					startDate: item.StartDate,
					endDate: item.EndDate,
					userid
				},
				{
					key: '公开会议',
					value: item.MeetingNum,
					startDate: item.StartDate,
					endDate: item.EndDate,
					userid
				}
			])) :data.map(item => ([
				{
					key: '路演',
					value: item.RoadShowNum,
					startDate: item.StartDate,
					endDate: item.EndDate,
					userid
				},
				{
					key: '沙龙',
					value: item.SalonNum,
					startDate: item.StartDate,
					endDate: item.EndDate,
					userid
				},
			]))
			
			return list.flat(Infinity);
		},

		/* 路演详情弹窗 */
		openDiaHandle({ startDate,endDate,userid,value,key }) {
			if(value === 0) return;

			this.diaTitle = key === '路演' || key === '沙龙' || key === '公开会议' ? `${key}详情` : '路演详情';
			this.dialogForm = {
				startDate,
				endDate,
				userid,
				key,
				departmentType:this.departmentAct
			}
			this.isShowDia = true;
		},

		/* 获取近几个月的日期范围 */
		filterDate(month) {
			if(month) {
				let date_before = moment().subtract(month,'M').format("YYYY-MM-DD");
				let date_now = moment().format("YYYY-MM-DD");
				let date = [date_before,date_now]
				this.select_date = date;
			}else {
				this.select_date = '';
			}
		},

		/* 	获取几周前 周一周日日期 或前几月月份*/
		getWeekOrMonthDate(weeknum,type='week') {
			if(type === 'week') {
				const weekStart = this.$moment().subtract(weeknum, 'week').startOf('isoWeek').format('YYYY.MM.DD'); //周一
				const weekEnd = this.$moment().subtract(weeknum, 'week').endOf('isoWeek').format('YYYY.MM.DD'); //周日
	
				// console.log(weekStart,weekEnd)
				return `${weekStart}~${weekEnd}`;
			} else {
				const month = this.$moment().subtract(weeknum,'M').format('YYYY.MM');
				return month;
			}
		},

		/* 选择日期改变清空高亮按钮 */
		dateChange() {
			this.default_tab = '';
			this.tableTheadColumns = this.departmentTheadColumns;

			this.getTableData();
		},
		// 头部的点击事件
		tabsHandler(item) {
			// this.default_tab = '周度统计表';
			this.departmentAct = item.val;
			this.changeTabHandle('周度统计表')
			// this.getTableData();
		}
	}
}