<template>
	<view>
		<cu-custom ref="cuCustom" bgColor="bg-gradual-blueness" :isBack="true" :isRingt="true" :backUrl="backUrl">
			<block slot="backText">返回</block>
			<block slot="content">我的任务</block>
		</cu-custom>
		<scroll-view scroll-x class="bg-white nav">
			<view class="flex text-center">
				<view class="cu-item flex-sub" :class="index==TabCur?'text-orange cur':''"
					v-for="(item,index) in tabNav" :key="index" @tap="tabSelect" :data-id="index">
					{{tabNav[index]}}
				</view>
			</view>
		</scroll-view>
		<block>
			<mescroll-body ref="mescrollRef" @init="mescrollInit"  @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
				<block v-if="TabCur==0">
					<uni-collapse :accordion="true">
						<uni-collapse-item :title="period" v-for="(period,index) in periodList" :key="index">
							<view class="tasks-warp" v-if="item.PERIOD_NAME==period" v-for="(item,index) in equipList" :key="item.SPECIALEQUIPMENT_ID">
								<view class="tasks-item">
									<view class="title">
										设备名称：{{item.EQUIPMENTNAME}}
									</view>
									<view class="tasks-state">
										设备类型：{{item.EQUIPMENTTYPENAME}}
									</view>
									<view class="tasks-state">
										巡检周期：{{item.PERIOD_NAME}}
									</view>
									<view class="flex">
										<view class="tasks-time">
											{{handleIsChecked(item)}}
										</view>
										<view class="">
											<button class="cu-btn bg-orange round sm" @click="$noMultipleClicks(goToAdd, item)">立即巡检</button>
										</view>
									</view>
								</view>
							</view>
						</uni-collapse-item>
					</uni-collapse>
				</block>
				<view class="tasks-warp" v-if="TabCur==1 && equipList.length>0" v-for="(item,index) in equipList" :key="item.SPECIALEQUIPMENT_ID">
					<view class="tasks-item">
						<view class="title">
							设备名称：{{item.EQUIPMENTNAME}}
						</view>
						<view class="tasks-state">
							设备类型：{{item.EQUIPMENTTYPENAME}}
						</view>
						<view class="tasks-state">
							巡检周期：{{item.PERIOD_NAME}}
						</view>
						<view class="flex">
							<view class="tasks-time">
								巡检次数: {{item.XJCOUNT}}
							</view>
							<view class="">
								<button class="cu-btn bg-orange round sm" @click="$noMultipleClicks(goToRecordList, item)">巡检记录</button>
							</view>
						</view>
					</view>
				</view>
				<view v-if="equipList.length<1" class="dy-null">
					<view class="dy-null-img">
						<image src="../../../static/null.png" mode=""></image>
					</view>
					<view class="dy-null-title">
						暂无数据
					</view>
				</view>
				<view class="cu-tabbar-height"></view>
			</mescroll-body>
		</block>
	</view>
</template>
<script>
  import { decryptAes } from "@/common/aesUtil";
	import {
		basePath,baseImgPath,corpinfoId,loginSession,loginUser,formatDate
	} from '@/common/tool.js';
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js"
	import writingBoard from "@/components/writing-board/writing-board.vue"
	export default {
		mixins: [MescrollMixin], // 使用mixin
		components: {
			writingBoard
		},
		data() {
			return {
        initflag:false,
				baseImgPath: baseImgPath,
				CustomBar: this.CustomBar,
				backUrl: '/pages/index/index',
				sTop: 0,
				totalHeight: 0,
				downOption: {
					use: true,
					auto: false
				},
				upOption: {
					use: true,
					auto: false,
					noMoreSize: 1,
					textNoMore: '--已经没有更多了--'
				},
				periodList:['每日','每周','每旬','每月'],
				scrollLeft: 0,
				equipList: [], //任务列表返回数据
				currentPage: 1, //分页参数--页数
				showCount: 100, //分页参数--每页数据条数
				totalCount: 0, //分页参数--初始化页数
				isEnd: false, //防止多次刷新
				totalPage: 0, //分页参数--分页数量
				noClick: true,
				TabCur: 0,
				tabNav: ['开始巡检', '巡检记录'],
				UNCHECK:'1'
			};
		},
		onLoad() {
			//页面显示或从后台跳回小程序时显示此页面时触发，从跳转页面返回时触发，不能传递参数
			var _this = this
			_this.background = require('@/common/background.json')
			_this.currentPage = 1
			_this.isEnd = false
			_this.getEquipInit()
			loginSession();
		},
    onShow(e) {
      var _this = this;
      if (_this.initflag) {
        _this.initflag = false;
        _this.currentPage = 1
        _this.isEnd = false
        this.getEquipInit()
      }
    },
		methods: {
			tabSelect(e) {
				this.TabCur = e.currentTarget.dataset.id;
				this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
				if (this.TabCur == 0) {
					this.equipList = []
					this.UNCHECK = '1'
					this.getEquipInit()
				} else if (this.TabCur == 1){
					this.equipList = []
					this.UNCHECK = ''
					this.getEquipInit()
				}
			},
			//重置分页
			initPageData() {
				var _this = this;
				_this.currentPage = 1;
				_this.isEnd = false;
			},
			//翻页
			turnPage() {
				var _this = this;
				_this.currentPage = _this.currentPage + 1;
				_this.isEnd = false;
			},
			//跳转事件
			downCallback(page) { //搜索按钮事件
				var _this = this;
				_this.getEquipQuery(page)
			},
			// 滑动到底方法
			upCallback(page) {
				var _this = this;
				_this.scrolltolowerEquip(page)
			},
			//切换选项卡初始化任务列表
			getEquipInit() { //搜索按钮事件
				var _this = this
				_this.initPageData()
				_this.equipList = []
				_this.getEquipData();
			},
			//下拉刷新任务列表
			getEquipQuery(page) {
				var _this = this;
				_this.initPageData()
				_this.equipList = [];
				_this.getEquipData();
			},
			//  上滑加载更多
			scrolltolowerEquip(page) {
				var _this = this;
				_this.turnPage()
				if (_this.totalPage >= _this.currentPage) {
					_this.getTaskData(); //开始分页查询
				}
			},

			getEquipData() {
				var _this = this;
				if (_this.isEnd) {
					return
				} else {
					this.isEnd = true;
				}
				uni.showLoading({
					title: "加载中..."
				}); //加载中动画
				uni.request({
					url: basePath + '/app/specialequipmentaqxj/list?showCount=' + _this.showCount + '&currentPage=' + _this.currentPage,
					method: 'POST',
					dataType: 'json',
					header: {
						'Content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						UNCHECK: _this.UNCHECK,
						tm: new Date().getTime(),
            CORPINFO_ID:loginUser.CORPINFO_ID,
            USER_ID:loginUser.USER_ID,
					},
					success: (res) => {
            res.data = decryptAes(res.data);
						uni.hideLoading(); //结束加载中动画
						if (res.data != null) {
							_this.totalPage = res.data.page.totalPage
							// 从后端获取的数据，将这一步骤放到异步获取后端数据的地方，对后端返回的数据进行处理
							if (_this.equipList.length == 0) {
								_this.equipList = res.data.varList;
							} else {
								if (res.data.varList != null) {
									_this.equipList = _this.equipList.concat( res.data.varList);
								}
							}
							_this.mescroll.endSuccess(res.data.varList.length, (res.data.page.totalPage > res.data.page.currentPage))
							_this.mescroll.endUpScroll(res.data.page.totalPage == res.data.page.currentPage)
						}
					},
					fail: () => {
						//  请求失败,隐藏加载状态
						_this.mescroll.endErr()
					}
				})
			},

			handleIsChecked(pd){
				var date =  formatDate(new Date(), 'yyyy-MM-dd hh:mm');
				if(pd.PERIOD_START_DATE > date){//如果今天在下次检查周期之前
					return "已检查"
				}else{
					return "未检查"
				}
			},
			goToAdd(item) {
				uni.navigateTo({
					url: '/pages/application/speEquip/add?SPECIALEQUIPMENT_ID=' + item.SPECIALEQUIPMENT_ID
				})
			},

			goToRecordList(item) {
				uni.navigateTo({
					url: '/pages/application/speEquip/record_list?SPECIALEQUIPMENT_ID=' + item.SPECIALEQUIPMENT_ID
				})
			}

		}
	}
</script>
