// pages/waterabnormal/settings/settings.js
const app = getApp();
const warningTypes = [{
  value: 3,
  label:'单日用水量少于0.01吨'
},{
  value:4,
  label:'单日用水量超过2吨'
}];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    error: '',
    warningTypes,
    formData: {
      warningType: '',
    },
    rules: [],
    selectShow: false
  },
  selectHide() {
    this.setData({
      selectShow: false
    })
  },
  formChange(e) {
    const {
      file
    } = e.currentTarget.dataset;
    let label = e.detail.label || '';
    this.setData({
      [`formData.${file}`]: e.detail.value,
      [`formData.${file}Txt`]: label,
    })
  },
  submit() {
    const {
      warningType
    } = this.data.formData;
    let error = '';
    if (!warningType) {
      error = '请选择预警类型';
    }
    if (error) {
      this.setData({
        error
      })
      return;
    }
    this.addWarningList(warningType);
  },
  remove(e) {
    const {
      index
    } = e.currentTarget.dataset;
    this.deleteWarningList(index);
  },
  //获取预警规则 /warningRule/getList
  getWarningList() {
    app.$http.get('/warningRule/getList').then(({
      status,
      data = {}
    }) => {
      if (status == 0) {
        this.setData({
          rules: data
        })
      }
    }).catch(() => {})
  },
  //添加预警规则  warningType,
  addWarningList(warningType) {
    app.$http.postForm('/warningRule/add', {
      warningType: warningType
    }).then(({
      status,
      msg,
      data = {}
    }) => {
      if (status == 0) {
        wx.showToast({
          title: '添加成功',
          icon: 'none'
        })
        this.getWarningList();
      } else {
        wx.showToast({
          title: msg,
          icon: 'none'
        })
      }
    }).catch(() => {})
  },
  //删除预警规则
  deleteWarningList(id) {
    app.$http.deleteForm('/warningRule/del', {
      id
    }).then(({
      status,
      data = {}
    }) => {
      if (status == 0) {
        wx.showToast({
          title: '删除成功',
          icon: 'none'
        })
        this.getWarningList();
      }
    }).catch(() => {})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getWarningList();
  },

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

  }
})