<template>
	<view class="my_table">
		<!-- 表头 -->
		<view class="my_table_row my_table_head">
			<!-- 是否复选框 -->
			<template v-if="props.haveSelection">
				<view class="selection td" @click.stop="handleClickAll">
					<view :class="
					{
						'checkbox':true,
						'active':details.isCheckAll === 2 || details.isCheckAll === 1,
						'PartialActivation': details.isCheckAll === 1
					}">
					</view>
				</view>
			</template>

			<block v-for="item in props.columnList">
				<view class="td" :style="{width: item.width || '100px'}">
					{{item.label}}
				</view>
			</block>
		</view>

		<!-- 表体 -->
		<template v-if="props.data.length > 0">
			<view class="my_table_body" :style="{height: props.isScroll && (props.tableHeight || details.tableHeight)}">

				<block v-for="(value, index) in props.data" :key="value">

					<view class="my_table_row my_table_data">
						<!-- 是否复选框 -->
						<template v-if="props.haveSelection">
							<view class="selection td" @click.stop="()=>handleCheckItem(value, index)">
								<view :class="{'checkbox':true, 'active':value.isCheck,}">
								</view>
							</view>
						</template>

						<block v-for="(item, index) in props.columnList" :key="item.label">
							<view class="td" :style="{width: item.width || '100px'}"
								@click="()=> item.click && item.click(item, index)">
								<template v-if="item.isDIY">
									<slot :scope="{
										row: value,
										column: item
									}"> </slot>
								</template>
								<template v-else>
									{{value[item.prop]}}
								</template>
							</view>
						</block>
					</view>
				</block>
			</view>
		</template>

		<template v-else>
			<view class="my_table_empty">
				暂无数据
			</view>
		</template>
	</view>
</template>

<script lang="ts" setup>
	import { reactive, defineProps, defineEmits, watch, onMounted, nextTick } from 'vue';
	import utils from '@/utils/utils.js';

	const props = defineProps({
		// 表头
		columnList: {
			type: Array,
			required: true
		},
		// 数据
		data: {
			type: Array,
			required: true
		},
		haveSelection: {
			type: Boolean,
			required: false,
			default: false
		},
		isScroll: {
			type: Boolean,
			required: false,
			default: false
		},
		tableHeight: {
			type: String,
			required: false,
			default: ''
		},
	})

	const emits = defineEmits(['selectionChange'])

	const details = reactive({
		/** 是否全选 0 -- 全部未选, 1 -- 部分选择, 2 -- 全部选择 */
		isCheckAll: 0 as 0 | 1 | 2,
		/** 被选中的数组 */
		selectionList: [],
		tableHeight: '50vh'
	})

	onMounted(async () => {
		if (!props.isScroll) return
		await nextTick()
		const timer = setTimeout(async () => {

			details.tableHeight = await utils.getViewDistanceFormTop('.my_table_body')
			clearTimeout(timer)
		}, 500)
	})


	/** 点击全选按钮 */
	const handleClickAll = () => {
		/** 全部取消 */
		const handleCheckByClose = () => {
			details.isCheckAll = 0

			for (let i = 0; i < props.data.length; i++) {
				props.data[i].isCheck = false
			}

			details.selectionList = []
		}

		/** 全选 */
		const handleCheckByAll = () => {
			details.isCheckAll = 2

			for (let i = 0; i < props.data.length; i++) {
				props.data[i].isCheck = true
			}

			details.selectionList = [...props.data]
		}

		switch (details.isCheckAll) {
			case 0:
				handleCheckByAll()
				break;

			case 1:
				handleCheckByAll()
				break;

			case 2:
				handleCheckByClose()
				break;
			default:
				break;
		}

	}

	/** 是否选中 */
	const handleIsCheck = () => {
		let _flag = false
		let _haveNot = false
		for (let i = 0; i < props.data.length; i++) {
			const item = props.data[i]

			!item.isCheck && (_haveNot = true)
			item.isCheck && (_flag = true)
		}

		if (!_flag) details.isCheckAll = 0
		else if (_haveNot) details.isCheckAll = 1
		else details.isCheckAll = 2
	}

	/** 点击 */
	const handleCheckItem = (value, index) => {
		value.isCheck = !value.isCheck

		if (value.isCheck) {
			details.selectionList.splice(index, 0, value)
		} else details.selectionList.splice(index, 1)

		handleIsCheck()
	}

	watch(() => props.data, () => {
		if (!props.haveSelection) return
		details.selectionList = []
		handleIsCheck()
	}, { deep: false, immediate: true })

	watch(() => details.selectionList, () => {
		if (!props.haveSelection) return
		console.log('details.selectionList :>> ', details.selectionList);
		emits('selectionChange', details.selectionList)
	}, { deep: true, immediate: true })
</script>

<style lang="scss" scoped>
	.my_table {
		$border: 2upx solid #bbb;

		border: $border;
		width: 100%;
		overflow-x: scroll;
		box-sizing: border-box;
		// color: #606266;
		font-size: 0.8rem;

		.my_table_row {
			width: fit-content;
			min-width: 100%;
			display: flex;

			>view {}
		}

		.my_table_head {
			font-weight: bold;

			.td {
				background-color: #f5f7fa;
				box-sizing: border-box;
			}
		}

		.my_table_data {
			font-size: 0.75rem;

			.td {
				background-color: #fff;
				box-sizing: border-box;
			}

			&:last-child {
				border-bottom: none;

				.td {
					border-bottom: none;
				}
			}
		}

		.my_table_empty {
			// border-top: $border;
			height: 80upx;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
		}


		.td {
			padding: 8upx;
			border-bottom: $border;
			// border-right: $border;
			display: inline-flex;
			align-items: center;
			// flex: 1;
			word-break: break-all;
			flex: none;

			&:last-child {
				border-right: none;
			}
		}

		.selection {
			padding: 8upx 20upx;
			flex: none;
		}
	}

	// 全选按钮
	.checkbox {
		border: 4upx solid #8d97a3;
		background-color: #fff;
		width: 40upx;
		height: 40upx;
		box-sizing: border-box;
		color: #8d97a3;
		border-radius: 6upx;
		position: relative;
		transition: all 0.3s;

		&::after {
			opacity: 0;
			content: '';
			border-bottom: 2px solid;
			border-left: 2px solid;
			position: absolute;
			left: 50%;
			width: 0;
			height: 0;
			transition: all 0.3s;
		}

		// 全选
		&.active {
			border-color: var(--subjectColor);
			color: var(--subjectColor);

			&::after {
				width: 30%;
				height: 70%;
				opacity: 1;
				border-color: var(--subjectColor);
				transform: translateX(-50%) rotateY(180deg) rotate(-45deg);
			}
		}

		// 部分选择
		&.PartialActivation {

			&::after {
				transform: translateX(-50%) rotateY(180deg) rotate(-90deg);
				border-bottom: none;
			}
		}
	}

	.my_table_body {
		min-height: 50vh;
		overflow-y: scroll;
		transition: all 0.3s;
		width: fit-content;
	}
</style>