const _my = require("../../../../__antmove/api/index.js")(my);
const wx = _my;
// pages/wx_airport/Xiaosha_record/edit/inidex.js
var {
  disinfectEdit
} = require("../../../../utils/api/xiaosha");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    userInfo: wx.getStorageSync("userInfo")
  },
  addTime() {
    let {
      list
    } = this.data;
    let data = {
      startTime: null,
      endTime: null
    };
    list.disinfectAreaTimeVOList.unshift(data);
    this.setData({
      list
    });
  },
  delTime(e) {
    let list = this.data.list;
    let index = e.currentTarget.dataset.index;
    list.disinfectAreaTimeVOList.splice(index, 1);
    this.setData({
      list
    });
  },
  getVal(e) {
    let list = this.data.list;
    list[e.currentTarget.dataset.type] = e.detail.value;
    this.setData({
      list
    });
  },
  //获得时间
  timeChange(e) {
    let index = e.currentTarget.dataset.index;
    let type = e.currentTarget.dataset.type;
    let value = e.detail.value;
    let list = this.data.list;
    list.disinfectAreaTimeVOList[index][type] = value;
    this.setData({
      list
    });
  },
  // 提交
  report() {
    let {
      list,
      userInfo
    } = this.data;
    list.disinfectAreaTimeDTOList = list.disinfectAreaTimeVOList;
    list.adminId = userInfo.orgId;
    list.userId = userInfo.userId;
    if (!list.disinfectAreaName || !list.disinfectAreaAddress || !list.disinfectAreaOnus || !list.disinfectAreaPhone || !list.disinfectAreaNotice) {
      wx.showToast({
        title: "请将内容填写完整",
        icon: "none"
      });
      return;
    }
    let isOk = this.completeFilling(list.disinfectAreaTimeDTOList);
    if (isOk) {
      disinfectEdit(list).then(res => {
        if (res.code === 200) {
          my.alert({
            title: "提示",
            content: "编辑成功！",
            success: res => {
              wx.navigateBack({
                delta: 1
              });
            }
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none"
          });
        }
      });
    } else {
      wx.showToast({
        title: "消杀时段不能为空",
        icon: "none"
      });
    }
  },
  completeFilling(list) {
    let isOk = true;
    list.forEach(item => {
      if (!item.endTime || !item.startTime) {
        console.log(item);
        isOk = false;
      }
    });
    return isOk;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options) {
      this.setData({
        list: JSON.parse(options.item)
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  antmoveAction: function () {
    //执行时动态赋值，请勿删除
  }
});