<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 class="scanbox flexcc">
			<image :src="scanimg"></image>
			扫码查看
		</view>
	</view>
</template>

<script>
	import config from '@/config'
	const baseUrl = config.baseUrl
	const baseName = config.baseName
	import busList from "@/work/components/business/list.vue"
	import {getListOss} 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)
			},
			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){
				console.log(path,name)
				// #ifdef APP-PLUS
				plus.android.requestPermissions([
						'android.permission.WRITE_EXTERNAL_STORAGE',
						'android.permission.READ_EXTERNAL_STORAGE',
						'android.permission.INTERNET',
						'android.permission.ACCESS_WIFI_STATE'
					], error => {
						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();
					}, success=> {
						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;}
	image{width: 30rpx;height: 30rpx;margin-right: 16rpx;flex: 0 0 auto;}
}
.listbox{padding-top: 120rpx;padding-bottom: 100rpx;}
.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>