<template>
	<view class="goods">
		<!-- 	<view class="goods-top">
			<input type="textarea" v-model="content" placeholder="输入SKU直接导入获取商品信息" />
			<view class="btn" :style="{'opacity': content?1:0.6 }" @click="onClickAccess">一键获取</view>
		</view> -->
		<view class="goods-hd">
			<view class="item">
				<view class="a">商品名称<text>*</text></view>
				<view class="b">
					<input type="text" v-model="from.goods_name" placeholder="请输入商品价格，最低0.1元" />元
				</view>
			</view>
			<view class="item">
				<view class="a">商品分类</view>
				<view class="b" @click="openPage(1)">
					<view class="select">请选择商品分类<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
			</view>
			<view class="items">
				<view class="a">商品照片<text>*</text>
					<view class="tip">最多上传100张</view>
				</view>
				<view class="c">
					<u-upload :action="action" :header="header" @on-uploaded="goosSuccess" @on-remove="goodsRemove"
						width="180" height="180" :file-list="goodsImageList" :custom-btn="true" max-count="100">
						<template v-slot:addBtn>
							<view class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150">
								<image :src="$picUrl+'/static/news/icon-upload.png'"></image>
								<view class="1">上传图片</view>
							</view>
						</template>
					</u-upload>
				</view>
			</view>
			<view class="items">
				<view class="a" style="align-items: flex-start">商品详情<text>*</text>
					<view class="input">
						<textarea placeholder="可输入商品的详细描述" v-model="from.goods_details" class="t"></textarea>
					</view>
				</view>
				<view class="c">
					<u-upload :action="action" :header="header" @on-uploaded="detailSuccess" @on-remove="detailsRemove"
						width="180" height="180" :file-list="goodsDetailImageList" :custom-btn="true" max-count="6">
						<template v-slot:addBtn>
							<view class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150">
								<image :src="$picUrl+'/static/news/icon-upload.png'"></image>
								<view class="1">上传图片</view>
							</view>
						</template>
					</u-upload>
				</view>
			</view>
			<view class="items">
				<view class="a">商品素材<text>*</text></view>
				<view class="c">
					<u-upload :action="action" :header="header" @on-uploaded="sourceSuccess" @on-remove="sourceRemove"
						width="180" height="180" :file-list="goodSourceImageList" :custom-btn="true" max-count="6">
						<template v-slot:addBtn>
							<view class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150">
								<image :src="$picUrl+'/static/news/icon-upload.png'"></image>
								<view class="1">上传图片</view>
							</view>
						</template>
					</u-upload>
				</view>
			</view>
		</view>
		<view class="goods-hd">
			<view class="items">
				<view class="a" style="justify-content: space-between;">多规格<u-switch v-model="skuchecked"
						active-color="#55BD6A"></u-switch>
				</view>
				<view class="b">
					最多添加3个商品规格组，生成SKU数量不能超出50个
				</view>
			</view>
			<view class="item">
				<view class="a">商品价格<text>*</text></view>
				<view class="b">
					<input type="text" @input="keyGoodsPrice" v-model="from.goods_price"
						placeholder="请输入商品价格，最低0.1元" />元
				</view>
			</view>
			<view class="item">
				<view class="a">商品原价</view>
				<view class="b">
					<input type="text" @input="keyOriginalPrice" v-model="from.goods_original_price"
						placeholder="请输入商品划线价格" />元
				</view>
			</view>
			<view class="item">
				<view class="a">商品库存<text>*</text></view>
				<view class="b">
					<input type="number" v-model="from.goods_stock" placeholder="请输入商品库存" />件
				</view>
			</view>
			<view class="item">
				<view class="a">商品重量<text>*</text></view>
				<view class="b">
					<input type="text" v-model="from.goods_weight" placeholder="请输入商品重量" />kg
				</view>
			</view>
		</view>
		<view class="goods-hd">
			<view class="item">
				<view class="a">配送方式<text>*</text></view>
				<view class="b" style="width: 350rpx;height: 40rpx;text-align: right;">
					<picker @change="changeDelivery" mode="selector" placeholder="请选择配送方式"
						style="width: 350rpx;height: 40rpx;" range-key="text" :value="index" :range="deliveryList">
						<text :style="{color: from.delivery_name? '#303030' : '#C7C7C7'}">
							{{from.delivery_name?from.delivery_name:'请选择配送方式'}}</text>
						<u-icon name="arrow-right" color="#C7C7C7"></u-icon>
					</picker>
				</view>
			</view>
			<view class="item">
				<view class="a">运费模版<text>*</text></view>
				<view class="b" style="width: 350rpx;height: 40rpx;text-align: right;">
					<picker @change="changeDeliveryTemplate" mode="selector" placeholder="请选择运费模版"
						style="width: 350rpx;height: 40rpx;" range-key="text" :value="index" :range="deliveryTemplate">
						<text :style="{color: from.template_name? '#303030' : '#C7C7C7'}">
							{{from.template_name?from.template_name:'请选择运费模版'}}</text>
						<u-icon name="arrow-right" color="#C7C7C7"></u-icon>
					</picker>
				</view>
			</view>
			<view class="item">
				<view class="a">销售状态<text>*</text></view>
				<view class="b">
					<u-switch v-model="salesChecked" active-color="#55BD6A"></u-switch>
				</view>
			</view>
		</view>
		<view class="goods-fd">
			<view class="btn" @click="onSubmit">确认</view>
		</view>
		<u-popup v-model="isShow" mode="bottom" border-radius="12" mask-close-able="false" closeable>
			<view class="goods-classify">
				<view class="goods-classify-hd">
					<view class="a" @click="openPage(2)">
						<u-icon name="plus"></u-icon>新增分类
					</view>
					商品分类
				</view>
				<view class="goods-classify-bd">
					<scroll-view scroll-y class="l">
						<view class="item" @click="tabLItem(0)">
							<view :class="tabLIndex == 0?'item-on':''">数码</view>
						</view>
						<view class="item" @click="tabLItem(1)">
							<view :class="tabLIndex == 0?'item-on':''">食品</view>
						</view>
						<view class="item" @click="tabLItem(2)">
							<view :class="tabLIndex == 0?'item-on':''">服饰鞋包</view>
						</view>
						<view class="item" @click="tabLItem(3)">
							<view :class="tabLIndex == 0?'item-on':''">家居百货</view>
						</view>
						<view class="item" @click="tabLItem(4)">
							<view :class="tabLIndex == 0?'item-on':''">美妆/个户</view>
						</view>
						<view class="item" @click="tabLItem(5)">
							<view :class="tabLIndex == 0?'item-on':''">家居百货</view>
						</view>
						<view class="item" @click="tabLItem(6)">
							<view :class="tabLIndex == 0?'item-on':''">美妆/个户</view>
						</view>
						<view class="item" @click="tabLItem(7)">
							<view :class="tabLIndex == 0?'item-on':''">食品</view>
						</view>
						<view class="item" @click="tabLItem(8)">
							<view :class="tabLIndex == 0?'item-on':''">服饰鞋包</view>
						</view>
						<view class="item" @click="tabLItem(9)">
							<view :class="tabLIndex == 0?'item-on':''">家居百货</view>
						</view>
						<view class="item" @click="tabLItem(10)">
							<view :class="tabLIndex == 0?'item-on':''">美妆/个户</view>
						</view>
					</scroll-view>
					<scroll-view scroll-y class="r">
						<view class="item" v-for="(a,i) in 10" @click="tabRItem(i)">
							<view :class="tabRIndex == i?'item-on':''">水产肉类/新鲜水果</view>
						</view>
					</scroll-view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import Config from '@/core/config'
	import * as newFunApi from '@/api/newFun'
	export default {
		data() {
			return {
				from: {
					goods_name: '',
					goods_class: '',
					goods_image_id: '',
					details_image_id: '',
					source_image_id: '',
					goods_details: '',
					goods_price: '',
					goods_original_price: '',
					goods_stock: '',
					goods_weight: '',
					delivery_id: '',
					delivery_name: '',
					template_name: '',
					template_id: '',
				},
				content: '',
				skuchecked: false,
				salesChecked: false,
				goodSourceImageList: [],
				goodsDetailImageList: [],
				goodsImageList: [],
				deliveryTemplate: [{
					text: '南京市',
					id: 0
				}, {
					text: '西藏',
					id: 1
				}],
				deliveryList: [{
					text: '物流',
					id: 0
				}, {
					text: '送货上门',
					id: 1
				}],
				isShow: false,
				value: "",
				tabLIndex: 0,
				tabRIndex: 0,
				action: '',
				header: '',
			};
		},
		onReady() {
			this.action = (Config.get('apiUrl') + 'upload/image').replace("index.php?s=/", "")
			this.header = {
				'Storeid': uni.getStorageSync('Store').storeInfo.store_id,
				'Access-Token': uni.getStorageSync('AccessToken'),
				'platform': "MP-WEIXIN",
			}
		},
		methods: {
			// 提交信息
			async onSubmit() {
				const that = this;
				console.log(that.from)
				return
				if (!that.from.goods_name) {
					return that.$toast('请输入商品名称')
				}
				let {
					status,
					message,
					data
				} = await newFunApi.addTransfer(that.form);
				if (status == 200) {
					uni.showToast({
						title: "上架成功"
					})
					setTimeout(function() {
						uni.$emit('refreshData');
						uni.navigateBack({
							delta: 1
						})
					}, 2000);
				}
			},
			// 原价
			keyOriginalPrice(e) {
				if (Number(e.detail.value) == 0 || Number(e.detail.value) < 0) {
					this.from.goods_original_price = ''
					return this.$toast('商品价不能低于0.1元')
				}
				this.from.goods_original_price = this.blurAmount(e.detail.value)
			},
			// 商品价
			keyGoodsPrice(e) {
				if (Number(e.detail.value) == 0 || Number(e.detail.value) < 0) {
					this.from.goods_price = ''
					return this.$toast('商品价不能低于0.1元')
				}
				this.from.goods_price = this.blurAmount(e.detail.value)
			},
			// 价格规则
			blurAmount(amount) {
				let num = null
				num = amount.replace(new RegExp('^(\\d+\\.\\d{2}).+'), '$1')
				const startPoint = /^\./g
				if (startPoint.test(num)) {
					num = amount.replace(startPoint, '0.')
				}
				if (num && !num.includes('.') && num !== '0') {
					num = +num
				}
				const morePoint = /\.+(\d*|\.+)\./g
				if (morePoint.test(num)) {
					num = amount
						.replace(/\.{2,}/g, ".")
						.replace(".", "$#$")
						.replace(/\./g, "")
						.replace("$#$", ".")
				}
				return num
			},
			// 获取配送方式
			changeDelivery(e) {
				this.from.delivery_name = this.deliveryList[e.detail.value].text;
				this.from.delivery_id = this.deliveryList[e.detail.value].id;
			},
			// 运费模板
			changeDeliveryTemplate(e) {
				this.from.template_name = this.deliveryTemplate[e.detail.value].text;
				this.from.template_id = this.deliveryTemplate[e.detail.value].id;
			},
			// 一键获取
			onClickAccess() {

			},
			//接受上传返回的数据商品
			goosSuccess(list) {
				if (list.length > 0) {
					const {
						response
					} = list[0];
					if (response.status == 200) {
						uni.showToast({
							title: "上传成功"
						})
						this.from.goods_image_id = this.handleRemove('', list)
					}
				}
			},
			// 详情
			detailSuccess(list) {
				if (list.length > 0) {
					const {
						response
					} = list[0];
					if (response.status == 200) {
						uni.showToast({
							title: "上传成功"
						})
						this.from.details_image_id = this.handleRemove('', list)
					}
				}
			},
			// 素材
			sourceSuccess(list) {
				if (list.length > 0) {
					const {
						response
					} = list[0];
					if (response.status == 200) {
						uni.showToast({
							title: "上传成功"
						})
						this.from.source_image_id = this.handleRemove('', list)
					}
				}
			},
			goodsRemove: function(file, fileList) {
				this.from.goods_image_id = this.handleRemove(file, fileList)
			},
			detailsRemove: function(file, fileList) {
				this.from.details_image_id = this.handleRemove(file, fileList)
			},
			sourceRemove: function(file, fileList) {
				this.from.source_image_id = this.handleRemove(file, fileList)
			},
			handleRemove: function(file, fileList) {
				console.log(file, fileList)
				let idList = []
				fileList.forEach(item => {
					idList.push(item.response.data.fileInfo.file_id)
				});
				return idList.join(',')
			},
			openPage(i) {
				if (i == 1) {
					this.isShow = true;
				} else if (i == 2) {
					this.isShow = false;
					uni.navigateTo({
						url: "/pages/news/goods/classify"
					})
				}
			},
			tabLItem(i) {
				this.tabLIndex = i
				console.log(this.tabLIndex)
			},
			tabRItem(i) {
				this.tabRIndex = i
				console.log(this.tabRIndex)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.goods {
		padding: 0 0 130rpx;
		overflow: hidden;
		margin-top: 12rpx;

		&-top {
			background-color: #fff;
			padding: 40rpx 25rpx;
			overflow: hidden;
			margin-bottom: 20rpx;

			input {
				display: block;
				line-height: 60rpx;
				font-size: 28rpx;
				color: #212121;
			}

			.btn {
				width: 158rpx;
				line-height: 50rpx;
				background: #FF5050;
				border-radius: 50rpx;
				text-align: center;
				font-size: 24rpx;
				font-weight: 500;
				color: #FFFFFF;
				float: right;

			}
		}

		&-hd {
			background-color: #fff;
			padding: 0 25rpx 0;
			overflow: hidden;
			margin-bottom: 20rpx;

			.item {
				padding: 20rpx 30rpx;
				line-height: 50rpx;
				font-size: 28rpx;
				color: #212121;
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1px solid #F7F7F7;

				.a {
					text {
						color: #F34A40;
						margin-left: 5rpx;
					}
				}

				.b {
					font-size: 28rpx;
					color: #212121;
					display: flex;
					align-items: center;
					justify-content: flex-end;
					flex: 1;

					input {
						font-size: 28rpx;
						color: #6D6D6D;
						flex: 1;
						line-height: 50rpx;
						font-size: 28rpx;
						text-align: right;
						margin-right: 10rpx;
					}

					.select {
						width: 100%;
						display: flex;
						align-items: center;
						color: #C7C7C7;
						justify-content: flex-end;

						&-on {
							color: #212121;
						}
					}
				}
			}

			.items {
				padding: 20rpx 30rpx;
				line-height: 50rpx;
				font-size: 28rpx;
				color: #212121;
				border-bottom: 1px solid #F7F7F7;

				.t {
					width: 480rpx;
					height: 40rpx;
					padding-left: 30rpx;
					font-size: 28rpx;
					color: #212121;
					margin-top: 5rpx;
				}

				.a {
					flex: 1;
					display: flex;
					align-items: center;

					input {
						flex: 1;
						font-size: 26rpx;
						color: #212121;
						line-height: 50rpx;
						margin-left: 20rpx;
					}

					text {
						color: #F34A40;
						margin-left: 5rpx;
					}

					.select {
						flex: 1;
					}

					.tip {
						margin-left: 15rpx;
						font-size: 24rpx;
						font-weight: 500;
						color: #9F9F9F;
					}
				}

				.b {
					padding-top: 20rpx;
					font-size: 24rpx;
					font-weight: 400;
					color: #C7C7C7;
				}

				.c {
					padding-top: 20rpx;
					overflow: hidden;

					.slot-btn {
						width: 180rpx;
						height: 180rpx;
						background: #F6F6F6;
						border-radius: 10rpx;
						border: 1px solid #C0C0C0;
						text-align: center;
						font-size: 28rpx;
						font-weight: 500;
						color: #6D6D6D;

						image {
							width: 50rpx;
							height: 50rpx;
							margin-top: 30rpx;
						}
					}
				}
			}

		}

		&-fd {
			width: 100%;
			position: fixed;
			left: 0;
			bottom: 0;
			padding: 30rpx;
			z-index: 99;
			background-color: #fafafa;
			box-sizing: border-box;

			.btn {
				width: 630rpx;
				line-height: 88rpx;
				background: #F34A40;
				border-radius: 88rpx;
				text-align: center;
				font-size: 28rpx;
				font-weight: 500;
				color: #FFFFFF;
			}
		}

		&-classify {
			width: 100%;
			overflow: hidden;

			&-hd {
				padding: 30rpx 60rpx;
				position: relative;
				font-size: 32rpx;
				font-weight: 500;
				color: #303030;
				text-align: center;

				.a {
					display: flex;
					align-items: center;
					font-size: 28rpx;
					font-weight: 500;
					color: #FF624F;
					position: absolute;
					top: 30rpx;
					left: 40rpx;
					z-index: 2;
				}
			}

			&-bd {
				overflow: hidden;
				display: flex;
				align-items: flex-start;

				.l {
					width: 210rpx;
					height: 560rpx;
					background-color: #fff;

					.item {
						text-overflow: ellipsis;
						overflow: hidden;
						height: 80rpx;
						font-size: 28rpx;
						font-weight: 400;
						color: #303030;
						white-space: nowrap;
						line-height: 80rpx;
						padding-left: 30rpx;
						box-sizing: border-box;

						&-on {
							background-color: #fff;
							font-weight: 500;
						}
					}
				}

				.m {
					width: 100%;
					text-align: center;
					max-height: 560rpx;

					.item {
						text-overflow: ellipsis;
						overflow: hidden;
						height: 70rpx;
						font-size: 28rpx;
						font-weight: 400;
						color: #303030;
						white-space: nowrap;
						line-height: 70rpx;
						box-sizing: border-box;

						&-on {
							color: #FF624F;
						}
					}
				}

				.r {
					width: 540rpx;
					height: 560rpx;

					.item {
						text-overflow: ellipsis;
						overflow: hidden;
						height: 80rpx;
						font-size: 28rpx;
						font-weight: 400;
						color: #303030;
						white-space: nowrap;
						line-height: 80rpx;
						padding-left: 80rpx;
						box-sizing: border-box;

						&-on {
							color: #FF624F;
						}
					}
				}
			}
		}
	}

	::v-deep .u-list-item {
		margin: 10rpx 20rpx 10rpx 0 !important;
	}

	::v-deep .uni-textarea {
		max-height: 100rpx !important;
	}

	::v-deep .uni-input-placeholder {
		color: #C7C7C7 !important;
	}
</style>
