<template>
	<view>
		<uni-fab
			horizontal="right"
			vertical="bottom"
			@fabClick="toAdd"
		></uni-fab>
		<u-search :show-action="false" v-model="searchForm.mailDTO.title" @change="inputWord" margin="20rpx 50rpx"></u-search>
		<view>
			<u-swipe-action>
				<view
					v-for="(obj, index) in dataList"
					:key="index">
						<u-swipe-action-item @click="del(obj.id)"  :key="obj.id" :threshold="60" duration="500" 
						:options="options">
						  <u-cell-group>
							  <u-cell @click="toDetail(obj)">
								  <u-avatar slot="icon" size="32" randomBgColor :src="obj.sender.photo"></u-avatar>
								  <view slot="title" class="content">
										<view class="text-bold text-grey">
											<view class="ellipsis-description">
												标题：{{obj.mailDTO && obj.mailDTO.title}}
											</view>
										</view>
										<view class="text-grey text-sm">
											<view class="ellipsis-description">
												 发件人：{{obj.sender.name}}, {{obj.sendTime}}
											</view>
										</view>
										<view class="text-sm">
											<view class="ellipsis-description" v-html="`内容：${obj.mailDTO && obj.mailDTO.content || ''}`"></view>
										</view>
								  </view>
								  <view slot="right-icon" class="action">
									<u-tag v-if="obj.status === '0'" text="草稿" plain shape="circle" type="warning"></u-tag>
									<u-tag v-if="obj.status === '1'" text="已发送" plain shape="circle" type="primary"></u-tag>
									<u-tag v-if="obj.status === '2'" text="未读" plain shape="circle" type="error"></u-tag>
									<u-tag v-if="obj.status === '3'" text="已读" plain shape="circle" type="success"></u-tag>
								  </view>
								</u-cell>
						  </u-cell-group>
						</u-swipe-action-item>
				</view>
			</u-swipe-action>
		</view>
		<u-loadmore :status="status"  @loadmore="loadmore" :line="true" />
		<u-gap height="20" bgColor="#fff"></u-gap>

	</view>
</template>

<script>
	import mailTrashService from "@/api/mail/mailTrashService";
	export default {
		data() {
			return {
				status: 'loadmore',
				searchForm: {
					mailDTO: {
						title: "",
					}
				},
				dataList: [],
				tablePage: {
					pages: 0,
					currentPage: 0,
					pageSize: 10,
					orders: [{ column: "a.create_time", asc: false }],
				},
				loading: false,
				options: [ {
							text: '删除',
							style: {
								backgroundColor: '#f56c6c'
							}
				}]
			}
		},
		onLoad() {
			this.loadmore()
		},
		methods: {
			// 跳转到详细页面
			toDetail (mail) {
				uni.navigateTo({
				   url: '/pages/apps/mail/trashMailDetail?id='+mail.id
				})
			},
			toAdd (){
				uni.navigateTo({
				  url: '/pages/apps/mail/sendEmailForm'
				})
			},
			// 输入监听
			inputWord(e){
				this.searchTimer && clearTimeout(this.searchTimer)
				this.searchTimer = setTimeout(()=>{
					this.doSearch()
				},300)
			},
			// 搜索
			doSearch(){
				this.dataList = []; 
				this.tablePage.currentPage = 0;
				this.tablePage.pageSize = 10;
				this.tablePage.pages = 0;
				this.loadmore()
			},
			onReachBottom() {
				this.loadmore()
			},
			loadmore() {
				if(this.tablePage.currentPage!==0 && this.tablePage.pages <= this.tablePage.currentPage ) {
					this.status = 'nomore';
					return;
				}
				this.tablePage.currentPage = ++ this.tablePage.currentPage;
				//联网加载数据
				this.status = 'loading';
				mailTrashService.list({
					current: this.tablePage.currentPage,
					size: this.tablePage.pageSize,
					orders: this.tablePage.orders,
					...this.searchForm
				}).then((data)=>{
					//追加新数据
					this.dataList=this.dataList.concat(data.records);
					this.tablePage.pages = data.pages;
					if(this.tablePage.pages <= this.tablePage.currentPage){
						this.status = 'nomore'
					} else {
						this.status = 'loadmore'
					}
				})
				
			},
			del (id) {
				mailTrashService.delete(id).then((data)=>{
					this.doSearch()
					uni.showToast({
						title: data,
						icon:"success"
					})
				})
			},
		}
	}
</script>