<template>
	<view class="listbox">
		<view class="fixedt">
			<!-- 选择器 -->
			<view class="flexc checkbox">
				<picker mode="date" fields="year"  @change='bindDateChangea'>
					<view class="chekt">
						<view>{{sbsj|| "申报时间"}}</view>
						<image :src="upimg"></image>
					</view>
				</picker>
				<!-- <picker range-key='label' :value="shidx" :range="shlist"  @change='bindDateChangeb'>
					<view class="chekt">
						<view>{{shzt || "申贷次数"}}</view>
						<image :src="upimg"></image>
					</view>
				</picker> -->
				<view class="reset" @click="getResetFn">
					<image :src="resetimg"></image>
				</view>
				<view class="topc flexc">
					<image :src="secimg"></image>
					<input  placeholder="请输入企业名称" confirm-type="search" v-model="usename" @confirm="getConfirm"/>
				</view>
			</view>
		</view>
		<!-- 列表 -->
		<view class="plr18">
			<bus-list :datalist="list" :wtdt="wtdt" :type="type" @getXzFn="getXzFn"></bus-list>
		</view>
		<view style="height: 100rpx;"></view>
		<view class="scanbox flexcc" @click='getScanFn'>
			<image :src="scanimg"></image>
			扫码查看
		</view>
	</view>
</template>

<script>
	import config from '@/config'
	const baseUrl = config.baseUrl
	const baseName = config.baseName
	import self from '@/utils/location.js';
	import busList from "@/work/components/business/list.vue"
	import {getListOss,getEwmList} from "@/api/common.js"
	import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
	export default{
		components:{busList},
		data(){
			return{
				secimg:require("@/static/images/home/hsearch.png"),
				upimg:require('@/work/static/images/sup.png'),
				resetimg:require('@/work/static/images/reset.png'),
				scanimg:require('@/work/static/images/scan.png'),
				list:[],
				pageSize: 10,
				pageNum: 1,
				reachflag: true,
				wtdt:'',
				// limit:''
				type:'nfiles',//file 老版,   nfiles新版
				sbsj:'',
				usename:'',
				// type:'//file  ',
			}
		},
		// 上拉触底加载更多触发事件
		onReachBottom() {
			if (this.reachflag) {
				this.pageNum++
				this.getDataFn()
			}
		},
		onLoad() {
			this.getDataFn()
		},
		methods:{
			checkPermi, checkRole,
			getDetail(e){
				this.$tab.navigateTo("/work/pages/file/wjdetails?id="+e)
			},
			getScanFn(){
				// 扫码识别
				var that = this;
				uni.scanCode({
					onlyFromCamera: true,
					autoZoom:false,
					scanType: ['qrCode'],
					success: function(red) {
						let result = red.result;
						var a=JSON.parse(result)
						// console.log(a)
						if (typeof result == 'string') {
							try {
								//type1,认证,2 文件
								if(a.type==1){
									that.$tab.navigateTo(`/work/pages/prove/index`)
								}else if(a.type==2){
									// {"type":"2","loanApplicationNumber":"RZDB202405281425037292766","bigType":"c","fileType":"dbyxh"},
									var params={
										loanApplicationNumber:a.loanApplicationNumber,
										bigType:a.bigType,
										type:a.fileType,
									}
									getEwmList(params).then(res=>{
										if(res.code==200){
											var data=res.data;
											if(data.url){
												that.getDown(data.url)
											}
										}
									})
								}else{
									
								}
							} catch (e) {
								uni.navigateBack({
									delta: 1
								})
								uni.showToast({
									title: '扫码查询失败,请检查二维码是否正确',
									icon: "none"
								})
								return
							}
						}
						
					},
					fail: function(e) {
						if (e && e.errMsg && e.errMsg.indexOf('scanCode:fail cancel') != -1) {
							return;
						}
						uni.showToast({
							title: '扫码失败',
							icon: "none"
						})
					},
				});
			},
			getDown(e){
				uni.showLoading({
					title: '加载中'
				});
				var url=baseUrl+e;
				let index1 = e.lastIndexOf("."); // 得到一个索引值
				let index2 = e.length;
				let type = e.substring(index1, index2);
				if ((type.indexOf('jpg') > -1 || type.indexOf('jpeg') > -1 || type.indexOf('png') > -1)) {
					uni.previewImage({
						current: 0,
						urls: [url],
						// background: '#ffffff'
					});
					uni.hideLoading();
				} else {
					uni.downloadFile({
						url: url,//文件的下载路径
						success(result) {
								uni.hideLoading()
							var filePath = result.tempFilePath;
							   uni.openDocument({
							     filePath: filePath,
							     showMenu: true,
							     success: function (res) {
							       // console.log('打开文档成功');
							     }
							   });
						},
						fail(res) {uni.hideLoading()}
					})
				}
			},
			getConfirm(){
				this.getrefreshData()
			},
			getTabFn(val){
				this.tabval=val
			},
			bindDateChangea(e){
				var val=e.detail.value;
				this.sbsj=val;
				this.getrefreshData()
			},
			bindDateChangeb(e){
				var val=e.detail.value;
				this.shzt=this.shlist[val].label;
				this.shztid=this.shlist[val].value;
				this.getrefreshData()
			},
			getResetFn(){
				// 重置
				this.sbsj='';
				this.usename='';
				this.getrefreshData()
			},
			getrefreshData(){
				this.pageNum=1;
				this.list=[];
				this.reachflag=true;
				this.getDataFn()
			},
			getDownloader(path,name){
				// #ifdef APP-PLUS
				self.getFilePermissions(function(res){
					if(res==1){
						uni.showLoading({
							title: '加载中'
						});
						var url = baseUrl + path;
						let dtask = plus.downloader.createDownload(url, {
							filename: 'file://storage/emulated/0/'+baseName+'/' + name
						}, (d, status) => {
							//d为下载的文件对象
							if (status == 200) {
								uni.hideLoading();
								uni.showToast({
									icon: 'none',
									mask: true,
									title: '已保存到文件夹:/'+baseName+'/'+ name, //保存路径
									duration: 3000,
								});
								
								//下载成功,d.filename是文件在保存在本地的相对路径,使用下面的API可转为平台绝对路径
								// let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
								// setTimeout(() => {
								// 	plus.runtime.openFile(d.filename); //选择软件打开文件
								// }, 1500)
							} else {
								//下载失败
								uni.hideLoading();
								plus.downloader.clear(); //清除下载任务
								uni.showToast({
									icon: 'none',
									mask: true,
									title: '下载失败,请稍后重试',
								});
							}
						})
						dtask.start();
					}else{
						uni.hideLoading();
						uni.showToast({
							title: '无法获取权限,文件下载将出错!',
							icon: 'none',
						})
					}			
				})
				// #endif
			},
			getXzFn(ite){
				// 本地路径开头使用file://,跟上手机文件本地目录storage/emulated/0,
				// 后缀是用于文件命名和格式修改,大家可以使用变量。
				var url =ite.url;
				var name=ite.name;
				this.getDownloader(url,name)			
			},
			getDataFn(){
				var params={
					pageSize:this.pageSize,
					pageNum: this.pageNum,
					// loanApplicationType:4,
				}
				if(this.sbsj){
					params['params[beginTime]']=this.sbsj
				}
				
				if(this.usename){
					params.enterpriseName=this.usename
				}
				getListOss(params).then(res=>{
					if(res.code==200){
						if (res.rows.length < this.pageSize) {
							this.reachflag = false
							this.wtdt = '到底了~';
						} else {
							var num = parseInt(res.rows.length) + parseInt(this.pageSize) * parseInt(this.pageNum -	1)
							if (num < res.total) {
								this.reachflag = true
								this.wtdt = ''
							} else {
								this.reachflag = false
								this.wtdt = '到底了~';
							}
						}
						if (this.pageNum == 1) {
							this.list = res.rows;
						} else {
							this.list = this.list.concat(res.rows)
						}
					}else{
						this.$toast(res.msg)
					}
				}) 
			},
		},
		
	}
</script>

<style lang="scss" scoped>
.fixedt{position: fixed;left: 0;right: 0;top: 0;z-index: 2;}
// .fixedt /deep/ uni-picker{flex: 1;}

.checkbox{padding:16rpx 32rpx;background-color: #efefef;
	.chekt{display: flex;align-items: center;margin-right: 12rpx;height:88rpx;
		view{text-align: center;
			font-weight: bold;font-size: 26rpx;color: #374B61;width: 136rpx;word-break: break-all;text-overflow: ellipsis;overflow: hidden;display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical;white-space: normal;}
		image{width: 16rpx;height: 12rpx;flex: 0 0 auto;margin-left: 4rpx;}
	}
}
.reset{width:56rpx; height: 88rpx;display: flex;align-items: center;justify-content: center;
	image{width: 26rpx;height: 30rpx;}
}
.topc{border-radius: 34rpx;height:68rpx;box-sizing: border-box;padding:0 28rpx;position: relative;flex:1;background-color:rgba(90, 135, 186, 0.2);width: 260rpx;
	margin-left: 20rpx;
	input{font-weight: 500;font-size: 26rpx;color: #222327;flex: 1;}
	image{width: 30rpx;height: 30rpx;margin-right: 16rpx;flex: 0 0 auto;}
}
// .listbox{padding-top: 120rpx;padding-bottom: 100rpx;}
.listbox{padding-top: 120rpx;}
.scanbox{position: fixed;left: 0;right: 0;bottom: 0;height: 100rpx;
background: #00A9F0;font-weight: bold;font-size: 30rpx;color: #FFFFFF;
	image{width: 26rpx;height: 26rpx;margin-right: 24rpx;}
}
</style>