<template>
	<!-- 添加外卖商品 -->
	<view class="productpages">
		<!-- <view class="flexv ali-c" style="padding: 30upx 0 0 0;">
			<image class="mb-20 img"  @tap="saoma"  src="/static/icon/saoma.png"></image>
			<view>扫码入库</view>
		</view>	 -->
		
		<view class="noecellbox">
			<view class="title">产品名称</view>
			<view class="inputbox flex">
				<input class="input flex1"  type="text" v-model="form.product_name" value="" placeholder-class="plclass" placeholder="请填写产品名称"  />
				
			</view>
		</view>
		
		<view class="noecellbox">
			<view class="title">产品价格</view>
			<view class="inputbox flex">
				<input class="input flex1"  type="text" v-model="form.price" value="" placeholder-class="plclass" placeholder="请填写产品价格"  />
				
			</view>
		</view>
		
		
		<!-- 
			stock 商品库存
			purchase 进货价钱
			code 条形编码
			commission 返佣比例
			original_name 原始商品名 
		-->
		<!-- 2020-10-12 -->
		<view class="noecellbox">
			<view class="title">商品库存</view>
			<view class="inputbox flex">
				<input class="input flex1"  type="text" v-model="form.stock" value="" placeholder-class="plclass" placeholder="请填写产品价格"  />
				
			</view>
		</view>
		
		<!-- <view class="noecellbox">
			<view class="title">条形编码</view>
			<view class="inputbox flex">
				<input class="input flex1" type="text" :disabled='codeDisabled' @tap="onCodeFocus" :value="form.code" placeholder-class="plclass" placeholder="请填写产品价格"  />
			</view>
		</view> -->
		
		<view class="noecellbox">
			<view class="title">返佣比例</view>
			<view class="inputbox flex">
				<input class="input flex1"  type="text" v-model="form.commission" value="" placeholder-class="plclass" placeholder="请填写产品价格"  />
			</view>
		</view>
		
		<view class="noecellbox">
			<view class="title">进货价</view>
			<view class="inputbox flex">
				<input class="input flex1"  type="text" v-model="form.cost_price" value="" placeholder-class="plclass" placeholder="请填写产品价格"  />
			</view>
		</view>
		
		<view class="briefIntroduction">
			<view class="title">产品描述</view>
			<textarea class="textarea" v-model="form.desc" placeholder="请输入产品描述"/>
		</view>
		
		<view class="noecellbox">
			<view class="title">餐盒费</view>
			<view class="inputbox flex">
				<input class="input flex1"  type="text" v-model="form.lunch_box_fee" value="0" placeholder-class="plclass" placeholder="请填写产品餐盒费"  />
			</view>
		</view>
		
		<!-- 分类 -->
		<view class="Specificationsbox">
			<view class="splcsinfo flex ali-c jus-b">
				<view class="name">产品分类</view>
				<view class="ali-c">
					<text></text>
					<image class="imgs" src="/static/news/xiamassfd.png" mode=""></image>
				</view>
			</view>
			<view class="sort flex ali-c ">
				<view class="name">分类</view>
				<view class="inputs" @tap='pickerone'>
					<view class="" style="padding-left: 15upx;">{{cate_name}}</view>
				</view>
				<lb-picker ref="picker" :list="chindformList" :value='index' range-key="label" :props="myProps" @confirm='confirm'></lb-picker>
			</view>
		</view>


		<view class="addmigs">
			<view class="titlehe">缩略图</view>
			<robby-image-upload 
				fileKeyName="img" 
				:value="c_pics" 
				:server-url="uploadurl1+'/merchant.php/FileUpload/uploadify'" 
				:form-data="c_formData" 
				:server-url-delete-image="uploadurl1"
				 @delete="c_delImage" 
				 @add="c_onImg1" 
				 @move="onMove_c" 
				 :limit="1"
				 >
			 </robby-image-upload>
		</view>
		<!-- 多选列表  勾选的位置 -->
		<view class="checkboxs">
			<view class="title">属性:</view>
			
			<checkbox-group @change="checkboxChange">
				<view class="chindlost ali-c flex flex-wrap">
					<label class="lableitem flex ali-c" v-for="(item,index) in listchind" :key="index">
						<view>
							<checkbox color="#FF6900" :value="item.value" :checked="item.checked" />
						</view>
						<view class="titles">{{item.name}}</view>
					</label>
				</view>
				
				<view v-if="activity_type==1">
					<label class="lableitem flex ali-c" v-for="item of listissetkill" :key="index">
						<view>
							<checkbox color="#FF6900" :value="item.value" :checked="item.checked" />
						</view>
						<view class="titles">{{item.name}}</view>
					</label>
				</view>	
			</checkbox-group>
		</view>
		<view v-if="isshowsetkilltime && activity_type==1">
			<view class="noecellbox">
				<view class="title">秒杀价格</view>
				<view class="inputbox flex">
					<input class="input flex1"  type="text" v-model="form.activity_price" value="" placeholder-class="plclass" placeholder="请填写产品价格"  />
					
				</view>
			</view>
			
			<view class="noecellbox">
				<view class="title">限购数量</view>
				<view class="inputbox flex">
					<input class="input flex1"  type="number" v-model="form.limit_num" value="" placeholder-class="plclass" placeholder="请填写产品价格" @input="isInt" />
				</view>
			</view>
			
			<view class="noecellbox">
				<view class="title">秒杀库存</view>
				<view class="inputbox flex">
					<input class="input flex1"  type="number" v-model="form.store_count" value="" placeholder-class="plclass" placeholder="请填写产品价格" @input="isInt" />
					
				</view>
			</view>
		</view>
		<view class="checkboxs" v-if="isshowsetkilltime && activity_type==1">
			<view class="title">选择秒杀时段:</view>
			<picker-view :value="ischeckedtime" indicator-style="height: 50px;" style="width: 100%; height: 200px;"   @change="pickerChange">
			      <picker-view-column >
					<view v-for="(item,index) in seckilltimelist" :key="index"  style="line-height: 50px; text-align: center;">{{item.title}}</view>
				  </picker-view-column>
			</picker-view>
		</view>

		<view class="submitbtn active" @tap="getForm">提交</view>

	</view>
</template>

<script>
	import QSApp from '@/components/QS-inputs-split/js/app.js';
	import {addEditEleProduct,typeManager,eleProductInfo,findEleProduct} from "@/utils/api/api.js";
	import robbyImageUpload from '@/components/plan-image-upload/up_img';//上传资质图片组件
	import {getActivityList} from '@/utils/api/merchant.js';
	export default {
		components:{
			robbyImageUpload
		},
		data() {
			return {
				ischeckedtime:[0],//默认选中，
				myProps: {
				      label: 'cate_name',
				      value: 'cate_id',
				},
				name: 'product',
				result:[],
				form: {
					//cost_price 成本价 stock 商品库存purchase 进货价钱code 条形编码commission 返佣比例original_name 原始商品名 
					stock:'', //商品库存
					cost_price:'', //进货价钱
					code:'' ,//条形编码
					commission:'', //返佣比例
					commodity_data:[], //原始商品名
					checkbox: [],//断选
					product_id:"",//修改的时候的产品id
					product_name:"",//产品名称
					desc:"",//产品描述
					cate_id:"",//产品分类id
					photo:"",//产品图片
					lunch_box_fee:'',//餐盒费
					is_new:"",//是否新产品
					is_hot:"",//是否热卖产品
					action:"add",//	方式（添加：add，修改:edit）
					price:"",//价格
					activity_price:"",//秒杀价
					store_count:""	,// 秒杀库存
					is_seckill:"0",//0 1 是否秒杀产品
					activity_id:'0',//秒杀activity_id
					activity_list:'',//秒杀列表
					limit_num:"0" //秒杀限购数量 0 不限制 
				},
				isShow:false,
				chindformList:[],
				//上传图片相关的
				c_pics: [],
				c_photos: [],
				c_formData: {
					model:"shopphoto",
				},
				product_id:"",//修改产品时候用的修改id
				defaultinfo:{},//修改的时候的默认初始数据
				listchind:[
					{name:"是否新产品",value:"is_new",checked:false},
					{name:"是否热卖产品",value:"is_hot",checked:false},
				],
				isshowsetkilltime:false,//是否显示秒杀时间
				listissetkill:[{name:"是否秒杀产品",value:"is_seckill",checked:false}],
				activity_type:"",//判断是否可以秒杀
				seckilltimelist:[],//秒杀时段列表
				selchildList:[],
				cate_name:"",
				index:0,
				isScanCode:true,//是不是点击了扫码 
				codeDisabled:false//条形码是否能输入
			}
		},
		computed: {
			uploadurl1(){
				return this.$store.state.uploadurl1;
			},
			staticUrl(){
				return this.$store.state.staticUrl;
			}
		},
		onLoad(opdata) {
			this.c_formData.user_token = this.$store.state.token;
			this.activity_type = opdata.activity_type,
			this.isShow = this.activity_type==1?true:false
			if(opdata.id!=undefined){
				this.product_id = opdata.id;
				uni.setNavigationBarTitle({
					title:"修改外卖产品"
				})
			}else{
				uni.setNavigationBarTitle({
					title:"添加外卖产品"
				})
			}
			this.typeManagerfun();
			this.getActivityListfun(); //获取数据列表
			opdata.results={
	"data": {
		"code": 1,
		"msg": "数据返回成功！",
		"data": {
			"goodsName": "原木抽纸24包整箱面巾纸妇婴儿纸巾卫生纸家用餐巾纸便携装",
			"barcode": "6931950703724",
			"price": "30.00",
			"img": "https://gw.alicdn.com/bao/uploaded/i2/2550145431/TB2D7YydfQs8KJjSZFEXXc9RpXa_!!2550145431.jpg",
			"brand": "",
			"supplier": "",
			"standard": ""
		}
	},
	"statusCode": 200,
	"header": {
		"Content-Type": "application/json;charset=UTF-8",
		"Vary": "Accept-Encoding",
		"Connection": "keep-alive",
		"Server": "nginx",
		"Date": "Thu, 15 Oct 2020 07:30:36 GMT",
		"Transfer-Encoding": "chunked"
	},
	"errMsg": "request:ok",
	"cookies": [],
	"isScanCode": false,
	"code": "6931950703724"
}
			if(opdata.results){
				// let results=JSON.parse(opdata.results);//扫描得到得商品信息
				// this.findEleProduct(results);
				this.findEleProduct(opdata.results);
			}
		},
		methods: {
			onCodeFocus(){  //条形码输入框聚焦事件
				if(this.codeDisabled){
					this.$api.msg('不能修改条形编码');
				}
			},
			async findEleProduct(results){
				uni.showLoading({title:'请稍后'});
				let res=await findEleProduct({code:results.code});
				uni.hideLoading();
				if(res.code==-1){ //没有就录入
					this.form.code=results.code;//条形码
					this.form.original_name = results.data.data.goodsName;
					this.form.product_name = results.data.data.goodsName;
					// this.c_pics = [{code:0,data:results.data.data.img,msg:"success",time:"1587101904"}];
					this.form.commodity_data[0] = results.data.data;
					this.form.price=results.data.data.price;
					this.isScanCode=results.isScanCode;
					this.codeDisabled=false;//条形码输入框是否禁止输入
				}else if(res.code==0){ //有就把信息展示
					this.form.stock=res.data.stock, //商品库存
					this.form.cost_price=res.data.cost_price, //进货价钱
					this.form.code=res.data.code ,//条形编码
					this.form.commission=res.data.commission, //返佣比例
					this.form.commodity_data=res.data.commodity_data, //原始商品名
					this.form.product_id=res.data.product_id,//修改的时候的产品id
					this.form.product_name=res.data.product_name,//产品名称
					this.form.desc=res.data.desc,//产品描述
					this.form.cate_id=res.data.cate_id,//产品分类id
					this.form.lunch_box_fee=res.data.lunch_box_fee,//餐盒费
					this.form.is_new=res.data.is_new,//是否新产品
					this.form.is_hot=res.data.is_hot,//是否热卖产品
					this.form.price=res.data.price,//价格
					this.form.activity_price=res.data.activity_price,//秒杀价
					this.form.store_count=res.data.store_count	,// 秒杀库存
					this.form.activity_id=res.data.activity_id,//秒杀activity_id
					this.form.activity_list=res.data.activity_list,//秒杀列表
					this.form.limit_num=res.data.limit_num//秒杀限购数量 0 不限制 
					this.isScanCode=false;
					this.codeDisabled=true;//条形码输入框是否禁止输入
					this.c_pics=[{code:0,data:res.data.photo,msg:"success",time:"1587101904"}];
					for(var i = 0; i < this.chindformList.length; i++) {
						 if(this.chindformList[i].cate_id==res.data.cate_id){
						 	this.cate_name=this.chindformList[i].cate_name;
							break;
						 }
					}
				}
			},
			saoma(e){
				uni.scanCode({
				    success:(res) => {
						var code =  res.result; 
						uni.request({
							url: 'https://www.mxnzp.com/api/barcode/goods/details', //仅为示例，并非真实接口地址。
							data: {
							    barcode:code,
								needImg: true,
								app_id:'qkkbptetyohxoiod',
								app_secret:'Wkp2a3NiNjAzTFJLRmwrZXN3QW1jZz09'
							},
							method:'GET',
							success: (results) => {
								if(results.data.code==0){
									this.$api.msg(results.data.msg);
								}else if(results.data.code==1){
									results.code=code;
									results.isScanCode=false;
									this.findEleProduct(results);
								}
							
							 }
							 
						})
				    },
					fail:(err)=>{
						console.log(err)
					}
				});
			},
			confirm(e){
				console.log(e)
				this.cate_name = e.item.cate_name;
				this.form.cate_id =e.item.cate_id;
				this.index = e.index;
			},
			pickerone(){
				this.$refs.picker.show() // 显示
			},
			
			isInt(e){
				if(!this.isInteger(e.detail.value)){
					this.$api.msg("秒杀库存或限购数量不能为小数");
				}
			},
			//分类选中
			bindPickerChange(e){
				console.info(e.detail.value);
				this.cate_name = this.chindformList[e.detail.value].cate_name;
				this.form.cate_id = this.chindformList[e.detail.value].cate_id;
				this.index = e.detail.value;
				
			},
			//标记选中属性列表
			checkboxChange(e){
				this.selchildList = e.detail.value;
				this.isshowsetkilltime = e.detail.value.indexOf('is_seckill') ==-1? false:true;
				if(this.isshowsetkilltime){
					this.form.activity_list ="49,"
				}else{
					this.form.activity_list =""
				}
				this.selchildList = e.detail.value;
			},
			//修改的时候赋初始值
		 	async eleProductInfofun(){
				let res = await eleProductInfo({
					product_id:this.product_id,
				});
				console.log(res,'法大师傅大师傅大师傅士大夫大师傅');
				if(res.code==0){
					this.defaultinfo = res.data;
					this.form.product_name = res.data.product_name;//名称
					this.form.desc = res.data.desc;//描述
					this.form.lunch_box_fee = res.data.lunch_box_fee/100;//餐盒费
					this.form.price = res.data.price;//价格
					this.form.photo = res.data.photo;//图片
					this.form.activity_price = res.data.activity_price //秒杀价
					this.form.store_count = res.data.store_count //秒杀库存
					this.form.cate_id = res.data.cate_id;//分类id
					this.form.activity_id = res.data.activity_id//秒杀时段id
					this.form.limit_num = res.data.limit_num
					//下拉分类 初始值
					this.chindformList.forEach((item,index)=>{
						if(item.cate_id==res.data.cate_id){
							this.index =index;
							this.cate_name = item.cate_name;
						}
					});
					
					//属性选默认值
					if(res.data.is_new==1){
						this.selchildList.push("is_new");
						this.listchind[0].checked = true;
					}else{
						this.listchind[0].checked = false;
					}
					if(res.data.is_hot==1){
						this.selchildList.push("is_hot");
						this.listchind[1].checked = true;
					}else{
						this.listchind[1].checked = false;
					}
					
					if(res.data.activity_list == '49,'){
						this.listissetkill[0].checked = true
						this.isshowsetkilltime = true
						this.form.activity_list = res.data.activity_list
						
					let indexlist =	this.seckilltimelist.filter((item,index)=>{
						if(item.activity_id==this.form.activity_id){
							return item
						}
					})
					let temparr = indexlist[0] 
					let index = this.seckilltimelist.indexOf(temparr)
						this.ischeckedtime[0] = index
					}
					//处理上传的图片默认值
					this.c_pics = [{code:0,data:res.data.photo,msg:"success",time:"1587101904"}];
					this.form.photo = res.data.photo;
					this.c_photos = ["res.data.photo"]
					
				}else{
					this.$api.msg(res.msg);
				}
			},
			getForm() {
				if(this.isshowsetkilltime && (this.form.activity_price==""||this.form.store_count==""||this.form.limit_num=="") ){
					this.$api.msg("请填写完整秒杀数据");
					 return false;
				}
				if(!this.isInteger(this.form.limit_num) || !this.isInteger(this.form.store_count)){
					this.$api.msg("秒杀库存或限购数量不能为小数");
					return false;
				}
				
				let can = {
					stock:this.form.stock, //商品库存
					cost_price:this.form.cost_price, //进货价钱
					code:this.form.code,//条形编码
					commission:this.form.commission, //返佣比例
					commodity_data:this.form.commodity_data, //产品数据
					product_name:this.form.product_name,//产品名称
					desc:this.form.desc,//产品描述
					lunch_box_fee:this.form.lunch_box_fee,//餐盒费
					action:this.form.action,//方式（添加：add，修改:edit）
					price:this.form.price,//价格
					cate_id:this.form.cate_id,
					photo:this.c_pics[0].data, //图片
					activity_price:this.form.activity_price,//秒杀价
					store_count:this.form.store_count ,//秒杀库存
					activity_id:this.form.activity_id,//秒杀时间id
					activity_list:this.form.activity_list,
					limit_num:this.form.limit_num ,//秒杀限购数量
					
				};
				if(this.selchildList.includes("is_new")){
					can.is_new = 1;
				}else{
					can.is_new = 0;
				}
				if(this.selchildList.includes("is_hot")){
					can.is_hot = 1;
				}else{
					can.is_hot = 0;
				}
				if(this.product_id!=""){
					can.action = "edit";
					can.product_id = this.product_id;
				}
				//添加修改外卖产品
				console.log('这里了')
				addEditEleProduct(can).then((res1)=>{
					console.log(res1)
					if(res1.code==0){
						this.$api.msg(res1.msg);
						setTimeout(()=>{
							uni.navigateBack({});
						},1000)
					}else{
						this.$api.msg(res1.msg);
					}
				})
			},
			
			pickerChange(e){
				console.log(e)
				this.form.activity_id  = this.seckilltimelist[e.detail.value[0]].activity_id;
			},
			// 秒杀列表
			async getActivityListfun(){
				let res = await getActivityList({
					audit:2,
				})
				if(res.code==0){
					this.seckilltimelist =  res.data.head
				}
			},
			//获取外卖分类列表 用于下拉选择
			async typeManagerfun(){
				let res = await typeManager({});
				if(res.code==0){
					this.chindformList = res.data;
					if(this.product_id!=undefined && this.product_id!=""){
						this.eleProductInfofun();
					}
				}else{
					this.$api.msg(res.msg);
				}
			},
			// 判断是否为整数
			isInteger(obj){
				return  obj%1 === 0;
			},
			//图片上传
			c_delImage(e) {
				console.log(e,'反对发射点')
				let arr = []
				for (let k in e.allImages) {
					arr[k] = e.allImages[k].id
				}
				this.c_photos = arr
			},
			c_onImg1(e) {
				this.isScanCode=true;
				this.c_pics = e.allImages
				const obj = e.allImages
				for (let index in obj) {
					let id = obj[index].data
					this.c_photos[index] = id
				}
			},
			onMove_c(e) {
				this.c_pics = e;
				console.log(e,'反对法地方')
				const obj = e
				for (let index in obj) {
					let id = obj[index].data
					this.c_photos[index] = id
				} 
			}
		},
	}
</script>


<style lang="scss">
	@import  "../edit/product/addProjuct.scss";
	page{
		background: #fff;
	}
	.productpages{
		background: #FFFFFF;
	}
	
	.submitbtn{
		margin: 60upx 24upx;
		height:88upx;
		line-height: 88upx;
		text-align: center;
		background:rgba(254,197,156,1);
		border-radius:6upx;
		font-size:32upx;
		color:rgba(255,255,255,1);
	}
	
	.submitbtn.active{
		background: #FF6900;
	}
	.briefIntroduction,.Specificationsbox,.addmigs,.checkboxs{
		border-bottom:1px solid #F5F2F2;
		border-radius: 0 !important;
	}
	.img{
		width: 100upx;height: 100upx;
	}
</style>