const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists:[{type:'',maxCount:'',maxTemp:'',minTemp:'',name:''}],
    seleteType:[{type:2,name:'报警'},{type:'1',name:'预警'}],
    name:''
  },
  /**
   * name,
    childList:[{
      maxCount:policFrequency,
      maxTemp:max,
      minTemp:min,
      type:2
    },{
      maxCount:earlyFrequency,
      maxTemp:earlyMax,
      minTemp:earlyMin,
      type:1
    }]
   * 
   */
  setName(e){
    this.setData({name:e.detail.value})
  },
  submit(){
    var that = this;
    var lists = that.data.lists;
    var name = that.data.name;
    this.isList(lists).then(res=>{
      app.requestP({
        url:'/tempRule/add',
        method:'post',
        contentType:'application/json',
        data:{
          name,
          childList:lists
        }
      }).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);
    })
  },
  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 = /[0-9]$/;
    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,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})