<template>
	<view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='巡检记录'></cus-header>
		<div class="top" @tap="placeShow = true;">
			<div class="left">
				<image :src="imgBase+'inspection/record_location.png'"></image>
				<text>{{location}}</text>
			</div>
			<div class="right">
				<u-icon name="arrow-right" color="#FFFFFF" size="32"></u-icon>
			</div>
		</div>
		<div class="filtrate">
			<!-- <div class="pre" @tap="timeShow = true;">
				<text>巡检时间</text>
				<div style="transform: rotate(30deg);"><u-icon name="play-left-fill" color="#999999" size="24"></u-icon></div>
			</div> -->
			<div class="pre" @tap="typeShow = true">
				<text>{{xjType}}</text>
				<div style="transform: rotate(30deg);"><u-icon name="play-left-fill" color="#999999" size="24"></u-icon></div>
			</div>
			<div class="pre" @tap="userShow = true">
				<text>{{xjPerson}}</text>
				<div style="transform: rotate(30deg);"><u-icon name="play-left-fill" color="#999999" size="24"></u-icon></div>
			</div>
		</div>
		<div class="title">共有<span>{{total}}</span>条记录</div>
		<div class="boxs" v-if="list.length">
			<div class="box" v-for="(item,index) in list" :key="index">
				<div class="place">{{item.position||''}}</div>
				<div class="pre">巡检类型<span>{{xjObj[item.circuitType]||''}}</span></div>
				<div class="pre">巡检人员<span>{{item.circuitPerson.realName||''}}</span></div>
				<div class="pre">联系电话<span>{{item.circuitPerson.mobile||''}}</span></div>
				<div class="pre">巡检时间<span>{{item.circuitTime||''}}</span></div>
			</div>
		</div>
		<template v-else>
			<page-empty :height="'calc(100vh - '+(mt+150)+'px)'" marginTop="150px"></page-empty>
		</template>
		<u-picker :show="placeShow" :columns="placeColumns" keyName="orgName" @cancel="placeShow=false" @change="changeHandler" @confirm="placeConfirm" ref="uPicker"></u-picker>
		<!-- <u-picker :show="timeShow" :columns="timeColumns" title="巡检时间" @cancel="timeShow=false" @confirm="e=>paramsConfirm(e,'')"></u-picker> -->
		<u-picker :show="typeShow" :columns="typeColumns" title="巡检类型" keyName="label" @cancel="typeShow=false" @confirm="e=>paramsConfirm(e,'circuitType','xjType')"></u-picker>
		<u-picker :show="userShow" :columns="userColumns" title="巡检人员" keyName="label" @cancel="userShow=false" @confirm="e=>paramsConfirm(e,'circuitPersonId','xjPerson')"></u-picker>
	</view>
</template>

<script>
	import pageEmpty from '@/components/pageEmpty/index.vue'
	export default {
		components:{
			pageEmpty
		},
		data(){
			return {
				location:'',
				timeShow:false,
				timeColumns:[
					['不限','2025-01-09','2025-01-08']
				],
				placeShow:false,
				placeColumns:[],
                placeColumnData: [],
				xjType:'巡检类型',
				xjPerson:'巡检人员',
				typeShow:false,
				typeColumns:[
					[{value:'',label:'全部'}]
				],
				userShow:false,
				userColumns:[
					[{value:'',label:'全部'}]
				],
				isOver:false,
				params:{
					page:1,
					limit:10,
					buildingId:'',
					storeyId:'',
					circuitType:'',
					circuitPersonId:''
				},
				list:[],
				total:0,
				xjObj:{},
			}
		},
		onReachBottom() {
			if(this.isOver) return
			this.getList();
		},
		async onLoad() {
			await this.getXjTypeData();
			this.getUserList();
			this.getFloorInfo();
			this.getList();
		},
		methods:{
			async getXjTypeData(){
				let res = await	this.$api.get('/sys/dict/data/getListByType/ReviewType');
				if(res.data.code===0){
					res.data.data.forEach(d=>{
						this.xjObj[d.dictValue] = d.dictLabel;
						this.typeColumns[0].push({value:d.dictValue,label:d.dictLabel})
					})
				}else this.$showToast(res.data.msg)
			},
			getUserList(){
				this.$api.get('/user/list',{page:1,limit:9999}).then(res=>{
					if(res.data.code===0){
						res.data.data.forEach(d=>{
							this.userColumns[0].push({value:d.id,label:d.realName})
						})
					}else this.$showToast(res.data.msg)
				})
			},
			getFloorInfo(){
				this.$api.get('/control/getOrgStructureTree/').then(res=>{
					if(res.data.code===0){
						this.location = res.data.data[0].orgName;
						this.placeColumns = [
							res.data.data[0].childrenList,
							res.data.data[0].childrenList[0].childrenList
						]
						let allfloor = [];
						for (let i = 0; i < res.data.data[0].childrenList.length; i++) {
							allfloor.push(res.data.data[0].childrenList[i].childrenList)
						}
						this.placeColumnData = allfloor;
					}else this.$showToast(res.data.msg)
				})
			},
			getList(){
				this.$api.get('/circuitrecord/page',this.params).then(res=>{
					if(res.data.code===0){
						this.total = res.data.data.total;
						if(this.list.length<res.data.data.total){
							this.params.page++;
							this.list = [...this.list,...res.data.data.list];
						}else this.isOver = true
					}else this.$showModal(res.data.msg)
				});
			},
            changeHandler(e) {
                const {
                    columnIndex,
                    value,
                    values, // values为当前变化列的数组内容
                    index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
                    picker = this.$refs.uPicker
                } = e
                // 当第一列值发生变化时，变化第二列(后一列)对应的选项
                if (columnIndex === 0) {
                    // picker为选择器this实例，变化第二列对应的选项
                    picker.setColumnValues(1, this.placeColumnData[index])
                }
            },
			placeConfirm(e){
				this.params.buildingId = e.value[0].orgId;
				this.params.storeyId = e.value[1].orgId;
				this.placeShow = false;
				this.init()
			},
			paramsConfirm(e,type,key){
				this.placeShow = this.timeShow= this.typeShow = this.userShow = false;
				this.params[type] = e.value[0].value;
				this[key] = e.value[0].label;
				this.init();
			},
			init(){
				this.params.page = 1;
				this.isOver = false;
				this.list = [];
				this.getList();
			}
		}
	}
</script>

<style scoped lang="less">
	.page{
		padding-bottom: 20rpx;
		background: #F4F8FB;
		box-sizing: border-box;
		.top{
			width: 100%;
			height: 102rpx;
			background: #198CFF;
			padding: 0 30rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.left{
				display: flex;
				align-items: center;
				image{
					width: 36rpx;
					height: 36rpx;
				}
				text{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 30rpx;
					color: #FFFFFF;
					line-height: 42rpx;
					margin-left: 10rpx;
				}
			}
		}
		.filtrate{
			width: 100%;
			height: 90rpx;
			background: #FFFFFF;
			display: flex;
			.pre{
				width: calc(100% / 2);
				height: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				text{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #4E5969;
					line-height: 40rpx;
					margin-right: 12rpx;
				}
			}
		}
		
		.title{
			font-family: PingFang-SC, PingFang-SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #1D2129;
			line-height: 32rpx;
			margin-top: 36rpx;
			padding-left: 24rpx;
			span{
				color: #198CFF;
				margin: 0 10rpx;
			}
		}
		
		.boxs{
			width: 100%;
			padding: 0 24rpx;
			box-sizing: border-box;
			margin-top: 16rpx;
			overflow: hidden;
			.box{
				margin-top: 20rpx;
				background: #FFFFFF;
				border-radius: 16rpx;
				padding: 36rpx 24rpx;
				.place{
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 32rpx;
					color: #1D2129;
					line-height: 36rpx;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				.pre{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #86909C;
					line-height: 24rpx;
					margin-top: 26rpx;
					span{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 26rpx;
						color: #1D2129;
						line-height: 26rpx;
						margin-left: 20rpx;
					}
				}
			}
		}
	}
</style>