// pages/order/index.js
import http from '../../utils/http'
import util from '../../utils/util'
import api from '../../utils/api'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: -1,
    project: null,
    tabs: ['全部订单', '设备租赁', '设备调用'],
    statuses: ['', '1', '2'],
    list: [
      [],
      [],
      [],
      [],
      []
    ],
    pages: [1, 1, 1, 1, 1],
    tabIndex: 0,
    touchBottom: [false, false, false, false, false],
    work_points: [],
    work_point_id: '',
    pointIndex: -1,
    isSearch: false,
    role: '',
    // list|check
    type: 'list'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id ? options.id : 1
    var type = options.type ? options.type : 'list'
    var tabIndex = options.index ? options.index : 0
    this.setData({
      id,
      type,
      tabIndex
    })
    api.getProject(this)
    api.getByName(this, 'work-points/get', 'work_points', {type: 'drop_menu'});
    api.getByName(this, 'orders/getRole', 'role', {id: id});
    this.getList();
  },
  doAction: function(e) {
    var type = e.currentTarget.dataset.type
    if(type == 'edit' || type == 'detail') {
      this.navigate(e)
    } else if(type == 'confirm') {
      var that = this
      var order = e.currentTarget.dataset.order
      Dialog.confirm({
        title: '提示',
        message: '确定订单吗',
      })
        .then(() => {
          that.submitCheck(order)
        })
    }
  },

  submitCheck(order) {
    var that = this
    http({
      url: 'orders/check',
      data: {
        id: order.id,
        type: 'confirm',
        remark: order.remark
      },
      success: function (res) {
        if (res.code == 0) {
          that.search()
        }
      }
    })
  },

  navigate: function(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

  search() {
    this.setData({
      list: [
        [],
        [],
        [],
        [],
        []
      ],
      pages: [1, 1, 1, 1, 1],
      touchBottom: [false, false, false, false, false],
      isSearch: true
    })
    this.getList()
  },

  getList: function () {
    var index = this.data.tabIndex
    var touchBottom = this.data.touchBottom[index]
    if (touchBottom) return false;
    var status = this.data.statuses[index]
    var page = this.data.pages[index]
    var that = this
    var work_point_id = this.data.work_point_id
    http({
      url: 'orders/get',
      data: {
        project_id: this.data.id,
        page: page,
        work_point_id: work_point_id,
        keyword: this.data.keyword,
        type: status,
        is_draft: 1
      },
      success: function (res) {
        if (res.code == 0) {
          var list = that.data.list
          var touchBottom = that.data.touchBottom
          list[index] = list[index].concat(res.data);
          if (res.data.length <= 0) {
            touchBottom[index] = true;
          }
          that.setData({
            touchBottom,
            list,
            isSearch: false
          })
        }
      }
    })
  },

  switchTab: function (e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      tabIndex: index
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
    this.search()
  },

  onChange: function (e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      [name]: e.detail.value
    })
  },

  onDropChange: function(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      [name]: e.detail
    })
    if(name == 'work_point_id') {
      this.search()
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var index = this.data.tabIndex
    if(!this.data.isSearch && !this.data.touchBottom[index]) {
      var pages = this.data.pages
      pages[index] = pages[index] + 1;
      this.setData({
        pages
      })
      this.getList()
    }
    if(this.data.touchBottom[index]) {
      util.error('没有更多数据了')
    }
  },

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

  }
})