// pages/create-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: {
    tabs: ['设备租赁订单', '租赁设备添加'],
    tabIndex: 1,
    id: -1,
    order: null,
    device_total: 0,
    role: null,
    // 审核(check)|确认(pass)|重新提交(re-submit)
    actionType: null,
    changePrice: false,
    remark: '',
    order_device: {},
    showPrice: false,
    device_quantity: '',
    right: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id ? options.id : 49
    this.setData({
      id
    })
    this.init()
  },

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

  init() {
    var that = this
    var id = this.data.id
    api.getByName(this, 'orders/detail', 'order', {
      id: id
    }, function () {
      that.setData({
        remark: that.data.order.remark
      })
      that.updateDeviceTotal()
      api.getByName(that, 'orders/getRole', 'role', {
        id: that.data.order ? that.data.order.project_id : ''
      }, function() {
        that.updateActionType()
      });
      
    });
  },

  updateActionType: function () {
    var actionType = ''
    var role = this.data.role
    var order = this.data.order
    var changePrice = false
    if (order.level == role.level) {
      if(['checking', 'checked'].indexOf(order.status_key) != -1 && role && role.rights && role.rights.rentCheck) actionType = 'check';
      else if(order.status_key == 'checked' && role.key == 'work') actionType = 'pass'
      else if(order.status_key == 'reject' && role.key == 'work') actionType = 're-submit'

      changePrice = role && role.rights && role.rights.rentMoneyChange
    }
    this.setData({
      actionType,
      changePrice
    })
  },

  changePrice: function() {
    var order_device = this.data.order_device
    var quantity = this.data.device_quantity
    var that = this
    http({
      url: 'orders/change',
      data: {
        id: order_device.id,
        quantity: quantity
      },
      success: function (res) {
        if (res.code == 0) {
          that.init()
        }
      }
    })
  },

  switchShowPrice: function(e) {
    var data =  e.currentTarget.dataset
    var show = data.show
    var item = data.item
    if(show) {
      this.setData({
        order_device: item,
        device_quantity: item.quantity
      })
    }
    this.setData({
      showPrice: e.currentTarget.dataset.show
    })
  },

  updateDeviceTotal: function () {
    var order = this.data.order
    var devices = order.order_devices ? order.order_devices : []
    var total = devices.length
    this.setData({
      device_total: total,
      devices: devices
    })
  },

  switchTab: function (e) {
    this.setData({
      tabIndex: e.currentTarget.dataset.index
    })
  },

  check: function(e) {
    var type = e.currentTarget.dataset.type
    var that = this
    var msg = '确认通过审核吗？'
    if(type == 'reject') msg = '确认驳回申请吗？'
    else if(type == 'pass' || type == 're-submit') msg = '确认提交吗？'
    Dialog.confirm({
      title: '提示',
      message: msg,
    })
      .then(() => {
        that.submitCheck(e)
      }).catch(()=>{
        Dialog.close()
      })
  },
  submitCheck: function (e) {
    var type = e.currentTarget.dataset.type
    var that = this
    http({
      url: 'orders/check',
      data: {
        id: this.data.id,
        type: type,
        remark: this.data.remark
      },
      success: function (res) {
        if (res.code == 0) {
          Dialog.close()
          util.success('操作成功')
          setTimeout(function() {
            that.init()
          }, 1000)
        }
      }
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})