const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    warningInfo:{},
    lists:[],
    seleteType:[{type:2,name:'报警'},{type:1,name:'预警'}],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {groupId,isEdit} = options;
    this.setData({isEdit,groupId})
    this.warningInfo(groupId)
  },
  warningInfo(groupId){
    var that = this;
    var seleteType = that.data.seleteType;
    app.requestP({
      url:'/tempRuleGroup/info',
      methods:'post',
      data:{
        groupId
      }
    }).then(res=>{
      that.setData({
        warningInfo:res.data.data,
        name:res.data.data.name,
        warningList:res.data.data.childList,
        lists:res.data.data.childList.map((item,index)=>{
          return {...item , name: item.type == 1 ?'预警': item.type == 2 ?'报警':'--'}
        })
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg)
    })
  },
  getValue(e){
    var that = this;
    //下标
    var index = e.currentTarget.dataset.index;
    //数据
    var value = e.detail.value;
    //类型
    var type = e.currentTarget.dataset.type;
    value = value.replace(/[^\d\.-]/g,'')
    that.setValue(index,value,type);
  },
  setValue(index,value,type){
    var that = this;
    //动态设置属性值
    var type = "lists["+index+"]."+type;
    that.setData({
      [type] : value
    })
  },
  addList: function(){
    var that = this;
    var lists = this.data.lists;
    this.isList(lists).then(res=>{
      var newData={type:'',maxCount:'',maxTemp:'',minTemp:''};
      lists.push(newData);
      that.setData({lists});
    }).catch(err=>{
      app.showToptip(that, "error",err.text);
    })
  },
  isList(lists){
    let flag;
    var reg = /^(\-|\+)?\d+(\.\d+)?$/;
    var text;
    return new Promise((resolve,reject)=>{
      for(var i =0;i<lists.length;i++){
        if(String(lists[i].type).length==0 || String(lists[i].maxCount).length==0 || String(lists[i].maxTemp).length==0 || String(lists[i].minTemp).length==0){
          reject({status:false,text:'温度不能为空'})
          break;
        }
        if(lists[i].maxTemp < lists[i].minTemp){
          reject({status:false,text:'最高温度不能小于最低温度'})
          break;
        }
        resolve({status:true,text:'输入正确'})
      }
    })
  },
  setType(e){
    var that = this;
    //下标
    var index = e.currentTarget.dataset.index;
    //数据
    var value = e.detail.value;
    var type = "lists["+index+"].type";
    var name = "lists["+index+"].name";
    var seleteType = this.data.seleteType;
    this.setData({
      [type] : seleteType[value].type,
      [name] : seleteType[value].name,
    })
  },
   // 手指触摸动作开始
   touchStart: function (e) {
    let that = this;
    //开始触摸时 重置所有删除
    that.data.lists.forEach(function (v, i) {
      if (v.isTouchMove) v.isTouchMove = false; // 只操作为true的
    })
    // 记录手指触摸开始坐标
    that.setData({
      startX: e.changedTouches[0].clientX, // 开始X坐标
      startY: e.changedTouches[0].clientY, // 开始Y坐标
      lists: that.data.lists
    })
  },
  // 手指触摸后移动
  touchMove: function (e) {
    let that = this,
      index = e.currentTarget.dataset.index, // 当前下标
      startX = that.data.startX, // 开始X坐标
      startY = that.data.startY, // 开始Y坐标
      touchMoveX = e.changedTouches[0].clientX, // 滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY, // 滑动变化坐标
      // 获取滑动角度
      angle = that.angle({
        X: startX,
        Y: startY
      }, {
        X: touchMoveX,
        Y: touchMoveY
      });
    // 判断滑动角度
    that.data.lists.forEach(function (v, i) {
      v.isTouchMove = false
      // 滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        // 右滑
        if (touchMoveX > startX)
          v.isTouchMove = false
        // 左滑
        else
          v.isTouchMove = true
      }
    })
    // 更新数据
    that.setData({
      lists: that.data.lists
    })
  },
  // 计算滑动角度
  angle: function (start, end) {
    let that = this,
      _X = end.X - start.X,
      _Y = end.Y - start.Y;
    // 返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  submit(){
    var that = this;
    var name = this.data.name;
    var lists = this.data.lists;
    var warningInfo = this.data.warningInfo;
    this.isList(lists).then(res=>{
      if(!name){
        app.showToptip(that, "error",'名称不能为空');
        return ;
      }
      app.requestP({
        url:'/tempRule/edit',
        method:'post',
        contentType:'application/json',
        data:{
          name,
          childList:lists,
          groupId:warningInfo.groupId
        }
      }).then(res=>{
        wx.navigateBack({
          delta:1
        })
        app.showToptip(that,"success",res.data.msg);
      }).catch(err=>{
        app.showToptip(that,"error",err.data.msg);
      })
    }).catch(err=>{
      app.showToptip(that, "error",err.text);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  setName(e){
    this.setData({name:e.detail.value})
  },
  delList(e){
    var index = e.currentTarget.dataset.index;
    var lists = this.data.lists;
    var arr = lists;
    arr.splice(index,1);
    this.setData({
      lists:arr
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

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

  },

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

  },

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

  }
})