const app = getApp()
const util = require("../../utils/jmsUtil.js");
const api = require('../../api/jms.js');

import moment from 'moment'



Page({
  data: {
		bookno:'',
		showPicker:false,
		currentItem:'',
		supplyList:[{itemName:'',number:1,price:0,difTotalPrice:0}],
		columns:[],
		typeList:[],
		totalAmount:0,
		diffId:0,

  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		
		if(options.bookno){
			this.setData({
				bookno:options.bookno
			});
			this.getSupplyTypeList();
		}
		if(options.id){
			this.setData({
				diffId:options.id
			});
			this.getDiffOrderDetail();
		}
	
     
	},
	getDiffOrderDetail(){
		wx.showLoading({
			title: '加载中...',
		});
		util.request(api.getDiffDetail, {
			diffOrderId:this.data.diffId
	
		}, 'GET').then(res => {
			wx.hideLoading();
			if (res.errno === 0) {
				this.setData({
					orderInfo:res.data,
					supplyList:res.data.itemList,
					totalAmount:res.data.difTotalPrice,
					bookno:res.data.bookDetailId
				});
				this.getSupplyTypeList();
			}
		}).catch(err => {
			wx.hideLoading();
		});
	},
 getSupplyTypeList(){
	wx.showLoading({
		title: '加载中...',
	});
	util.request(api.getDiffTypeList, {
		bookDetailId:this.data.bookno

	}, 'GET').then(res => {
		wx.hideLoading();
		if (res.errno === 0) {
			let list=res.data;
			list=list.map(item=>{return item.itemName});
			this.setData({
				typeList:res.data,
				columns:list
			});
		}
	}).catch(err => {
		wx.hideLoading();
	});
 },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

	},
	minusNum(e){
		 let index=e.currentTarget.dataset.index;
		 let num=e.currentTarget.dataset.num;
		 let price=e.currentTarget.dataset.price;
		 if(num<=1){
			wx.showToast({
        title: '数量不能再少了',
        icon: 'none',
        duration: 3000
			});
			return false;
		 }else{
			let item1='supplyList['+index+']'+'.number';
			let item2='supplyList['+index+']'+'.difTotalPrice';
			this.setData({
				[item1]:num-1,
				[item2]:Number((num-1)*price).toFixed(2)
			});
			
		 }

		 let total=0;
		 this.data.supplyList.forEach(item=>{
        total+=Number(item.difTotalPrice);
		 });
		 total=Number(total).toFixed(2);
		 this.setData({
			 totalAmount:total
		 });

	},
	addNum(e){
		let index=e.currentTarget.dataset.index;
		let num=e.currentTarget.dataset.num;
	   let price=e.currentTarget.dataset.price;
		 let item1='supplyList['+index+']'+'.number';
		 let item2='supplyList['+index+']'+'.difTotalPrice';
		 this.setData({
			 [item1]:num+1,
			 [item2]:Number((num+1)*price).toFixed(2)
		 });
	

		 let total=0;
		 this.data.supplyList.forEach(item=>{
				total+=Number(item.difTotalPrice);
			
		 });
		 total=total.toFixed(2);
		 this.setData({
			 totalAmount:total
		 });
		
 },
	delItem(e){
		let index=e.currentTarget.dataset.index;
		let supplyList=this.data.supplyList;
		supplyList.splice(index, 1);
    this.setData({
      supplyList: supplyList
    });
	},
	addItem(){
		this.setData({
			supplyList: this.data.supplyList.concat({itemName:'',number:1,price:0,difTotalPrice:0}),
		})
	},
	pickerCancel(){
		this.setData({
			showPicker:false
		});
	},
	showTypePicker(e){
		let index=e.currentTarget.dataset.index;
		this.setData({
			currentItem:index,
			showPicker:true
		})
	},
	pickerConfirm(e){
	let value=e.detail.value;
		let index=e.detail.index;
		let item='supplyList['+this.data.currentItem+']';
		let item1='supplyList['+this.data.currentItem+']'+'.itemName';
		let item2='supplyList['+this.data.currentItem+']'+'.price';
		let item3='supplyList['+this.data.currentItem+']'+'.difTotalPrice';
		
		this.setData({
			// [item]:{itemName:this.data.typeList[index].itemName,
			// },
			[item1]:value,
			[item2]:this.data.typeList[index].price,
			[item3]:Number((this.data.typeList[index].price)*(this.data.supplyList[this.data.currentItem].number)).toFixed(2),
			showPicker:false,
		});
		let total=0;
		this.data.supplyList.forEach(item=>{
			 total+=Number(item.difTotalPrice);
			 console.log(total);
		});
		total=total.toFixed(2);
		this.setData({
			totalAmount:total
		});
	},
	createOrder(){
		let isValid=this.data.supplyList.every(item=>{return item.itemName!=''});
		if(isValid){
			wx.showLoading({
				title: '加载中...',
			});
			util.request(api.createDiffOrder, {
				bookDetailId:this.data.bookno,
				diffId:this.data.diffId,
				diffList:this.data.supplyList
		
			}, 'POST').then(res => {
				wx.hideLoading();
				if (res.errno === 0) {
					app.globalData.diffId=res.data.diffOrder.id;
					app.globalData.diffOrder=res.data.diffOrder;
					app.globalData.diffItem=res.data.diffItem;
					wx.navigateTo({
						url: '/pages/supplyOrder/supplyOrder',
					});
				}else{
					wx.showToast({
						title: res.errmsg,
						icon: 'none',
					});
				}
			}).catch(err => {
				wx.hideLoading();
			});
		}else{
			wx.showToast({
				title: '补差项内容不能为空',
				icon:'none'
			})
		}
  
	},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
		this.setData({
			diffId:app.globalData.diffId
		});
  },
 
  onHide: function () {

  },

  onUnload: function () {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})