<template>
	<view>
		<view class="section">
			<view class="title">检查情况</view>
			<view class="input_box">
				<view class="box">
					<uni-datetime-picker v-model="range" type="daterange" rangeSeparator="至" />
				</view>
			</view>
			<view class="btn" @click="get_trouble_grid_list()">查询</view>
		</view>

		<view class="content">
			<view class="list">
				<view class="item" v-for="(item,index) in grid_list" :key="index">
					<view class="name" @click="change_active(index)">
						<view class="left">
							<view class="text">{{item.gridName}}</view>
							<view class="tip">{{item.detailsGrid}}</view>
						</view>
						<view class="right">
							<uni-icons type="arrowright" v-if="active == index"></uni-icons>
							<uni-icons type="arrowdown" v-if="active != index"></uni-icons>
						</view>
					</view>
					<view class="box" v-if="active == index">
						<view class="box_item" v-for="(item_2,index_2) in item.gridList" :key="index_2">
							<view class="text">{{item_2.name}}</view>
							<view @click="go_detail(item_2.check_id)" v-if="item_2.status == 2" class="btn">已巡检</view>
							<view v-if="!item_2.status"class="btn" style="background-color: #E74C3C;">未巡检</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	Date.prototype.format = function(fmt){
	  var o = {
	    "M+" : this.getMonth()+1,                 //月份
	    "d+" : this.getDate(),                    //日
	    "h+" : this.getHours(),                   //小时
	    "m+" : this.getMinutes(),                 //分
	    "s+" : this.getSeconds(),                 //秒
	    "q+" : Math.floor((this.getMonth()+3)/3), //季度
	    "S"  : this.getMilliseconds()             //毫秒
	  };
	
	  if(/(y+)/.test(fmt)){
	    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
	  }
	        
	  for(var k in o){
	    if(new RegExp("("+ k +")").test(fmt)){
	      fmt = fmt.replace(
	        RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));  
	    }       
	  }
	
	  return fmt;
	}
	export default {
		data() {
			return {
				active:9999,
				
				// 时间范围
				range: [],
				
				// 网格数据
				grid_list:[]
			};
		},
		watch: {
			range(newval) {
				console.log("范围选:", this.range);
			},
		},
		onLoad() {
			// 初始化时间
			this.range[0] = new Date().format("yyyy-MM-01")
			this.range[1] = new Date().format("yyyy-MM-dd")
			
			this.get_trouble_grid_list()
		},
		methods:{
			change_active(index){
				if(this.active == index){
					// 关闭当前项
					this.active = 9999
				}else{
					// 展开当前项
					this.active = index
				}
			},
			
			get_trouble_grid_list(){
				uni.showLoading({
					mask: true
				})
				this.$api.trouble_grid_list({
					type:1,
					start_time:this.range[0],
					end_time:this.range[1]
				}).then((res)=>{
					uni.hideLoading()
					console.log(res.data.data)
					
					this.grid_list = res.data.data
				})
			},
			
			go_detail(id){
				uni.navigateTo({
					url:"./detail/detail?id="+id
				})
			}
		}
	}
</script>

<style lang="scss">
	.section {
		box-sizing: border-box;
		padding: 25rpx;

		.title {
			line-height: 60rpx;
			border-left: 8rpx solid #009FE8;
			box-sizing: border-box;
			padding-left: 20rpx;
			margin-bottom: 20rpx;
		}

		.input_box {
			margin-bottom: 20rpx;
			font-size: 32rpx;

			.box {
				width: 700rpx;
				min-height: 90rpx;
				line-height: 90rpx;
				background: #FFFFFF;
				border-radius: 8rpx;
				border: 2rpx solid #E9EBF2;

				box-sizing: border-box;
				padding: 0 25rpx;

				/deep/.uni-date-editor--x {
					.uniui-clear {
						display: none;
					}
				}

				/deep/.uni-date-x--border {
					box-sizing: border-box;
					border-radius: 4px;
					border: none;
				}

				/deep/.uni-date-x {
					padding: 0;
				}
			}
		}

		.btn {
			background-color: #009FE8;
			padding: 25rpx 50rpx;
			color: #FFFFFF;
			text-align: center;
		}
	}

	.content {
		box-sizing: border-box;
		padding: 25rpx;

		.list {
			.item {
				margin-bottom: 30rpx;
				box-sizing: border-box;
				padding: 25rpx;

				border: 1rpx solid #E9EBF2;
				border-radius: 4rpx;

				.name {
					display: flex;
					justify-content: space-between;
					align-items: center;

					border-left: 6rpx solid #009FE8;
					padding-left: 10rpx;

					.left {
						.text {
							line-height: 50rpx;
							font-size: 30rpx;
						}

						.tip {
							line-height: 40rpx;
							font-size: 28rpx;
							color: #999;
						}
					}

					.right {}
				}

				.box {
					margin-top: 10rpx;
					.box_item {
						display: flex;
						justify-content: space-between;
						align-items: center;
						
						box-sizing: border-box;
						padding: 20rpx 0;
						border-bottom: 1rpx solid #E9EBF2;

						.text {
							width: 450rpx;
							line-height: 40rpx;
							font-size: 30rpx;
						}

						.btn {
							padding: 10rpx 20rpx;
							background-color: #2ECC71;
							color: #FFFFFF;
							font-size: 26rpx;
						}
					}
				}
			}
		}
	}
</style>
