const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code:'',
    modelList:[],
  },
  scanCode() {
    var that = this;
    wx.scanCode({ //扫描API
      scanType:['barCode','qrCode'],
      success(res) { //扫描成功
        console.log(res) //输出回调信息
        that.setData({
          code: res.result
        });
        wx.showToast({
          title: '扫码成功',
          duration: 1000
        })
      },
      fail:function(err){
        app.showToptip(that, 'error', '二维码无效');
      }
    })
  },
  setCode(e){
    this.setData({
      code:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var arr = [];
    var item = JSON.parse(options.item);
    this.setData({item,code:item.code});
    var modelList = JSON.parse(options.content);
    if(typeof modelList[0] == 'string'){
      var content = [];
      modelList.map((item,index)=>{
        var temp = item.replace('{','').replace('}','').split(',');
        temp.map((temps,index)=>{
          var value = temps.split(':');
          var valueStr = value[0].replace(/\s/g,'')
          content.push({[valueStr]:value[1]})
        })
      })
      content.map((item,index)=>{
        if(index % 2 != 0){
          arr.push(Object.assign(content[index],content[index-1]))
        }
      })
    }else if(typeof modelList[0] == 'object'){
      arr = modelList;
    }
    console.log(arr);
    this.setData({modelList:arr});
  },
  formSubmit(e){
    var value = e.detail.value;
    var code = this.data.code;
    const {name,companyName} = value;
    var that = this;
    var arr = [];
    delete value.code
    Object.keys(value).forEach(function (e,index) {
      if(value[e].length == 0){
        app.showToptip(that, "error",'信息不能为空！');
        arr = [];
        throw new Error('err');
      }else {
        if(index > 1){
          arr.push({'keyStr':e,'valueStr':value[e]})
          // arr.push("{keyStr:"+e+",valueStr:"+value[e]+"}")
        }
      }
    })
    if(arr){
      app.requestP({
        url:'/product/updateProduct',
        method:'post',
        contentType:' application/json',
        data:{
          code,
          name,
          companyName,
          id:that.data.item.id,
          content:JSON.stringify(arr),
          typeId:that.data.item.typeId
        }
      }).then(res=>{
        app.showToptip(that,'success',res.data.msg)
        wx.navigateBack({
          delta: 1,
        })
      }).catch(err=>{
        app.showToptip(that,'error',err.data.msg)
      })
    }    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})