<template>
	<view class="lists">
		<view class="status_bar" :style="navHeight"></view>
		<!-- 	<view class="header-bg">
			<image class="header-bg-img" src="@/static/image/nav_bg@3x.png" mode="scaleToFill"></image>
		</view> -->
		<view class="main" :style="mainHeight">
			<view class="navs-block">
				<view class="navs-tab">
					<view class="navs-tab-item" @click="checkTab(0)">
						<text class="navs-tab-item_text" :class="{'item_text-active':activeTabIndex===0}">待处理</text>
						<view class="tab-item_line" :class="{'tab-item_line-active':activeTabIndex===0}"></view>
					</view>
					<view class="navs-tab-item"></view>
					<view class="navs-tab-item" @click="checkTab(1)">
						<text class="navs-tab-item_text" :class="{'item_text-active':activeTabIndex===1}">已处理</text>
						<view class="tab-item_line" :class="{'tab-item_line-active':activeTabIndex===1}"></view>
					</view>
				</view>
			</view>

			<list :style="scrollHeight" ref="list" class="scroll-area view" loadmoreoffset='100'
				@loadmore="scrolltolower" :show-scrollbar='false'>
				<refresh @refresh="refresh" :display="refreshing ? 'show' : 'hide'">
					<!-- <loading-indicator></loading-indicator> -->
				</refresh>
				<cell v-for="(item,index) in lists" :key="index">
					<view class="scroll-item">
						<view v-if="index===0" class="item-block item-block-sum">
							<text class="item-block-label">共 {{loadParams.total}} 项</text>
						</view>
						<view class="block-detal">
							<text class="block-detal-no">编号：{{item.ticketId}}</text>
							<view class="block-detal-head">
								<text
									class="detal-head-item-value detal-head-item-value-first">{{item.createdate}}</text>
								<text class="detal-head-item-value">{{item.viloationName}}</text>
								<text class="detal-head-item-value">{{item.childTypeName}}</text>
								<text
									class="detal-head-item-value detal-head-item-value-last status-done">{{item.status}}</text>
							</view>
							<view class="block-detal-body">
								<text class="detal-item-value">{{item.terminalName}}</text>
								<text class="detal-item-value">{{item.terminalAddress}}</text>
								<text class="detal-item-value">{{item.productName}}</text>
								<text class="detal-item-value">{{item.productQty}}</text>
								<!-- <view class="detal-item-value" v-for="(product,index2) in item.entries" :key='index2'>
									<text class="product-val">{{product.productName}}</text>
									<text class="product-val">{{product.barcodeTypeName}}</text>
									<text class="product-val"> {{product.barcode}}</text>
								</view> -->
							</view>
						</view>
						<button v-if="activeTabIndex===0 && handleStatustext(item) !=='--'" class="item-btn"
							type="default" @click="checkOrderStatus(item)"><text
								class="item-btn-text">{{handleStatustext(item)}}</text></button>
						<button v-if="activeTabIndex===1" class="item-btn" type="default"
							@click="checkOrderDetail(item)"><text class="item-btn-text">查看详情</text></button>
					</view>
				</cell>
				<cell>
					<view class="loading-more" v-if='lists.length>2'>
						<text class="loading-more-text">{{loadingText}}</text>
					</view>
				</cell>
				<cell>
					<view class="empty-block" v-if='lists.length===0'>
						<image class="icon-empty" src="@/static/image/no_data@3x.png" mode=""></image>
						<text class="empty-text">暂无数据</text>
					</view>
				</cell>
				<view class="planout-block-item__last"> </view>
			</list>

		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	import {
		apiGetMissionList,
		apiGetClueList
	} from '@/servers/breakwords.js'

	export default {
		data() {
			return {
				scrollTop: 0,
				isOpened: 'none',
				loadingText: '加载中...',
				refreshing: false,
				condition: {
					distributer: true,
					userId: '',
					type: '', // 全部9 代办0 已办2
				},
				page: {
					"currentPage": "1",
					"pageSize": "10",
				},
				loadParams: {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				},
				lists: [],
				activeTabIndex: 0
			}
		},
		computed: {
			...mapState(['sysinfo', 'userInfo']),
			navHeight() {
				return {
					'height': `${this.sysinfo.safeArea.top + 44}px`
				}
			},
			mainHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 44 }px`,
				}
			},
			scrollHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 44 - 27 }px`
				}
			},

		},
		created() {
			this.condition.userId = this.userInfo.code
			this.reset()
			this.getClueList()
		},
		methods: {
			async getClueList(isRefresh) {
				if (this.loadParams.isDone) {
					return
				}

				let params = {}

				if (this.activeTabIndex === 0) {
					this.condition.type = 0
				} else {
					this.condition.type = 2

				}

				params = {
					...this.page,
					...this.condition
				}

				uni.showLoading({
					title: '加载中'
				});
				setTimeout(function() {
					uni.hideLoading();
				}, 2000);
				const res = await apiGetMissionList(params)
				// const res = await apiGetClueList()
				// let res = {
				// 	"result": "S",
				// 	"message": "操作成功",
				// 	"data": {
				// 		"count": 27,
				// 		"page": 1,
				// 		"pagesize": 10,
				// 		"data": [{
				// 				"rno": "1",
				// 				"requestname": "测试111",
				// 				"pcurl": "",
				// 				"departmentname": "",
				// 				"sysname": "市场秩序",
				// 				"createdate": "2021-07-30",
				// 				"workflowtype": "-4001",
				// 				"appurl": "string",
				// 				"requestid": "-206",
				// 				"ticketId": "string",
				// 				"entryType": "1",
				// 				"status": "已处理"
				// 			},
				// 			{
				// 				"rno": "2",
				// 				"requestname": "联络函待填写收货信息",
				// 				"pcurl": "",
				// 				"departmentname": "",
				// 				"sysname": "市场秩序",
				// 				"createdate": "2021-07-30",
				// 				"workflowtype": "-4001",
				// 				"appurl": "T00001STE",
				// 				"requestid": "-203",
				// 				"ticketId": "T00001STE",
				// 				"entryType": "3",
				// 				"status": "已处理",
				// 				"viloationName": "影响质量",
				// 				"childTypeName": "childTypeName",
				// 				"terminalName": "terminalName",
				// 				"terminalAddress": "terminalAddress",
				// 				"productName": "productName",
				// 				"productQty": "productQty",
				// 			},
				// 			{
				// 				"rno": "3",
				// 				"requestname": "string",
				// 				"pcurl": "",
				// 				"departmentname": "",
				// 				"sysname": "市场秩序",
				// 				"createdate": "2021-07-30",
				// 				"workflowtype": "-4001",
				// 				"appurl": "string",
				// 				"requestid": "-200",
				// 				"ticketId": "string",
				// 				"entryType": "4",
				// 				"status": "已处理"
				// 			},
				// 			{
				// 				"rno": "4",
				// 				"requestname": "联络函待填写收货信息",
				// 				"pcurl": "",
				// 				"departmentname": "",
				// 				"sysname": "市场秩序",
				// 				"createdate": "2021-08-01",
				// 				"workflowtype": "-4001",
				// 				"appurl": "T00001UD0",
				// 				"requestid": "-208",
				// 				"ticketId": "T00001UD0",
				// 				"entryType": "1",
				// 				"status": "已处理"
				// 			}
				// 		]
				// 	}
				// }
				
				uni.hideLoading();

				const {
					count,
					pagesize,
					data,
					results
				} = res.data
				this.loadParams.total = count
				this.loadParams.totalPage = Math.ceil(count * 1 / pagesize * 1)


				if (isRefresh) {
					this.lists = data
					this.loadParams.isDone = false
				} else {
					this.lists = this.lists.concat(data)
				}

				setTimeout(() => {
					this.refreshing = false;
				}, 300)

			},
			refresh(e) {
				this.refreshing = true;
				this.reset()
				this.getClueList(true)
				// #ifdef APP-NVUE
				try {
					// console.log('-------', this.$refs.list)
					this.$refs.list.resetLoadmore();
				} catch (e) {
					console.log('onrefresh', e)
				}
				// #endif

			},
			scrolltolower() {
				console.log('onReachBottom');
				if (this.page.currentPage < this.loadParams.totalPage) {
					this.page.currentPage++
					this.getClueList()
				} else {
					this.loadParams.isDone = true
					this.loadingText = '-- 到底了 --'
				}
			},
			reset() {
				this.loadParams = {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				}
				this.page.currentPage = 1
				this.lists = []
				this.loadingText = '加载中'
			},
			checkTab(i) {
				this.activeTabIndex = i
				this.lists = []
				this.getClueList()
			},
			handleStatustext(item) {
				let text = '--'
				switch (item.entryType * 1) {
					case 1:
						text = '填写收货'
						break
					case 4:
						text = '确认收货'
						break
					case 3:
						text = '罚分申诉'
						break
				}
				return text
			},
			checkOrderStatus(item) {
				let entryType = ''
				switch (item.entryType * 1) {
					case 1:
						entryType = '2'
						uni.navigateTo({
							url: `/pages/breakwords-mission-receiver/breakwords-mission-receiver?info=${JSON.stringify(item)}&pageType=${entryType}`
						})
						break
					case 4:
						entryType = '5'
						uni.navigateTo({
							url: `/pages/breakwords-mission-confirm/breakwords-mission-confirm?info=${JSON.stringify(item)}&pageType=${entryType}`
						})
						break
					case 3:
						entryType = '6'
						uni.navigateTo({
							url: `/pages/breakwords-mission-appeal/breakwords-mission-appeal?info=${JSON.stringify(item)}&pageType=${entryType}`
						})
						break
				}

			},
			checkOrderDetail(item) {
				uni.navigateTo({
					url: `/pages/breakwords-mission-detail/breakwords-mission-detail?info=${JSON.stringify(item)}&pageType=10`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.view {
		flex-direction: column;
	}

	.header-bg {
		height: 284rpx;
		margin-top: -88rpx;
	}

	.header-bg-img {
		width: 750rpx;
	}

	.status_bar {
		background-color: #fff;
	}

	.lists {
		position: relative;
		flex-direction: column;
		background: #f0f4f5;
		flex: 1;
	}

	.main {
		position: relative;
		flex-direction: column;
		// margin-top: -164rpx;
		padding-top: 40rpx;
		background-color: #fff;
	}

	.scroll-area {
		background: #fff;
	}

	.navs-block {
		flex-direction: column;
		background-color: #fff;
	}

	.navs-tab {
		display: flex;
		flex: 1;
		justify-content: center;
		height: 50rpx;
		border-bottom: 1px solid #F4F5F6;
	}

	.navs-tab-item {
		position: relative;
		width: 60px;
		height: 50rpx;
	}

	.navs-tab-item_text {
		width: 60px;
		text-align: center;
		color: #333;
		font-size: 28rpx;
	}

	.item_text-active {
		color: #F62C29;
	}

	.tab-item_line {
		position: absolute;
		bottom: 0;
		left: 10px;
		width: 40px;
		height: 2px;
		background: transparent;
		border-radius: 1px;
	}

	.tab-item_line-active {
		background: #f62c29;
	}

	.middle {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 32rpx 64rpx 40rpx 48rpx;
	}

	.text-block {
		overflow: hidden;
		height: 96rpx;
		margin-bottom: 14rpx;
		border-bottom: 1px solid #f4f5f6;
	}

	.text-block-last {
		margin-bottom: 0;
		border-bottom: 0;
	}

	.middle-date {
		flex: 2;
		font-size: 28rpx;
		line-height: 96rpx;
		color: #333;
		margin-bottom: 8rpx;
	}

	.middle-date-des {
		flex: 3;
		font-size: 28rpx;
		line-height: 96rpx;
		height: 96rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		color: #888;
	}

	.picker-year {
		position: relative;
		border-radius: 4px;
		align-items: center;
		line-height: normal;
	}

	.uni-list-cell {
		flex: 1;
	}

	.uni-list-cell-db {
		flex: 1;
	}

	.picker-block {
		flex: 1;
	}

	.uni-input-text {
		font-size: 14px;
		color: #888;
	}

	.icon-arrow {
		position: absolute;
		right: 0;
		top: 16px;
		width: 12rpx;
		height: 20rpx;
		z-index: 10;
	}

	.planout-block-lists {
		display: flex;
		flex-direction: column;
	}

	.planout-block-item {
		margin-bottom: 32rpx;
	}

	.scroll-item {
		position: relative;
		flex: 1;
		display: flex;
		flex-direction: column;
		padding: 0 64rpx 0 48rpx;
		background-color: #fff;
	}

	.planout-block-item__last {
		height: 40px;
	}

	.item-block {
		overflow: hidden;
	}

	.item-block-label {
		font-size: 24rpx;
		color: #333;
	}

	.item-block__label {
		flex: 1;
		text-align: left;
		font-size: 32rpx;
		color: #333;
		font-weight: 500;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.item-block-code {
		padding-bottom: 16rpx;
	}

	.item-block-name {
		padding-top: 24rpx;
		padding-bottom: 32rpx;
	}

	.item-block-sum {
		padding-bottom: 10px;
		margin-top: 28rpx;
		border-bottom: 1px solid #f4f5f6;
	}

	.item-block-label-des {
		flex: 1;
		text-align: left;
		font-size: 28rpx;
		color: #333;
	}

	.block-detal {
		display: flex;
		flex-direction: column;
		background: #f8f8f8;
		border-radius: 20rpx;
		padding: 32rpx;
		margin-top: 32rpx;
	}

	.block-detal-no {
		margin-bottom: 10px;
		font-size: 24rpx;
	}

	.block-detal-head {
		display: flex;
		justify-content: space-between;
		border-bottom: 1px solid #ddd;
		padding-bottom: 6px;
	}

	.detal-head-item-value {
		flex: 1;
		text-align: center;
		font-size: 24rpx;
	}

	.detal-head-item-value-first {
		flex: 1.2;
		text-align: left;
	}

	.detal-head-item-value-last {
		flex: 0.7;
		text-align: right;
	}

	.status-done {
		color: #F62C29;
	}

	.product-val {
		margin-right: 10px;
	}

	.block-detal-body {
		display: flex;
		flex-direction: column;
		padding-top: 20rpx;
	}

	.detal-item-value {
		color: #333;
		font-size: 24rpx;
		margin-bottom: 8rpx;
		height: 32rpx;
	}

	.item-btn {
		position: absolute;
		right: 94rpx;
		bottom: 24rpx;
		height: 32px;
		line-height: 30px;
		background: #f61d30;
		border-radius: 8px;
		padding-top: 0;
		padding-bottom: 0;
	}

	.item-btn:after {
		border: 0;
	}


	.item-btn-text {
		color: #fff;
		font-size: 24rpx;
	}

	.btn {
		flex: 1;
		height: 92rpx;
		background: $wly-primary-color;
		border-radius: 46rpx;
	}

	.btn-text {
		color: #fff;
		font-size: 34rpx;
	}

	.loading-more {
		flex: 1;
		align-items: center;
		justify-content: center;
		padding-top: 14px;
		padding-bottom: 14px;
		text-align: center;
	}

	.loading-more-text {
		font-size: 12px;
		color: #999;
	}

	.empty-block {
		width: 750rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.icon-empty {
		width: 150rpx;
		height: 150rpx;
		margin-top: 300rpx;
	}

	.empty-text {
		color: #999;
		margin-top: 56rpx;
		font-size: 28rpx;
	}
</style>
