// pages/salesOrderMan/add.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    defaultText: '请选择',
    isSubmitting: false,

    orderTypeArray:[],//订单类型
    saleOrderType:'',

    customerArray:[], //客户
    customerIndex:null,

    saleOrderArray:[], //销售订单
    saleOrderIndex:null,

    confTimeArray:[], //提货时间
    confTimeIndex:null,

    supplierArray:[],//供应商
    supplierIndex:null,

    freightPrice:'',//运费单价
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //订单类型
    let treeData = app.globalData.treeArr
    for (const element of treeData) {
      let arrStr;
      switch (element.dictValue) {
        case 'order_type':
          arrStr = "orderTypeArray";
          break;
      }
      this.setData({
        [arrStr]: element.children
      })
    }

    //获取客户列表接口
    this.getCustomerData()
    //供应商
    this.getsupplierData()
  },

  //选择客户
  getCustomerData:function(){
    //收货单位列表接口
    app.request.GET({
      url: app.API.getCustomerList,
      params: {},
      page: this,
      successFun: true
    }).then(res => {
      this.setData({
        customerArray:res.data.data
      })
    })
  },

  //选择供应商
  getsupplierData:function(){
    //收货单位列表接口
    app.request.GET({
      url: app.API.supplierPage,
      params: {},
      page: this,
      successFun: true
    }).then(res => {
      this.setData({
        supplierArray:res.data.data
      })
    })
  },

  //下拉选择
  bindPickerChange: function (e) { //下拉菜单
    let { pickername } = e.target.dataset
    let getIndex = pickername + 'Index'
    app.util.getPickerIndex(this, getIndex, e);
    switch (pickername) {
      case 'customer': //客户
        this.getSaleOrderData() //获取销售订单
        break;
      case 'saleOrder': //销售订单
        this.setData({
          saleOrderWeight:this.data.saleOrderArray[this.data.saleOrderIndex].saleOrderWeight,
          saleOrderType:this.data.saleOrderArray[this.data.saleOrderIndex].saleOrderType,
        })
        this.getConfTimeData() //获取提货时间段
        break;
    }
  },

  //修改订单重量时
  orderWeightChange:function(event){
    this.setData({
      saleOrderWeight:event.detail
    })
  },

  //修改运费单价时
  freightPriceChange:function(event){
    this.setData({
      freightPrice:event.detail
    })
  },

  //选择销售订单
  getSaleOrderData:function(){
    app.request.GET({
      url: app.API.getOrderByCustomerId,
      params: {
        id:this.data.customerArray[this.data.customerIndex].id,
        flag:'add'
      },
      page: this,
      successFun: true
    }).then(res => {
      let orderList = res.data.data
      orderList.forEach((element,index) => {
        orderList[index].name = element.saleGoodsName + '-' + element.saleOrderNumber
      });
      this.setData({
        saleOrderArray:orderList
      })
    })
  },

  //提货时间
  getConfTimeData:function(){
    app.request.GET({
      url: app.API.getTakeList,
      params: {
        goodsName:this.data.saleOrderArray[this.data.saleOrderIndex].saleGoodsName,
        needWeight:this.data.saleOrderArray[this.data.saleOrderIndex].saleOrderWeight,
        goodsCode:this.data.saleOrderArray[this.data.saleOrderIndex].saleGoodsCode
      },
      page: this,
      successFun: true
    }).then(res => {
      let resData  = res.data.data
      resData.forEach((element,index) => {
        resData[index].timeRange = element.confStartTime + '至' + element.confEndTime
      });
      this.setData({
        confTimeArray:resData
      })
    })
  },

  // 单选
  dangerStatusChange: function (e) {
    this.setData({
      [e.currentTarget.dataset.radiotype]: e.detail.value
    })
  },
  //多选
  checkboxChange: function (e) {
    this.setData({
      [e.currentTarget.dataset.checkboxtype]: e.detail
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  bindscrolltolowerFun() {
    
  },

  //表单提交 carTaskAdd
  formSubmit: function ({detail:{value}}) {
    var warn = ""; //弹框时提示的内容
    if (value.customerId == '') {
      warn = "请选择客户！";
    } else if(value.orderName == ''){
      warn = "请选择销售订单！";
    } else if(value.deliveryTimeId == ''){
      warn = "请选择提货时间！";
    } else if(value.supplierId == ''){
      warn = "请选择供应商！";
    } else if(value.freightPrice == ''){
      warn = "请输入运费单价！";
    }
    if (warn != '') {
      app.util.checkForm(warn);
    } else {
      app.request.POST({
        url: app.API.orderAdd,
        params: value,
        page: this,
        isLoadingTxt: '提交中...',
        isSubmitting: true,
        successFun: true
      }).then(res => {
        wx.showToast({
          title: '新增成功',
          icon: 'success',
          duration: 2000,
          mask:true,
          complete: function () {
            setTimeout(() => {
              wx.navigateBack()
            }, 1500) //延迟时间  
          }
        })
      })

    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

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

  }
})