import {
  getMsgDet,
  sendMsg,
  getNewMsgDet
} from "~/api/message"
import {
  createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
  store
} from '~/store/index'
const app = getApp()
Page({
  data: {
    targetUid: '',
    value: '',
    list: [],
    totalSize: 0,
    isIos: app.globalData.isIOS,
    uid: wx.getStorageSync('uid')
  },
  onLoad(options) {
    console.log(options);
    wx.setNavigationBarTitle({
      title: options.title,
    })
    this.setData({
      targetUid: options.uid
    })
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: {
        userInfo: 'userInfo'
      },
    })
    this.storeBindings.updateStoreBindings()
    this.getMsgDet()
    this.getNewMsgDet()
  },
  async getMsgDet() {
    let data = await getMsgDet({
      senderUid: this.data.targetUid,
      pageNo: 1,
      pageSize: 10
    })
    let {
      list,
      totalSize
    } = data
    this.setData({
      list,
      totalSize
    })
    console.log(data, this.data.userInfo);
  },
  async getNewMsgDet() {
    let res = await getNewMsgDet({
      senderUid: this.data.targetUid,
    })
    console.log(res);
  },
  async sendReply() {
    if (!this.data.value) {
      return
    }
    await sendMsg({
      content: this.data.value,
      receiverUid: this.data.targetUid
    })
  },

  chooseImage() {
    wx.chooseImage({
      count: 1, // 可选择的图片数量
      sizeType: ['compressed'], // 压缩图片
      sourceType: ['album', 'camera'], // 来源:相册或相机
      success: (res) => {
        // 将选择的图片上传到服务器
        this.uploadImage(res.tempFilePaths[0]);
      }
    })
  },
  uploadImage(imagePath) {
    wx.uploadFile({
      url: 'https://reader-api.ai160.com/file/upload',
      filePath: imagePath,
      header: {
        uid: wx.getStorageSync('uid')
      },
      success: (res) => {
        console.log(res.data);
      }
    })
  },
  bindKeyInput(e) {
    this.setData({
      value: e.detail.value
    })
  },
  jumpUserInfo({
    currentTarget
  }) {
    wx.navigateTo({
      url: `/pages/personal/index?uid=${currentTarget.dataset.uid}&type=user`,
    })
  },
})