Program.js 6.6 KB
// pages/Program/Program.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    temobj: {
      name: '',
      firm: '',
      text: '',
      last_state: true,
      add_state: false,
      input_stete:false
    },

  },

  //获取预设方案列表
  getlist() {
    let that = this;
    // var list=this.data.list
    // list.push({
    //   name: '',
    //   firm: '',
    //   text: '',
    //   id: 0,
    //   last_state: true
    // })
    // that.setData({
    //   list: list
    // })
    let url = 'counselor/SchemeList';
    let params = {
      userId: app.globalData.UserId
    }
    app.post(url, params).then((res) => {
      console.log(res)
      if (res.data.code == 200) {
        var list = res.data.data.list
        if (list.length == 0) {
          list.push({
            name: '',
            firm: '',
            text: '',
            id: 0,
            last_state: true
          })
          that.setData({
            list: list
          })
        } else {
          that.setData({
            list: res.data.data.list
          })
        }
        that.getLength()
      }
    }).catch((err) => {
      // console.log(err)
    })
  },

  //设置名字
  setName(e) {
    let that = this;
    let list = that.data.list;
    let index = e.currentTarget.dataset.index;
    list[index].name = e.detail.value;
    that.setData({
      list: list
    })
  },

  //设置公司名
  setFirm(e) {
    let that = this;
    let list = that.data.list;
    let index = e.currentTarget.dataset.index;
    list[index].firm = e.detail.value;
    that.setData({
      list: list
    })
  },

  //保障内容
  setText(e) {
    let that = this;
    let list = that.data.list;
    let index = e.currentTarget.dataset.index;
    list[index].text = e.detail.value;
    that.setData({
      list: list
    })
  },

  //添加方案
  addPlan(e) {
    let that = this;
    let list = that.data.list;
    let index = e.currentTarget.dataset.index;
    list[index].add_state = false
    list[index].input_stete=true
    this.setData({
      list: list
    })
    let url = 'counselor/SchemeSave';
    let params = {
      userId: app.globalData.UserId,
      schemeId: e.currentTarget.dataset.id,
      name: list[index].name,
      firm: list[index].firm,
      text: list[index].text
    }
    app.post(url, params).then((res) => {
      console.log(res)
      if (res.data.code == 200) {
        list.push({
          name: '',
          firm: '',
          text: '',
          id: 0,
          last_state: true,
          add_state: false,
          input_stete:false
        })
        that.setData({
          list: list
        })
        that.getLength2();
      }
    })
  },

  getLength() {
    var list = this.data.list
    list[list.length - 1].add_state = true
    for (var obj in list) {
      if (obj == list.length - 1) {
        list[list.length - 1].last_state = false
      } else {
        list[obj].last_state = true
      }
    }
    this.setData({
      list: list
    })
  },

  getLength2() {
    // this.getlist()
    var list = this.data.list
    list[list.length - 1].add_state = true
    for (var obj in list) {
      if (obj == list.length - 1) {
        list[list.length - 1].last_state = false
      } else {
        list[obj].last_state = true
      }
    }
    this.setData({
      list: list
    })

  },

  //保存按钮
  del(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let list = that.data.list;
    let id = e.currentTarget.dataset.id;
    if (list.length != 1) {
      wx.showModal({
        title: '提示',
        content: '是否删除预设方案',
        cancelColor: '#000000',
        confirmColor: '#3cc51f',
        showCancel: true,
        cancelText: '取消',
        confirmText: '确定',
        success(res) {
          if (res.confirm) {
            let url = 'counselor/SchemeDelete';
            let params = {
              SchemeId: id
            }
            app.post(url, params).then((res) => {
              // console.log(res)
              that.getlist();
            })
          } else if (res.cancel) {
            // console.log('用户点击取消')
          }
        }
      })
    } else {
      this.setData({
        last_state: false
      })
    }
  },


  settemname(e) {
    let that = this;
    let temobj = that.data.temobj;
    temobj.name = e.detail.value;
    that.setData({
      temobj: temobj
    })
  },

  settemfirm(e) {
    let that = this;
    let temobj = that.data.temobj;
    temobj.firm = e.detail.value;
    that.setData({
      temobj: temobj
    })
  },

  settemtext(e) {
    let that = this;
    let temobj = that.data.temobj;
    temobj.text = e.detail.value;
    that.setData({
      temobj: temobj
    })
  },

  deltemobj() {
    let that = this;
    let temobj = {
      name: '',
      firm: '',
      text: '',
    }
    that.setData({
      temobj: temobj
    })
  },



  //保存所有
  allsave() {
    let that = this;
    let list = this.data.list;
    console.log(list[list.length - 1].name);
    console.log(list[list.length - 1].id)
    if (list[list.length - 1].name != '' && list[list.length - 1].id != 0) {
      let url = 'counselor/SchemeSave';
      let params = {
        userId: app.globalData.UserId,
        schemeId: list[list.length - 1].id,
        name: list[list.length - 1].name,
        firm: list[list.length - 1].firm,
        text: list[list.length - 1].text
      }
      app.post(url, params).then((res) => {
        console.log(res)
        if (res.data.code == 200) {
          // wx.showToast({
          //   title: '添加成功',
          // })
          wx.navigateBack({
            delta: 1
          })
        }

      })
    } else {
      list.pop()
      that.setData({
        list: list
      })
      wx.navigateBack({
        
      })
    }
    that.getlist();
    console.log(that.data.list)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    that.getlist();

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    that.getlist();
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})