<template>
	<view>
		<template v-if="title">
			<view><list-item :title="title" :lists="lists"></list-item></view>
			<view v-if="lists.length<=0" style="text-align: center;margin-top: 40rpx;">暂无{{typeName}}记录</view>
		</template>
	</view>
</template>

<script>
	import listItem from "@/components/listItem/listItem.vue";
	import api from "@/api/api";
	export default {
		components:{
			listItem
		},
		data() {
			return {
				title:[],
				lists:[],
				fileId:'',
				type:'1',
				userId: uni.getStorageSync('cffp_userId'),
				typeName:'',
				shareId:'',
				readId:''
			}
		},
		methods: {
			queryLists(obj){
				const params = {
					userId:this.userId,
					fileId:this.fileId,
					shareId:this.shareId?this.shareId:undefined,
					readId:this.readId?this.readId:undefined
				}
				api[obj.req](params).then(res=>{
					if(res['success']){
						const r = res['data'][obj.result];
						if(r && r.length>0){
							for(let i=0;i<r.length;i++){
								this.lists.push({id:r[i][obj.id],children:JSON.parse(JSON.stringify(obj.children))});
								for(let m=0;m<this.lists[i]['children'].length;m++){
									this.lists[i]['children'][m]['value'] = r[i][this.lists[i]['children'][m]['alias']] ? r[i][this.lists[i]['children'][m]['alias']] : this.lists[i]['children'][m]['value'];
									if(this.lists[i]['children'][m].needParam){
										this.lists[i]['children'][m]['link'] += r[i][obj.id]
									}
								}	
							}
						}
					}else{
						this.lists = []
					}
				})
			}
		},
		onLoad(option) {
			this.fileId = option.fileId;
			// type 1为分享明细 2阅读明细 3购买明细
			this.type = option.type;
			this.shareId = option.shareId;
			this.readId = option.readId;
		},
		mounted() {
			console.log('mounted')
			if(this.type=='1'){
				this.typeName = '分享';
				this.title = [
					{no:1,name:'分享时间',width: '40%'},
					{no:2,name:'分享链接',width: '35%'},
					{no:3,name:'阅读明细',width: '25%'},
				];
				this.queryLists(
					{
						req:'userShareLinkList',
						result:'userShareLinks',
						id:'shareId',
						children:[
							{no:1,name:'分享时间',width: '40%',value:'',link:null,alias:'shareDate'},
							{no:2,name:'分享链接',width: '35%',value:'',link:null,alias:'shareUrl'},
							{no:3,name:'阅读明细',width: '25%',value:'阅读明细',link:`/pages/commonDetail/commonDetail?fileId=${this.fileId}&type=2&shareId=`,needParam:true},
						]
					}
				);
			}else if(this.type=='2'){
				this.typeName = '阅读';
				this.title = [
					{no:1,name:'用户',width: '20%'},
					{no:2,name:'阅读链接',width: '30%'},
					{no:3,name:'阅读时间',width: '25%'},
					{no:4,name:'购买明细',width: '25%'}
				];
				this.queryLists(
					{
						req:'userShareReadList',
						result:'userShareReads',
						id:'readId',
						children:[
							{no:1,name:'用户',width: '20%',value:'',link:null,alias:'readName'},
							{no:2,name:'阅读链接',width: '30%',value:'',link:null,alias:'readUrl'},
							{no:3,name:'阅读时间',width: '25%',value:'',link:null,alias:'readDate'},
							{no:3,name:'购买明细',width: '25%',value:'购买明细',link:`/pages/commonDetail/commonDetail?fileId=${this.fileId}&type=3&readId=`,needParam:true},
						]
					}
				);
			}else if(this.type=='3'){
				this.typeName = '购买';
				this.title = [
					{no:1,name:'用户',width: '30%'},
					{no:2,name:'阅读链接',width: '30%'},
					{no:3,name:'购买订单',width: '40%'},
				];
				this.queryLists(
					{
						req:'userShareBuyList',
						result:'userShareLinks',
						id:'orderId',
						children:[
							{no:1,name:'用户',width: '30%',value:'',link:null,alias:'buyName'},
							{no:2,name:'阅读链接',width: '30%',value:'',link:null,alias:'buyUrl'},
							{no:3,name:'购买订单',width: '40%',value:'',link:`/pages/orderDetail/orderDetail?id=`,alias:'orderNo',needParam:true},
						]
					}
				);
			}
		}
	}
</script>

<style>

</style>
