// pages/loadAppoint/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    str:'&emsp;',
    loginStatus: wx.getStorageSync('loginStatus') ? wx.getStorageSync('loginStatus') : false,
    roleCodeList: wx.getStorageSync('roleCodeList') ? wx.getStorageSync('roleCodeList') : '',
    searchVal: '',
    
    //分页
    limit: 10,
    requestStatu: '加载中...',
    page: 1,
    totalPages: 1,
    more: false,
    nomore: '',
    resData:[],

    showOver: false,
    showFill: false,
    endReason:'',//结束说明
    fillWeight:'',//填报重量

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //预约审核状态
    let treeData = app.globalData.treeArr
    for (const element of treeData) {
      let arrStr;
      switch (element.dictValue) {
        case 'arrive_status':
          arrStr = "arriveStatusArray";
          break;
      }
      let getArray =[{
        dictValue: "",
        dictLabel: "全部状态"
      }]
      this.setData({
        [arrStr]: getArray.concat(element.children)
      })
    }

    this.setData({
      safeAreaHeight: app.globalData.windowInfo.screenHeight - app.globalData.systemInfo.safeArea.bottom
    })

    // 使用选择器选择节点
    this.getFilterHeight()
    //获取页面高度设置新增按钮的初始位置
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        // 高度,宽度 单位为px
        that.setData({
          windowHeight: res.windowHeight, //屏幕宽度、高度
          windowWidth: res.windowWidth,
          buttonTop: res.windowHeight * 0.8 - that.data.safeAreaHeight-50, //这里定义按钮的初始位置
          buttonLeft: res.windowWidth * 0.80, //这里定义按钮的初始位置
        })
      }
    })
  },

  //获取顶部筛选区域的高度
  getFilterHeight:function(){
    let that = this;
    // 使用顶部筛选高度选择器选择节点的高度
    const query = wx.createSelectorQuery();
    query.select('.topSeach').boundingClientRect(function (rect) {
      if(rect){
        that.setData({
          filterWidth: rect.width,
          filterHeight: rect.height,
        });
      }
    }).exec();
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const roleCodeList = wx.getStorageSync('roleCodeList')
    let loginStatus = wx.getStorageSync('loginStatus')
    this.setData({
      searchVal: '',
      roleCodeList: roleCodeList ? roleCodeList : '',
      loginStatus: loginStatus ? loginStatus : false
    })
    if(wx.getStorageSync('loginStatus')){
      this.getListFun();
    }
  },

  //页面跳转
  toLink: function (e) {
    let { url } = e.currentTarget.dataset
    wx.navigateTo({
      url: url
    })
  },

  //搜索框请求S
  onChange(e) {
    this.setData({
      searchVal: e.detail,
    });
  },
  onSearch: function (event) {
    this.setData({
      searchVal: event.detail
    })
    this.getListFun();
  },
  onCancel: function () {
    this.setData({})
  },
  toSearch: function (event) {
    this.getListFun();
  },

  //列表
  getListFun: function () {
    this.setData({
      page: 1
    })
    let params = {
      current   : this.data.page,
      size      : this.data.limit,
      loadNumber: this.data.searchVal
    }
    app.request.pageFirst({
      url: app.API.bizloadappointPage,
      page: this,
      params: params,
    })
  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      searchVal: ''
    })
    if (wx.getStorageSync('loginStatus')) {
      this.getListFun();
    } else {
      wx.stopPullDownRefresh();
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.page++;
    let params = {
      current: this.data.page,
      size: this.data.limit,
      loadNumber:this.data.searchVal,
    }
    app.request.pageOther({
      url: app.API.appointmentList,
      page: this,
      params: params,
      loadType: true //加载类型，是否是下拉加载
    });
  },

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

  },


  // 结束endLoad
  showPopupOver(e) {
    let { id } = e.currentTarget.dataset
    this.setData({
      showOver : true,
      id   : id
    });
  },
  onCloseOver() {
    this.setData({
      showOver: false,
      id: '',
      endReason: ''
    });
  },
  // 结束表单提交 
  formSubmitOver: function ({detail:{value}}) {
    let _this = this
    var warn = ""; //弹框时提示的内容
    if (value.endReason == '') {
      warn = "请输入结束说明！";
    } 
    if (warn != '') {
      app.util.checkForm(warn);
    } else {
      app.request.POST({
        url: app.API.endLoad,
        params: value,
        page: this,
        isLoadingTxt: '提交中...',
        isSubmitting: true,
        successFun: true
      }).then(res => {
        wx.showToast({
          title: '订单结束成功',
          icon: 'success',
          duration: 1000,
          mask: true
        })
        _this.onCloseOver()
        _this.getListFun(); //刷新列表
      })
    }
  },

  // 填报
  showPopupFill(e) {
    let { id } = e.currentTarget.dataset
    this.setData({
      showFill : true,
      id   : id
    });
  },
  onCloseFill() {
    this.setData({
      showFill: false,
      id: '',
      fillWeight: ''
    });
  },
  // 填报表单提交 
  formSubmitFill: function ({detail:{value}}) {
    let _this = this
    var warn = ""; //弹框时提示的内容
    if (value.fillWeight == '') {
      warn = "请输入填报重量！";
    } 
    if (warn != '') {
      app.util.checkForm(warn);
    } else {
      app.request.POST({
        url: app.API.fillLoad,
        params: value,
        page: this,
        isLoadingTxt: '提交中...',
        isSubmitting: true,
        successFun: true
      }).then(res => {
        wx.showToast({
          title: '填报成功',
          icon: 'success',
          duration: 1000,
          mask: true
        })
        _this.onCloseFill()
        _this.getListFun(); //刷新列表
      })
    }
  },


})