const app = getApp();
Page({
  data: {

  },
  onLoad(){
    this.getList()
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  delSelect(){
    var list = this.data.list
    var checkedList = list.filter(item => item.checked == true)
    list = list.filter(item => {
      let arrlist = checkedList.map(v => v.id)
      return !arrlist.includes(item.id)
    })
    this.setData({
      list
    })
  },
  delItem(e){
    var id = e.currentTarget.dataset.id
    var list = this.data.list
    for(var i in list){
      if(list[i].id == id){
        list.splice(i, 1)
        break
      }
    }
    this.setData({
      list
    })
  },
  getList(){
    var list = [
      {id:1,nickName:"张三",phoneNumber:"12345678901"},
      {id:2,nickName:"张三",phoneNumber:"12345678901"},
      {id:3,nickName:"张三",phoneNumber:"12345678901"},
      {id:4,nickName:"张三",phoneNumber:"12345678901"},
      {id:5,nickName:"张三",phoneNumber:"12345678901"},
      {id:6,nickName:"张三",phoneNumber:"12345678901"},
      {id:11,nickName:"张三",phoneNumber:"12345678901"},
      {id:12,nickName:"张三",phoneNumber:"12345678901"},
      {id:13,nickName:"张三",phoneNumber:"12345678901"},
      {id:14,nickName:"张三",phoneNumber:"12345678901"},
      {id:15,nickName:"张三",phoneNumber:"12345678901"},
      {id:16,nickName:"张三",phoneNumber:"12345678901"},
    ]
    this.setData({
      list
    })
  },
  setAll(){
    var list = this.data.list
    var checkedList = list.filter(item => item.checked == true)
    if(list.length == checkedList.length){
      for(var i in list){
        list[i].checked = false
      }
      this.setData({
        list,
        selectAll:false
      })
    }else{
      for(var i in list){
        list[i].checked = true
      }
      this.setData({
        list,
        selectAll:true
      })
    }
  },
  setChecked(e){
    var list = this.data.list
    for(var i in list){
      if(list[i].id == e.currentTarget.dataset.id){
        list[i].checked = !list[i].checked
      }
    }
    var checkedList = list.filter(item => item.checked == true)
    if(list.length == checkedList.length){
      this.setData({
        list,
        selectAll:true
      })
    }else{
      this.setData({
        list,
        selectAll:false
      })
    }
  },
  closeAction(){
    this.setData({
      action:false
    })
  },
  openAction(){
    this.setData({
      action:true
    })
  },
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },
  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },
  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection =='left'){
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
})