<!-- 底部菜单--库存--新增商品 -->
<template>
	<view class="page1">
		<view class="formBox">
			<uni-forms ref="form" :modelValue="formData" :rules="rules" labelWidth="170rpx">
				<uni-forms-item label="商品名称" name="productName" required>
					<uni-easyinput type="text" v-model="formData.productName" placeholder="请输入商品名称" trim="all" />
				</uni-forms-item>
				<uni-forms-item label="商品主图" name="productPicture">
					<view v-if="formData.productPicture !=''">
						<img :src="formData.productPicture" alt="" class="upload-img">
					</view>
					<u-upload :fileList="formData.productPicture" @afterRead="afterRead" @delete="deletePic" name="1"
						multiple :maxCount="1"></u-upload>
				</uni-forms-item>
				<uni-forms-item label="批发价" name="wholesalePrice">
					<!-- <uni-easyinput type="digit" v-model="formData.wholesalePrice" placeholder="请输入批发价" trim="all" /> -->
					<input type="digit" class="content-input" v-model="formData.wholesalePrice"
						@focus="obtainPrice($event,'wholesalePrice')" @blur="changessPrice($event,'wholesalePrice')"
						placeholder="请输入批发价">
				</uni-forms-item>
				<uni-forms-item label="商品分类" name="categoryId">
					<view class="content-list">
						<view class="list-val">
							<input type="text" placeholder="请选择商品分类" disabled :value="typeItem.categoryName"
								style="border-bottom: 1px solid #fff;text-align: center;" @tap="goGategoryLow">
							<uni-icons type="right" size="16"
								style="margin-left: 40rpx;line-height: 70rpx;"></uni-icons>
						</view>
					</view>
				</uni-forms-item>
				<view style="display: flex;" v-if="customerCategoryList != null">
					<view class="" style="width: 28%;"></view>
					<view class="" style="width: 72%;min-height: 80rpx;padding: 0 20rpx 20rpx 0;">
						<view v-for="(item,index) in customerCategoryList" :key="index">
							{{item.categoryName}}:{{item.categoryPrice}}
						</view>
					</view>
				</view>
				<uni-forms-item label="添加规格" name="categoryId">
					<view class="content-list">
						<view class="list-val">
							<input type="text" placeholder="点击选择规格" disabled
								style="border-bottom: 1px solid #fff;text-align: center;" @tap="ejectRuleSelect">
						</view>
					</view>
				</uni-forms-item>
				<view v-for="(item,index) in multipleList" :key="index" style="display: flex;">
					<view class="" style="width: 28%;">
						{{item.attributeName}}
					</view>
					<view class="" style="width: 72%;min-height: 80rpx;padding: 0 20rpx 20rpx 0;">
						<view v-for="(item1,index1) in item.productAttributeValueVoList" :key="index1"
							style="float: left;padding-right: 20rpx;">
							{{item1.value}}
						</view>
					</view>
				</view>
				<uni-forms-item label="商品条码" name="barcode">
					<uni-easyinput type="text" v-model="formData.barcode" placeholder="可以扫码商品条码添加" trim="all" />
					<u-icon name="camera" size="36" @tap.stop='scanCodeAdd'></u-icon>
				</uni-forms-item>
				<uni-forms-item label="多图上传" name="productPicture">
					<view class="upload-img" @tap="pictureAdd"
						style="background: #eee;text-align: center;line-height: 160rpx;">
						<uni-icons type="camera" size="28" color="#777"></uni-icons>
					</view>
					<view style="overflow-x: scroll;display: flex;width: 350rpx;">
						<view v-if="formData.productPictures !=''" v-for="(item,index) in formData.productPictures"
							:key="index" style="width: 160rpx;height: 160rpx;margin-left: 20rpx;">
							<img :src="item.productPicture" alt="" class="upload-img">
						</view>
					</view>

				</uni-forms-item>
				<uni-forms-item label="尾货预警" name="salesWeek">
					<!-- <uni-easyinput type="digit" v-model="formData.salesWeek" placeholder="请输入零售价" trim="all" /> -->
					<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.salesWeek" />
				</uni-forms-item>
				<uni-forms-item label="货品简介" name="productIntro">
					<uni-easyinput type="textarea" autoHeight v-model="formData.productIntro" placeholder="请输入货品简介"
						trim="all" />
				</uni-forms-item>
			</uni-forms>
			<view>
				<button @tap.stop="submit()">保存</button>
			</view>
		</view>
		<!-- 添加分类弹出层 -->
		<uni-popup ref="addTypeDialog" background-color="#fff" @change="changeAddType">
			<view class="add-popup-content">
				<view class="add-popup-title">
					<text>颜色尺码库</text>
					<uni-icons @tap='$refs.addTypeDialog.close()' type="closeempty" color='red' size="16"
						style="height:40rpx;line-height: 40rpx;position: absolute !important;bottom:0;right: 30rpx;"></uni-icons>
				</view>
				<view class="add-popup-container">
					<ul>
						<li v-for="(item,index) in defaultList" :key="index">
							<view class="title-fur">
								<view class="name">{{item.name}}</view>
								<u-icon size="20" name="plus" color="#5fd9ee" @tap="toggle(index) "></u-icon>
							</view>
							<view class="content-fur">
								<view class="content-low" :class="item1.checked?'checked':''"
									v-for="(item1,index1) in item.list" :key="index1"
									@tap.stop="isCheckPrec(item,item1.value,index1)">{{item1.value}}</view>
							</view>
						</li>
					</ul>
					<view class="add-popup-bottom">
						<view class="add-popup-btn" @tap="dialogInputConfirm()">确认</view>
					</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="addDialog" type="dialog">
			<uni-popup-dialog ref="addClose" mode="input" :title="'请输入规格名'" :value="specVal" placeholder="请输入规格名称"
				@confirm="addSpec" @close="dialogClose">
			</uni-popup-dialog>
		</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;">
					{{formData.productSn}}有以下库存，确认继续删除吗？
				</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="cancelCheck">取消选中</view>
					</view>
				</view>
			</view>
		</uni-popup>
		<!-- 维护客户价格弹出层 -->
		<uni-popup ref="customPopup" background-color="#fff">
			<view class="popup-content" style="height: auto;max-height: 600rpx;margin-bottom: 0;">
				<view v-for="(item,index) in customerCategoryList" :key="index">
					<view style="height: 60rpx;line-height: 60rpx;">
						{{item.categoryName}}
					</view>
					<input type="digit" v-model='item.categoryPrice' placeholder="请输入客户价格" style="height: 60rpx;padding-left: 20rpx;border: 1px solid #eee;border-radius: 10rpx;">
				</view>
				<view @tap="updateCate" style="text-align:center;margin-top:20rpx;width: 100%;height: 60rpx;line-height: 60rpx;background: #088FEB;color: #fff;">
					确认
				</view>
			</view>
		</uni-popup>
		<!-- 给供应商回款的弹层 -->
		<uni-popup ref="warnPopup" background-color="#fff">
			<view class="popup-content" style="width: 500rpx;height: 760rpx;">
				<view style="font-size: 36rpx;font-weight: bold;text-align: center;">提示</view>
				<view
					style="width:100%;font-weight:bold;font-size: 28rpx;line-height: 40rpx;margin: 20rpx 0;display: flex;flex-direction: column;">
					<text>历次入库总数量:{{guigeNum}}</text>
					<text style="margin: 20rpx 0;">差价:{{guigeNum}} * ({{oldPurchasePrice}} - {{formData.purchasePrice}})
						= {{zPrice}}</text>
					<view>是否使用此<input type="text" v-model="zPrice"
							style="width: 150rpx;border-bottom:1px solid #000;color:red;text-align:center;display: inline-block;">
						（金额可修改）金额，给供应商“{{dataList.supplierName}}”回款？</view>
				</view>
				<view style="border: 1px solid #eee;height: 70rpx;margin: 30rpx auto 0;">
					<uni-datetime-picker type="datetime" :clear-icon="false" v-model="single" />
				</view>
				<view style="border: 1px solid #eee;height: 140rpx;margin: 30rpx auto 0;">
					<textarea maxlength="-1" @input='changeRemark($event)' v-model="rechargeRemark" placeholder="请输入充值备注" style='width: 100%;height: 100%;padding:10rpx' />
				</view>
				<view class="popup-bottom" style="position: unset;margin-top:20rpx;">
					<view class="popup-bottom-left">
						<view class="bottom-btn" @tap="this.$refs.warnPopup.close()"
							style="background: linear-gradient(90deg, #FF9797, #FFC1E0);">不需要</view>
					</view>
					<view class="popup-bottom-right">
						<view class="bottom-btn" @tap="supplierCollection">回款</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				single:'',
				rechargeRemark:'',
				fileList1: [],
				oldMultipleList: [],
				specVal: '',
				snList: [],
				prefixRule: '',
				addIndex: 0,
				numRule: '',
				goodsList: [],
				snName: '',
				formData: {
					productName: '',
					price: 0,
					productSn: '',
					purchasePrice: 0,
					wholesalePrice: 0,
					commission:0,
					productPicture: '',
					productIntro: '',
					categoryId: '',
					salesWeek: '',
					tailWarn: '',
					barcode: '',
					productPictures: [],
					productCategoryVo: {}
				},
				defaultList: [{
					name: '颜色',
					list: [{
						value: '均色',
						checked: false,
					}, {
						value: '白色',
						checked: false,
					}, {
						value: '黑色',
						checked: false,
					}, {
						value: '浅蓝',
						checked: false,
					}, {
						value: '蓝色',
						checked: false,
					}, {
						value: '灰色',
						checked: false,
					}, {
						value: '咖色',
						checked: false,
					}, {
						value: '紫色',
						checked: false,
					}, {
						value: '红色',
						checked: false,
					}, {
						value: '米色',
						checked: false,
					}]
				}, {
					name: '尺码',
					list: [{
						value: '均码',
						checked: false,
					}, {
						value: 'XS码',
						checked: false
					}, {
						value: 'S码',
						checked: false,
					}, {
						value: 'M码',
						checked: false,
					}, {
						value: 'L码',
						checked: false,
					}, {
						value: 'XL码',
						checked: false,
					}, {
						value: '2XL码',
						checked: false,
					}, {
						value: '3XL码',
						checked: false,
					}, {
						value: '4XL码',
						checked: false,
					}, {
						value: '25码',
						checked: false
					}, {
						value: '26码',
						checked: false
					}, {
						value: '27码',
						checked: false
					}, {
						value: '28码',
						checked: false
					}, {
						value: '29码',
						checked: false
					}, {
						value: '30码',
						checked: false
					}, {
						value: '31码',
						checked: false
					}, {
						value: '32码',
						checked: false
					}, {
						value: '33码',
						checked: false
					}, {
						value: '34码',
						checked: false
					}, {
						value: '35码',
						checked: false
					}, {
						value: '36码',
						checked: false
					}, {
						value: '37码',
						checked: false
					}, {
						value: '38码',
						checked: false
					}, {
						value: '39码',
						checked: false
					}, {
						value: '40码',
						checked: false
					}, {
						value: '41码',
						checked: false
					}, {
						value: '42码',
						checked: false
					}, {
						value: '43码',
						checked: false
					}, {
						value: '44码',
						checked: false
					}, {
						value: '45码',
						checked: false
					}]
				}],
				categoryList: [],
				cancelArr: [],
				productId: '',
				shopId:'',
				arrtList: [],
				guigeNum: 0, //总共有多少件库存
				differencePurchasePrice: 0, //旧采购价和新维护采购价的差额差额
				oldPurchasePrice: 0,

				userType: '',
				typeItem: {},
				arrtChange: [],
				categoryId: '',
				dataList: {},
				zPrice: 0,
				type: 'add',
				customerCategoryList:[],
				multipleList: [],
				rules: {
					categoryId: {
						rules: [{
							required: true,
							errorMessage: '请选择商品分类',
						}, ],
					},
					productSn: {
						rules: [{
							required: true,
							errorMessage: '请填写货号',
						}, ],
					},
					productName: {
						rules: [{
							required: true,
							errorMessage: '请填写商品名称',
						}, ],
					}
				}
			}
		},
		filters: {
			sliceMsg(val) {
				var name = ''
				if (typeof(val) == 'string') {
					let newObj = JSON.parse(val)
					for (let as in newObj) {
						name += newObj[as] + '/'
					}
				}
				return name;
			}
		},
		onShow() {
			let that = this;
			uni.authorize({
				scope: 'scope.writePhotosAlbum',
				success() {

				},
				fail() {
					this.tui.toast("您未授权，图片上传功能可能会出现错误")
				}
			})
			this.shopId = uni.getStorageSync('shopId')
			this.typeItem = JSON.parse(JSON.stringify(this.typeItem)) || ''
			if (this.typeItem) {
				this.formData.categoryId = this.typeItem.id
			}
			
		},
		onLoad(option) {
			this.getCustomerCategoryList()
			if (option.dataList) {
				this.dataList = JSON.parse(option.dataList)
				this.tui.request("/app/product/getById/" + this.dataList.id, "POST", {

				}, false, true).then((res) => {
					if (res.code == 200) {
						this.type = 'edit'
						this.customerCategoryList = this.checkPriceRule((res.result.customerCategoryRule != '' && res.result.customerCategoryRule != null)? JSON.parse(res.result.customerCategoryRule): null)
						
						this.formData.attrId = res.result.attrId
						this.formData.productName = res.result.productName
						this.formData.barcode = res.result.barcode
						this.formData.price = res.result.price
						this.formData.productSn = res.result.productSn
						this.formData.stockCount = this.dataList.stockCount
						this.formData.minusStockCount = this.dataList.minusStockCount
						this.formData.purchasePrice = res.result.purchasePrice
						this.oldPurchasePrice = res.result.purchasePrice
						this.formData.wholesalePrice = res.result.wholesalePrice
						
						this.formData.commission = res.result.commission
						this.formData.productPicture = res.result.productPicture
						this.formData.productIntro = res.result.productIntro
						this.formData.categoryId = res.result.categoryId
						this.formData.salesWeek = res.result.salesWeek
						this.formData.tailWarn = res.result.tailWarn
						this.getChildrenPic()
						this.getGoodsSpec()
					} else {
						this.tui.toast(res.message)
					}
				}).catch((res) => {})
			}
			this.userType = uni.getStorageSync('type')
			
			this.getGategoryList()
			this.getSnList()
		},
		methods: {
			//查询商品规格
			getGoodsSpec() {
				this.tui.request("/app/productAttribute/selectAttributeAndValueByCategoryId", "POST", {
					categoryId: this.formData.attrId,
				}, false, true).then((res) => {
					if (res.code == 200) {
						if (typeof(res.result.attributeAndValue) == 'string') {
							let newObj = JSON.parse(res.result.attributeAndValue)
							let newArr = []
							for (let val in newObj) {
								let valueObj = {}
								valueObj.attributeName = val
								valueObj.productAttributeValueVoList = []
								for (var i = 0; i < newObj[val].length; i++) {
									valueObj.productAttributeValueVoList.push({
										value: newObj[val][i],
										checked: true
									})
								}
								this.multipleList.push(valueObj)
							}
						} else {
							that.tui.toast('失败了')
						}
					} else {
						this.tui.toast(res.message)
					}
				}).catch((res) => {})
			},
			//获取子图
			getChildrenPic() {
				this.tui.request("/app/productPicture/getProductPictureByProductId", "POST", {
					productId: this.dataList.id,
				}, false, true).then((res) => {
					if (res.code == 200) {
						for (let i = 0; i < res.result.length; i++) {
							let data = {
								productPicture: res.result[i].productPicture
							}
							this.formData.productPictures.push(data)
						}
					} else {
						this.tui.toast(res.message)
					}
				}).catch((res) => {})
			},
			supplierCollection() {
				this.tui.request('/app/debt/recharge', "post", {
					userId: this.dataList.supplierId,
					amountOwed: this.zPrice,
					dealingsTime:this.single,
					remark:this.rechargeRemark,
					userType: '1'
				}, false, false).then((res) => {
					if (res.code == 200) {
						this.tui.toast('回款成功')
						this.$refs.warnPopup.close()
					} else {
						this.tui.toast(res.message)
						this.$refs.warnPopup.close()
					}
				})
			},
			//获取客户分类
			getCustomerCategoryList(){
				this.tui.request("/app/customerCategory/getCustomerCategoryListByShopId", "POST", {
					shopId:uni.getStorageSync('shopId')
				}, false, true).then((res) => {
					if (res.code == 200) {
						if(res.result.length > 0){
							this.customerCategoryList = res.result
							for(let i=0;i<this.customerCategoryList.length;i++){
								let data = {
									categoryName:this.customerCategoryList[i].categoryName,
									categoryId:this.customerCategoryList[i].id,
									categoryPrice:""
								}
								this.customerCategoryList[i] = data
							}
						}else{
							this.customerCategoryList = null
						}
						
					} else {
						this.tui.toast(res.message)
					}
				}).catch((res) => {})
			},
			//进入页面，查询货号列表
			getSnList() {
				this.snList = []
				this.snName = ''
				this.tui.request("/app/productSn/getProductSnByShopId", "POST", {}, false, true).then((res1) => {
					if (res1.code == 200) {
						if (res1.result != '') {
							for (let i = 0; i < res1.result.length; i++) {
								let data = {
									text: '前缀:' + res1.result[i].snPre + ',位数:' + res1.result[i].snSufLength,
									value: res1.result[i].snPre
								}
								this.snList.push(data)
							}
							this.snName = res1.result[0].snPre
						}
					} else {
						this.tui.toast(res.message)
					}
					this.$forceUpdate()
				}).catch((res) => {})
			},
			//初始化获取分类列表
			getGategoryList() {
				this.tui.request("/app/productCategory/list", "get", {
					// shopId: 1,
				}, false, true).then((res) => {
					if (res.code == 200) {
						if (res.result.length > 0) {
							if(this.formData.categoryId != '' && this.formData.categoryId != undefined){
								for(let i=0;i<res.result.length;i++){
									if(res.result[i].id == this.formData.categoryId){
										this.typeItem = res.result[i]
									}
								}
							}else{
								this.typeItem = res.result[0]
								this.formData.categoryId = this.typeItem.id
							}
							
						}
					} else {
						this.tui.toast(res.message)
					}
				}).catch((res) => {})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			addSpec(e) {
				let data = {
					value: e,
					checked: false
				}
				this.defaultList[this.addIndex].list.push(data)
				this.$refs.addClose.val = ''
			},
			cancelCheck() {
				let data = this.cancelArr
				this.addMultipleType(data[0], data[1], data[2])
				this.$refs.carPopup.close()
			},
			//点击取消按钮
			dialogClose() {
				this.inputVal = '';
				this.specVal = '';
				this.$refs.inputClose.val = ''
				this.$refs.addClose.val = ''

			},
			// 新增图片
			async afterRead(event) {
				let lists = [].concat(event.file)
				if (this.formData.productPicture == undefined) this.formData.productPicture = [];
				let fileListLen = this[`fileList${event.name}`].length

				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})

				for (let i = 0; i < lists.length; i++) {
					
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			obtainPrice(e, type, num) {
				this.linshiPrice = e.target.value
				if (type == 'price') {
					this.formData.price = ''
				} else if (type == 'wholesalePrice') {
					this.formData.wholesalePrice = ''
				} else if (type == 'commission') {
					this.formData.commission = ''
				} else if (type == 'purchasePrice') {
					this.formData.purchasePrice = ''
				}
			},
			generateSn(value) {
				this.tui.request("/app/productSn/getNewProductSn", "POST", {
					snPre: value
				}, false, true).then((res1) => {
					if (res1.code == 200) {
						this.tui.request('/app/product/getByProductSn', "post", {
							productSn: res1.result
						}, false, true).then((res) => {
							if (res.code == 200) {
								if (res.result != '') {
									this.generateSn(value)
								} else {
									this.formData.productSn = res1.result
									this.$forceUpdate()
								}
							}
						})
						
					} else {
						this.tui.toast(res.message)
					}
				}).catch((res) => {})
			},
			updateCate(){
				this.$refs.customPopup.close()
				this.$forceUpdate()
			},
			checkPriceRule(ruleArr){
				if(ruleArr != null && ruleArr != ''){
					if(this.customerCategoryList != '' && this.customerCategoryList != null){
						let categorySon = []
						for(let i=0;i<this.customerCategoryList.length;i++){
							let isHae = false
							for(let j=0;j<ruleArr.length;j++){
								if(this.customerCategoryList[i].categoryId == ruleArr[j].categoryId){
									isHae = true
								}
							}
							if(isHae == false){
								ruleArr.push(this.customerCategoryList[i])
							}
						}
						
					}else{
						ruleArr = this.customerCategoryList
					}
				}else{
					ruleArr = this.customerCategoryList
				}
				
				return ruleArr
			},
			async startGetSn() {
				var that = this;
				if (this.prefixRule == '') {
					this.tui.toast('请输入货号生成前缀规则')
					return
				}
				if (this.numRule == '') {
					this.tui.toast('请输入货号生成位数规则')
					return
				}
				await this.tui.request("/app/productSn/addAndGetNewProductSn", "POST", {
					snPre: this.prefixRule,
					snSufLength: this.numRule
				}, false, false).then((res1) => {
					if (res1.code == 200) {
						if (res1.message == '货号前缀已存在！') { //被删除，调启用方法
							uni.showModal({
								title: "提示",
								content: "货号已经被删除，是否重新启用？",
								success: function(res) {
									if (res.confirm) {
										that.tui.request("/app/productSn/useAgainProductSn",
											"POST", {
												id: res1.result.id
											}, false, true).then((res2) => {
											if (res2.code == 200) {
												that.tui.toast('重新启用成功')
												setTimeout(res => {
													that.getSnList()
													that.generateSn(this
														.prefixRule)
												}, 1000)
											} else {
												that.tui.toast(res2.message)
											}
											that.$forceUpdate()
										}).catch((res) => {})
									}
								}
							})
						} else {
							this.formData.productSn = res1.result
						}

					} else {
						this.tui.toast(res.message)
					}
				}).catch((res) => {})
			},
			changeAddType(e) {
				if (e.show == false && this.multipleList == '') {
					this.multipleList = this.oldMultipleList
				}
			},
			async dialogInputConfirm(e) {
				let that = this;
				if (that.multipleList == '') {
					that.multipleList = [{
						attributeName: '颜色',
						productAttributeValueVoList: [{
							checked: true,
							value: "均色"
						}]
					}, {
						attributeName: '尺码',
						productAttributeValueVoList: [{
							checked: true,
							value: "均码"
						}]
					}]
				} else {
					
						if (that.multipleList[0].productAttributeValueVoList == '') {
							that.multipleList[0].productAttributeValueVoList = [{
								checked: true,
								value: "均色"
							}]
						} 
						if (that.multipleList[1].productAttributeValueVoList == '') {
							that.multipleList[1].productAttributeValueVoList = [{
								checked: true,
								value: "均码"
							}]
						}
						
				}
				
				that.$refs.addTypeDialog.close()
			},
			changessPrice(e, type) {
				if (type == 'price') {
					if (this.linshiPrice == e.target.value) {
						this.formData.price = e.target.value
					} else {
						if (e.target.value == '') {
							this.formData.price = this.linshiPrice
						} else {
							this.formData.price = e.target.value
						}

					}
				} else if (type == 'wholesalePrice') {
					if (this.linshiPrice == e.target.value) {
						this.formData.wholesalePrice = e.target.value
					} else {
						if (e.target.value == '') {
							this.formData.wholesalePrice = this.linshiPrice
						} else {
							this.formData.wholesalePrice = e.target.value
						}

					}
				} else if (type == 'commission') {
					if (this.linshiPrice == e.target.value) {
						this.formData.commission = e.target.value
					} else {
						if (e.target.value == '') {
							this.formData.commission = this.linshiPrice
						} else {
							this.formData.commission = e.target.value
						}

					}
				} else if (type == 'purchasePrice') {
					uni.showLoading({
						title: '加载中...',
						mask: true
					})
					if (this.linshiPrice == e.target.value) {
						this.formData.purchasePrice = e.target.value
					} else {
						if (e.target.value == '') {
							this.formData.purchasePrice = this.linshiPrice
						} else {
							this.formData.purchasePrice = e.target.value
							
							if (this.dataList.id == undefined) {
								uni.hideLoading()
								return
							}
							this.differencePurchasePrice = Number(this.oldPurchasePrice) - Number(e.target.value)
							this.tui.request("/app/purchase/getPurchaseListByProductId", "post", {
								productId: this.dataList.id,
								pageSize: 9999,
								pageNum: this.pageNum
							}, false, false).then((res) => {
								if (res.code == 200) {
									this.guigeNum = 0
									for (let i = 0; i < res.result.records.length; i++) {
										this.guigeNum += Number(res.result.records[i].purchase.totalAmount)
									}

									setTimeout(res => {
										this.zPrice = this.guigeNum * this.differencePurchasePrice
										if (this.zPrice != 0) {
											this.getMonth()
											this.$refs.warnPopup.open()
										}

										uni.hideLoading()
									}, 300)
								} else {
									this.tui.toast(res.message)
								}
							}).catch((res) => {})

						}
					}
					uni.hideLoading()
				}

			},
			getMonth() {
				this.single = Date.now()
			},
			toggle(index) {
				this.addIndex = index
				this.$refs.addDialog.open()
			},
			uploadFilePromise(url) {
				let that = this;
				return new Promise((resolve, reject) => {
					uni.compressImage({
						src: url,
						quality: 50,
						success: res => {

							let a = uni.uploadFile({
								url: that.tui.interfaceUrl() + '/upload/file',
								filePath: res.tempFilePath,
								name: 'file',
								header: {
									"content-type": "multipart/form-data",
									'accessToken': this.tui.getToken()
								},
								formData: {},
								success: (uploadFileRes) => {

									let path = JSON.parse(uploadFileRes.data)
									//后端返回的地址，存入图片地址
									this.formData.productPicture = path.result
								}
							});
						}
					})

				})
			},
			categoryChange(e) {
				this.categoryId = e
			},
			ejectRuleSelect() {
				
				if (this.dataList.id) {
					//颜色
					for (let m = 0; m < this.multipleList[0].productAttributeValueVoList.length; m++) {
						let isTrue = true
						for (let i = 0; i < this.defaultList[0].list.length; i++) {
							if (this.defaultList[0].list[i].value == this.multipleList[0].productAttributeValueVoList[m]
								.value) {
								isTrue = false
								this.defaultList[0].list[i].checked = true
							} else {
								if (this.defaultList[0].list.length == (i + 1) && isTrue == true) {
									this.multipleList[0].productAttributeValueVoList[m].checked = true
									this.defaultList[0].list.push(this.multipleList[0].productAttributeValueVoList[m])
								}
							}
						}
					}
					//尺码
					for (let m = 0; m < this.multipleList[1].productAttributeValueVoList.length; m++) {
						let isTrue = true
						for (let i = 0; i < this.defaultList[1].list.length; i++) {
							if (this.defaultList[1].list[i].value == this.multipleList[1].productAttributeValueVoList[m]
								.value) {
								isTrue = false
								this.defaultList[1].list[i].checked = true
							} else {
								if (this.defaultList[1].list.length == (i + 1) && isTrue == true) {
									this.multipleList[1].productAttributeValueVoList[m].checked = true
									this.defaultList[1].list.push(this.multipleList[1].productAttributeValueVoList[m])
								}
							}
						}
					}
				}
				this.$nextTick(() => {
					this.oldMultipleList = this.multipleList
					if (this.dataList == '') {
						this.multipleList = []
					}
					this.$refs.addTypeDialog.open('bottom')
				})
			},
			// 触发提交表单
			submit() {
				let that = this;
				let url = ''
				this.$refs.form.validate().then(res => {
					if (that.dataList.id) {
						url = '/app/product/edit'
						this.formData.id = that.dataList.id
						this.formData.stockCount = that.dataList.stockCount
						this.formData.supplierId = that.dataList.supplierId
						this.formData.supplierName = that.dataList.supplierName
						this.formData.delFlag = that.dataList.delFlag
					} else {
						url = '/app/product/save'
						if (this.userType == 1) {
							this.formData.purchasePrice = ''
						}
					}
					if (res.productPicture == '') {
						res.productPicture = ''
					}
					if(this.customerCategoryList != null){
						this.formData.customerCategoryRule = JSON.stringify(this.customerCategoryList)
					}
					this.formData.productCategoryVo = {
						productAttributeOfAddVos: this.multipleList.length > 0 ? this.multipleList : [{
							attributeName: '颜色',
							productAttributeValueVoList: [{
								checked: true,
								value: "均色"
							}]
						}, {
							attributeName: '尺码',
							productAttributeValueVoList: [{
								checked: true,
								value: "均码"
							}]
						}]
					}
					
					that.tui.request(url, "post", this.formData, false, false).then((res1) => {
						if (res1.code == 200) {
							if (that.dataList.id) { //编辑商品
								let pages = getCurrentPages();
								let prevPage = pages[pages.length - 3];
								let shopList = prevPage.$vm.shopList
								for (let i = 0; i < shopList.length; i++) {
									if (shopList[i].id == this.formData.id) {
										//修改了分类，就直接删除列表中的商品
										if (this.formData.categoryId != that.dataList.categoryId) {
											shopList.splice(i, 1);
										} else {
											shopList[i] = this.formData
										}
									}
								}
								uni.navigateBack({
									delta: 2
								})
							} else { //新增商品
								uni.navigateBack({
									delta: 1
								})
							}

						} else {
							uni.showToast({
								title: "添加失败",
								icon: 'none'
							})
						}
					})
				}).catch(err => {

				})
			},
			//跳转分类页
			goGategoryLow() {
				uni.navigateTo({
					url: '/package1/inventory/gategory'
				})
			},
			pictureAdd(id, huan) {
				let that = this
				uni.chooseMedia({
					count: 9,
					mediaType: ['image'],
					sourceType: ['album'],
					success(res) {
						uni.showLoading({
							title: '加载中...',
							mask: true
						})
						for (let i = 0; i < res.tempFiles.length; i++) {
							that.upLoadFile(res.tempFiles[i].tempFilePath)
						}

					}
				})
			},
			async upLoadFile(path) {
				let that = this;
				let hiver_token = uni.getStorageSync("hiver_token")
				await uni.uploadFile({
					url: that.tui.interfaceUrl() + '/upload/file',
					filePath: path,
					name: 'file',
					header: {
						"content-type": "multipart/form-data",
						'accessToken': hiver_token
					},
					formData: {},
					success: (uploadFileRes) => {
						let pathData = JSON.parse(uploadFileRes.data)
						let data = {
							productPicture: pathData.result
						}
						this.formData.productPictures.push(data)
						that.$forceUpdate()
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showToast({
							title: JSON.stringify(err),
							icon: 'none'
						})
					}
				});
				await setTimeout(res => {
					uni.hideLoading();
				}, 1000)
			},
			scanCodeAdd() {
				let that = this;
				uni.scanCode({
					scanType: ['barCode'],
					success: (res) => {
						that.tui.request('/app/product/getByBarcode', "post", {
							barcode: res.result
						}, false, true).then((res1) => {
							if (res1.code == 200) {
								if (res1.result != '') {
									that.tui.toast('当前条码已被商品：“' + res1.result[0].productName +
										'”占用，请重新输入')
								} else {
									that.formData.barcode = res.result;
								}
							}
						})
					},
					fail: (res) => {
						uni.showToast({
							title: '扫描失败',
							icon: 'none'
						})
					}
				})
			},
			isCheckPrec(item, item1, index) {
				if (item.list[index].checked == true) {
					this.getTypeToNum(item, item1, index)
				} else {
					this.addMultipleType(item, item1, index)
				}
			},
			//选择多个规格保存
			addMultipleType(item, item1, index) {
				item.list[index].checked = !item.list[index].checked
				var list = []
				if (this.multipleList == '' && item.list[index].checked == true) {
					if(item.name == '颜色'){
						var data = {
							attributeName: '尺码',
							productAttributeValueVoList: []
						}
						this.multipleList.push(data)
					}
					if(item.name == '尺码'){
						var data = {
							attributeName: '颜色',
							productAttributeValueVoList: []
						}
						this.multipleList.push(data)
					}
					list.push(item.list[index])
					var data = {
						attributeName: item.name,
						productAttributeValueVoList: list
					}
					this.multipleList.push(data)
				} else {
					
					if (this.multipleList[0].attributeName == item.name) { //同种分类
						if(this.multipleList[0].productAttributeValueVoList.length == 0){
							this.multipleList[0].productAttributeValueVoList.push(item.list[index])
						}else{
							for (var m = 0; m < this.multipleList[0].productAttributeValueVoList.length; m++) {
								if (this.multipleList[0].productAttributeValueVoList[m].value == item.list[index]
									.value) { //规格相同
									this.multipleList[0].productAttributeValueVoList.splice(m, 1)
									return;
								} else { //规格不同
									if (m + 1 == this.multipleList[0].productAttributeValueVoList.length) {
										this.multipleList[0].productAttributeValueVoList.push(item.list[index])
										return;
									}
								}
							}
						}
						
					}
					
					if (this.multipleList[1].attributeName == item.name) { //同种分类
						if(this.multipleList[1].productAttributeValueVoList.length == 0){
							this.multipleList[1].productAttributeValueVoList.push(item.list[index])
						}else{
							for (var m = 0; m < this.multipleList[1].productAttributeValueVoList.length; m++) {
								if (this.multipleList[1].productAttributeValueVoList[m].value == item.list[index]
									.value) { //规格相同
									this.multipleList[1].productAttributeValueVoList.splice(m, 1)
									return;
								} else { //规格不同
									if (m + 1 == this.multipleList[1].productAttributeValueVoList.length) {
										this.multipleList[1].productAttributeValueVoList.push(item.list[index])
										return;
									}
								}
							}
						}
						
					}
				}
			},
			//查询分类下有没有库存
			getTypeToNum(item, item1, index) {
				let that = this;
				this.tui.request("/app/stock/getlistByAttributeList", "post", {
					categoryId: this.formData.attrId,
					attributeList: item.list[index].value,
				}, false, true).then((res) => {
					if (res.code == 200) {
						if (res.result != '') { //有库存
							this.goodsList = res.result
							this.cancelArr = [item, item1, index]
							this.$refs.carPopup.open('bottom')
						} else {
							this.addMultipleType(item, item1, index)
						}
					}
				}).catch((res) => {})
			}
		}
	}
</script>

<style lang="scss">
	page,.page1{
		font-size: 28rpx;
	}
	.list-val {
		width: 486rpx;
		height: 70rpx;
		line-height: 70rpx;
	}

	.list-val input {
		width: 400rpx;
		height: 70rpx;
		display: inline-block;
		line-height: 70rpx;
		text-align: left !important;
		padding-left: 20rpx;
		float: left;
		font-size: 25rpx;
	}

	.formBox {
		margin: 20rpx;
		padding: 20rpx 26rpx;
		border-radius: 14rpx;
		box-shadow: 0px 1px 10px 2px #e2e2e2;
		background-color: #fff;

		.uni-data-select {
			width: 100%;
		}

		.attrs {
			text {
				display: inline-block;
				border: 1px solid $u-primary;
				padding: 14rpx 20rpx;
				margin: 20rpx;
				background-color: #fff;
				color: $u-primary;
			}

			.checkedText {
				background-color: $u-primary;
				color: #fff;
			}
		}
	}

	.upload-img {
		width: 160rpx;
		height: 160rpx;
	}

	.checked {
		background: #60F3FF !important;
		color: #fff !important;
		border-radius: 5px !important;
	}

	.content-list {
		height: 70rpx;
		border: 1px solid #eee;
	}

	.sn-content-list {
		width: 95%;
		margin: 0 auto;
		display: flex;
		height: 80rpx;
		border-bottom: 1px solid #eee;
	}

	.sn-content-list:last-child {
		border-bottom: none;
		height: auto;
	}

	.sn-list-name {
		width: 200rpx;
		text-align: left;
		line-height: 80rpx;
		height: 80rpx;
		padding-left: 20rpx;
	}

	.sn-list-val {
		width: 560rpx;
		height: 80rpx;
		line-height: 80rpx;
	}

	.sn-list-val input {
		width: 400rpx;
		height: 60rpx;
		display: inline-block;
		line-height: 60rpx;
		float: left;
		margin-top: 10rpx;
		padding-left: 20rpx;
		border-bottom: 1px solid #088FEB;
	}

	button {
		height: 90rpx;
		line-height: 90rpx;
		font-size: 36rpx;
		color: #fff;
		background: #5fd9ee;
		border-radius: 10px;
	}

	.content-input {
		width: auto;
		position: relative;
		overflow: hidden;
		flex: 1;
		line-height: 1;
		font-size: 28rpx;
		height: 70rpx;
		border: 2px solid #eee;
		padding-left: 20rpx;
		border-radius: 5px;
	}

	.add-popup-content {
		align-items: center;
		justify-content: center;
		padding: 30rpx;
		width: 100%;
		height: 1050rpx;
		background-color: #fff;
		overflow: scroll;
	}

	.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);
	}

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

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

	.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;
		border-radius: 5px;
	}

	.type-popup {
		width: 500rpx;
		height: auto;
		background: #fff;
		border-radius: 10px;
		position: relative;
		margin-bottom: 40rpx;
	}

	.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;
	}

	.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;
	}
	@media screen and (min-width: 760px) {
		.add-popup-content{
			height: 800px !important;
		}
		.vue-ref{
			padding-bottom:0 !important;
		}
	}
	
</style>