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

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    showType: 0,
    page: 1,
    limit: 10,
    totalPages: 1,
    myqr_show: false, //我的分享二维码
    myqr_url: null,
    myqr_blur: 0, //背景模糊值
    navigateType: 0, //0 进入详情页 1问题报备 1需要刷新页面
    comments: {
      ticketId:0,
      ticketSn:'',
      selected:0,//选中的星描述
      selectedDes:'',
      comment: '',
      stars: [{level:1,img: '/static/images/star.png',imgSelected:'/static/images/star@selected.png',checked:false,des:'非常差'},
      {level:2,img: '/static/images/star.png',imgSelected:'/static/images/star@selected.png',checked:false,des:'差'},
      {level:3,img: '/static/images/star.png',imgSelected:'/static/images/star@selected.png',checked:false,des:'一般'},
      {level:4,img: '/static/images/star.png',imgSelected:'/static/images/star@selected.png',checked:false,des:'满意'},
      {level:5,img: '/static/images/star.png',imgSelected:'/static/images/star@selected.png',checked:false,des:'非常满意'},]

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

  },
  clickStar(e){ 
    let level = e.currentTarget.dataset.level;
    console.info(level)
    let stars=this.data.comments.stars;
    let des=''
    stars.forEach(item=>{
      if(item.level<=level) {
        item.checked=true;
        des=item.des
      }else{
        item.checked=false;
      }
    })
    this.setData({
      'comments.stars':stars, 
      'comments.selectedDes':des,
      'comments.selected':level
    })
  },
  goSaveCommnet(){//保存评论
    let that=this;
    if(that.data.comments.selected==0){
      wx.showToast({
        title:'请选择满意度',
        duration:2000
      })
      return;
    }

    if(that.data.comments.selected<4&&that.data.comments.comment==''){
      wx.showToast({
        title:'请填写有待提升的地方',
        duration:2000
      })
      return;
    }

    wx.showModal({
      title: '提示',
      content: '确定要评价此处理过程吗？提交后 无法修改',
      success: function (sm) {
        if (sm.confirm) {
          wx.showLoading(); 
          let data={
            ticketId:that.data.comments.ticketId,
            ticketSn:that.data.comments.ticketSn,
            comment:that.data.comments.comment,
            star:that.data.comments.selected
          }
          util.request(api.ProblemComment,data,"POST").then(res=>{
            wx.hideLoading();
            let list=that.data.list;
            list.forEach(item=>{
              if(item.ticketId==that.data.comments.ticketId){
                item.isCommented=true;
              }
            })
            let comments=that.data.comments;
            comments.selected=0;
            comments.selectedDes="";
            comments.comment="";
            comments.stars.forEach(star=>{
              star.checked=false;
            })
            that.setData({ 
              myqr_blur: 0,
              myqr_show: false,
              comments:comments,
              list:list
            })
            wx.showToast({
              title: '评价成功',
              duration: 3000 
            }).catch(res=>{ 
              wx.hideLoading();
              wx.showToast({
                title: res.errmsg,
                duration: 3000
              });
            });
          })
        }
      }
    })
  },
  goComment(e) { //去评论
    // wx.pageScrollTo({
    //   scrollTop: 0
    // });

    let id = e.currentTarget.dataset.id;
    let sn = e.currentTarget.dataset.sn;

    this.setData({
      myqr_show: true,
      myqr_blur: 0,
      'comments.ticketId':id, 
      'comments.ticketSn':sn
    })
  },
  qrtouchstart: function () {
    this.setData({
      myqr_blur: 0,
      myqr_show: false
    });
  },
  bindCommentRemark(e){
    this.setData({
      'comments.comment':e.detail.value
    }) 
  },
  goProblemList() {
    this.setData({
      navigateType: 1
    })
    wx.navigateTo({
      url: '/pages/problem/list/list',
    })
  },
  goUrge(e) { //催促办理 
    let sn = e.currentTarget.dataset.sn;
    let that = this;
    wx.showLoading();
    util.request(api.ProblemUrge, {
      ticketSn: sn
    }).then(function (res) {
      wx.hideLoading();
      if (res.errno === 0) {
        //设置为已催办
        let list = that.data.list;
        list.forEach(item => {
          if (item.ticketSn == sn) {
            item.status = "处理中-升级"
          }
        })
        that.setData({
          list: list
        })
        wx.showToast({
          title: '已催促负责人尽快办理',
          duration: 3000
        });
      }
    });
  },
  goDetail(e) {
    this.setData({
      navigateType: 0
    })
    let sn = e.currentTarget.dataset.sn;
    wx.navigateTo({
      url: '/pages/problem/action/action?ticketSn=' + sn,
    })
  },
  goCancel(e) { //取消报备 
    let that = this;
    let sn = e.currentTarget.dataset.sn;
    wx.showModal({
      title: '取消报备',
      content: '确定要取消此报备吗？',
      showCancel: true, //是否显示取消按钮
      cancelText: "否", //默认是“取消”
      cancelColor: 'skyblue', //取消文字的颜色
      confirmText: "是", //默认是“确定”
      confirmColor: 'skyblue', //确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          //点击确定
          util.request(api.ProblemCancel, {
            'ticketSn': sn
          }).then(res => {
            let list = that.data.list;
            list.forEach(item => {
              if (item.ticketSn == sn) {
                item.status = "作废";
                item.statusId = 3;
              }
            })
            that.setData({
              list: list
            })
            wx.showToast({
              title: '已取消成功',
              duration: 3000
            });
          })
        }
      },
      fail: function (res) {}, //接口调用失败的回调函数
      complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })

  },
  goQuickList() {
    this.setData({
      navigateType: 1
    })
    wx.navigateTo({
      url: '/pages/problem/quick/quick',
    })
  },
  getProblemList() {
    let that = this;
    wx.showLoading();
    util.request(api.ProblemList, {
      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
        });
      }
    });
  },
  switchTab: function (event) {
    let showType = event.currentTarget.dataset.index;
    console.log(showType);
    wx.setStorageSync('tab', showType);

    this.setData({
      list: [],
      showType: showType,
      page: 1,
      limit: 10,
      totalPages: 1
    });
    this.getProblemList();
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getProblemList();
    } else {
      wx.showToast({
        title: '没有更多报备了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },

  makeCall(e) {
    let mobile = e.currentTarget.dataset.mobile;
    wx.makePhoneCall({
      phoneNumber: mobile,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.hasLogin == false) {
      wx.navigateTo({
        url: '/pages/index/index',
      })
      return;
    }
    if (this.data.navigateType == 0 && this.data.list.length > 0) return;
    this.setData({
      list: [],
      showType: this.data.showType,
      page: 1,
      limit: 10,
      totalPages: 1,
      today: util.formatDate(new Date()),
    });
    this.getProblemList();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  }

})