// pages/workbench/product/addProdutType/addProdutType.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists:[{name:''}],
    productTypeName:'',
    modelList:[],
    scrollHeight: 0, // scroll-view高度
    startX: 0, // 开始X坐标
    startY: 0, // 开始Y坐标
  },
  bindKeyInput:function (e) {
    var that = this;
    //下标
    var index = e.currentTarget.dataset.index;
    //数据
    var value = e.detail.value;
    var name = that.data.lists[index].name,
    name = "lists["+index+"].name";
    this.setData({
      [name] : value
    })
  },
  setPruductTypeName(e){
    let productTypeName = e.detail.value;
    this.setData({
      productTypeName
    })
  },
  addList: function(){
    var lists = this.data.lists;
    var flag;
    var p = new Promise((resolve,reject)=>{
      for(var i in lists){
        if(lists[i].name.length != 0){
          flag = true;
        }else{
          flag = false;
          break;
        }
      }
      if(flag){
        resolve(flag)
      }else{
        reject(flag)
      }
    })
    p.then(()=>{
      var newData = {name:''};
      lists.push(newData);//实质是添加lists数组内容，使for循环多一次
      this.setData({
        lists: lists,
      })
    }).catch(()=>{
      app.showToptip(this, "error",'产品信息为空');
    })
  },
  submit(){
    var that = this;
    var lists = that.data.lists.concat(that.data.modelList);
    var productTypeName = that.data.productTypeName;
    for(var i in lists){
      // if(lists[i].name.length == 0){
      //   app.showToptip(that, "error",'产品信息为空');
      //   return;
      // }
      if(productTypeName.length == 0){
        app.showToptip(that, "error",'产品类别名称为空');
        return;
      }
    }
    var fields = [];
    lists.map(item=>{
      fields.push(item.name)
    })
    if(that.data.modelList!=0){
      app.requestP({
        url:'/productType/edit',
        method:'post',
        contentType:' application/json',
        data:{
          name:productTypeName,
          fields,
          id:that.data.id || 0,
        }
      }).then(res=>{
        app.showToptip(that, "success",res.data.msg);
        wx.navigateBack({
          delta: 1,
        })
      }).catch(err=>{
        app.showToptip(that, "error",err.data.msg);
      })
    }else{
      app.requestP({
        url:'/productType/add',
        method:'post',
        contentType:' application/json',
        data:{
          name:productTypeName,
          fields,
        }
      }).then(res=>{
        app.showToptip(that, "success",res.data.msg);
        wx.navigateBack({
          delta: 1,
        })
      }).catch(err=>{
        app.showToptip(that, "error",err.data.msg);
      })
    }
  },
  delList: function () {
    var lists = this.data.lists;
    lists.pop();      //实质是删除lists数组内容，使for循环少一次
    this.setData({
      lists: lists,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {id,name} = options;
    this.getModel(id,name);
  },
  getModel(id,name){
    var that = this;
    if(id && name){
      app.requestP({
        url:'/productModel/ListByType',
        method:'post',
        contentType:' application/json',
        data:{
          type:id
        }
      }).then(res=>{
        that.setData({
          scrollHeight: wx.getSystemInfoSync().screenHeight,
          modelList:res.data.data,
          productTypeName:name,
          id
        })
      }).catch(err=>{
        app.showToptip(that,'error',err.data.msg)
      })
    }
  },
  // 手指触摸动作开始
  touchStart: function (e) {
    let that = this;
    //开始触摸时 重置所有删除
    that.data.modelList.forEach(function (v, i) {
      if (v.isTouchMove) v.isTouchMove = false; // 只操作为true的
    })
    // 记录手指触摸开始坐标
    that.setData({
      startX: e.changedTouches[0].clientX, // 开始X坐标
      startY: e.changedTouches[0].clientY, // 开始Y坐标
      modelList: that.data.modelList
    })
  },
  // 手指触摸后移动
  touchMove: function (e) {
    let that = this,
      index = e.currentTarget.dataset.index, // 当前下标
      startX = that.data.startX, // 开始X坐标
      startY = that.data.startY, // 开始Y坐标
      touchMoveX = e.changedTouches[0].clientX, // 滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY, // 滑动变化坐标
      // 获取滑动角度
      angle = that.angle({
        X: startX,
        Y: startY
      }, {
        X: touchMoveX,
        Y: touchMoveY
      });
    // 判断滑动角度
    that.data.modelList.forEach(function (v, i) {
      v.isTouchMove = false
      // 滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        // 右滑
        if (touchMoveX > startX)
          v.isTouchMove = false
        // 左滑
        else
          v.isTouchMove = true
      }
    })
    // 更新数据
    that.setData({
      modelList: that.data.modelList
    })
  },
  // 计算滑动角度
  angle: function (start, end) {
    let that = this,
      _X = end.X - start.X,
      _Y = end.Y - start.Y;
    // 返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  delModel(e){
    var that = this;
    const index = e.currentTarget.dataset.index;
    //切割当前下标元素，更新数据
    that.data.modelList.splice(index, 1);
    that.setData({
      modelList: that.data.modelList
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})