<template>
	<view>
		<!-- 详情 -->
		<u-popup v-model="popupShow" mode="center" width="650" :closeable="true" border-radius="10">
			<view style="padding: 60rpx 40rpx 10rpx;">
				<u-form label-width="150" :model="form">
					<u-form-item label="楼栋名称" :required="true"><u-input disabled v-model="form.name" /></u-form-item>
					<u-form-item label="所属小区" :required="true">
						<u-input  disabled  v-model="form.residentialName" />
					</u-form-item>
					<u-form-item label="负责人" ><u-input v-model="form.personName" /></u-form-item>
					<u-form-item label="联系电话" ><u-input v-model="form.personPhone" /></u-form-item>
				</u-form>
				<view @click="submit" class="flex cu-btn bg-blue" style="margin: 60rpx 0 10rpx;padding: 40rpx;">
					提交
				</view>
			</view>
		</u-popup>
		
		<!-- 筛选标签条 -->
		<view class="cu-bar bg-white flex justify-around" style="z-index: 100;width: 100%;position: fixed;top: -2rpx;">
			<view class="u-line-1" @click="showDown(index)" v-for="(item,index) in filterMenu" :key="index"  style="width: 24%;text-align: center;height: 100%;">
				{{data[index]==""? item:data[index]}}
				<u-icon  name="arrow-down-fill" size="20" style="padding-left: 10rpx;"></u-icon>
			</view>
			
			<view class="u-line-1" @click="filterShow=true"   style="width: 26%;position: relative;text-align: center;height: 100%;">
				筛选
				<text class="cuIcon-filter padding-left-10" ></text>
				<u-badge  :offset="[2, 20]"  size="mini" type="error" :count="filterCount"></u-badge>
			</view>
			
			<view class="u-line-1"   style="text-align: center;height: 100%;">
				<view @click="form={};popupShow=true" class="cu-btn radius bg-base sm">
					<text class="cuIcon-add"></text>
					添加
				</view>
			</view>
		</view>
		
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view style="margin-top: 110rpx;">
				<card @delItem="delItem" @edit="edit" :list="list"></card>
			</view>
		</mescroll-body>
		
		<!-- 地区选择器 -->
		<u-picker  @confirm="regionConfirm"  mode="region" v-model="regionShow"></u-picker>
		<!-- 小区 -->
		<u-popup  border-radius="60" height="60%"  mode="bottom" v-model="residentialShow">
			<view class="fixed cu-bar search bg-white">
				<view class="search-form round">
					<text class="cuIcon-search"></text>
					<u-input style="width: 90%;" v-model="keyword" type="text" :adjust-position="false" placeholder="请输入关键字搜索" confirm-type="search"/>
				</view>
			</view>
			<scroll-view v-if="!$isEmpty(residentialList)"  style="padding-top: 110rpx;height: 100%;" :scroll-y="true" >
				<view @click="residentailConfirm(item)" hover-class="hoverClass" class="text-center padding-30 solid-bottom"  v-for="(item,index) in residentialList" :key="index">
					<text>{{item.label}}</text>
				</view>
				<u-divider v-if="residentialList.length>=10" height="80">只显示十条数据</u-divider>
			</scroll-view>
			<u-empty v-else name="search"></u-empty>
		</u-popup>
		

		<!-- 筛选 -->
		<u-modal :show-cancel-button="true" cancel-text="重置"   @cancel="reset" @confirm="filterConfirm" title="筛选" :mask-close-able="true" v-model="filterShow" >
			<view class="slot-content" style="margin: 20rpx;">
				<u-form label-width="150" :model="params" ref="uForm">
					<u-form-item :border-bottom="false" label="楼栋名"><u-input placeholder="请输入楼栋名" v-model="params.name" /></u-form-item>
				</u-form>
			</view>
		</u-modal>
		<!-- 通知alert -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import card from "./card.vue"
	import MescrollMixin from "@/components/mescroll-body/mescroll-mixins.js";
	let that;
	export default {
		components:{
			card
		},
		mixins:[MescrollMixin],
		data() {
			return {
				//新增修改详情弹窗
				popupShow:false,
				form:{},
				operaType:0,
				
				//顶部菜单栏
				// filterMenu: ["所属地区","所属小区"],
				filterMenu: ["所属小区"],
				data:[''],
				
				//地区
				// regionShow:false,
				areaValue:'',
				
				//小区
				residentialShow:false,
				residentialList:[],
				
				//筛选
				filterShow:false,
				filterCount:0,
				
				//参数
				params:{},
				//小区关键字
				keyword:'',
				
				list: [],
				downOption: { 
					use: true,
					auto: false
				},
				upOption: {
					page: {
						page: 0,
						size: 10
					},
					noMoreSize: 5, 
					empty: {
						tip: '暂无相关数据'
					}
				},
				
			}
		},
		onLoad(){
			that=this
			this.getResidentailList({})
		},
		watch:{
			keyword(){
				let that=this
				//节流函数
				if (this.timer){
					clearTimeout(this.timer)//阻止setTimeout函数的执行
				}
				this.timer = setTimeout(() => {
					that.searchList=[]
					let params = {
						// isSelect: true,
						// regionArea: that.areaValue,
						name: that.keyword
					}
					this.getResidentailList(params)
				}, 500)
			}
			
		},
		methods: {
			edit(item){
				let {orgId,orgPosition,...form}=item
				this.form=form
				this.popupShow=true
				console.log(this.form);
			},
			delItem(item){
				this.$dialog.showModal('确定要删除此记录吗？').then(res=>{
					this.$api.building.del({id:item.id}).then(res=>{
						if (res.data==true) {
							this.$u.toast('删除成功')
							this.mescroll.resetUpScroll()
						}else{
							this.$u.toast('删除失败')
						}
					})
				})
			},
			
			submit(){
				if (this.$isEmpty(this.form.name)) {
					this.$u.toast('请输入楼栋名')
					return
				}
				if (this.$isEmpty(this.form.residentialId) || this.$isEmpty(this.form.residentialName)) {
					this.$u.toast('请选择所属小区')
					return
				}
				if (!this.$isEmpty(this.form.personPhone)) {
					if (!this.$verify.phone(this.form.personPhone)&&!this.$verify.landline(this.form.personPhone)) {
						this.$u.toast('请输入正确的联系方式')
						return
					}
				}
				console.log(this.form);
				if (this.$isEmpty(this.form.id)) {
					console.log('我是新增');
					this.$api.building.add(this.form).then(res=>{
						if (res.code==200) {
							this.popupShow=false
							this.$u.toast('添加成功')
							this.mescroll.resetUpScroll()
						}
					})
				}else{
					let {createAccountId,createDate,floorNumber,roomNumber,status,unitNumber,personNumber,...params}=this.form
					console.log(params);
					this.$api.building.update(params).then(res=>{
						if (res.code==200) {
							this.popupShow=false
							this.$u.toast('修改成功')
							this.mescroll.resetUpScroll()
						}
					})
				}
			},
			//获取小区列表
			getResidentailList(params){
				let list = [];
				this.$api.residential.page(params).then(res=>{
					let list=[]
					res.data.records.forEach(item=>{
						let obj={
							label:item.name,
							value:item.id
						}
						list.push(obj)
					})
					this.residentialList =  list;
				})
			},
			/**
			 * 获取筛选的条件数
			 */
			getFilterCount(){
				let n=0
				console.log(this.areaValue);
				if (!this.$isEmpty(this.areaValue)) {
					//地区
					n++
				}
				if (!this.$isEmpty(this.params.name)) {
					//楼栋
					n++
				}
				this.filterCount=n
			},
			
			/**
			 * 下拉回调
			 */
			downCallback(){
				setTimeout(()=>{
					this.mescroll.resetUpScroll()
				},1500)
			},
			/**
			 * 上拉回调
			 * @param {Object} mescroll
			 */
			upCallback(mescroll) {
				let params=this.params
				params.current=mescroll.num
				params.size=mescroll.size
				this.getFilterCount()
				try{
					this.$api.building.page(params).then(res=>{
						let data=res.data.records
						let length=data.length
						let total=res.data.total
						mescroll.endBySize(length, total);
						if(mescroll.num == 1) this.list = []; 
						this.list=this.list.concat(data); 
					})
				}catch(e){
					mescroll.endErr();
				}
			},
			/**
			 * 重置
			 */
			reset(){
				//查询数据列表的参数
				this.params={}
				this.data=['','']
				//地区
				this.areaValue=''
				//小区列表
				this.residentialList=[]
				//小区关键字
				this.keyword=''
				//数据列表
				this.list=[],
				this.getResidentailList()
				this.mescroll.resetUpScroll()
			},
			/**
			 * 点击选择小区，重新获取楼栋列表
			 */
			residentailConfirm(res){
				if (this.operaType==0) {
					// 筛选楼栋列表的选择小区
					this.params.residentialId =res.value
					this.data[0] = res.label
					this.residentialShow = false
					this.mescroll.resetUpScroll()
				}else if(this.operaType==1){
					// 添加操作的选择小区
					this.form.residentialName=res.label
					this.form.residentialId=res.value
					this.residentialShow=false
					this.operaType=0
				}
			},
			/**
			 * 顶部菜单选择
			 * @param {Object} index 菜单下标
			 */
			showDown(index){
				// if (index==0) {
				// 	//地区
				// 	this.regionShow=true
				// 	return
				// }
				if(index==0){
					//小区
					this.residentialShow = true;
					return;
				}
			},
			/**
			 * 确认筛选
			 */
			filterConfirm(){
				this.mescroll.resetUpScroll()
			},
			/**
			 * 地区确认回调
			 * @param {Object} object
			 */
			regionConfirm(object){
				this.data[0]= object.area.label;
				this.areaValue = object.area.value;
				let params = {
					// isSelect: true,
					// regionArea: this.areaValue
				};
				this.getFilterCount()
				this.getResidentailList(params);
			},
		}
	}
</script>

<style>

</style>
