<template>
	<view class="page1">
		<view style="width: 100%;position: fixed;top: 0;z-index: 99;">
			<view style="background: #fff;padding-top: 20rpx;display: flex;">
				<view @tap="topAddShip" style="width: 100%;text-align: center;height: 80rpx;margin: 20rpx;background: #60F3FF;line-height: 80rpx;border-radius: 10px;color: #fff;">
					新增模板
				</view>
			</view>
		</view>
		<view style="margin-top:160rpx;">
			<view>
				<view class="list-1" v-for="item in categoryList" :key="item" @tap="clickList(item)"
					style="height: 100rpx;line-height: 100rpx;display: flex;">
					<view class="name-tel" style="width: 63%;">
						<text style="line-height: 75rpx;">{{item.printName}}</text>
					</view>
					<view style="display: flex;width: 37%;">
						<view class="name-edit" style="margin-right: 20rpx;" @tap.stop="delCateName(item)">
							删除
						</view>
						<view class="name-edit" @tap.stop="editCateName(item)">
							编辑
						</view>
					</view>
					
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				categoryList: [],
				shippList:[],
				categoryId:'',
				printName:'',
				shipId:''
			}
		},
		onShow(){
			this.getCategoryList()
		},
		methods: {
			delItem(index) {
				let that = this;
				uni.showModal({
					title: "提示",
					content: "确定要删除吗？",
					success: function(res) {
						if (res.confirm) {
							that.shippList.splice(index, 1)
						}
					}
				})
			
			},
			topAddShip(){
				uni.navigateTo({
					url: "/package1/index/addTemplate"
				})
			},
			addShippList(){
				let data = {
					name:'',
					value:''
				}
				this.shippList.push(data)
				this.$forceUpdate()
			},
			getCategoryList(){
				this.tui.request("/app/shopprint/getList", "POST", {
					shopId:uni.getStorageSync('shopId')
				}, false, false).then((res) => {
					if (res.code == 200) {
						this.categoryList = res.result
					} else {
						this.tui.toast(res.message)
					}
				}).catch((res) => {})
			},
			goAddClass(){
				let data = '{'
				for(let i = 0 ; i < this.shippList.length; i++){
					if(this.shippList[i].name == ''){
						this.tui.toast('请输入标签名称')
						return
					}
					data += '"'+this.shippList[i].name+'":'+'"'+this.shippList[i].value+'",'
				}
				data = data.slice(0,data.lastIndexOf(','))  
				data+='}'
				if(this.shipId != ''){
					this.tui.request("/app/shopprint/update", "POST", {
						printName:this.printName,
						id:this.shipId,
						content:data
					}, false, false).then((res) => {
						if (res.code == 200) {
							this.tui.toast('修改成功')
							this.printName = ''
							this.shipId = ''
							this.getCategoryList()
						} else {
							this.tui.toast(res.message)
						}
					}).catch((res) => {})
				}else{
					this.tui.request("/app/shopprint/add", "POST", {
						printName:this.printName,
						shopId:uni.getStorageSync('shopId'),
						content:data
					}, false, false).then((res) => {
						if (res.code == 200) {
							this.tui.toast('新增成功')
							this.getCategoryList()
						} else {
							this.tui.toast(res.message)
						}
					}).catch((res) => {})
				}
				setTimeout(res=>{
					let pages = getCurrentPages();
					let prevPage = pages[pages.length - 1];
					prevPage.$vm.getCustomerCategoryList()
				},300)
			},
			delCateName(item){
				var that = this;
				uni.showModal({
					title: "提示",
					content: "确定删除此条模板吗？",
					success: function(res) {
						uni.showLoading({
							title: '加载中...'
						})
						if (res.confirm) {
							that.tui.request("/app/shopprint/delete?id="+item.id, "GET" ,false, false).then((res) => {
								if (res.code == 200) {
									that.tui.toast('删除成功')
									that.getCategoryList()
								} else {
									that.tui.toast(res.message)
								}
							}).catch((res) => {})
						}
						setTimeout(res => {
							uni.hideLoading();
						}, 500)
					}
				})
			},
			editCateName(item){
				
				let qrcode = undefined
				let newObj = JSON.parse(item.content)
				if(newObj[0].imgUrl != undefined){
					uni.setStorageSync('qrcode',newObj[0])
					newObj.shift();
				}
				item.content = newObj
				uni.navigateTo({
					url: "/package1/index/addTemplate?item="+JSON.stringify(item)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	page,.page1{
		font-size: 28rpx;
	}
	.list-1 {
		border-bottom: 1px solid #eee;
		background: #fff;
		font-size: 32rpx;
		margin: 20rpx auto 0;
		width: 95%;
		padding-left: 30rpx;
		position: relative;
	}

	.name-tel {
		width: 75%;
		flex-direction: column;
		height: 100rpx;
		font-size: 30rpx;
		line-height: 100rpx;
	}
	.name-edit{
		width: 110rpx;
		height: 55rpx;
		background: #088FEB;
		color: #fff;
		font-size: 22rpx;
		line-height: 55rpx;
		text-align: center;
		border-radius: 5px;
		margin-top: 22.5rpx;
	}
	
	.type-popup {
		width: 500rpx;
		height: auto;
		max-height: 900rpx;
		overflow: scroll;
		background: #fff;
		border-radius: 10px;
		position: relative;
		margin-bottom: 40rpx;
	}

</style>
