<template>
	<u-popup v-model="isShow" class="type-picker" @close="isShow=false" mode="bottom">
		<view class="type-header flex justify_between align_center">
			<text class="type-picker-btn" @click="onClean">清空</text>
			<text>产品分类</text>
			<text class="type-picker-btn color-blue" @click="isShow=false">
				<u-icon name="close"></u-icon>
			</text>
		</view>
		<u-row class="choose-info">
			<u-col :span="3">当前已选：</u-col>
			<u-col :span="7" :style="{color: $config('primaryColor')}">
				<view v-for="(item, index) in infoDataS" :key="index" style="display: inline-block;">{{item.productTypeName}}<text v-if="index+1 != infoData.length"> > </text></view>
			</u-col>
			<u-col :span="2"><u-button v-if="infoDataS.length>0" @click="handlerConfirm" type="success" size="mini" :custom-style="customStyle" hover-class="none" shape="circle">确定</u-button></u-col>
		</u-row>
		<scroll-view class="picker-content" scroll-y>
			<view class="picker-main">
				<view class="picker-main-item" v-for="(item, index) in typeList" :key="item.productTypeSn" @click="chooseItem(index)">
					<view class="item-name">{{item.productTypeName}}</view>
					<u-icon v-show="item.productTypeSn==(infoDataS[nowInd]&&infoDataS[nowInd].productTypeSn)" class="item-icon" name="checkbox-mark" :color="$config('primaryColor')" size="28"></u-icon>
				</view>
				<view v-if="typeList && typeList.length == 0">
					<u-empty text="数据为空" mode="list" :img-width="200" :margin-top="-60"></u-empty>
				</view>
			</view>
		</scroll-view>
	</u-popup>
</template>

<script>
	import { dealerProductTypeList } from '@/api/dealerProductType'
	export default {
		props: {
			openModal: { //  弹框显示状态
				type: Boolean,
				default: false
			},
			nowData: {
				type: Object,
				default: ()=>{
					return {
						data: [],
						indArr: [],
						nowInd: 0
					}
				}
			}
		},
		data() {
			return {
				isShow: this.openModal, //  是否打开弹框
				listData: [],
				typeList: [],
				nowInd: 0,
				indArr: [],
				infoData: [],
				infoDataS: [],
				customStyle: {
					background: this.$config('primaryColor')
				}
			}
		},
		mounted() {
			this.getTypeList()
		},
		methods: {
			getTypeList(){
				dealerProductTypeList({}).then(res => {
					if(res.status == 200 && res.data){
						this.listData = res.data
						this.typeList = res.data
					}else{
						this.listData = []
						this.typeList = []
					}
				})
			},
			//  选择
			chooseItem(ind){
				if(this.nowInd == 0){
					this.infoData[this.nowInd] = { productTypeName: this.typeList[ind].productTypeName, productTypeSn: this.typeList[ind].productTypeSn }
					this.infoData[1] ? delete this.infoData[1] : null
					this.infoData[2] ? delete this.infoData[2] : null
					this.indArr[this.nowInd] = ind
					this.indArr[1] ? delete this.indArr[1] : null
					this.indArr[2] ? delete this.indArr[2] : null
					this.nowInd++
				}else if(this.nowInd == 1){
					this.infoData[this.nowInd] = { productTypeName: this.typeList[ind].productTypeName, productTypeSn: this.typeList[ind].productTypeSn }
					this.infoData[2] ? delete this.infoData[2] : null
					this.indArr[this.nowInd] = ind
					this.indArr[2] ? delete this.indArr[2] : null
					this.nowInd++
				}else if(this.nowInd == 2){
					this.infoData[this.nowInd] = { productTypeName: this.typeList[ind].productTypeName, productTypeSn: this.typeList[ind].productTypeSn }
					this.indArr[this.nowInd] = ind
				}
				this.infoDataS = JSON.parse(JSON.stringify(this.infoData))
			},
			handlerConfirm(){
				const params = {
					data: this.infoDataS,
					indArr: this.indArr,
					nowInd: this.nowInd
				}
				this.$emit('choose', params)
				this.isShow = false
			},
			// 清空
			onClean(){
				this.infoData = []
				this.infoDataS = []
				this.indArr = []
				this.nowInd = 0
				this.$emit('clean')
				this.isShow = false
			}
		},
		watch: {
			//  父页面传过来的弹框状态
			openModal (newValue, oldValue) {
				this.isShow = newValue
			},
			//  重定义的弹框状态
			isShow (newValue, oldValue) {
				if (!newValue) {
					this.$emit('close')
				}else{
					this.infoData = JSON.parse(JSON.stringify(this.nowData.data))
					this.infoDataS = JSON.parse(JSON.stringify(this.nowData.data))
					this.indArr = this.nowData.indArr
					this.nowInd = this.nowData.nowInd
				}
			},
			nowInd (newValue, oldValue) {
				if (newValue || newValue == 0) {
					if (newValue == 0) {
						this.typeList = this.listData || []
					}else if (newValue == 1) {
						this.typeList = (this.indArr[0] || this.indArr[0]==0) && this.listData[this.indArr[0]] && this.listData[this.indArr[0]].children ? this.listData[this.indArr[0]].children : []
					}else if (newValue == 2) {
						let oList = (this.indArr[0] || this.indArr[0]==0) && this.listData[this.indArr[0]] && this.listData[this.indArr[0]].children ? this.listData[this.indArr[0]].children : []
						let tList = (this.indArr[1] || this.indArr[1]==0) && oList[this.indArr[1]] && oList[this.indArr[1]].children ? oList[this.indArr[1]].children : []
						this.typeList = tList
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.type-picker{
		.type-header{
			padding: 0 30upx;
			position: relative;
			.brand-picker-btn{
				color: rgb(96, 98, 102);
				margin: 10upx;
			}
			.color-blue{
				display: inline-block;
				padding: 10upx 20upx;
				margin: 10upx 0;
			}
		}
		.type-header:after{
			content: "";
			position: absolute;
			border-bottom: 1px solid #eaeef1;
			-webkit-transform: scaleY(.5);
			transform: scaleY(.5);
			bottom: 0;
			right: 0;
			left: 0;
		}
		.choose-info{
			padding: 14upx 30upx 14upx;
			color: rgb(96, 98, 102);
		}
		.picker-content{
			height: 50vh;
			padding: 6upx 0 20upx;
			position: relative;
			.picker-main{
				width: 100%;
				.picker-main-item{
					position: relative;
					padding: 0 30upx;
					.item-name{
						padding: 14upx 0;
						text-align: center;
					}
					.item-icon{
						position: absolute;
						right: 100upx;
						top: 19upx;
					}
					&:active{
						background: #f8f8f8;
					}
				}
			}
		}
	}
</style>
