import moment from 'moment';

export default {
	data() {
		return {
			dataLoading: false,
			default_tab: '周度统计表',
			select_date: '',
			staticTabs: [ '周度统计表','月度统计表','近1个月','近3个月','近6个月' ],
			tableTheadColumns: [
				`本周(${this.getWeekOrMonthDate(0)})`,
				`上一周(${this.getWeekOrMonthDate(1)})`,
				`上两周(${this.getWeekOrMonthDate(2)})`,
				`上三周(${this.getWeekOrMonthDate(3)})`,
				`上四周(${this.getWeekOrMonthDate(4)})`,
				`上五周(${this.getWeekOrMonthDate(5)})`],//动态表头配置
			datalist:[],//表格数据
			totalGroupArr: [],//总合计列表
			// activeTab:{
			// 	tabName:'FICC', // FICC QY
			// 	tabSummationName:'ficc总合计',
			// 	productionId:1, // 1 FICC 2 权益
			// }

			filterDaysOptions: [
				{ label:'+60D',value: 1,msg:'合同到期后两个月内未签约' },
				{ label:'+30D',value: 4,msg:'合同到期后一个月内未签约' },
				{ label:'到期',value: 5,msg:'合同到期未签约' },
				{ label:'-30D',value: 6,msg:'合同到期前一个月' },
				{ label:'-60D',value: 7,msg:'合同到期前两个月' },
				{ label:'-90D',value: 8,msg:'合同到期前三个月' },
				{ label:'-120D',value: 9,msg:'合同到期前四个月' },
			],
			actFilterDay: 1
		}
	},
	computed: {
		Role() {
			return localStorage.getItem('Role');
		},
		activeTab(){
			let tabName = sessionStorage.getItem('renewalTab')?sessionStorage.getItem('renewalTab'):'FICC'
			return {
				tabName, // FICC QY
				tabSummationName:tabName == 'FICC'?'ficc总合计':'权益总合计',
				productionId:tabName == 'FICC'?1:2, // 1 FICC 2 权益
			}
		}
	},
	created(){
		if(this.Role.indexOf('rai')!=-1){
			this.activeTab.tabName = 'QY'
			this.activeTab.tabSummationName = '权益总合计'
			this.activeTab.productionId = 2
		}
	},
	methods: {
		// 切换 FICC 和 权益 TAB
    switchTab(tabName){
      this.activeTab.tabName = tabName
			this.activeTab.tabSummationName = tabName == 'FICC'?"ficc总合计":"权益总合计"
			this.activeTab.productionId = tabName == 'FICC'?1:2
			this.getTableData()
    },
		/* 切换顶部tab */
		changeTabHandle(tab) {
			$('.table-body-wrapper')[0].scrollTop = 0;
			this.default_tab = tab;

			switch(tab) {
				case '周度统计表':
					this.tableTheadColumns = [
						`本周(${this.getWeekOrMonthDate(0)})`,
						`上一周(${this.getWeekOrMonthDate(1)})`,
						`上两周(${this.getWeekOrMonthDate(2)})`,
						`上三周(${this.getWeekOrMonthDate(3)})`,
						`上四周(${this.getWeekOrMonthDate(4)})`,
						`上五周(${this.getWeekOrMonthDate(5)})`];
					break;
				case '月度统计表':
					this.tableTheadColumns = [
						this.getWeekOrMonthDate(0,'month'),
						this.getWeekOrMonthDate(1,'month'),
						this.getWeekOrMonthDate(2,'month'),
						this.getWeekOrMonthDate(3,'month'),
						this.getWeekOrMonthDate(4,'month'),
						this.getWeekOrMonthDate(5,'month')];

					break;
				default:
					this.tableTheadColumns = ['试用','活跃','正式'];

					break;
			}

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

			this.getTableData();

		},

	/* 	获取几周前 周一周日日期 或前几月月份*/
		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;
			}
		},

		/* 处理数据结构 便于页面渲染 */
		filterTableData(data,other_param={},type='new') {
			let list=[]
			if(type=='new'){
				list = data.map(item => ([
					{
						key: '试用',
						value: item.TryOutNum,
						ids: item.TryOutIds,
						...other_param
					},
					{
						key: '活跃',
						value: this.activeTab.tabName=='FICC'?[item.AllActiveNum,item.NoIncrementalActiveNum]:item.AllActiveNum,
						ids: this.activeTab.tabName=='FICC'?[item.AllActiveIds,item.NoIncrementalActiveIds]:item.AllActiveIds,
						...other_param
					},
					{
						key: '正式',
						value: item.FormalNum,
						ids: item.FormalIds,
						...other_param
					}
				]))
			}else if(type == 'renew'){
				list=data.map(item=>{
					if(this.activeTab.tabName !== 'QY'){
						return [
							{
								key: '到期',
								value: item.ExpireNum,
								ids: item.ExpireIds,
								...other_param
							},
							{
								key: '续约',
								value:item.RenewNum,
								ids: item.RenewIds,
								...other_param
							},
							{
								key: '续约异常',
								value:item.UnusualRenewNum,
								ids: item.UnusualRenewIds,
								...other_param
							}
						]
					}else{
						return [
							{
								key: '到期',
								value: item.ExpireNum,
								ids: item.ExpireIds,
								...other_param
							},
							{
								key: '续约',
								value:item.RenewNum,
								ids: item.RenewIds,
								...other_param
							},
						]
					}
				})
				// list = data.map(item => ([
				// 	{
				// 		key: '到期',
				// 		value: item.ExpireNum,
				// 		ids: item.ExpireIds,
				// 		...other_param
				// 	},
				// 	{
				// 		key: '续约',
				// 		value:item.RenewNum,
				// 		ids: item.RenewIds,
				// 		...other_param
				// 	}
				// ]))
			}else if(type==='ficcproduct'){
				list = data.map(item => ([
					{
						key: '正式',
						value: item.FormalNum,
						ids: item.FormalIds,
						...other_param
					},
					{
						key: '试用',
						value: item.TryOutNum,
						ids: item.TryOutIds,
						...other_param
					},
					{
						key: '合计',
						value: item.AllNum,
						ids: item.AllIds,
						...other_param
					},
				]))
				

			}

			return list.flat(Infinity);
		},

		/* 获取近几个月的日期范围 */
		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 = '';
			}
		},

		/* 选择日期改变清空高亮按钮 */
		dateChange() {
			this.default_tab = '';
			this.tableTheadColumns = ['试用','活跃','正式'];

			this.getTableData();
		}
	}
}