// pages/myWork/workDetail.js
const api = require('../../api/index')
const app = getApp()
const pay = require('../../utils/pay')
const showImage = require('../../utils/showImage')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgHttp:app.globalData.imghttp,
    ticketTypeFiled:{
      '1':'ticketFillInVO',
      '2':'ticketRightsProtectionVO',
      '3':'ticketLitigationRespondingVO',
      '4':'ticketPatentApplyVO'
    },
    ticketTypes:{
      '1':'专利解读',
      '2':'我要维权',
      '3':'我要申诉',
      '4':'我要申请'
    },
    ticketProgress:{
      '1':'等待受理',
      '2':'已受理，待支付',
      '3':'正在处理中',
      '4':'已完成',
      '5':'已取消',
      '6':'已上传结果',
    },
    id:null,
    work:{},
    show:false,
    ticketFlows:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData(
      {
        id:options.id
      }
    )
    this.getTicketDetail()
  },

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

  },

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

  },

  showProgress(){
    var params = {
      ticketId:this.data.id
    }
    api.queryTicketFlows(params).then(res=>{
      if(res.code == 200){
        this.setData(
          {
            ticketFlows:res.data.data.reverse()
          }
        )
      }
    })
    this.setData(
      {
        show:true
      }
    )
  },
  onClose(){
    this.setData(
      {
        show:false
      }
    )
  },

  getTicketDetail(){
    var params = {
      ticketId:this.data.id
    }
    api.getTicketDetail(params).then(res=>{
      if(res.code == 200){
        this.setData(
          {
            work:res.data.data
          }
        )
      }
    })
  },
  clickBtn(e){
    var {method} =e.currentTarget.dataset
    this[method](this.data.work)
  },
   //支付3
   successPay(e){
    this.setData(
      {
        ["work.ticketProgress"]:3
      }
    )
  },
   payMoney(data){
    var payData ={
      title:'支付工单',
      type:data.ticketType,
      goods:[
        {
          id:data.id,
          name:this.data.ticketTypes[data.ticketType]+'-工单',
          price:data.price,
          num:1
        }
      ],
      servicePrice:0.00,
      price:data.price,
      discount:null,
    }
    pay.openPay(payData)
    return
    var params = {
      goodType:2,
      goodUuid:data.num
    }
    wx.showLoading({
      title: '加载中',
    })
    pay.payMoney(params).then(res=>{
      console.log(res)
      if(res.errMsg == 'requestPayment:ok'){
        api.notify(
          {
            type:'success',
            message:'支付成功',
            top:wx.getStorageSync('height')
          }
        )
        this.setData(
          {
            ["work.ticketProgress"]:3
          }
        )
      }
    }).catch(error=>{

    })
  },
  //取消5
  cancel(data){
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定取消该工单吗？',
      complete: (res) => {
        if (res.cancel) {
          
        }
        if (res.confirm) {
          var params = {
            id:data.id,
            process:5
          }
          api.updateTicketProcess(params).then(res=>{
            if(res.code == 200){
              that.setData(
                {
                  ["work.ticketProgress"]:5
                }
              )
            }
          })
        }
      }
    })
   
   
  },
  //确认4
  confirm(data){
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定将该工单置为完成吗？',
      complete: (res) => {
        if (res.cancel) {
          
        }
        if (res.confirm) {
          var params = {
            id:data.id,
            process:4
          }
          api.updateTicketProcess(params).then(res=>{
            if(res.code == 200){
              that.setData(
                {
                  ["work.ticketProgress"]:4
                }
              )
            }
          })
        }
      }
    })
    
  },
 //查看图片
 previewImage(e) {
  console.log(e)
  var { current,imglist,type } = e.currentTarget.dataset
  var imgs = []
  for(var i =0;i<imglist.length;i++){
    if(type == 1){
      imgs.push(this.data.imgHttp + imglist[i].guid)
    }else{
      imgs.push(this.data.imgHttp + imglist[i])
    }
    
  }
  current = this.data.imgHttp + current
  showImage.showImageList(
    {
      current,
      urls: imgs.map((n) => ({ image: n, remark: n })),
      indicatorDots: true,
      indicatorColor: '#fff',
      indicatorActiveColor: '#04BE02',
      showDelete:false
    }
  )
},


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

  },

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

  },

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

  },

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

  },

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

  }
})