// pages/my/my.ts
const app = getApp<IAppOption>()
import { bindPhoneList } from "../../apis/consumer";
import { info, cleanPhoneDate, labelStatistics } from "../../apis/phone";
import { list } from "../../apis/phone";
import { sysConfig } from "../../apis/config";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    dialogRecords: false,
    dialogAddReport: false,
    dialogInvite: false,
    dialogShare: false,
    dialogSwitchAccount: false,
    dialogCommentRecord: false,
    dialogTagRecord: false,
    commentRecordList: []
  },
  handleTagRecord() {
    const { checkCondition } = this.data
    const size = this.data.userInfo.evaluatedTimes
    if (size < checkCondition) {
      this.setData({
        dialogRecords: true
      })
    } else {
      this.setData({
        dialogTagRecord: true
      })
      wx.setPageStyle({
        style: {
          overflow: 'hidden'
        }
      })
      this.getTagRecord()
    }
  },
  dialogTagRecordClose() {
    this.setData({
      dialogTagRecord: false
    })
  },
  getTagRecord() {
    labelStatistics(this.data.userInfo.phone).then(res => {
      // console.log(res);
      let total = 0;
      res.d.map(item => {
        total = total + Number(item.contentCount)
      })
      this.setData({
        tagRecordList: res.d,
        tagRecordTotal: total
      })
    })
  },
  closeDialogShare() {
    this.setData({
      dialogShare: false
    })
  },
  showDialogShare() {
    this.setData({
      dialogShare: true,
      dialogRecords: false,
    })
  },
  getSysConfig() {
    sysConfig().then((res: any) => {
      this.setData({
        checkCondition: res.d.checkCondition,
        money: res.d.refreshAmount / 100
      })
    })
  },
  handleShare() {
    // wx.showShareImageMenu({  //分享给朋友
    //   // path,
    //   url: '/assets/images/my/icon_question.png',
    //   success: (res) => {
    //     console.log("分享成功：", res);
    //   },
    //   fail: (err) => {
    //     console.log("分享失败：", err);
    //     wx.showToast({
    //       title: "分享失败",
    //       duration: 2000
    //     })
    //   },
    // })
  },
  getBindPhoneList(phone: String) {
    bindPhoneList().then((res: any) => {
      // console.log('##', phone);

      // console.log(res.d.find((item: any) => item.phone === phone));

      this.setData({
        accountList: res.d,
        userInfo: res.d.find((item: any) => item.phone === phone)
      })
      this.getInfo()
    })
  },
  closeDialogSwichAccount() {
    this.setData({
      dialogSwitchAccount: false
    })
  },
  getInfo() {
    info({ phone: this.data.userInfo.phone, type: 0 }).then((res: any) => {
      this.setData({
        consumer: res.d
      })
    })
  },
  handleRecords() {
    const { checkCondition } = this.data
    const size = this.data.userInfo.evaluatedTimes
    if (size === 0) {
      this.setData({
        dialogShare: true
      })
    } else if (size < checkCondition) {
      this.setData({
        dialogRecords: true
      })
    } else {
      this.setData({
        dialogCommentRecord: true
      })
      wx.setPageStyle({
        style: {
          overflow: 'hidden'
        }
      })
      this.getCommentRecords()
    }
  },
  dialogRecordsClose() {
    this.setData({
      dialogRecords: false
    })
  },
  dialogAddReportShow() {
    this.setData({
      dialogAddReport: true
    })
  },
  dialogAddReportClose() {
    this.setData({
      dialogAddReport: false
    })
  },
  handleClear() {
    this.selectComponent('#shadow2').show()
  },
  goPay() {
    wx.navigateTo({
      url: `/pages/pay/pay?phone=${this.data.userInfo.phone}`
    })
    // return new Promise((resolve, reject) => {
    //   cleanPhoneDate(this.data.userInfo.phone).then((res: any) => {
    //     wx.requestPayment({
    //       ...res.d,
    //       success: (res: any) => {
    //         resolve(res)
    //       },
    //       fail: (err: any) => {
    //         wx.showToast({
    //           icon: 'none',
    //           title: '支付失败, 请稍后再试!'
    //         })
    //         reject(err)
    //       }
    //     })
    //   })
    // })
  },
  dialogInviteShow() {
    console.log('dialogInviteShow');

    this.setData({
      dialogInvite: true
    })
  },
  closeInvite() {
    console.log('closeInvite');
    this.setData({
      dialogInvite: false
    })
  },
  showDialogSwitchAccount() {
    this.setData({
      dialogSwitchAccount: true
    })
  },
  goReports(e: WechatMiniprogram.BaseEvent) {
    console.log('goReports', e);

    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/report/report?phone=${this.data.userInfo.phone}&id=${id}`,
    })
    this.setData({
      dialogAddReport: false
    })
  },
  accountChange(e: WechatMiniprogram.BaseEvent) {
    const { accountList } = this.data
    let accList = accountList.find((item: any) => item.id === e.currentTarget.dataset.id)
    this.setData({
      userInfo: accList,
      dialogSwitchAccount: false
    })
    wx.setStorageSync("userInfo", accList)
    this.getInfo()
  },
  goBind() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },
  share() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  showTagList() {
    this.setData({
      dialogCommentRecord: true
    })
    wx.setPageStyle({
      style: {
        overflow: 'hidden'
      }
    })
    this.getCommentRecords()
  },
  dialogCommentRecordClose() {
    this.setData({
      dialogCommentRecord: false
    })
    wx.setPageStyle({
      style: {
        overflow: 'auto'
      }
    })
  },
  getCommentRecords() {
    let payload = {
      current: 1,
      size: 99999,
      phone: this.data.userInfo.phone
    }
    list(payload).then(res => {
      console.log(res);
      this.setData({
        commentRecordList: res.d.records
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({
      now: Math.floor(new Date().getTime() / 1000)
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
      accountList: wx.getStorageSync('accountList')
    })
    this.getInfo()
    this.getSysConfig()
  },

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

  },

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

  },

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

  },

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

  },
  sharePage() {
    // wx.showShareImageMenu({  //分享给朋友
    //   path:'/assets/images/my/sharePic.png',
    //   success: (res) => {
    //     console.log("分享成功：", res);
    //   },
    //   fail: (err) => {
    //     console.log("分享失败：", err);
    //     wx.showToast({
    //       title: "分享失败",
    //       duration: 2000
    //     })
    //   },
    // })
    // wx.showActionSheet({
    //   itemList: ['A', 'B', 'C'],
    //   success (res) {
    //     console.log(res.tapIndex)
    //   },
    //   fail (res) {
    //     console.log(res.errMsg)
    //   }
    // })
    // wx.onAppRoute((res) => {
    //   // console.log('route', res)
    //   let pages = getCurrentPages()
    //   let view = pages[pages.length - 1]
    //   if(view) {
    //     wx.showShareMenu({
    //       menus: ['shareAppMessage', 'shareTimeline'],
    //       success(res) {},
    //       fail(e) {}
    //     })
    //   }
    // })
    // console.log(2);
    wx.showShareMenu({
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log(res);
      },
      fail(e) {
        console.log(e);
      }
    })
  },
  goShareTimeLine() {
    wx.navigateTo({
      url: "/pages/shareTimeLine/shareTimeLine?active=false"
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: '您对我的印象评价是什么？',
          path: `/pages/searchResult/searchResult?phone=${this.data.userInfo.phone}`,
        })
      })
    })
    return {
      title: '您对我的印象评价是什么？',
      path: '/pages/login/login',
      promise
    }
  },
  // onShareTimeline() {
  //   const promise = new Promise(resolve => {
  //     setTimeout(() => {
  //       resolve({
  //         title: '你的印象评价是什么？',
  //         path: `/pages/shareTimeLine/shareTimeLine`,
  //       })
  //     })
  //   })
  //   return {
  //     title: '你的印象评价是什么？',
  //     path: '/pages/login/login',
  //     promise
  //   }
  // }
})
