// pages/project/index.js
import http from '../../utils/http'
import util from '../../utils/util'
import api from '../../utils/api'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: -1,
    userInfo: {},
    device_rent_menus: [{
      img: 'http://t18.9026.com/mini/create-order.png',
      title: '创建订单',
      desc: '创建外部设备租赁订单',
      url: '/pages/create-order/index',
      hidden: true
    }, {
      img: 'http://t18.9026.com/mini/rent-check.png',
      title: '租赁审核',
      desc: '设备租赁订单审核',
      url: '/pages/order/index?type=check'
    }, {
      img: 'http://t18.9026.com/mini/error-handle.png',
      title: '异常处理',
      desc: '订单流程变更审核处理',
      url: '/pages/order/index?index=4'
    }, {
      img: 'http://t18.9026.com/mini/all-order.png',
      title: '所有订单',
      desc: '查看全部设备租赁订单',
      url: '/pages/order/index'
    }],
    device_use_menus: [{
      img: 'http://t18.9026.com/mini/use-apply.png',
      title: '调用申请',
      desc: '机电负责人调用设备',
      url: '/pages/create-order-inner/index',
      hidden: true
    }, {
      img: 'http://t18.9026.com/mini/rent-check.png',
      title: '调用审核',
      desc: '设备调用订单审核',
      url: '/pages/order-inner/index?type=check'
    }, {
      img: 'http://t18.9026.com/mini/error-handle.png',
      title: '异常处理',
      desc: '订单流程变更审核处理',
      url: '/pages/order-inner/index?index=4'
    }, {
      img: 'http://t18.9026.com/mini/all-order.png',
      title: '所有订单',
      desc: '查看全部设备调用订单',
      url: '/pages/order-inner/index'
    }],
    device_depot_menus: [{
      img: 'http://t18.9026.com/mini/all-order.png',
      title: '所有设备',
      desc: '查看仓库所有设备',
      url: '/pages/device-inner/index'
    }, {
      img: 'http://t18.9026.com/mini/rent-check.png',
      title: '维修上报',
      desc: '设备维修上报',
      url: '/pages/repair/index'
    }],
    role: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id ? options.id : 1
    this.setData({
      id: id,
      userInfo: app.globalData.userInfo
    })
    api.getByName(this, 'getUserInfo', 'userInfo', {}, function(res) {
      app.updateUserInfo(res);
    });
    var that = this;
    api.getByName(this, 'project-roles/getRoleAndRights', 'role', {id: id}, function() {
      that.updateMenuItemShow()
    });
    wx.setStorageSync('sg-added-devices', [])
  },

  updateMenuItemShow() {
    var role = this.data.role
    var device_rent_menus = this.data.device_rent_menus
    device_rent_menus[0].hidden = !(role && role.rights && role.rights.rentCreate)
    if(['sub', 'admin'].indexOf(role.key) != -1) {
      device_rent_menus[1].hidden = true;
      device_rent_menus[2].hidden = true;
    }
    var device_use_menus = this.data.device_use_menus
    device_use_menus[0].hidden = !(role && role.rights && role.rights.applyCreate)
    this.setData({
      device_rent_menus,
      device_use_menus
    })
  },

  navigate: function(e) {
    var url = e.currentTarget.dataset.url
    var id = this.data.id
    if(url == '/pages/device-inner/index?id=' + id) {
      app.resetFilter();
    }
    wx.navigateTo({
      url: url,
    })
  },

  getProject() {
    var that = this
    http({
      url: 'projects/detail',
      data: {
        id: this.data.id
      },
      success: function(res) {
        if(res.code == 0) {
          that.setData({
            project: res.data
          })
          wx.setNavigationBarTitle({
            title: res.data.name,
          })
        }
      }
    })
  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

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

  }
})