<template>
	<view>
		<!-- 弹框 -->
		<uni-popup ref="share" type="share">
			<view class="allcon">
				<view class="xcon">
					<view class="xcell" :class="cur==1?'cur':''" @click="qhclick(1)">常用地址</view>
					<view class="xcell" :class="cur==2?'cur':''" @click="qhclick(2)">全部地址</view>
				</view>
				<view class="hcon">
					<view class="htitle">
						<u-search class="searchinput" v-model="searchKey" @change="searchchange" @custom="searchcustom" placeholder="请输入地址查询"
						shape="square" bg-color="#EFF4F9" :clearabled="true" :show-action="showAction" input-align="left" @clear="clear"></u-search>
					</view>
					<view class="rclose" @tap="_cancel">取消</view>
				</view>
				<view class="selcon">
					<radio-group class="checklist-group" @change="chagne">
						<view class="sitem" v-for="(item,index) in clistnow" :key="index"> 
							<view class="conright">
								<view class="sname">
									 {{retType(item.departCode,2)}}
								</view>
								<view class="adress">
									{{item.workshop}}
								</view>
								<view v-if="cur==1" class="adress">
									{{item.location}}
								</view>
								<view v-if="cur==1" class="adress">
									{{item.address}}
								</view>
							</view>
							<view class="selbtn">
								<radio :value="item.id" :checked="item.isChecked" />
							</view> 
						</view>
					</radio-group>
					<view class="lineb"></view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
export default {
    props: {
		value: {
		  type: String,
		  default: "",
		},
		fcur: {
		  type: Number,
		  default: 1,
		},
		clist:{
			type:Array,
			default:[],
		}
    },
	data() {
		return {
			cur:1,
			clistnow:[],
			clistall:[],
			depts:[],
			searchKey:"",
			showAction:false,
		};
	},
	watch: {
		clist(list) {
		  console.log('监视值的改变1');
		  this._initTree(list);
		},
		fcur(v) {
		  console.log('监视值的改变2');
		  this.cur=v;
		},
		deep: true, // 深度监听
		immediate: true, // 初次监听即执行
	},
	created(){
		this.getTypes();
	},
	methods: {
		qhclick(index){
			this.cur=index;
			this.$forceUpdate();
			this._initTree(this.clist);
		},
		searchchange(value) {
			// 搜索框内容变化时，会触发此事件，value值为输入框的内容
			if(value){
				this.showAction=true;
				this.$forceUpdate();
			}
		},
		searchcustom(value) {
			if (this.searchKey) {
				this.clistall.forEach((v, i) => {
				  this.clistall[i].isChecked = false;
				});
				this.clistnow = this.clistall.filter((p) => {
				   //filter过滤的意思，这个是过滤函数，将每一项符合结果的值返回到personList
				   return ( p.address.indexOf(this.searchKey) !== -1 );
				});
				this.$forceUpdate();
			}
		},
		clear() {
			this.showAction=false;
			this.clistnow=[];
			this.clistall.forEach((v, i) => {
			  this.clistall[i].isChecked = false;
			  this.clistnow.push(this.clistall[i]);
			});
		},
		_initTree(list){
			this.clistall=list;
			this.clistnow=[];
			this.clistall.forEach((v, i) => {
				if(this.cur==2){
					// 全部地址
					var findx = this.clistnow.findIndex(item => item.departCode === v.departCode && item.workshop === v.workshop); 
					if(findx<0){
						// 新增
						var add={
							id:v.id,
							departCode:v.departCode,
							workshop:v.workshop
						}
						if(this.value == v.id){
							add.isChecked = true;
						}
						else{
							add.isChecked = false;
						}
						this.clistnow.push(add);
					}
				}
				else{
					// 常用地址
					if(this.value == v.id){
						this.clistall[i].isChecked = true;
					}
					else{
						this.clistall[i].isChecked = false;
					}
					this.clistnow.push(this.clistall[i]);
				}
			});
			console.log(this.clistnow);
			this.$forceUpdate();
		},
		chagne(e){
			var rt= null;
			if(e.detail.value){
				this.clist.forEach(cell=>{
					if(cell.id==e.detail.value){
						rt=cell;
					}
				});
			}
			if(this.cur==2){
				rt.location="";
				rt.address="";
			}
			this.$emit("change", rt);
			this._hide();
		},
		_show(id) {
			this.cur=this.fcur;
			this._initTree(this.clist);
			this.$refs.share.open();
			this.value=id;
			this.clistnow.forEach(cell=>{
				if(this.value == cell.id){
					cell.isChecked=true;
				}
				else{
					cell.isChecked=false;
				}
			});
			this.$forceUpdate();
		},
		_hide() {
			this.$refs.share.close();
		},
		_cancel() {
			this._hide();
			this.$emit("cancel", "");
		},
		getTypes() {
			//获取数据字典
			var commondata = uni.getStorageSync("commondata");
			if(commondata){
				var data = JSON.parse(commondata);
				if(data){
					this.depts=data.depts;
					this.$forceUpdate();
				}
			}
		},
		retType(type,str) {
			var rets = '';
			if(str==2){
				// 部门
				this.depts.forEach((cell,idx)=>{
					if(type==cell.departCode){
						rets=cell.departName;
						return;
					}
				});
			}
			if(!rets){
				rets=type
			}
			return rets;
		},
	}
}
</script>

<style lang="scss" scoped>
	 
	::v-deep.uni-radio-input-checked::before{
		display: none!important;
	}
	.allcon{
		height: calc((0.8*100vh));
		min-height: 300rpx;
		overflow: hidden;
		background-color: #FFFFFF;
	}
	.xcon{
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 20rpx 20rpx 0 20rpx;
		.xcell{
			border: 1rpx solid #ddd;
			color: #949494;
			margin-right: 20rpx;
			padding: 10rpx 20rpx;
			border-radius: 10rpx;
		}
		.xcell.cur{
			color: #007aff;
			border: 1rpx solid #007aff;
		}
	}
	.hcon{
		display: flex;
		flex-direction: row;
		padding: 20rpx;
		.htitle{
			flex:1;
			justify-content: flex-start;
			font-size: 32rpx;
			font-weight: 600;
			letter-spacing: 5rpx;
			padding-right: 20rpx;
		}
		.rclose{
			width: 70rpx;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			font-size: 28rpx;
			color:#949494;
		}
	}
	.checklist-group{
		width: 100%;
	}
	.selcon{
		padding: 20rpx;
		position: relative;
		display: block;
		overflow-y: auto;
		height: calc((100vh*0.9 - 280rpx));
		.lineb{
			height: 50rpx;
		}
		.sitem {
			display: flex;
			flex-direction: row;
			flex: 1;
			background-color: #FFFFFF;
			border-bottom: 1rpx solid #eeeeee;
			padding: 10rpx 0;
			.conright{
				flex: 1;
				display: flex;
				flex-direction: row;
				width: 100%;
				justify-content:flex-start;
			}
			.selbtn{
				width: 80rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
			.sname{
				display: flex;
				justify-content: flex-start;
				align-items: center;
				font-size: 28rpx;
				font-weight: 600;
				color: rgb(29, 29, 29);
				padding: 10rpx 10rpx 10rpx 0;
			}
			.adress{
				display: flex;
				justify-content: flex-start;
				align-items: center;
				padding: 10rpx 10rpx 10rpx 10rpx;
				font-size: 26rpx;
				color:#949494;
			}
		}
	}
</style>
 