var util = require('../../../utils/util.js');
var mall = require('../../../api/mall.js'); 
var app = getApp();
Page({
  data: {
		index:'',
		columns:[],
		skus:[],
		showPicker:false,
	 id:'',
	 toolkitType:'',//1上线礼包 2晋升礼包
	 detailList:[
		
	 ]
  },
  onLoad: function(options) {
		// 页面初始化 options为页面跳转所带来的参数
		if(options){
			this.setData({
				id:options.id
			})
		}
   this.getOrderInfo();
  },
  getOrderInfo() {
    
    util.request(mall.ToolkitToComplete, {
      toolId: 'autoTool_'+this.data.id,
    }).then(res=>{
      if (res.errno === 0) {
				console.log(res.data);
				let list=res.data.detailList;
				 list.forEach(item=>{
					//  if(item.needConfirm==0){

					//  }else if(item.needConfirm==1){
						 
					//  }
					 let pickerProps=item.canChoiceProps.split('|');
					 let pickerPrices=item.canChoicePrice.split('|');
					 let pickerSkus=item.canChoiceSkus.split(',');
					 item.pickerProps=pickerProps;
					 item.pickerSkus=pickerSkus;
					 item.pickerPrices=pickerPrices;
					 item.finalChoice='';
					 item.finalChoiceName='待确认';
					 item.pickerPrice='';
					//  item.finalChoice=pickerSkus[0];
					//  item.finalChoiceName=pickerProps[0];
				 });
        this.setData({
					detailList:list,
					toolkitType:res.data.toolkitType,
        });
      }
    });
	},
	showSpec(e){
		let index=e.currentTarget.dataset.index;
		let columns=e.currentTarget.dataset.columns;
		let skus=e.currentTarget.dataset.skus;
   this.setData({
		 showPicker:true,
		 index:index,
		 columns:columns,
		 skus:skus
	 });
	},
	pickerCancel(){
		this.setData({
			showPicker:false
		});
	},
  pickerConfirm(e){
		let value=e.detail.value;
		let index=e.detail.index;
		let item1='detailList['+this.data.index+']'+'.finalChoiceName';
		let item2='detailList['+this.data.index+']'+'.finalChoice';
		let item3='detailList['+this.data.index+']'+'.pickerPrice';
		this.setData({
			[item1]:value,
		});
		this.setData({
			[item2]:this.data.detailList[this.data.index].pickerSkus[index],
			[item3]:this.data.detailList[this.data.index].pickerPrices[index],
			showPicker:false
		});
		
	},
	confirmSpec(){
     let isUnSelected=this.data.detailList.some(item=>{
			 return item.finalChoice=='';
		 });
		 if(isUnSelected){
			 wx.showToast({
				 title: '请选择商品规格',
				 icon:'none',
			 });
		 }else{ 
			util.request(mall.ToolkitReceive, {
				agree:true,
				detail:this.data.detailList,
				allDefault:false,
				toolkitId: this.data.id,
			},'POST').then(res=>{
				if (res.errno === 0) {
					app.globalData.specRefresh=true;
					wx.navigateBack({
						delta: 1,
					});
				}else{
					util.showErrorToast(res.errmsg);
				}
			});

		 }
	},
 
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  }
})