import moment from 'moment';
import {computed,ref} from "vue"
import $ from 'jquery'

/* 	获取几周前 周一周日日期 或前几月月份*/
const getWeekOrMonthDate=(weeknum,type='week')=>{
	if(type === 'week') {
		const weekStart = moment().subtract(weeknum, 'week').startOf('isoWeek').format('YYYY.MM.DD'); //周一
		const weekEnd = moment().subtract(weeknum, 'week').endOf('isoWeek').format('YYYY.MM.DD'); //周日

		// console.log(weekStart,weekEnd)
		return `${weekStart}~${weekEnd}`;
	} else {
		const month = moment().subtract(weeknum,'M').format('YYYY.MM');
		return month;
	}
}

const 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:'合同到期前四个月' },
]

const Role=computed(()=>{
	return localStorage.getItem('Role');
})

export const dataReportStatisticHook=(params)=>{
	const {getTableData} = params||{}

	const dataLoading=ref(false)
	const default_tab=ref('周度统计表')
	const select_date=ref('')
	const totalGroupArr=ref([])//总合计列表
	const datalist=ref([])//表格数据

	const tableTheadColumns=ref([
		`本周(${getWeekOrMonthDate(0)})`,
		`上一周(${getWeekOrMonthDate(1)})`,
		`上两周(${getWeekOrMonthDate(2)})`,
		`上三周(${getWeekOrMonthDate(3)})`,
		`上四周(${getWeekOrMonthDate(4)})`,
		`上五周(${getWeekOrMonthDate(5)})`])//动态表头配置
	const staticTabs=ref([ '周度统计表','月度统计表','近1个月','近3个月','近6个月' ])
	const actFilterDay=ref(1)

	const activeTab=computed(()=>{
		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 权益
		}
	})

			// 切换 FICC 和 权益 TAB
	const switchTab=(tabName)=>{
		activeTab.value.tabName = tabName
		activeTab.value.tabSummationName = tabName == 'FICC'?"ficc总合计":"权益总合计"
		activeTab.value.productionId = tabName == 'FICC'?1:2
		getTableData && getTableData()
	}
	/* 处理数据结构 便于页面渲染 */
	const 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: activeTab.value.tabName=='FICC'?[item.AllActiveNum,item.NoIncrementalActiveNum]:item.AllActiveNum,
					ids: activeTab.value.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(activeTab.value.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);
	}

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

		getTableData && getTableData()
	}

	/* 切换顶部tab */
	const changeTabHandle=(tab)=>{
		$('.table-body-wrapper')[0].scrollTop = 0;
		default_tab.value = tab;

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

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

				break;
		}

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

		getTableData && getTableData();

	}
	
	/* 获取近几个月的日期范围 */
	const 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]
			select_date.value = date;
		}else {
			select_date.value = '';
		}
	}

	// start
	if(Role.value.indexOf('rai')!=-1){
		console.log(activeTab.value,'activeTab.value');
		activeTab.value.tabName = 'QY'
		activeTab.value.tabSummationName = '权益总合计'
		activeTab.value.productionId = 2
	}

	return {
		datas:{
			dataLoading,
			default_tab,
			select_date,
			totalGroupArr,
			datalist,
			tableTheadColumns,
			staticTabs,
			actFilterDay,
			filterDaysOptions
		},
		computeds:{
			Role,
			activeTab,
		},
		functions:{
			filterTableData,
			switchTab,
			dateChange,
			changeTabHandle
		}
	}
}


