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

  /**
   * 页面的初始数据
   */
  data: {
    tableData:[],
    queryParams:{
      current:1,
      size:10
    },
    total:0,
    loading:false,
    imgHttp:app.globalData.imghttp,
    ticketTypeFiled:{
      '1':'ticketFillInVO',
      '2':'ticketRightsProtectionVO',
      '3':'ticketLitigationRespondingVO',
      '4':'ticketPatentApplyVO'
    },
    ticketTypes:{
      '1':'专利解读',
      '2':'我要维权',
      '3':'我要申诉',
      '4':'我要申请'
    },
    ticketTypesMenu:[
      {
        key:0,
        value:'全部'
      },
      {
        key:1,
        value:'专利解读'
      },
      {
        key:2,
        value:'我要维权'
      },
      {
        key:3,
        value:'我要申诉'
      },
      {
        key:4,
        value:'我要申请'
      },
    ],
    ticketType:null,
    ticketProgress:{
      '1':'等待受理',
      '2':'已受理，待支付',
      '3':'正在处理中',
      '4':'完成',
      '5':'取消',
      '6':'已上传结果',
    },
    //操作栏位
    menu:[
      {
        label:'支付',
        method:'payMoney',
        icon:'',
        type:2
      },
      {
        label:'确认',
        method:'confirm',
        icon:''
      },
      {
        label:'取消',
        method:'cancel',
        icon:''
      },
    ],
    triggered:false,
    load:false,
    hasMore:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

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

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

  },


  /**
   * 页面功能
   */
  //初始化加载
  init(){
    var params = {
      current:1,
      size:10
    }
    this.setData(
      {
        queryParams:params,
        total:0,
        tableData:[],
        hasMore:true
      }
    )
    this.getMyWork()
  },

  //下拉刷新
  onRefresh() {
    console.log('onRefresh')
    this.setData({
      triggered: true,
    })
    this.init()
  },
  changeData(e){
    var {name,title} = e.detail
    this.setData(
      {
        ticketType:name==0?null:name
      }
    )
    this.init()
  },
  checkDetail(e){
    var {item} = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/myWork/workDetail?id='+item.id,
    })
  },
   //查看图片
   previewImage(e) {
    var { current,imglist } = e.currentTarget.dataset
    var imgs = []
    for(var i =0;i<imglist.length;i++){
      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
      }
    )
  },
  //获取工单记录
  getMyWork(){
    wx.showLoading({
      title: '加载中',
    })
    var params = {
      ...this.data.queryParams
    }
    if(this.data.ticketType){
      params.ticketType = this.data.ticketType
    }
    api.queryTicket(params).then(res=>{
      if(res.code == 200){
        var data = this.data.tableData
        data = data.concat(res.data.data)
        this.setData(
          {
            tableData:data,
            total:res.data.total,
            triggered: false,
            load:false
          }
        )
        if(this.data.total<=data.length){
          this.setData({
            hasMore:false
          })
        }
      }
    }).catch(error=>{
      this.setData({
        triggered: false,
        load:false
      })
    })
  },
  //加载更多
  loadMore(){
    var current = this.data.queryParams.current
    this.setData(
      {
        ['queryParams.current']:current+1,
        load :true
      }
    )
    this.getMyWork()
  },
  //按钮点击
  clickBtn(e){
    var {method,item,index} = e.currentTarget.dataset
    this[method](item,index)
  },
  //菜单点击
  clickMenu(e){
    console.log(e)
    var data = e.detail
    this[data.method](data.data,data.index)
  },
  //支付3
  successPay(e){
    var data = e.detail
    var index = data.index
    this.setData(
      {
        ["tableData["+index+"].ticketProgress"]:3
      }
    )
  },
  payMoney(data,index){
    var payData ={
      title:'支付工单',
      type:data.ticketType,
      index:index,
      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(
          {
            ["tableData["+index+"].ticketProgress"]:3
          }
        )
      }
    }).catch(error=>{

    })
  },
  //取消5
  cancel(data,index){
    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(
                {
                  ["tableData["+index+"].ticketProgress"]:5
                }
              )
            }
          })
        }
      }
    })
    
   
  },
  //确认4
  confirm(data,index){
    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(
                  {
                    ["tableData["+index+"].ticketProgress"]:4
                  }
                )
              }
            })
        }
      }
    })
   
  },


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

  },

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

  },

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

  },

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

  },

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

  }
})