// pages/server_detail/server_detail.js.
const { api, formatTime, previewPic, rapi, mapApi, mockApi, imgName, alertTip, confirmTip } = require('../../lib/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgLogo: imgName('logo.png'),
    order:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id
    this.setData({
      id
    })
   
  },
  onShow: function () {
    this.getMyOrderDetail(this.data.id)
  },
  getMyOrderDetail(id){
    rapi.get('userOrder/myOrderDetail', {
      id
    }).then(res => {
      let order = res.data
      let stateArray = ['已下单', '已接单', '待评价', '已评价', '已取消']
      order.state = stateArray[order.state]
      order.pics = order.pics.split(',')
      order.orderTime = formatTime(new Date(order.orderTime))|| ''
      order.receiveTime = formatTime(new Date(order.receiveTime)) || ''
      order.completeTime = formatTime(new Date(order.completeTime)) || ''
      order.itemList=order.itemList.map(item=>{
        item.itemFee = item.manHours * order.manHoursFee
        return item
      })
      
      this.setData({
        order: res.data
      })
    }).catch(err => {
      console.log(err)
    })
  },
  // 预览问题图片 
  lookPic(e) {
    let { src, list} = e.currentTarget.dataset;
    //图片预览
    previewPic(src,list)
  },
  //取消订单
  cancleOrder(){
    confirmTip('您确认取消工单？',()=>{
      rapi.get('userOrder/cancleOrder', {
        userId: wx.getStorageSync('user').userMapp.id,
        id: this.data.order.id
      }).then(res => {
        alertTip('取消成功', () => {
          this.getMyOrderDetail(this.data.order.id)
        })
      }).catch(err => {
        alertTip(err)
      })
    })
  },
  //评价订单
  evaluateOrder() {
    let orderId = this.data.order.id
    wx.navigateTo({
      url: "/pages/evaluate/evaluate?orderId=" + orderId,
    })
  }
})