<template>
	<view class="select_warehouse">
		<view class="wrapper flex_a_c_j_sb">
			<block v-for="item in goodsData" :key="item.name">
				<view class="select_item" @click="navigato(item.type)"
					:style="{'background-image': `url(${item.src})`}">
					<view class="title">{{ item.name }}</view>
					<view class="iconfont icon-jiantou"></view>
				</view>
			</block>
		</view>
		<view class="list-box listA" :class="goods.length > 0 ? 'fadeIn on' : ''">
			<view class="item" v-for="(item, index) in goods" :key="index">
				<view class="pictrue">
					<easy-loadimage mode="widthFix" :image-src="item.image"></easy-loadimage>
				</view>
				<view class="text-info">
					<view class="title">{{ item.store_name }}</view>
					<view class="price">¥<text>{{ item.price }}</text></view>
					<view class="add-btn" @click="addGoods(item)">加入仓库</view>
				</view>
			</view>
		</view>
		<u-popup :show="editGoodsShow" @close="editGoodsClose" :round="18" mode="center">
			<view class="edit_goods_box">
				<view class="edit_name flex_a_c edit_name_no">
					<text>商品编号：</text><input type="text" disabled placeholder="请输入商品名称" v-model="editGoodsCode">
				</view>
				<view class="edit_name flex_a_c edit_name_no">
					<text>商品名称：</text> <input type="text" disabled placeholder="请输入商品名称" v-model="editGoodsName">
				</view>
				<view class="edit_name flex_a_c">
					编辑商品价格：<input type="text" placeholder="请输入商品名称" v-model="editGoodsPrice">
				</view>
				<view class="edit_store flex_a_c">
					编辑商品库存：<input type="text" placeholder="请输入库存" v-model="editGoodsStore">
				</view>
				<view class="edit_goods_btn">
					<u-button type="error" text="取消" class="edit_btn" @click="editGoodsShow=false">
					</u-button>
					<u-button type="primary" text="保存" class="edit_btn" @click="editGoodsClose">
					</u-button>
				</view>
			</view>
		</u-popup>
		<u-modal :show="addGoodsShow" content='是否添加到你的仓库中?' @cancel="addGoodsclose()" @close="addGoodsclose()"
			@confirm="addGoodsopen()" :closeOnClickOverlay="true" :showCancelButton="true"></u-modal>
		<u-popup :show="searchGoodsShow" @close="searchGoodsClose" :round="18" mode="center">
			<view class="search_goods_box">
				<input type="text" v-model="searchGoodsName" class="searchGoods" placeholder="请输入商品名称">
				<view class="search_goods_btn">
					<view class="cancel edit_btn" @click="searchGoodsShow=false">取消</view>
					<view class="search edit_btn" @click="searchGoodsClose">搜索</view>
				</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	import {
		productCreate
	} from '@/api/product.js'
	import {
		microSeachBarCode,
		microEadtProduct
	} from '@/api/store.js'
	import {
		seachBarCodeAPI,
		post_product_import
	} from '@/api/api.js'
	import {
		Toast
	} from '@/libs/uniApi'
	export default {
		data() {
			return {
				goodsData: [{
						name: '',
						type: 1,
						src: 'https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/mingcheng.png'
					},
					{
						name: '扫码入库',
						type: 2,
						src: 'https://lihai001.oss-cn-chengdu.aliyuncs.com/public/kk/luzhou/static4/saoma.png'
					}
				],
				product_id: '',
				editGoodsCode: '',
				editGoodsName: '',
				editGoodsPrice: '',
				editGoodsStore: '',
				editGoodsShow: false, // 扫码修改商品信息
				mer_id: 0,
				searchGoodsShow: false,
				searchGoodsName: '',
				goods: [],
				item: [],
				addGoodsShow: false,
				stype: '',
			}
		},
		onLoad(e) {
			this.mer_id = e.mer_id
			this.stype = e.stype
			if (this.stype == 1) {
				return this.goodsData[0].name = '商品添加'
			}
			if (this.stype == 2) {
				return this.goodsData[0].name = '商品名称添加'
			}
		},
		onShow() {

		},
		methods: {
			addGoods(item) {
				this.addGoodsShow = true
				this.item = item
			},
			addGoodsopen() {
				if (this.item) {
					post_product_import(this.item.product_id).then(e => {
						if (e.status == 200) {
							Toast(e.data.msg)
							this.addGoodsShow = false
						} else {
							// console.log(e);
							Toast(e.message)
							this.addGoodsShow = false
						}
					}).catch(err => {
						// console.log(err);
						if (err == "已经导入过该商品了") {
							Toast('已经导入过该商品了')
							this.addGoodsShow = false
						} else {
							Toast('导入出错')
							this.addGoodsShow = false
						}
					})
				}
				this.item = []
			},
			addGoodsclose() {
				this.addGoodsShow = false
			},
			openSearch() {},
			async searchGoodsClose() {
				const rq = {
					mer_id: this.mer_id,
					name: this.searchGoodsName
				}
				const {
					data
				} = await seachBarCodeAPI(rq).catch(err => Toast(err))
				if (data.list.length < 1) {
					this.searchGoodsShow = false
					Toast("暂无搜索商品,请添加！")
					setTimeout(() => {
						uni.navigateTo({
							url: `/pages/product/addGoods/index?mer_id=${rq.mer_id}`
						})
					}, 2000)
					return
				}
				this.goods = data.list
				this.searchGoodsShow = false
			},
			async editGoodsClose() {
				const data = {
					price: this.editGoodsPrice,
					stock: this.editGoodsStore,
					id: this.product_id
				}
				try {
					const res = await microEadtProduct(data)
					Toast("更新成功")
					this.editGoodsShow = false
				} catch (e) {
					Toast("更新失败")
					this.editGoodsShow = false
				}
			},
			navigato(type) {
				if (type === 1) {
					if (this.stype == 1) {
						uni.navigateTo({
							url: `/pages/product/addGoods/index?mer_id=${this.mer_id}`
						});
						return;
					}
					this.searchGoodsShow = true
				} else {
					this.scanCode();
				}
			},
			scanCode() {
				const self = this
				uni.scanCode({
					scanType: ["qrCode", "barCode", "datamatrix", "pdf417"],
					success(res) {
						if (self.stype == 1) {
							uni.navigateTo({
								url: `/pages/product/addGoods/index?mer_id=${self.mer_id}&code=${res.result}`
							})
						} else {
							self.seachBarCode(res.result)
						}

					},
					fail(res) {
						// console.log(res);
					},
				})
			},
			async seachBarCode(code) {
				await seachBarCodeAPI({
					code: code,
					mer_id: this.mer_id
				}).then(res => {
					if (!res.data) {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none',
						});
					} else {
						if (res.data.list.length < 1) {
							uni.navigateTo({
								url: `/pages/product/addGoods/index?mer_id=${this.mer_id}&code=${code}`
							})
							return
						}
						this.goods = res.data.list
					}
				})


			},
		},
		onPullDownRefresh() {
			uni.stopPullDownRefresh()
		},
	}
</script>

<style lang="scss">
	.select_warehouse {
		padding-top: 20px;
	}

	.wrapper {
		width: 694.74rpx;
		margin: 0 auto;
	}

	.goods-wrapper {
		.item {
			display: flex;
			height: 175.44rpx;
			width: 100%;
			background-color: #fff;
			margin-top: 21.05rpx;
			border-radius: 8px;
			padding: 12px;

			.l_cont {
				image {
					width: 133.33rpx;
					height: 133.33rpx;
					border-radius: 8px;
				}
			}

			.r_cont {
				margin-left: 17.54rpx;
			}
		}
	}

	.select_item {
		width: 336.84rpx;
		height: 189.47rpx;
		border-radius: 8px;
		background-color: #eee;
		padding: 35.09rpx 28.07rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		color: #fff;
		background-size: cover;
		background-repeat: no-repeat;

		.title {
			font-size: 35.09rpx;
		}
	}

	.search_goods_box {
		width: 694.74rpx;
		padding: 28.07rpx;
		font-size: 31.58rpx;

		.searchGoods {
			height: 87.72rpx;
			border: 1px solid #f5f5f5;
			border-radius: 8px;
			padding: 12px;
			margin: 21.05rpx 0;
		}

		.search_goods_btn {
			margin-top: 28.07rpx;
			display: flex;
			justify-content: space-between;

			.edit_btn {
				color: #fff;
				border-radius: 8px;
				text-align: center;
				line-height: 70.18rpx;
				height: 70.18rpx;
				width: 210.53rpx;
			}

			.cancel {
				background-color: $u-error;
			}

			.search {
				background-color: $u-primary;
			}
		}
	}

	// 扫码编辑商品
	.edit_goods_box {
		width: 694.74rpx;
		padding: 28.07rpx;
		font-size: 31.58rpx;

		text {
			color: '#f8f9f9';
		}

		.edit_name,
		.edit_store {
			height: 105.26rpx;
		}

		.edit_name_no {
			color: #a4a4a4;
		}

		.edit_store {
			// border-top: 1px solid #a4a4a4;
			margin-top: 21.05rpx;
		}

		/deep/.u-button {
			width: 300rpx;
		}

		.edit_goods_btn {
			margin-top: 28.07rpx;
			display: flex;
			justify-content: space-between;

			.edit_btn {
				width: 210.53rpx;
			}
		}
	}

	.list-box {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 20rpx 28rpx 0;

		.item {
			width: 328rpx;
			margin-bottom: 20rpx;
			overflow: hidden;
			position: relative;
			border-radius: 8px;
			background-color: #fff;

			&.on {
				border-radius: 0;
			}

			.pictrue_log {
				width: 92rpx;
				height: 44rpx;
				font-size: 26rpx;
				line-height: 44rpx;
			}

			.pictrue,
			/deep/image,
			/deep/.easy-loadimage,
			/deep/uni-image {
				width: 100%;
				display: block;
				position: relative;

				.border-picture {
					position: absolute;
					top: 0;
					left: 0;
					width: 100%;
					height: 100%;
					background: center/cover no-repeat;
				}
			}

			.picture1,
			/deep/.picture1 image,
			/deep/.picture1 .easy-loadimage,
			/deep/.picture1 uni-image {
				height: 346rpx;
				position: relative;

				.border-picture {
					position: absolute;
					top: 0;
					left: 0;
					width: 100%;
					height: 100%;
					background: center/cover no-repeat;
				}

				.sell_out {
					display: flex;
					width: 150rpx;
					height: 150rpx;
					align-items: center;
					justify-content: center;
					border-radius: 100%;
					background: rgba(0, 0, 0, .6);
					color: #fff;
					font-size: 30rpx;
					position: absolute;
					top: 50%;
					left: 50%;
					margin: -75rpx 0 0 -75rpx;

					&::before {
						content: "";
						display: block;
						width: 140rpx;
						height: 140rpx;
						border-radius: 100%;
						border: 1px dashed #fff;
						position: absolute;
						top: 5rpx;
						left: 5rpx;
					}
				}
			}

			.cont1,
			/deep/.cont1 image,
			/deep/.cont1 .easy-loadimage,
			/deep/.cont1 uni-image,
			.cont1 .border-picture {
				border-radius: 16rpx;
			}

			.text-info {
				padding: 10rpx 20rpx 15rpx;
				display: flex;
				flex-direction: column;

				.add-btn {
					align-self: flex-end;
					text-align: center;
					width: 175.44rpx;
					color: #fff;
					margin-top: auto;
					padding: 7.02rpx 21.05rpx;
					border-radius: 100px;
					background: $uni-theme-bg-color;
				}

				.title {
					font-size: 31.58rpx;
					margin-bottom: 10.53rpx;
					color: #222222;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}

				.old-price {
					margin-top: 4rpx;
					font-size: 26rpx;
					color: #999;
					text-decoration: line-through;

					text {
						margin-right: 2px;
						font-size: 20rpx;
					}
				}

				.price {
					display: flex;
					margin-top: 20rpx;
					font-size: 26rpx;
					align-items: center;

					text {
						font-size: 36rpx;
						font-weight: 550;
					}

					.ot-price {
						color: #aaa;
						font-size: 26rpx;
						text-decoration: line-through;
						margin-left: 6rpx;
						font-weight: normal;
						margin-top: 10rpx;
					}
				}
			}

			.pictrue {
				position: relative;
			}

			.border-picture {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
				border-radius: 8rpx;
				background: center/cover no-repeat;
			}
		}

		.merchant_info {
			display: flex;
			align-items: center;
			margin-top: 20rpx;

			.merchant_type {
				color: #fff;
				line-height: 30rpx;
				padding: 0 10rpx;
				border-radius: 2rpx;
				font-size: 22rpx;
			}

			.txt {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 56rpx;
				height: 28rpx;
				margin-left: 15rpx;
				border: 1px solid $theme-color;
				border-radius: 4rpx;
				font-size: 20rpx;
				font-weight: normal;

				&.delivery {
					margin-left: 0;
					color: #FF9000;
					border-color: #FF9000;
				}
			}
		}

		&.on {
			display: flex;
		}

		&.listA {
			.item {
				display: flex;
				width: 100%;

				.pictrue,
				/deep/image,
				/deep/.easy-loadimage,
				/deep/uni-image {
					width: 220rpx;
					height: 220rpx;
				}

				.sell_out {
					display: flex;
					width: 110rpx;
					height: 110rpx;
					align-items: center;
					justify-content: center;
					border-radius: 100%;
					background: rgba(0, 0, 0, .6);
					color: #fff;
					font-size: 24rpx;
					position: absolute;
					top: 50%;
					left: 50%;
					margin: -55rpx 0 0 -55rpx;

					&::before {
						content: "";
						display: block;
						width: 100rpx;
						height: 100rpx;
						border-radius: 100%;
						border: 1px dashed #fff;
						position: absolute;
						top: 5rpx;
						left: 5rpx;
					}
				}

				.text-info {
					width: 490rpx;
				}
			}
		}

		&.listB {
			justify-content: inherit;

			.item {
				width: 31.3%;
				margin-right: 3.05%;

				.pictrue,
				/deep/image,
				/deep/.easy-loadimage,
				/deep/uni-image {
					height: 220rpx;
				}

				.sell_out {
					display: flex;
					width: 110rpx;
					height: 110rpx;
					align-items: center;
					justify-content: center;
					border-radius: 100%;
					background: rgba(0, 0, 0, .6);
					color: #fff;
					font-size: 24rpx;
					position: absolute;
					top: 50%;
					left: 50%;
					margin: -55rpx 0 0 -55rpx;

					&::before {
						content: "";
						display: block;
						width: 100rpx;
						height: 100rpx;
						border-radius: 100%;
						border: 1px dashed #fff;
						position: absolute;
						top: 5rpx;
						left: 5rpx;
					}
				}

				&:nth-child(3n) {
					margin-right: 0;
				}

				.price {
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 20rpx;

					text {
						font-size: 28rpx;
					}
				}

				.text-info {
					padding: 10rpx 4rpx;
				}
			}
		}

		&.listC {
			.item {
				width: 100%;

				.pictrue,
				/deep/image,
				/deep/.easy-loadimage,
				/deep/uni-image {
					height: 320rpx;
				}

				.price {
					margin-top: 20rpx;
					font-size: 40rpx;
					display: flex;
					align-items: center;

					.old-price {
						font-weight: normal;
						font-size: 22rpx;
						margin-left: 10rpx;
					}
				}
			}
		}

		&.listS {
			.price {
				font-size: 40rpx;
				display: flex;
				align-items: baseline;

				.old-price {
					font-weight: normal;
					font-size: 22rpx;
					margin-left: 10rpx;
				}
			}
		}
	}
</style>