<template>
	<div class="upload-box">
		<el-upload
			v-model:file-list="_fileList"
			ref="uoloadRef"
			action=""
			list-type="picture-card"
			:class="['upload', self_disabled ? 'disabled' : '', drag ? 'no-border' : '', _fileList.length < limit ? '' : 'hide_upload']"
			:multiple="limit !== 1"
			:disabled="self_disabled"
			:limit="limit"
			:auto-upload="true"
			:before-upload="beforeUpload"
			:http-request="handleHttpUpload"
			:on-exceed="handleExceed"
			:on-error="uploadError"
			:drag="drag"
			:accept="fileType.join(',')"
		>
			<div class="upload-empty">
				<slot name="empty">
					<!-- <el-icon><Plus /></el-icon>
					<span v-if="emptyText">{{ emptyText }}</span> -->
					<div class="upload_inside">
						<el-icon><Plus /></el-icon>
						<text>从本地上传</text>
					</div>
				</slot>
			</div>
			<template #file="{ file }">
				<img :src="file.url" class="upload-image" />
				<div class="uploading" v-if="progressType">
					<el-progress class="custom-width" width="90" :stroke-width="3" type="circle" :percentage="fileData.progress" />
				</div>
				<div class="upload-handle" @click.stop>
					<div class="handle-icon" @click="handlePictureCardPreview(file)">
						<el-icon><ZoomIn /></el-icon>
					</div>
					<div v-if="!self_disabled" class="handle-icon" @click="handleRemove(file)">
						<el-icon><Delete /></el-icon>
					</div>
				</div>
			</template>
			<template #progress>
				<el-progress type="circle" v-if="progressType" :percentage="fileData.progress" />
			</template>
		</el-upload>
		<div class="el-upload__tip">
			<slot name="tip"></slot>
		</div>
		<el-image-viewer v-if="imgViewVisible" :url-list="[viewImageUrl]" @close="imgViewVisible = false" />
	</div>
</template>

<script setup lang="ts" name="UploadImgs">
import { ref, reactive, computed, inject, watch, onMounted } from 'vue';
import Sortable from 'sortablejs';
import OSS from 'ali-oss';
import { nanoid } from 'nanoid';
import { Plus } from '@element-plus/icons-vue';
import { getSsoUrl } from '/@/api/api';
import type { UploadProps, UploadFile, UploadUserFile } from 'element-plus';
import { ElNotification, formContextKey } from 'element-plus';

// let imgUrl = ref();

// let dialogCropping = ref(false);
// let rawFileRef = ref();

interface UploadFileProps {
	fileList: UploadUserFile[];
	api?: (params: any) => Promise<any>; // 上传图片的 api 方法，一般项目上传都是同一个 api 方法，在组件里直接引入即可 ==> 非必传
	fileCode?: string;
	drag?: boolean; // 是否支持拖拽上传 ==> 非必传（默认为 true）
	disabled?: boolean; // 是否禁用上传组件 ==> 非必传（默认为 false）
	limit?: number; // 最大图片上传数 ==> 非必传（默认为 5张）
	fileSize?: number; // 图片大小限制 ==> 非必传（默认为 5M）
	fileType?: any[]; // 图片类型限制 ==> 非必传（默认为 ["image/jpeg", "image/png"]）
	height?: string; // 组件高度 ==> 非必传（默认为 150px）
	width?: string; // 组件宽度 ==> 非必传（默认为 150px）
	emptyText?: string | boolean;
	dragSort?: boolean; // 是否支持拖拽排序 ==> 非必传（默认为 false）
	packageName?: string; //oss文件夹名称
}

const props = withDefaults(defineProps<UploadFileProps>(), {
	fileList: () => [],
	drag: true,
	disabled: false,
	limit: 1,
	fileSize: 5,
	fileType: () => ['image/jpeg', 'image/png'],
	height: '90px',
	width: '160px',
	fileCode: 'product',
	emptyText: '上传图片',
	dragSort: false,
	packageName: 'other',
});

let ossData: any = reactive({
	accessKeyId: '',
	accessKeySecret: '',
	securityToken: '',
});

let ossClient: any;
const progressType = ref(false);
let fileData = reactive({
	progress: 0,
});

const uoloadRef = ref();

// 获取 el-form 组件上下文
const formContext = inject(formContextKey, void 0);
// 获取 el-form-item 组件上下文
// const formItemContext = inject(formItemContextKey, void 0);
// 判断是否禁用上传和删除
const self_disabled = computed(() => {
	return props.disabled || formContext?.disabled || (progressType.value && fileData.progress != 100);
});

// eslint-disable-next-line vue/no-setup-props-destructure
const _fileList = ref<UploadUserFile[]>(props.fileList);

// 监听 props.fileList 列表默认值改变
watch(
	() => props.fileList,
	(n: UploadUserFile[]) => {
		_fileList.value = n;
	}
);

/**
 * @description 文件上传之前判断
 * @param rawFile 选择的文件
 * */
const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
	const imgSize = rawFile.size / 1024 / 1024 < props.fileSize;
	const imgType = props.fileType.includes(rawFile.type);
	if (!imgType)
		ElNotification({
			title: '温馨提示',
			message: '上传图片不符合所需的格式！',
			type: 'warning',
		});
	if (!imgSize)
		setTimeout(() => {
			const size = props.fileSize < 1 ? (props.fileSize * 1024).toFixed(0) + 'KB' : props.fileSize + 'M';
			ElNotification({
				title: '温馨提示',
				message: `上传图片大小不能超过 ${size}！`,
				type: 'warning',
			});
		}, 0);
	// 裁剪
	// if (imgType && imgSize) {
	// 	dialogCropping.value = true;
	// 	rawFileRef.value = rawFile;
	// 	console.log('rawFileRef:', rawFileRef);
	// 	imgUrl.value = window.URL.createObjectURL(rawFile);
	// }

	return imgType && imgSize;
};

/**
 * @description 图片上传
 * @param options upload 所有配置项
 * */
const handleHttpUpload = async (option: any) => {
	try {
		let res: any = await upload(option.file);
		if (res.res.status == 200) {
			const index = _fileList.value.findIndex((item) => item.uid === option.file.uid);
			_fileList.value.splice(index, 1, { url: res.res.requestUrls[0].split('?')[0], name: option.file.name });
			emit('update:fileList', _fileList.value);
			option.onSuccess({ url: res.res.requestUrls[0].split('?')[0] });
		} else {
			option.onError('上传失败');
		}
	} catch (error) {
		option.onError(error as any);
	}
};

//获取oss临时上传
const getOSSUrl = async () => {
	progressType.value = false;
	await getSsoUrl({}).then((res: any) => {
		if (res.code == 200) {
			ossData = res.data;
			console.log('🚀 ~ awaitgetSsoUrl ~ ossData:', ossData);
			ossClient = new OSS({
				endPoint: ossData.endPoint,
				region: ossData.region, // 为存储空间对应的地域域名，根据你的实际情况填写，如https://oss-cn-hangzhou.aliyuncs.com
				accessKeyId: ossData.accessKeyId, //  你的阿里云accessKeyId
				accessKeySecret: ossData.accessKeySecret, //  临时token  accessKeySecret: credentials.accessKeySecret,  //  你的阿里云accessKeySecret
				stsToken: ossData.securityToken,
				bucket: ossData.bucket, //  oss存储桶的名称
				// secure: ossData.secure, //http是false，https是true
				secure: true, //http是false，https是true
			});
		}
	});
};

//oss切片上传
const upload = async (file: any) => {
	await getOSSUrl();
	const uuid = nanoid();
	const index = file.name.lastIndexOf('.');
	const suffix = file.name.substring(index + 1);
	const ossFileName = props.packageName + '/' + uuid + '.' + suffix;
	return ossClient.multipartUpload(ossFileName, file, {
		progress: function (p) {
			if (p == 0) {
				progressType.value = true;
			}
			if (p !== 1) {
				fileData.progress = Math.ceil(p * 100);
			} else {
				fileData.progress = 100;
				progressType.value = false;
			}
		},
		partSize: 20 * 1024 * 1024,
	});
};

/**
 * @description 图片上传成功
 * @param response 上传响应结果
 * @param uploadFile 上传的文件
 * */
interface UploadEmits {
	(e: 'update:fileList', value: UploadUserFile[]): void;
}
const emit = defineEmits<UploadEmits>();
// const uploadSuccess = (response: { fileUrl: string } | undefined, uploadFile: UploadFile) => {
//   if (!response) return;
//   uploadFile.url = response.fileUrl;
//   emit("update:fileList", _fileList.value);
//   // 调用 el-form 内部的校验方法（可自动校验）
//   formItemContext?.prop && formContext?.validateField([formItemContext.prop as string]);
//   ElNotification({
//     title: "温馨提示",
//     message: "图片上传成功！",
//     type: "success"
//   });
// };

/**
 * @description 删除图片
 * @param file 删除的文件
 * */
const handleRemove = (file: UploadFile) => {
	// deleteOssFile(file.name);
	_fileList.value = _fileList.value.filter((item) => item.url !== file.url || item.name !== file.name);
	emit('update:fileList', _fileList.value);
};

/**
 * @description 图片上传错误
 * */
const uploadError = () => {
	ElNotification({
		title: '温馨提示',
		message: '图片上传失败，请您重新上传！',
		type: 'error',
	});
};

/**
 * @description 文件数超出
 * */
const handleExceed = () => {
	ElNotification({
		title: '温馨提示',
		message: `当前最多只能上传 ${props.limit} 张图片，请移除后上传！`,
		type: 'warning',
	});
};

/**
 * @description 图片预览
 * @param file 预览的文件
 * */
const viewImageUrl = ref('');
const imgViewVisible = ref(false);
const handlePictureCardPreview: UploadProps['onPreview'] = (file) => {
	viewImageUrl.value = file.url!;
	imgViewVisible.value = true;
};

const initDragSort = () => {
	const el = uoloadRef.value.$el.querySelectorAll('.el-upload-list')[0];
	Sortable.create(el, {
		onEnd: ({ oldIndex, newIndex }: any) => {
			// 交换位置
			const arr = _fileList.value;
			const page = arr[oldIndex];
			arr.splice(oldIndex, 1);
			arr.splice(newIndex, 0, page);
			emit('update:fileList', arr);
		},
	});
};

onMounted(() => {
	if (props.dragSort) {
		initDragSort();
	}
});
</script>

<style scoped lang="scss">
.is-error {
	.upload {
		:deep(.el-upload--picture-card),
		:deep(.el-upload-dragger) {
			border: 1px dashed var(--el-color-danger) !important;
			&:hover {
				border-color: var(--el-color-primary) !important;
			}
		}
	}
}
:deep(.disabled) {
	.el-upload--picture-card,
	.el-upload-dragger {
		cursor: not-allowed;
		background: var(--el-disabled-bg-color) !important;
		border: 1px dashed var(--el-border-color-darker);
		&:hover {
			border-color: var(--el-border-color-darker) !important;
		}
	}
}
:deep(.hide_upload) {
	.el-upload--picture-card {
		display: none;
	}
}
.upload-box {
	.no-border {
		:deep(.el-upload--picture-card) {
			border: none !important;
		}
	}
	:deep(.upload) {
		.el-upload-list--picture-card {
			display: flex;
		}
		.el-upload-dragger {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 100%;
			// width: 160px;
			// height: 90px;
			background-color: #f6f6f6;
			padding: 0;
			overflow: hidden;
			border: 1px dashed var(--el-border-color-darker);
			border-radius: val(--el-border-radius-base);
			&:hover {
				border: 1px dashed var(--el-color-primary);
			}
		}
		.el-upload-dragger.is-dragover {
			background-color: var(--el-color-primary-light-9);
			border: 2px dashed var(--el-color-primary) !important;
		}
		.el-upload-list__item,
		.el-upload--picture-card {
			width: v-bind(width);
			height: v-bind(height);
			background-color: transparent;
			border-radius: val(--el-border-radius-base);
		}
		.upload-image {
			width: 100%;
			height: 100%;
			object-fit: contain;
		}
		.uploading {
			position: absolute;
			top: 0;
			right: 0;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 100%;
			cursor: pointer;
			background: rgb(0 0 0 / 60%);
			padding: 15px;
			box-sizing: border-box;

			.el-upload-list--picture-card .el-progress {
				width: 90px;
			}
		}
		.upload-handle {
			position: absolute;
			top: 0;
			right: 0;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 100%;
			cursor: pointer;
			background: rgb(0 0 0 / 60%);
			opacity: 0;
			transition: var(--el-transition-duration-fast);
			.handle-icon {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				padding: 0 6%;
				color: aliceblue;
				.el-icon {
					margin-bottom: 15%;
					font-size: 140%;
				}
				span {
					font-size: 100%;
				}
			}
		}
		.el-upload-list__item {
			&:hover {
				.upload-handle {
					opacity: 1;
				}
			}
		}
		.upload-empty {
			display: flex;
			flex-direction: column;
			align-items: center;
			font-size: 12px;
			line-height: 30px;
			color: var(--el-color-info);
			.el-icon {
				font-size: 28px;
				color: var(--el-text-color-secondary);
			}
		}
	}
	.el-upload__tip {
		line-height: 15px;
		text-align: left;
	}
}
</style>
<style lang="scss">
.custom-width.el-progress {
	width: 90px;
}
.upload_inside {
	display: flex;
	flex-direction: column;
	text-align: center;
	justify-content: center;
	align-items: center;

	text {
		color: rgba($color: #000000, $alpha: 0.45);
	}
}

:deep(.el-upload--picture-card) {
	height: 100px;
	padding-top: 10px;
}
</style>
