<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">
						<picker class="search-type-text" @change="bindPickerTypesChange" range-key='name'
							mode="selector" :value="punishTypeSelected.name" :range="selectDataOptions.PunishType">
							<text class="uni-input-text">{{punishTypeSelected.name}}</text>
						</picker>
						<image class="icon-arrow-down" src="@/static/image/arrow_down@3x.png" mode=""></image>
					</view>
					<view class="navs-tab-item">
						<picker class="search-type-text" @change="bindPickerStatusChange" range-key='name'
							mode="selector" :value="orderStatusSelected.name"
							:range="selectDataOptions.ProcessOrderStatus">
							<text class="uni-input-text">{{orderStatusSelected.name}}</text>
						</picker>
						<image class="icon-arrow-down" src="@/static/image/arrow_down@3x.png" mode=""></image>
					</view>
					<view class="navs-tab-item">
						<picker class="search-type-text" @change="bindPickerYearChange" mode="date"
							:value="condition.year" fields='year'>
							<text class="uni-input-text">年度 {{condition.year}}</text>
						</picker>
						<image class="icon-arrow-down" src="@/static/image/arrow_down@3x.png" mode=""></image>
					</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 class="top">
							<text class="title-no">{{item.id}}</text>
							<view class="title-info">
								<text class="order-date">{{item.createDate}}</text>
								<text class="item-block-btn__text">{{item.statusName}}</text>
							</view>
						</view>
						<view class="middle view">
							<view class="middle-title">
								<text class="middle-title__text">处罚说明：{{item.notes}}</text>
							</view>
							<view class="text-block-box" v-for="(item2,index2) in item.punishEntries" :key="index2">
								<view class="text-block">
									<text class="middle-date">类型</text>
									<text class="middle-date-des">{{item2.punishTypeName}}</text>
								</view>
								<view class="text-block" v-if="item2.terminalName">
									<text class="middle-date">终端名称</text>
									<text class="middle-date-des">{{item2.terminalName}}</text>
								</view>
								<view class="text-block text-block-last">
									<view class="middle-date-right">
										<text class="middle-date">截止时间</text>
									</view>
									<text class="middle-date-des middle-date-right">{{item2.punishEndTime}}</text>
								</view>
							</view>
						</view>
						<button v-if="item.status==='CONFIRMED'" class="item-btn item-btn-l" type="default"
							@click="checkDetal(item,'edit')"><text class="item-btn-text">发起申诉</text></button>
						<button class="item-btn" type="default" @click="checkDetal(item,'view')"><text
								class="item-btn-text">查看</text></button>
					</view>
				</cell>
				<cell>
					<view class="loading-more" v-if='lists.length>6'>
						<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 {
		apiGetDictionary,
		apiGetProcessOrder,
	} from '@/servers/breakwords.js'

	import timeFormat from '@/uview-ui/libs/function/timeFormat.js'

	export default {
		data() {
			return {
				scrollTop: 0,
				isOpened: 'none',
				loadingText: '加载中...',
				refreshing: false,
				condition: {
					year: '',
					punishTypes: [],
					statusList: []
				},
				page: {
					"currentPage": "1",
					"pageSize": "30",
				},
				loadParams: {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				},
				selectedSalePlan: {},
				selectedSalePlanTypes: [],
				punishTypeSelected: {
					'code': '',
					'name': '全部类型'
				},
				orderStatusSelected: {
					'code': '',
					'name': '全部状态'
				},
				selectDataOptions: {
					'PunishType': [{
						'code': '',
						'name': '全部类型'
					}],
					'ProcessOrderStatus': [{
						'code': '',
						'name': '全部状态'
					}],
				},
				typesIndex: 0,
				lists: [],
			}
		},
		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 - 30 }px`
				}
			},

		},
		created() {
			this.condition.year = timeFormat(new Date(), 'yyyy')
			// 处罚类型
			this.getDictionary('PunishType')
			// 任务单状态
			this.getDictionary('ProcessOrderStatus')
			this.getProcessOrder()
		},
		methods: {
			async getDictionary(key) {
				const params = {
					dictionaryKey: key
				}
				const res = await apiGetDictionary(params)
				this.selectDataOptions[key] = [...this.selectDataOptions[key], ...res.data]
			},
			async getProcessOrder(isRefresh) {
				if (this.loadParams.isDone) {
					return
				}

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

				uni.showLoading({
					title: '加载中'
				});
				setTimeout(function() {
					uni.hideLoading();
				}, 2000);
				const res = await apiGetProcessOrder(params)
				uni.hideLoading();

				const {
					currentPage,
					totalNumberOfResults,
					numberOfPages
				} = res.data.pagination

				this.page.currentPage = currentPage
				this.loadParams.total = totalNumberOfResults
				this.loadParams.totalPage = numberOfPages


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

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

			},
			refresh(e) {
				this.refreshing = true;
				this.reset()
				this.getProcessOrder(true)
				// #ifdef APP-NVUE
				try {
					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.getProcessOrder()
				} 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 = '加载中'
			},
			bindPickerTypesChange(e) {
				console.log('bindPickerTypesChange', e.detail.value)
				const idx = e.detail.value || 0
				this.punishTypeSelected = this.selectDataOptions['PunishType'][idx]
				if (idx === 0) {
					this.condition.punishTypes = []
				} else {
					this.condition.punishTypes[0] = this.punishTypeSelected.code
				}

				this.getProcessOrder(true)
			},
			bindPickerStatusChange(e) {
				console.log('bindPickerStatusChange', e.detail.value)
				const idx = e.detail.value || 0
				this.orderStatusSelected = this.selectDataOptions['ProcessOrderStatus'][idx]
				if (idx === 0) {
					this.condition.statusList = []
				} else {
					this.condition.statusList[0] = this.orderStatusSelected.code
				}
				this.getProcessOrder(true)
			},
			bindPickerYearChange(e) {
				console.log('bindPickerYearChange', e.detail.value)
				this.condition.year = e.detail.value || 0
				this.getProcessOrder(true)
			},
			checkDetal(item,type) {
				uni.navigateTo({
					url: `/pages/breakwords-handle-detail/breakwords-handle-detail?id=${item.id}&type=${type}`
				})
			}
		}
	}
</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: #f0f4f5;
		padding: 0 16rpx;
		overflow-y: scroll;
		padding-bottom: 60rpx
	}

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

	.navs-tab {
		display: flex;
		flex: 1;
		align-items: center;
		height: 60rpx;
		border-bottom: 1px solid #F4F5F6;
		padding-bottom: 8rpx;
	}

	.navs-tab-item {
		display: flex;
		justify-content: center;
		flex: 1;
		align-items: center;
		height: 50rpx;
	}

	.search-type-text {
		color: #333;
		font-size: 28rpx;
	}

	.icon-arrow-down {
		width: 6px;
		height: 4px;
		margin-left: 16rpx;
	}

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

	.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;
		display: flex;
		flex-direction: column;
		flex: 1;
		background: #ffffff;
		border-radius: 16rpx 100rpx 16rpx 16rpx;
		margin-top: 28rpx;
		padding-top: 30rpx;
	}

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

	.top {
		height: 72rpx;
		align-items: center;
		justify-content: space-between;
		padding: 0 48rpx;
		border-bottom: 1px solid #f4f5f6;
	}

	.title-no {
		font-size: 24rpx;
		color: #555555;
	}

	.title-info {
		align-items: center;
	}

	.order-date {
		color: #333;
		font-size: 24rpx;
		margin-right: 16px;
	}

	.item-block-btn__text {
		font-size: 12px;
		color: #FF1D32;
	}

	.title-info__status-fail__text {
		color: #888888;
	}

	.middle {
		position: relative;
		padding: 26rpx 48rpx 40rpx;
	}

	.middle-title {
		display: flex;
		align-items: center;
		margin-bottom: 32rpx;
	}

	.middle-title__text {
		font-size: 24rpx;
		color: $text-base-color;
		font-family: PingFangSC, PingFangSC-Medium;
	}

	.icon-car {
		width: 13px;
		height: 13px;
		margin-left: 16rpx;
	}

	.text-block {
		flex: 1;
		margin-bottom: 12rpx;
	}

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

	.middle-date {
		font-size: 24rpx;
		color: #999;
		margin-right: 16rpx;
	}

	.middle-date-des {
		font-size: 24rpx;
		color: $text-base-color;
	}

	.text-block-box {
		display: flex;
		flex-direction: column;
		margin-bottom: 38rpx;
	}

	.middle-date-right {
		display: flex;
		align-items: center;
		justify-content: flex-end;
		text-align: right;
	}

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

	.item-btn-l {
		right: 168rpx;
	}

	.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>
