// 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)|退回(back)
    actionType: null,
    changePrice: false,
    remark: '',
    order_device: {},
    showPrice: false,
    device_price: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id ? options.id : ''
    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 (res) {
      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 (res) {
        that.updateActionType()
      });
    });
  },

  updateActionType: function () {
    var actionType = ''
    var role = this.data.role
    var order = this.data.order
    if (order.project_role_id == role.id) {
      if (['assist', 'manager', 'admin'].indexOf(role.project_role.key) != -1) actionType = 'check'
      if(order.status_key == 'checked' && role.project_role.key == 'machine') actionType = 'pass'
      if(order.status_key == 'reject' && role.project_role.key == 'machine') actionType = 're-submit'
      if(order.status_key == 'pass') actionType = 'back'
    }
    var changePrice = false
    this.setData({
      actionType,
      changePrice
    })
  },

  changePrice: function() {
    var order_device = this.data.order_device
    var price = this.data.device_price
    var that = this
    http({
      url: 'orders/changePrice',
      data: {
        id: order_device.pivot.id,
        price: price
      },
      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_price: item.pivot.price / 100
      })
    }
    this.setData({
      showPrice: e.currentTarget.dataset.show
    })
  },

  updateDeviceTotal: function () {
    var order = this.data.order
    var devices = order.inner_devices ? order.inner_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 == 'confirm' || type == 're-submit') msg = '确认提交吗？'
    else if(type == 'back') 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 () {

  }
})