<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 v-model="form.name"  disabled/></u-form-item>
					<u-form-item label="详细地址" ><u-input v-model="form.address" /></u-form-item>
					<u-form-item label="所属社区" :required="true">
						<u-input type="text" disabled  v-model="form.agencyName" />
					</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-input v-model="form.agencyId" style="display: none;"/>
				</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>
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view style="margin-top: 110rpx;">
				<card @edit="edit" @delItem="delItem" :list="list"></card>
			</view>
		</mescroll-body>
		<!-- 所属社区 -->
		<u-popup  border-radius="60" height="60%"  mode="bottom" v-model="agencypopupShow">
			<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="agencyKeyword" type="text" :adjust-position="false" placeholder="请输入关键字搜索" confirm-type="search"/>
				</view>
			</view>
			<scroll-view v-if="!$isEmpty(searchList)"  style="padding-top: 110rpx;height: 100%;" :scroll-y="true" >
				<view @click="agencyConfirm(item)" hover-class="hoverClass" class="text-center padding-30 solid-bottom"  v-for="(item,index) in searchList" :key="index">
					<text>{{item.name}}</text>
				</view>
				<u-divider v-if="searchList.length>=10" height="80">只显示十条数据</u-divider>
			</scroll-view>
			<u-empty v-else name="search"></u-empty>
		</u-popup>
		
		<!-- 地区选择器 -->
		<u-picker  @confirm="regionConfirm"  mode="region" v-model="regionShow"></u-picker>
		
		<!-- 筛选 -->
		<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 label="小区名"><u-input v-model="params.name" /></u-form-item>
				</u-form>
			</view>
		</u-modal>
		<!-- 通知alert -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import card from "./comps/card.vue"
	import MescrollMixin from "@/components/mescroll-body/mescroll-mixins.js";
	import util from "../../utils/util";
	let that;
	export default {
		components:{
			card
		},
		mixins:[MescrollMixin],
		data() {
			return {
				//新增修改详情弹窗
				agencypopupShow:false,
				form:{},
				operaType:0,

				//顶部菜单栏
				filterMenu: ["地区","所属社区"],
				data:['','','',''],
				
				//地区
				regionShow:false,
				areaValue:'',
				
				//底部弹出框
				popupShow:false,
				searchList:[],
				//筛选框
				filterShow:false,
				filterCount:0,
				
				// 列表数据
				params:{},
				agencyKeyword:'',
				
				list: [],
				downOption: { 
					use: true,
					auto: false
				},
				upOption: {
					page: {
						page: 0,
						size: 10
					},
					noMoreSize: 5, 
					empty: {
						tip: '暂无相关数据'
					}
				},
				
			}
		},
		onLoad(){
			that=this
			this.getAgencyList()
		},
		onShow() {
			if (this.canReset) {
				this.mescroll.resetUpScroll()
			}
			this.canReset=true
		},
		watch:{
			agencyKeyword(){
				let that=this
				//节流函数
				if (this.timer){
					clearTimeout(this.timer)//阻止setTimeout函数的执行
				}
				this.timer = setTimeout(() => {
					that.searchList=[]
					let params = {
						name: that.agencyKeyword
					}
					this.getAgencyList(params)
				}, 500)
			},
		},
		methods: {
			/**
			 * 获取筛选的条件数
			 */
			getFilterCount(){
				let n=0
				if (!this.$isEmpty(this.params.regionArea)) {
					//地区
					n++
				}
				if (!this.$isEmpty(this.params.agencyId)) {
					//所属社区
					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.residential.pageBycondition(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.searchList=[]
				this.agencyKeyword=''
				this.list=[],
				this.mescroll.resetUpScroll()
			},
			/**
			 * 顶部菜单选择
			 * @param {Object} index 菜单下标
			 */
			showDown(index){
				if (index==0) {
					//地区
					this.regionShow=true
					return
				}
				if (index==1) {
					//所属社区
					this.agencypopupShow=true
					return
				}
			},
			/**
			 * 确认筛选
			 */
			filterConfirm(){
				this.mescroll.resetUpScroll()
			},
			/**
			 * 地区确认回调
			 * @param {Object} object
			 */
			regionConfirm(object){
				console.log(object);
				this.data[0]= object.area.label;
				this.areaValue = object.area.value;
				
				this.params={
					regionProvince:object.province.value+"0000",
					regionCity:object.city.value+"00",
					regionArea:object.area.value
				}
				this.mescroll.resetUpScroll()
			},
			/**
			 * 获取机构列表
			 * @param {Object} params
			 */
			getAgencyList(params){
				this.$api.agency.page(params).then(res=>{
					this.searchList=res.data.records
				})
			},
			/**
			 * 点击确认选择归属
			 */
			agencyConfirm(res){
				//筛选标题
				this.params.agencyId =res.id
				this.data[1] = res.name
				this.agencypopupShow = false
				this.mescroll.resetUpScroll()
			},
			/**
			 * 添加小区
			 */
			add(){
				uni.navigateTo({
					url:"./add"
				})
			},
			edit(item){
				this.popupShow=true
				this.form={
					id:item.id,
					name:item.name,
					address:item.address,
					agencyName:item.agencyName,
					personName:item.personName,
					personPhone:item.personPhone,
					agencyId: item.agencyId
				}
			},
			submit(){
				if (this.$isEmpty(this.form.id)) {
					console.log(this.form);
					//新增
					return
				}
				for (let key in this.form) {
					if (this.$isEmpty(this.form[key])) {
						delete this.form[key]
					}
				}
				//修改
				this.$api.residential.update(this.form).then(res=>{
					if (res.code==200) {
						this.$u.toast('修改成功')
						this.popupShow=false
						that.mescroll.resetUpScroll()
					}else{
						this.popupShow=false
						this.$u.toast('操作失败')
					}
				})
				
			},
			delItem(item){
				this.$dialog.showModal('确定要删除此项吗?').then(res=>{
					this.$api.residential.del({id:item.id}).then(res=>{
						if (res.data==true) {
							this.$showToast('删除成功')
							that.mescroll.resetUpScroll()
						}
					})
				})
			}
		}
	}
</script>

<style>

</style>
