<template>
	<view  class='container'>
		<view v-if="dataList.length>0">
			<view v-for="(item,index) in dataList" :key="index" >
				<uni-card :title="item.mission?item.mission.name:''" :extra="item.status_name">
					<view class='card_body'>
						<view>
							巡查日期：{{item.date}}
						</view>
						<view>
							问题类型：{{item.ask?item.ask.value:''}}
						</view>
					</view>
					<view>
						巡查地点：{{item.site?item.site.name:item.address}}
					</view>
					<view>
						问题描述：{{item.ask_introduce}}
					</view>
					<view slot="actions" class="card-actions">
						<view class="card-actions-item" v-if="item.status==0" @click='callbackDetail(item)'>
							<button style='background-color: #DCDCDC;color: #333;' type="primary" size="mini">撤回</button>
						</view>
						<view class="card-actions-item" v-if="item.status==0" @click='editDetail(item.id)'>
							<button type="primary" size="mini">编辑</button>
						</view>
						<view class="card-actions-item" @click='todetail(item.id)'>
							<button type="primary" size="mini">查看</button>
						</view>
					</view>
				</uni-card>
			</view>
		</view>
		<view v-else>
			<o-empty />
		</view>

	</view>
</template>

<script>
	export default{
		data(){
			return{
				dataList:[],
				currentPage: 1,
				isLoading: false,
				statusList:[
				// 	{
				// 	id:-1,
				// 	value:'已撤回'
				// },
				{
				  id:0,
				  value:'整改中'
				},{
				  id:1,
				  value:'整改中'
				},{
				  id:2,
				  value:'待接收'
				},{
				  id:3,
				  value:'整改中'
				},{
				  id:4,
				  value:'整改中'
				},{
				  id:5,
				  value:'已整改'
				},{
				  id:6,
				  value:'退回'
				},{
				  id:7,
				  value:'整改中'
				},{
				  id:8,
				  value:'整改中'
				},{
				  id:9,
				  value:'整改中'
				}],
				myself_update:1
			}
		},
		onLoad(options) {
			if(options.isall){
				this.myself_update = 0
			}else{
				this.myself_update = 1
			}
			this.getPatrol(1)
		},
		onPullDownRefresh() {
			if (!this.isLoading) {
				this.getPatrol(this.currentPage + 1);
			}
		},
		onReachBottom() {
			if (!this.isLoading) {
				this.getPatrol(this.currentPage + 1);
			}
		},
		methods:{
			todetail(id){
				uni.navigateTo({
					url:'/pages/index/detail?id='+id
				})
			},
			editDetail(id){
				uni.navigateTo({
					url:'/pages/index/patrol?id='+id+'&type=edit'
				})
			},
			callbackDetail(item){
				let that = this
				uni.showModal({
					title: '提示',
					content: '是否确认撤回该检查',
					success: function (res) {
						console.log(res)
						if(res.confirm){
							that.util.request({
								api: '/api/admin/inspection/destroy',
								data:{id:item.id},
								method: 'get',
								utilSuccess: function(res) {
									uni.showToast({
										title: '撤回成功',
										icon: 'none'
									})
									setTimeout(function(){
										that.getPatrol(1)
									},1000)
								},
								utilFail: function(res) {
									that.util.toast(res)
								}
							})
						}
						
					},
					fail: function (res) {
						
					}
				})
				
				
				
				
			},
			getPatrol(page){
				uni.hideKeyboard()
				var that = this;
				that.isLoading = true;
				this.util.request({
					api: '/api/admin/inspection/index',
					data: {
						page: page,
						page_size: 6,
						myself_update:this.myself_update
					},
					utilSuccess: function(r) {
						var res = r.data;
						that.isLoading = false;
						var hasNoMore = r.total < 6 && page > 1;
						if(page==1&&res.length == 0){
							return
						}
						if (hasNoMore || res.length == 0 && page > 1) {
							uni.stopPullDownRefresh(); // 服务器总条数 < 每页条数, 会将第一页的条数重新返回
							// 已加载到最后一页
							uni.showToast({
								title: '已加载到最后一页',
								icon: 'none'
							});
							return;
						}
						var dataList = that.dataList;
						for(var k of res){
							that.statusList.map(item =>{
								if(item.id==k.status){
									k.status_name = item.value
								}
							})
						}
						if (page == 1) {
							dataList = res;
						} else {
							dataList.push(...res);
						}
				
						that.currentPage = page;
						that.dataList = dataList;
					},
					utilFail: function(res) {
				
						if (page == 1) {
				
							that.currentPage = page;
						}
						uni.showToast({
							title: res,
							icon: 'none'
						});
						// that.util.alert(res);
					}
				});
			}
		}
	}
</script>

<style scoped>
	.container{
		min-height:100vh;
		background-color: #fff;
		padding-top:30rpx;
		margin-top:-30rpx;
		padding-bottom: 30rpx;
	}
	
	.card_body{
		display: flex;
		justify-content: space-between;
	}
	.card-actions{
		text-align: right;
		border-top: 1px #DCDCDC solid;
		padding: 20rpx;
		padding-bottom: 6rpx;
		padding-right:0
	}
	.card-actions-item{
		display: inline-block;
		margin-left:10rpx;
	}
</style>