<template>
	<view class="main">
		<view class="toptitle">
			<view class="toptitle_left" @click="ht">
				<u-icon name="arrow-left" color="#fff" size="20"></u-icon>
			</view>
			<view class="toptitle_title">
				备件详情
			</view>
		</view>
		<view class="" style="padding: 10px;background-color: #fff;border-radius: 5px;margin-bottom: 15px;">
			<u-subsection :list="list" fontSize="14" activeColor="#0052D8" :current="current" @change='change'></u-subsection>
		</view>
		
		<view class="box" v-if="current == 0">
			<view class="tips">
				<view class="tip">
				</view>
				<view class="tiptitle">
					基础资料
				</view>
			</view>
			<view class="box_item">
				<view class="f1">备件名称</view>
				<view class="f2">{{info.mc}}</view>
			</view>
			<view class="box_item" >
				<view class="f1">关联产品</view>
				<view class="f2">{{info.glcpmc || ''}}</view>
			</view>
			<view class="box_item" >
				<view class="f1">备件编号</view>
				<view class="f2">{{info.bh == null ? '' : info.bh}}</view>
			</view>
			<view class="box_item">
				<span class="f1">备件位置</span>
				<span class="f2">{{info.bjwz == null ? "" : info.bjwz}}</span>
			</view>
			<view class="box_item" style="display: flex;flex-direction: column;align-items: flex-start;">
				<view class="f1" style="margin-bottom: 15px;">备件介绍</view>
				<view class="f2" v-if="getBjjs()" v-html="getBjjs()" style="width: 100%;word-wrap: break-word;overflow-wrap: break-word;line-height: 1.6;"></view>
				<view class="f2" v-else style="color: #969696;">
					无
				</view>
			</view>
			<view class="box_item">
				<view class="f1">更换指引</view>
				<view class="f2">{{info.ghzy == null ? '' : info.ghzy}}</view>
			</view>
			<view class="box_item">
				<view class="f1">订货周期</view>
				<view class="f2">{{info.dhzq == null ? '' : info.dhzq }}</view>
			</view>
			<!-- <view class="box_item" style="display: flex;flex-direction: column;align-items: flex-start;">
				<view class="f1" style="margin-bottom: 15px;">宣传图</view>
			</view>
			<view class="f2" style="display: flex;flex-direction: column;align-items: center;justify-content: center;margin-top: -90rpx;padding: 20rpx;">
				<image style="width: 100%;margin-bottom: 20rpx;" v-for="item in info.xctp" :src="base_url + item.url" mode="widthFix"></image>
			</view> -->
		</view>
		<view class="box1" v-if="current == 1">
			<view class="tips">
				<view class="tip">
				</view>
				<view class="tiptitle">
					随机工具
				</view>
			</view>
			<view class="box_item1" v-for="(item, index) in info.sjbj" :key="index">
				<view class="left">
					<view class="left_image">
						<image v-if="item.type == 'pdf'" src='../../../static/PDF.png' mode=""></image>
						<image v-if="item.type == 'doc' || item.type == 'docx'" src='../../../static/WORD.png' mode=""></image>
						<image v-if="item.type == 'xlsx' || item.type == 'xls'" src='../../../static/WORD.png' mode=""></image>
						<image v-if="item.type == 'png' || item.type == 'jpg' || item.type == 'jpeg'" src='../../../static/img/picture.png' mode=""></image>
						<u-icon v-if="isVideoType(item.type)" name="play-circle-fill" color="#0052D8" size="40"></u-icon>
					</view>
					<view class="l_title">
						<view class="l_title_f1">
							{{item.name}}
						</view>
						<!-- <view class="l_title_f2">
							2024-11-13 12:09:21
						</view> -->
					</view>
					
				</view>
				<view class="right">
					<view @click="previewFile(item)">
						<u-button size="small" type="primary" text="查看"></u-button>
					</view>
				</view>
			</view>
		</view>
		
		<view class="box1" v-if="current == 2">
			<view class="tips">
				<view class="tip">
				</view>
				<view class="tiptitle">
					一年备件
				</view>
			</view>
			<view class="box_item1" v-for="(item, index) in info.ynbj" :key="index">
				<view class="left">
					<view class="left_image">
						<image v-if="item.type == 'pdf'" src='../../../static/PDF.png' mode=""></image>
						<image v-if="item.type == 'doc' || item.type == 'docx'" src='../../../static/WORD.png' mode=""></image>
						<image v-if="item.type == 'xlsx' || item.type == 'xls'" src='../../../static/WORD.png' mode=""></image>
						<image v-if="item.type == 'png' || item.type == 'jpg' || item.type == 'jpeg'" src='../../../static/img/picture.png' mode=""></image>
						<u-icon v-if="isVideoType(item.type)" name="play-circle-fill" color="#0052D8" size="40"></u-icon>
					</view>
					<view class="l_title">
						<view class="l_title_f1">
							{{item.name}}
						</view>
						<!-- <view class="l_title_f2">
							2024-11-13 12:09:21
						</view> -->
					</view>
					
				</view>
				<view class="right">
					<view @click="previewFile(item)">
						<u-button size="small" type="primary" text="查看"></u-button>
					</view>
				</view>
			</view>
		</view>
		
		<view class="box1" v-if="current == 3">
			<view class="tips">
				<view class="tip">
				</view>
				<view class="tiptitle">
					两年备件
				</view>
			</view>
			<view class="box_item1" v-for="(item, index) in info.lnbj" :key="index">
				<view class="left">
					<view class="left_image">
						<image v-if="item.type == 'pdf'" src='../../../static/PDF.png' mode=""></image>
						<image v-if="item.type == 'doc' || item.type == 'docx'" src='../../../static/WORD.png' mode=""></image>
						<image v-if="item.type == 'xlsx' || item.type == 'xls'" src='../../../static/WORD.png' mode=""></image>
						<image v-if="item.type == 'png' || item.type == 'jpg' || item.type == 'jpeg'" src='../../../static/img/picture.png' mode=""></image>
						<u-icon v-if="isVideoType(item.type)" name="play-circle-fill" color="#0052D8" size="40"></u-icon>
					</view>
					<view class="l_title">
						<view class="l_title_f1">
							{{item.name}}
						</view>
						<!-- <view class="l_title_f2">
							2024-11-13 12:09:21
						</view> -->
					</view>
					
				</view>
				<view class="right">
					<!-- 图片类型直接预览 -->
					<view v-if="item.type == 'png' || item.type == 'jpg' || item.type == 'jpeg'" @click="ylwj(item.url,item.type)">
						<u-button size="small" type="primary" text="查看"></u-button>
					</view>
					<!-- 其他文件类型：显示下载进度或查看按钮 -->
					<view v-else>
						<!-- 未下载：显示下载按钮 -->
						<view v-if="!item.downloaded && !item.downloading" @click="downloadFile(item, 'lnbj', index)">
							<u-button size="small" type="primary" text="下载"></u-button>
						</view>
						<!-- 下载中：显示进度条 -->
						<view v-if="item.downloading" class="download-progress">
							<view class="progress-text">{{item.downloadProgress || 0}}%</view>
							<view class="progress-bar">
								<view class="progress-fill" :style="{width: (item.downloadProgress || 0) + '%'}"></view>
							</view>
						</view>
						<!-- 已下载：显示查看按钮 -->
						<view v-if="item.downloaded" @click="ylwj(item.url,item.type, item.filePath)">
							<u-button size="small" type="success" text="查看"></u-button>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import viewRecordMixin from '@/common/mixins/viewRecordMixin.js'
	export default {
		mixins: [viewRecordMixin],
		data() {
			return {
				list: ['基础资料', '随机工具',"一年备件",'两年备件'],
				current: 0,
				info:{},
				base_url:"https://hhjj.antissoft.com"
			}
		},
		onLoad() {
			let info = uni.getStorageSync("bjxqdetail")
			if (typeof info === 'string') {
				try { info = JSON.parse(info); } catch (e) { info = null; }
			}
			const id = info && (info.id || info.Id)
			if (!id) {
				uni.showToast({ title: '缺少备件信息', icon: 'none' })
				setTimeout(() => uni.navigateBack(), 1500)
				return
			}
			this.bjzcxq({ id })
		},
		methods: {
			timestampToTime(timestamp) {
			    const date = new Date(timestamp); //时间戳为10位需*1000，13位不需乘
			    const Y = date.getFullYear() + '-';
			    const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
			    const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
			    const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
			    const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
			    const s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
			    
			    return Y + M + D + h + m + s;
			},
			getBjjs() {
				// 获取备件介绍，支持大小写兼容
				const bjjj = this.info.bjjj || this.info.Bjjj;
				if (bjjj != null && bjjj != '' && bjjj != undefined) {
					return bjjj;
				}
				return null;
			},
			bjzcxq(data){
				this.API.bjzcxq(data).then(res=>{
					if (!res || !res.data) {
						uni.showToast({ title: '获取备件详情失败', icon: 'none' })
						return
					}
					const resData = res.data
					if(resData.sjbj != null && resData.sjbj.length > 0){
						for(let i = 0; i < resData.sjbj.length; i++){
							if(resData.sjbj[i].url)
								resData.sjbj[i].type = resData.sjbj[i].url.substring(resData.sjbj[i].url.lastIndexOf('.') + 1)
						}
					}
					if(resData.ynbj != null && resData.ynbj.length > 0){
						for(let i = 0; i < resData.ynbj.length; i++){
							if(resData.ynbj[i].url)
								resData.ynbj[i].type = resData.ynbj[i].url.substring(resData.ynbj[i].url.lastIndexOf('.') + 1)
						}
					}
					if(resData.lnbj != null && resData.lnbj.length > 0){
						for(let i = 0; i < resData.lnbj.length; i++){
							if(resData.lnbj[i].url)
								resData.lnbj[i].type = resData.lnbj[i].url.substring(resData.lnbj[i].url.lastIndexOf('.') + 1)
						}
					}
					this.info = resData
					this.startViewTracking(this.info.id || data.id, '备件支持', this.info.mc, this.info.glcpmc)
				}).catch(err => {
					uni.showToast({ title: err.msg || err.message || '无权限查看或加载失败', icon: 'none' })
					setTimeout(() => uni.navigateBack(), 1500)
				})
			},
			// 判断是否为视频类型
			isVideoType(type) {
				const videoTypes = ['mp4', 'mov', 'avi', 'wmv', 'mkv', 'webm', 'mpeg', 'mpg', 'flv', '3gp'];
				return videoTypes.includes((type || '').toLowerCase());
			},
			// 判断是否为图片类型
			isImageType(type) {
				const imageTypes = ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp'];
				return imageTypes.includes((type || '').toLowerCase());
			},
			// 判断是否为PDF类型
			isPdfType(type) {
				return (type || '').toLowerCase() === 'pdf';
			},
			// 统一文件预览方法（在线预览，不支持下载）
			previewFile(item) {
				const fileUrl = 'https://hhjj.antissoft.com' + item.url;
				const fileType = (item.type || '').toLowerCase();
				
				// 图片预览
				if (this.isImageType(fileType)) {
					uni.previewImage({
						current: 0,
						urls: [fileUrl]
					});
					return;
				}
				
				// PDF预览
				if (this.isPdfType(fileType)) {
					uni.setStorageSync('wjurl', fileUrl);
					uni.navigateTo({
						url: '/pages/pdf/pdf'
					});
					return;
				}
				
				// 视频预览
				if (this.isVideoType(fileType)) {
					uni.setStorageSync('videoUrl', fileUrl);
					uni.setStorageSync('videoName', item.name || '视频');
					uni.navigateTo({
						url: '/pages/new/zlgl/video-preview'
					});
					return;
				}
				
				// 其他文件类型（Word、Excel等）提示不支持在线预览
				uni.showToast({
					title: '该文件类型暂不支持在线预览',
					icon: 'none',
					duration: 2000
				});
			},
			change(e){
				this.current = e
			},
			ht(){
				uni.navigateBack({
				    delta: 1
				});
			},
			cktp() {
				uni.previewImage({
					urls: ["http://115.29.210.249/tggPic/content/2021-09/1630475389708.jpg"], //需要预览的图片http链接列表，多张的时候，url直接写在后面就行了
					current: '', // 当前显示图片的http链接，默认是第一个
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.u-subsection--button{
		height: 50px;
	}
	.main {
		padding-top: 87px; // 标题栏80px + 间距7px
		background-color: #F8F8F8;
		min-height: 100vh;
		.toptitle{
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			width: 100%;
			background-color: #0052D8;
			display: flex;
			height: 80px;
			justify-content: center;
			align-items: center;
			z-index: 200;
			.toptitle_left{
				padding-top: 20px;
				position: absolute;
				left: 10px;
				z-index: 1000;
			}
			.toptitle_title{
				color: #fff;
				font-size: 14px;
				padding-top: 20px;
			}
		}
		.title {
			padding: 10px;
			border-radius: 5px;
			margin-bottom: 10px;
			background-color: #fff;
		}

		.box {
			border-radius: 5px;
			margin-bottom: 10px;
			background-color: #fff;
			
			.tips{
				display: flex;
				align-items: center;
				margin-bottom: 10px;
				border-bottom: 1px solid #E9E9E9;
				padding: 15px 10px;
				
				.tip{
					width: 7px;
					height: 20px;
					background-color: #0052D8;
					margin-right: 10px;
				}
				.tiptitle{
					font-size: 17px;
				}
			}
			
			.box_item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 10px 20px;
				margin-bottom: 20px;
				// border-bottom: 1px dashed #ccc;
				.f1 {
					font-size: 16px;
				}
				.f2{
					color:#969696;
					font-size: 16px;
				}
				.f3{
					color: #38beff;
					border-bottom: 1px solid #38beff;
				}
			}
		}
		.box1{
			background-color: #fff;
			.tips{
				display: flex;
				align-items: center;
				margin-bottom: 10px;
				border-bottom: 1px solid #E9E9E9;
				padding: 15px 10px;
				
				.tip{
					width: 7px;
					height: 20px;
					background-color: #0052D8;
					margin-right: 10px;
				}
				.tiptitle{
					font-size: 17px;
				}
			}
			.box_item1{
				padding: 10px 20px;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10px;
				.left{
					display: flex;
					align-items: center;
					flex: 1;
					.left_image{
						margin-right: 15px;
						image{
							width: 40px;
							height: 40px;
						}
					}
					.l_title{
						flex: 1;
						.l_title_f1{
							font-size: 15px;
							margin-bottom: 5px;
							width: 50vw;
							overflow:hidden; //超出的文本隐藏
							text-overflow:ellipsis; //溢出用省略号显示
							white-space:nowrap; //溢出不换行
						}
						.l_title_f2{
							font-size: 13px;
							color: #969696;
						}
					}
				}
				.right{
					min-width: 80px;
					display: flex;
					justify-content: flex-end;
				}
			}
		}
	}
</style>