const util = require("../../../utils/util");
const api = require('../../../api/api.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    showType: 0,
    page: 1,
    limit: 10,
    totalPages: 0,
    myqr_show: false, //显示追加信息框
    myqr_blur: 1, //背景模糊值 
    content: '',
    ticketSn: '', //当前处理的工单
    action: '' //操作工单动作 是处理 还是完结
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList();
  },
  ticketHandle() {
    if (this.data.action == "handle")
      this.saveHandle();
    else  this.saveCompleted(); 
  },
  saveHandle() { //工单处理
    let that = this
    if (!that.data.content) {
      wx.showToast({
        title: '请输入处理内容',
        duration: 3000
      });
      return;
    }
    wx.showLoading();
    util.request(api.TicketHandle, {
      ticketSn: that.data.ticketSn,
      content: that.data.content
    }).then(function (res) {
      wx.hideLoading();
      that.handleFilter();
      if (res.errno === 0) {
        that.setData({
          content: '',
          myqr_blur: 1,
          myqr_show: false
        })
        wx.showToast({
          title: '处理成功',
          duration: 3000
        });

      } else {
        util.showErrorToast(res.errmsg);
      }
    });
  },
  saveCompleted(e) {
    let that = this
    if (!that.data.content) {
      wx.showToast({
        title: '请输入处理内容',
        duration: 3000
      });
      return;
    }
    wx.showLoading();
    util.request(api.TicketCompleted, {
      ticketSn: that.data.ticketSn,
      content: that.data.content
    }).then(function (res) {
      wx.hideLoading();
      that.handleFilter();
      if (res.errno === 0) {
        that.setData({
          content: '',
          myqr_blur: 1,
          myqr_show: false
        })
        wx.showToast({
          title: '工单完结',
          duration: 3000
        });

      } else {
        util.showErrorToast(res.errmsg);
      }
    });
  },
  handleFilter(){
    var showType=this.data.showType;
    this.setData({
      list: [],
      showType: showType,
      page: 1,
      limit: 10,
      totalPages: 1
    });
    this.getList();
  },
  switchTab(e) {
    let showType = e.currentTarget.dataset.index;  
    this.setData({
      list: [],
      showType: showType,
      page: 1,
      limit: 10,
      totalPages: 1
    });
    this.getList();
  },
  goHandle(e) {
    wx.pageScrollTo({
      scrollTop: 0
    });

    this.setData({
      myqr_show: true,
      myqr_blur: 1,
      ticketSn: e.currentTarget.dataset.sn,
      content: '',
      action: 'handle'
    })
  },
  goCompleted(e) {
    wx.pageScrollTo({
      scrollTop: 0
    });

    this.setData({
      myqr_show: true,
      myqr_blur: 1,
      ticketSn: e.currentTarget.dataset.sn,
      content: '',
      action: 'completed'
    })
  },
  qrtouchstart: function () {
    this.setData({
      myqr_blur: 1,
      myqr_show: false,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  bindContent(e) {
    this.setData({
      content: e.detail.value
    })
  },
  getList() {
    let that = this;
    wx.showLoading();
    util.request(api.TicketList, {
      showType: that.data.showType,
      page: that.data.page,
      limit: that.data.limit
    }).then(function (res) {
      wx.hideLoading();
      if (res.errno === 0) {
        console.log(res.data);
        that.setData({
          list: that.data.list.concat(res.data.list),
          totalPages: res.data.pages
        });
      }
    });
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getList();
    } else {
      wx.showToast({
        title: '没有更多工单了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  viewTicket(e) {
    wx.navigateTo({
      url: '/pages/servant/crm/ticketAction?ticketSn=' + e.currentTarget.dataset.sn
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

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

  }
})