<template>
	<view class="pageInfo">
		<!-- 任务列表 -->
		<view v-for="item in list" :key="item.id" class="itemInfo">
			<view class="itemName">
				<span class="name">{{ item.name }}</span>
				<u-switch size="40" @change="changeItem(item)" v-model="item.status == 1"></u-switch>
			</view>
			<view class="itemName">
				<span>{{ item.startDate }} 至 {{ item.endDate }}</span>
				<span>
					<u-button type="success" size="mini" style="margin-right: 15upx;" @click="handetail(item)">查看</u-button>
					<u-button v-if="item.status == 0&&item.taskGenerateFlag==0" type="primary" size="mini" style="margin-right: 15upx;" @click="handedit(item)">编辑</u-button>
					<u-button v-if="item.status == 0&&item.taskGenerateFlag==0" type="error" size="mini" @click="handelete(item)">删除</u-button>
				</span>
			</view>
		</view>
		<view style="padding-top: 30vh;">
			<u-empty :text="noDataText" v-if="list.length == 0 && status != 'loading'" mode="list"></u-empty>
		</view>
		<view style="padding: 20upx;">
			<u-loadmore v-if="total > pageSize || status == 'loading'" :status="status" />
		</view>
	</view>
</template>

<script>
import {clzConfirm} from '@/libs/tools.js'
import { getCheckTaskConfigList, delCheckTaskConfig, enableCheckTaskConfig } from '@/api/checkTaskConfig.js';
export default {
	data() {
		return {
			status: 'loadmore',
			noDataText: '暂无数据',
			list: [],
			pageNo: 1,
			pageSize: 10,
			total: 0
		};
	},
	onLoad() {
		this.getList();
		uni.$on("updateCheckTaskConfigList",()=>{
			this.pageNo = 1;
			this.list = []
			this.getList();
		})
	},
	// 新增
	onNavigationBarButtonTap() {
		uni.navigateTo({
			url: '/pages/spotCheckConfigure/addSpotCheck'
		});
	},
	// 上拉分页
	onReachBottom() {
		console.log('onReachBottom');
		if(this.list.length< this.total){
		  this.pageNo++
		  this.getList()
		}else {
		  uni.showToast({
			title: '已经到底了',
			icon: 'none'
		  })
		  this.status = "nomore"
		}
	},
	methods: {
		// 获取列表
		getList() {
			let _this = this;
			let params = {
				pageNo: this.pageNo,
				pageSize: this.pageSize,
			};
			this.status = 'loading';
			getCheckTaskConfigList(params).then(res => {
				if (res.status == 200) {
					let list = res.data.list;
					if(_this.pageNo>1){
						_this.list = _this.list.concat(list);
					}else{
						_this.list = list
					}
					_this.total = res.data.count;
				} else {
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
					_this.noDataText = res.message;
				}
				_this.status = 'loadmore';
			});
		},
		// 启用禁用
		changeItem(item){
			let _this = this
			let status = item.status == 1 ? 0 : 1
			enableCheckTaskConfig({id:item.id,flag: status}).then(res=>{
				if(res.status == 200){
					console.log(_this.list,status)
					let i = _this.list.findIndex(a=>a.id == item.id)
					console.log(i)
					_this.list[i].status = status
					_this.list.splice()
				}
				uni.showToast({
					title: res.message,
					icon: 'none'
				})
			})
		},
		// 查看详情
		handetail(item) {
			uni.navigateTo({
				url: '/pages/spotCheckConfigure/spotCheckDetail/spotCheckDetail?id='+item.id
			});
		},
		// 编辑
		handedit(item) {
			uni.navigateTo({
				url: '/pages/spotCheckConfigure/addSpotCheck?id='+item.id
			});
		},
		// 删除
		handelete(item) {
			const _this = this;
			clzConfirm({
				title: '提示',
				content: '确认删除，删除后数据将无法恢复？',
				success: function(res) {
					if (res.confirm || res.index == 0) {
						delCheckTaskConfig({id:item.id}).then(ret=>{
							if(ret.status == 200){
								let i = _this.list.findIndex(a=>a.id == item.id)
								_this.list.splice(i,1)
							}
							uni.showToast({
								title: ret.message,
								icon: 'none'
							})
						})
					}
				}
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background: #f8f8f8;
	height: auto;
}
.itemInfo {
	margin: 20upx;
	background-color: #fff;
	border-radius: 3px;
	.itemName {
		padding: 25upx 35upx;
		display: flex;
		justify-content: space-between;
	}
	.itemName:first-child {
		border-bottom: 1px solid #f8f8f8;
	}
}
</style>
