const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
Page({
  data: {
    isshow1: false,
    isshow2: true,
    activebg: "#ffeae9",
    unactivebg: "#f2f2f2",
    mytip: "",
    listArr: []
  },
  // tab1
  tab1Show() {
    this.setData({
      isshow1: false,
      isshow2: true,
      activebg: "#ffeae9",
      unactivebg: "#f2f2f2"
    })
  },
  //tab2
  async tab2Show() {
    const sessionKey = await tools.checkSessionAndLogin();
    wx.request({
      url: app.globalData.publicUrl + '/wx/board/list',
      method: "post",
      data: {
        sessionKey: sessionKey
      },
      success: (res) => {
        if (res.data.code == 0) {
          if (res.data.list.length == 0) {
            wx.showModal({
              showCancel: false,
              content: "您还没有提问哦"
            })
          } else {
            this.setData({
              listArr: res.data.list,
              isshow1: true,
              isshow2: false,
              activebg: "#f2f2f2",
              unactivebg: "#ffeae9"
            })
          }
        } else {
          wx.showModal({
            showCancel: false,
            content: "您还没有提问哦"
          })
        }
      }
    })
  },
  // 去解答的详情页
  gojjDetails(e) {
    let id = e.currentTarget.dataset.item.id
    if (e.currentTarget.dataset.item.replyContent == "" || e.currentTarget.dataset.item.replyContent == null) {
      wx.showModal({
        showCancel: false,
        content: "您还没收到回复哦"
      })
    } else {
      wx.navigateTo({
        url: '/pages/myInteractionsDetais/myInteractionsDetais?id=' + id,
      })
    }

  },
  // 禁止input框输入表情
  regStrFn(str) {
    // 转换一下编码
    let reg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n])|(\s)/g,
      indexArr = reg.exec(str);
    if (str.match(reg)) {
      str = str.replace(reg, '');
    }
    let obj = {
      val: str,
      index: indexArr
    }
    return obj
  },
  bindinput(e) {
    // let name = 'form.' + e.target.dataset.name
    let val = e.detail.value,
      pos = e.detail.cursor;
    let reg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n])|(\s)/g
    if (!reg.test(val)) {
      return
    }
    let obj = this.regStrFn(val)
    if (pos != -1 && obj.index) {
      //计算光标的位置
      pos = obj.index.index
    }
    return {
      value: obj.val,
      cursor: pos
    }
  },
  // 提交内容
  async bindFormSubmit(e) {
    wx.showLoading({
      mask: true,
      title: '加载中',
    })
    let input_val = e.detail.value.textarea;
    const sessionKey = await tools.checkSessionAndLogin();
    if (input_val == "" || input_val == undefined) {
      wx.hideLoading();
      wx.showToast({
        title: '反馈不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    wx.request({
      url: app.globalData.publicUrl + '/wx/board/add',
      method: "post",
      data: {
        content: input_val,
        sessionKey: sessionKey
      },
      success: (res) => {
        console.log(res,"00000000000")
        if (res.data.code == 0) {
          wx.hideLoading()
          wx.showModal({
            showCancel: false,
            content: "提交成功",
            success: (res) => {
              if (res.confirm) {
                this.setData({
                  mytip: ""
                })
              }
            }
          })
        } else {
          wx.hideLoading()
          wx.showModal({
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  }
})