<template>
	<view class="u-wrap">
		<view style="width: 100%;position: fixed;z-index: 99;">
			<button size="default" type="default" @tap="inputDialogToggle('attrAdd',categoryId)"
				hover-class="is-hover">添加分类</button>
		</view>
		<view class="card-list">
			<ul>
				<li v-for="items in attrList" :key="items.id">
					<view class="title-fur">
						<view class="name">{{items.attributeName}}</view>
						<view class="rightBox">
							<uni-icons type="plusempty" size="20" color="#5fd9ee" @tap="inputDialogToggle('attrValAdd', items.id)"></uni-icons>
							<uni-icons type="gear" size="24" color="#5fd9ee" @tap="inputDialogToggle('attrEdit', items, item)"></uni-icons>
							<uni-icons type="trash" size="24" color="#f56c6c" @tap="delItem(items, 'attrDel')"></uni-icons>
						</view>
					</view>
					<view class="content-fur">
						<view class="content-low" v-for="item in items.attributeValue" :key="item.id"
							@tap="delItem(item, 'attrNameDel')">{{item.value}}</view>
					</view>
				</li>
			</ul>
		</view>
		<uni-popup ref="inputDialog" type="dialog">
			<view class="type-popup" style="width:100%;margin-bottom: 0;border-radius: 0;height: 450rpx;border-radius: 10px;">
				<view style="height: 120rpx;font-size: 36rpx;font-weight: bold;line-height: 120rpx;text-align: center;">
					新增规格
				</view>
				<view style="height: 80rpx;font-size: 28rpx;line-height: 80rpx;text-align: center;">
					请在下方输入框中输入新规格
				</view>
				<view style="width: 90%;margin: 40rpx auto 0;">
					<uni-easyinput type="text" v-model="categoryText" @blur="handleBlur" placeholder="请输入规格值" />
				</view>
				<view style="width: 100%;height: 90rpx;line-height: 90rpx;text-align: center;border-radius: 0;background: linear-gradient(90deg, #60F3FF, #088FEB);color: #fff;display: flex;position: absolute;bottom: 0;">
					<view style="width:50%;" @tap="$refs.inputDialog.close()">取消</view>
					<view style="width:50%;background: linear-gradient(90deg, #FF9797, #FFC1E0);" @tap="dialogInputConfirm">确认</view>
				</view>
			</view>
		</uni-popup>
		<!-- 查看商品库存弹出层 -->
		<uni-popup ref="carPopup" background-color="#fff">
			<view class="popup-content" style="height:auto !important;">
				<view style="font-size: 36rpx;font-weight: bold;text-align: center;">提示</view>
				<view v-if="goodsList !=''"
					style="width:100%;font-weight:bold;text-align: center;font-size: 28rpx;margin: 20rpx 0;">
					{{goodsList[0].productName}}商品有以下库存，确认继续删除吗？
				</view>
				<view class="tui-order-item boxbg">
					<view style="display: flex;border-bottom: 1px solid #eee;height: 50rpx;line-height: 50rpx;">
						<view style="width: 70%;text-align: center;border-right: 1px solid #eee;">商品规格</view>
						<view style="width: 29%;text-align: center;">数量</view>
					</view>
					<view class="shop-box" v-for="(item,index) in goodsList" :key="index" style="position: relative;">
						<view style='height: 50rpx;line-height: 50rpx;'>
							<view
								style="width: 70%;text-align: center;border-right: 1px solid #eee;float: left;height: 50rpx;overflow: hidden;">
								{{item.attributeList | sliceMsg}}
							</view>
							<view style="width: 29%;text-align: center;float: left;">
								{{item.stockCount}}
							</view>
						</view>
					</view>
				</view>
				<!-- <u-loadmore :status="status" /> -->
				<view class="popup-bottom">
					<view class="popup-bottom-left">
						<view class="bottom-btn" @tap="this.$refs.carPopup.close()"
							style="background: linear-gradient(90deg, #FF9797, #FFC1E0);">再想想</view>
					</view>
					<view class="popup-bottom-right">
						<view class="bottom-btn" @tap="this.show = true">确认删除</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				// indexList: [],
				show: false,
				inputVal: '',
				typeModel: '',
				editItem: {},
				delItems: {},
				isOpenId: '',
				attrList: [],
				openIndex: '',
				delType: '',
				dialogTit: '输入分类名',
				categoryId: '',
				goodsList: [],
				categoryText:''
			}
		},
		computed: {

		},
		filters: {
			sliceMsg(val) {
				var name = ''
				if (typeof(val) == 'string') {
					let newObj = JSON.parse(val)
					for (let as in newObj) {
						name += newObj[as] + '/'
					}
				}
				return name;
			}
		},
		onLoad(options) {
			this.categoryId = options.id
			this.getGategoryList(options.id)
		},
		methods: {
			//初始化获取子分类列表
			getGategoryList() {
				let data = []
				this.NB.sendRequest('/app/productAttribute/selectByCategoryId', {
					categoryId: this.categoryId
				}, false, 'POST', 'application/x-www-form-urlencoded').then(res1 => {
					if (res1.code == 200) {
						data = res1.result;
						if (res1.result.length > 0) {
							data.forEach((items) => {
								this.NB.sendRequest('/app/productAttributeValue/selectByAttributeId', {
									id: items.id
								}, false, 'get', 'application/x-www-form-urlencoded').then(res2 => {
									if (res2.code == 200) {
										items.attributeValue = res2.result;
										this.$forceUpdate()
									} else {
										this.tui.toast(res.message)
									}
								})
							})
						}
						
					} else {
						this.tui.toast(res.message)
					}
				})
				
				this.$forceUpdate()
				setTimeout(() => {
					this.attrList = data
				}, 1000)
			},
			inputDialogToggle(type, item, parent) {
				this.typeModel = type;
				if (type == 'edit') {
					this.inputVal = item.categoryName;
					this.editItem = item;
				} else if (type == 'add' || type == 'attrAdd' || type == 'attrValAdd') {
					this.inputVal = '';
					this.isOpenId = item;
					if (type == 'add') {
						this.dialogTit = '输入分类名'
					} else if (type == 'attrAdd') {
						this.dialogTit = '输入分类名'
						// this.$refs.addTypeDialog.open('bottom')
					} else if (type == 'attrValAdd') {
						this.dialogTit = '输入属性值'
					}
				} else if (type == 'attrEdit') {
					this.inputVal = item.attributeName;
					this.editItem = item;
				}
				this.$refs.inputDialog.open('bottom')
			},
			handleBlur(e){
				this.categoryText = e.detail.value
			},
			dialogInputConfirm(e) {
				switch (this.typeModel) {
					case 'add':
						this.NB.sendRequest('/app/productCategory/save', {
							categoryName: this.categoryText
						}, false, 'post', 'application/x-www-form-urlencoded').then(res => {
							if (res.code == 200) {
								this.getGategoryList()
								uni.showToast({
									title: "添加成功",
									icon: 'none'
								})
							} else {
								uni.showToast({
									title: "添加失败",
									icon: 'none'
								})
							}
						})
						break;
					case 'edit':
						this.NB.sendRequest('/app/productCategory/edit', {
							categoryName: this.categoryText,
							id: this.editItem.id
						}, false, 'post', 'application/x-www-form-urlencoded').then(res => {
							if (res.code == 200) {
								this.getGategoryList()
								uni.showToast({
									title: "修改成功",
									icon: 'none'
								})
							} else {
								uni.showToast({
									title: "修改失败",
									icon: 'none'
								})
							}
						})
						break;
					case 'attrAdd':
						for (let i = 0; i < this.attrList.length; i++) {
							if (this.attrList[i].attributeName == e) {
								uni.showToast({
									title: "请勿重复添加",
									icon: 'none'
								})
								return
							}
						}
						this.NB.sendRequest('/app/productAttribute/save', {
							attributeName: this.categoryText,
							categoryId: this.isOpenId
						}, false, 'post', 'application/x-www-form-urlencoded').then(res => {
							if (res.code == 200) {
								this.getGategoryList()
								uni.showToast({
									title: "添加成功",
									icon: 'none'
								})
							} else {
								uni.showToast({
									title: "添加失败",
									icon: 'none'
								})
							}
						})
						break;
					case 'attrValAdd':
						if (e == '') {
							uni.showToast({
								title: "不能为空！",
								icon: 'none'
							})
							return
						}
						this.NB.sendRequest('/app/productAttributeValue/save', {
							value: this.categoryText,
							attributeId: this.isOpenId
						}, false, 'post', 'application/x-www-form-urlencoded').then(res => {
							if (res.code == 200) {
								this.getGategoryList()
								uni.showToast({
									title: "添加成功",
									icon: 'none'
								})
								this.$refs.inputDialog.close()
							} else {
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
							}
						})
						break;
					case 'attrEdit':
						this.NB.sendRequest('/app/productAttribute/edit', {
							attributeName: this.categoryText,
							categoryId: this.editItem.categoryId,
							id: this.editItem.id
						}, false, 'post', 'application/x-www-form-urlencoded').then(res => {
							if (res.code == 200) {
								this.getGategoryList()
								uni.showToast({
									title: "修改成功",
									icon: 'none'
								})
							} else {
								uni.showToast({
									title: "修改失败",
									icon: 'none'
								})
							}
						})
						break;
					default:
				}
				this.$refs.inputClose.val = ''
			},
			dialogClose() {
				this.inputVal = '';
				this.$refs.inputClose.val = ''
			},
			dialogDel() {
				this.delItem(this.editItem, 'del')
			},
			delItemModal() {
				this.show = false;
				if (this.delType == 'del') {
					this.NB.sendRequest('/app/productCategory/delById', {
						id: this.delItems.id,
					}, false, 'post', 'application/x-www-form-urlencoded').then(res => {
						if (res.code == 200) {
							this.getGategoryList()
							uni.showToast({
								title: "删除成功",
								icon: 'none'
							})
							this.$refs.carPopup.close()
						} else {
							uni.showToast({
								title: "删除失败",
								icon: 'none'
							})
						}
					})
				} else if (this.delType == 'attrDel') {
					this.NB.sendRequest('/app/productAttribute/delById', {
						categoryId: this.delItems.categoryId,
						attributeName: this.delItems.attributeName,
						id: this.delItems.id,
					}, false, 'post', 'application/x-www-form-urlencoded').then(res => {
						if (res.code == 200) {
							this.getGategoryList()
							uni.showToast({
								title: "删除成功",
								icon: 'none'
							})
							this.$refs.carPopup.close()
						} else {
							uni.showToast({
								title: "删除失败",
								icon: 'none'
							})
						}
					})
				} else if (this.delType == 'attrNameDel') {
					this.NB.sendRequest('/app/productAttributeValue/delById', {
						attributeId: this.delItems.attributeId,
						value: this.delItems.value,
						id: this.delItems.id,
					}, false, 'post', 'application/x-www-form-urlencoded').then(res => {
						if (res.code == 200) {
							this.getGategoryList()
							uni.showToast({
								title: "删除成功",
								icon: 'none'
							})
							this.$refs.carPopup.close()
						} else {
							uni.showToast({
								title: "删除失败",
								icon: 'none'
							})
						}
					})
				}
			},
			delItem(item, type) {
				let that = this;
				this.delType = type;
				this.delItems = item;
				this.show = true
				uni.showModal({
					title: '提示',
					content: '是否确认删除此分类？',
					success: function (res) {
						if (res.confirm) {
							that.delItemModal()
						}
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	button {
		color: #ffffff;
		background: #5fd9ee;
		width: 90%;
		margin-top: 20rpx;
	}

	.card-list {
		width: 95%;
		border-radius: 10rpx;
		background: #fff;
		height: auto;
		margin: 140rpx auto 0;

	}

	.title-fur {
		width: 95%;
		height: 70rpx;
		margin: 0 auto;
		border-bottom: 1px solid #eee;
	}

	.name {
		font-size: 30rpx;
		width: 50%;
		float: left;
		height: 70rpx;
		line-height: 70rpx;
	}

	.rightBox {
		display: flex;
		float: right;
	}

	.content-fur {
		padding-bottom: 25rpx;
	}

	.content-low {
		width: auto;
		border: 1px solid #eee;
		display: inline-block;
		min-width: 90rpx;
		padding: 5rpx;
		font-size: 35rpx;
		text-align: center;
		color: #777;
		margin: 20rpx 0 0 20rpx;
	}

	.popup-content {
		align-items: center;
		justify-content: center;
		padding: 30rpx;
		height: 1200rpx;
		background-color: #fff;
		margin-bottom: 30rpx;
		overflow: scroll;
	}

	.popup-container {
		width: 100%;
		background: #fff;
		height: 50rpx;
	}

	.checkList {
		display: flex;
		height: 50rpx;
		font-size: 28rpx;
		line-height: 50rpx;
		margin: 20rpx 0;
	}

	.checkList-box {
		height: 50rpx;
		width: 60%;
	}

	.popup-bottom {
		width: 95%;
		height: 100rpx;
		position: fixed;
		bottom: 0;
		left: 2.5%;
		display: flex;
		z-index: 99;
		background: #fff;
	}

	.popup-bottom-left {
		width: 50%;
		line-height: 80rpx;
	}

	.popup-bottom-right {
		width: 50%;
		line-height: 100rpx;
	}

	.bottom-btn {
		width: 70%;
		line-height: 70rpx;
		height: 70rpx;
		text-align: center;
		/* margin-top: 10rpx; */
		color: #fff;
		font-size: 28rpx;
		margin: 0 auto;
		background: linear-gradient(90deg, #60F3FF, #088FEB);
		border-radius: 70rpx;
	}
	.checked{
		background: #60F3FF;
		color: #fff;
	}
	
	
	.add-popup-content {
		align-items: center;
		justify-content: center;
		padding: 30rpx;
		width: 100%;
		height: 1300rpx;
		background-color: #fff;
	}
	
	.add-popup-title {
		font-size: 36rpx;
		font-weight: bold;
		text-align: center;
		position: relative;
	}
	
	.add-popup-container {
		margin-top: 20rpx;
	}
	
	.add-popup-bottom {
		display: flex;
		height: 80rpx;
		color: #fff;
		line-height: 80rpx;
		margin-top: 50rpx;
	}
	
	.add-popup-btn {
		flex: 1;
		text-align: center;
		background: linear-gradient(90deg, #60F3FF, #088FEB);
	}
	.type-popup{
		width: 500rpx;
		height: auto;
		background: #fff;
		border-radius: 10px;
		position: relative;
		margin-bottom: 40rpx;
	}
</style>