import moment from 'moment';
import {ref} from "vue"
import $ from 'jquery'
import {useRoute} from "vue-router"

/* 处理数据结构 便于页面渲染 userid 时间用于弹窗获取列表*/
const filterTableData=(data,userid= '')=>{
	let list = 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
		}
	]))
	
	return list.flat(Infinity);
}

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

export const roadshowStatisticsHook=(params)=>{
	const {getTableData} = params||{}
	const $route = useRoute()

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

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

				break;
		}

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

		getTableData && getTableData();
		
	}

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

		diaTitle.value = key === '公开会议' ? `${key}详情` : '路演详情';
		dialogForm.value = {
			startDate,
			endDate,
			userid,
			key
		}
		isShowDia.value = true;
	}

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

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

		getTableData && getTableData();
	}

	return {
		datas:{
			dataLoading,
			default_tab,
			select_date,
			totalGroupArr,
			datalist,
			tableTheadColumns,
			staticTabs,
			dialogForm,
			isShowDia,
			diaTitle
		},
		functions:{
			filterTableData,
			dateChange,
			changeTabHandle,
			openDiaHandle
		}
	}
}