// component/scrollDel/scrollDel.js
var app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo

Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		cartList: {
			type: Array,
			value: []
		}
  },
  attached () {
	
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		delBtnWidth: 132,
    cartList: [{ content: "1", right: 0 }, { content: "2", right: 0 }],
    isScroll:true,
		windowHeight: 0,
		checkedColor: 'rgb(203, 60, 60)',
	},
	ready() {
		var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        });
      }
    });
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		checkPro(e) {
			// index
			var index = e.currentTarget.dataset.index
			wxService.nextTick(() => {
        this.triggerEvent('checkProChange', index)
      })
		},
		minusNum(e){
			// index
			var index = e.currentTarget.dataset.index
			wxService.nextTick(() => {
        this.triggerEvent('minusNumChange', index)
      })
		},
		addNum(e){
			// index
			var index = e.currentTarget.dataset.index
			wxService.nextTick(() => {
        this.triggerEvent('addNumChange', index)
      })
		},
		blurNum(e){
			// value index
			var index = e.currentTarget.dataset.index
			var value = e.detail.value
			let params = {
				index,value
			}
			wxService.nextTick(() => {
        this.triggerEvent('blurNumChange', params)
      })
		},
		inputNum(e){
			// value index
			var index = e.currentTarget.dataset.index
			var value = e.detail.value
			let params = {
				index,value
			}
			wxService.nextTick(() => {
        this.triggerEvent('inputNumChange', params)
      })
		},
		delItem(e){
			// skuId index
			var params = e.currentTarget.dataset
			wxService.nextTick(() => {
        this.triggerEvent('delItemChange', params)
      })
		},
		drawStart: function (e) {
			var touch = e.touches[0]
	
			for(var index in this.data.cartList) {
				var item = this.data.cartList[index]
				item.right = 0
			}
			this.setData({
				cartList: this.data.cartList,
				startX: touch.clientX,
			})
	
		},
		drawMove: function (e) {
			var touch = e.touches[0]
			var item = this.data.cartList[e.currentTarget.dataset.index]
			var disX = this.data.startX - touch.clientX
			
			if (disX >= 20) {
				if (disX > this.data.delBtnWidth) {
					disX = this.data.delBtnWidth
				}
				item.right = disX
				this.setData({
					isScroll: false,
					cartList: this.data.cartList
				})
			} else {
				item.right = 0
				this.setData({
					isScroll: true,
					cartList: this.data.cartList
				})
			}
		},  
		drawEnd: function (e) {
			var item = this.data.cartList[e.currentTarget.dataset.index]
			if (item.right >= this.data.delBtnWidth/2) {
				item.right = this.data.delBtnWidth
				this.setData({
					isScroll: true,
					cartList: this.data.cartList,
				})
			} else {
				item.right = 0
				this.setData({
					isScroll: true,
					cartList: this.data.cartList,
				})
			}
		}
  }
})
