<template>
	<view class="order-file-upload">
		<view class="upload-section">
			<view class="upload-title">
				<text class="title-text">{{title}}</text>
				<text class="upload-btn" @click="handleUpload">上传文件</text>
			</view>

			<view class="file-list" v-if="fileList.length > 0">
				<view class="file-item" v-for="(item, index) in fileList" :key="index">
					<view class="file-info">
						<text class="file-name">{{item.fileName}}</text>
						<text class="file-size">{{item.fileSize}}</text>
					</view>
					<view class="file-actions">
						<text class="delete-btn" @click="handleDelete(index)">删除</text>
					</view>
				</view>
			</view>

			<view class="upload-tip" v-else>
				<text class="tip-text">{{tipText}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	import upload from '../../mixins/upload';
	export default {
		name: 'OrderFileUpload',
		mixins : [upload],
		props: {
			title: {
				type: String,
				default: '文件上传'
			},
			orderFileType: {
				type: String,
				required: true
			},
			fileList: {
				type: Array,
				default: () => []
			},
			tipText: {
				type: String,
				default: '点击上传文件'
			},
		},
		data() {
			return {
			}
		},
		watch: {
			fileList: {
				handler(newVal) {
					this.uploadList = [...newVal];
				},
				immediate: true
			}
		},
		methods: {
			handleUploadSuccess(){
				uni.$u.toast("上传成功");
			},
			// 删除文件
			handleDelete(index) {
				uni.showModal({
					title: '提示',
					content: '确定要删除该文件吗？',
					success: (res) => {
						if (res.confirm) {
							this.uploadList.splice(index, 1);
							this.$emit('update:fileList', this.uploadList);
							this.$emit('change', {
								type: this.orderFileType,
								fileList: this.uploadList
							});
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.order-file-upload {
		.upload-section {
			background: #fff;
			margin-bottom: 20rpx;
			border-radius: 16rpx;
			overflow: hidden;
		}

		.upload-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 30rpx 40rpx;
			border-bottom: 1rpx solid #f5f5f5;

			.title-text {
				font-size: 32rpx;
				font-weight: 500;
				color: #333;
			}

			.upload-btn {
				padding: 12rpx 24rpx;
				background: #007aff;
				color: #fff;
				border-radius: 8rpx;
				font-size: 28rpx;
			}
		}

		.file-list {
			padding: 20rpx 40rpx;
		}

		.file-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 0;
			border-bottom: 1rpx solid #f5f5f5;

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

		.file-info {
			flex: 1;
			min-width: 0; // 防止flex子元素溢出

			.file-name {
				display: block;
				font-size: 30rpx;
				color: #333;
				margin-bottom: 8rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				max-width: 400rpx; // 设置最大宽度防止过长
			}

			.file-size {
				font-size: 26rpx;
				color: #999;
			}
		}

		.file-actions {
			.delete-btn {
				padding: 12rpx 24rpx;
				background: #ff4757;
				color: #fff;
				border-radius: 8rpx;
				font-size: 26rpx;
			}
		}

		.upload-tip {
			padding: 40rpx;
			text-align: center;

			.tip-text {
				font-size: 28rpx;
				color: #999;
			}
		}
	}
</style>