// pages/InstallOwn/InstallOwn.js
const {
  getMateriallist,
  usedMaterialOther
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ids: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      type: options.type,
      entryId: options.entryId,
      warehouseId: options.warehouseId,
      productId: options.productId,
    })
    wx.setNavigationBarTitle({
      title: options.type == 1 ? "添加并网箱" : "添加支架",
    })
    this.getMateriallist()
  },


  /**
   * 
   * 获取并网箱和支架列表
   * 
   * @param {*} e 
   */
  getMateriallist(e) {
    getMateriallist({
      entryId: this.data.entryId,
      warehouseId: this.data.warehouseId,
      type: this.data.type == 1 ? 2 : 4,
    }).then((res) => {
      if (res.code == 200) {
        res.data.forEach(element => {
          element.columns.isSelect = false
          element.columns.num = 1
        });
        this.setData({
          list: res.data
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },


  /**
   * 
   * 选择
   * 
   * @param {*} e 
   */
  bindchange(e) {
    this.setData({
      [`list[${e.currentTarget.dataset.index}].columns.isSelect`]: e.currentTarget.dataset.is ? false : true,
    })
  },

  /**
   * 
   * 
   * 输入
   * 
   * @param {*} e 
   */
  bindinput(e) {
    this.setData({
      [`list[${e.currentTarget.dataset.index}].columns.num`]: e.detail,
    })
  },

  /**
   * 
   * 提交
   * 
   * @param {*} e 
   */
  submit(e) {
    var data = [];
    for (let i = 0; i < this.data.list.length; i++) {
      const element = this.data.list[i];
      var dataItem = {}
      if (element.columns.isSelect) {
        if (!element.columns.num) {
          wx.showToast({
            title: '请输入第' + (i + 1) + "个的数量",
            icon: 'none'
          })
          return
        } else {
          dataItem.materialId = element.columns.id
          dataItem.num = element.columns.num
          data.push(dataItem)
        }
      }
    }
    if (data.length <= 0) {
      wx.showToast({
        title: this.data.type == 1 ? "请选择并网箱" : "请选择支架",
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '加载中',
    })
    usedMaterialOther({
      userId: wx.getStorageSync('userId'),
      entryId: this.data.entryId,
      projectId: this.data.productId?this.data.productId:'',
      materialList: data
    }).then((res) => {
      wx.hideLoading()
      if (res.code == 200) {
        wx.showToast({
          title: '提交成功',
          icon: "none",
          success: function () {
            var pages = getCurrentPages(); // 获取页面栈
            var currPage = pages[pages.length - 1]; // 当前页面
            var prevPage = pages[pages.length - 2]; // 上一个页面            
            prevPage.getWarehouseList()
            wx.navigateBack({ delta: 1})

            // setTimeout(function () {
            //   wx.navigateBack()
            // }, 1000)
          }
        })
      } else {
        wx.showToast({
          title: res.msg || res.errMsg,
          icon: 'none'
        })
      }
    })

  }
})