<template>
	<view>
		<view style="padding: 20rpx;background-color: #fff;">
			<u-search v-model="model.mainTitle" placeholder="请输入邮件标题" :showAction="true" actionText="搜索"
				:animation="true" @custom="search" @clear="search">

			</u-search>
		</view>
		<view style="background-color: #fff;">
			<u-cell-group><!-- $u.timeFormat(v.sendTime, 'yyyy-mm-dd') -->
				<u-cell v-for="(v,i) in list" :key="v.mailId" :title="v.sendUserName" :titleStyle="{fontSize:'36rpx'}"
					:value="showtime($u.timeFormat(v.sendTime, 'yyyy-mm-dd'))" @click="emailInfo(v.mailId,i)">
					<u-badge slot="icon" :isDot="true" type="error" v-if="v.isRead=='N'"></u-badge>
					<view slot="label" style="width: 100%;display: flex;align-items: center;margin-top: 20rpx;">
						<view style="max-width: 70%;">
							<u--text :lines="1" size="30rpx" :text="v.mainTitle"></u--text>
						</view>
						<view style="margin-left: 10rpx;" v-if="v.isHaveFile ==1">
							<u-icon name="attach" size="30rpx" color="#409EFF"></u-icon>
						</view>
					</view>
				</u-cell>
			</u-cell-group>
		</view>
		
		<view style="padding: 20rpx;" v-if="list.length> 0">
			<u-loadmore :status="status" fontSize="28rpx" />
		</view>
		<div v-if="list.length == 0">
			<u-empty mode="list" icon="/static/list.png" text="暂无内容" textSize="30rpx"></u-empty>
		</div>
		<u-action-sheet :actions="morelist" :closeOnClickOverlay="true" :closeOnClickAction="true" cancelText="取消"  title="更多" :show="show"  @select="selectClick" @close="show = false"></u-action-sheet>
	</view>
</template>

<script>
	import {showtime} from "@/utils/wjw.js"
	export default {
		data() {
			return {
				model: {
					pageNum: 1,
					pageSize: 20,
					mainTitle: null
				},
				total: 0,
				//加载状态
				status: 'loadmore',
				//邮件列表
				list: [],
				//搜索
				keyword: null,
				showtime:showtime,
				//更多
				show:false,
				morelist:[
					{
						name:'发件箱'
					},
				]
			}
		},
		onLoad() {
			let that = this
			this.getList()
		},
		onShow() {
			this.getUnreadEmail()
			this.getdbNum()
		},
		onNavigationBarButtonTap(e) {
		    this.show = true
		},
		onPullDownRefresh() {
			this.model.pageNum = 1
			this.list = []
			this.getList()
		},
		onReachBottom() {
			if (this.model.pageNum == this.total) {
				this.status = 'nomore'
			} else {
				this.model.pageNum++
				this.getList()
			}
		},
		methods: {
			//查询邮件列表
			getList() {
				let that = this
				uni.$u.http.get('/oa/mail/inbox', {
					params: that.model
				}).then(res => {
					uni.hideLoading()
					that.list = [...that.list, ...res.rows]
					if (res.total < this.model.pageSize) {
						that.status = "nomore"
					} else {
						that.status = "loadmore"
					}
					if (that.model.pageNum === 1) {
						that.total = Math.ceil(res.total / that.model.pageSize)
						uni.stopPullDownRefresh()
					}
				})
			},
			//查看邮件详细信息
			emailInfo(id, i) {
				if (this.list[i].isRead == 'N') {
					this.list[i].isRead = 'Y'
				}
				uni.navigateTo({
					url: "/pages/emailInfo/emailInfo?id=" + id+'&type=1'
				})
			},
			//获取未读邮件
			getUnreadEmail() {
				uni.$u.http.get('/oa/mail/count/unread').then(res => {
					if (res == 0) {
						uni.removeTabBarBadge({ //显示数字
							index: 2, //tabbar下标
						})
					} else {
						this.model.pageNum = 1
						this.list = []
						uni.startPullDownRefresh()
						uni.setTabBarBadge({ //显示数字
							index: 2, //tabbar下标
							text: '' + res + '' //数字
						})
					}
				})
			},
			//查询待办数量
			getdbNum(){
				uni.$u.http.get('/jflow/restful/DB_Todolist_Num?token='+uni.getStorageSync('jtoken')).then(res=>{
					// console.log(res)
					if(res.code==200){
						if (res.list.length > 0) {
							let qwe = 0
							for(let i in res.list){
								if(res.list[i].FK_Flow != '008' && res.list[i].FK_Flow != '009'){
									qwe = res.list[i].Total + qwe
								}
								if(i == res.list.length-1){
									uni.setTabBarBadge({ //显示数字
										index: 1, //tabbar下标
										text: '' + qwe + '' //数字
									})
								}
							}
						} else {
							uni.removeTabBarBadge({ //显示数字
								index: 1, //tabbar下标
							})
						}
					}
					
				})
			},
			//根据标题搜索邮件内容
			search() {
				uni.showLoading({
					title: "搜索中...",
					mask: true
				})
				this.pageNum = 1
				this.list = []
				this.getList()
			},
			//点击收件箱
			selectClick(e){
				uni.navigateTo({
					url:"/pages/inbox/inbox"
				})
			}
		}
	}
</script>

<style>

</style>