import {
    getMsgDet,
    sendMsg,
    getNewMsgDet
} from "~/api/message"
import {
    createStoreBindings
} from 'mobx-miniprogram-bindings'
import event from '~/mixins/event'
import {
    store
} from '~/store/index'
let interval = null
const app = getApp()
Page({
    behaviors: [event],
    data: {
        targetUid: '',
        value: '',
        list: [],
        pageNo: 1,
        totalNo: 1,
        scrollTop: 0,
        triggered: false,
        isIos: app.globalData.isIOS,
        navBarHeight: app.globalData.navBarHeight,
        uid: ''
    },
    onLoad(options) {
        wx.setNavigationBarTitle({
            title: options.title,
        })
        this.setData({
            targetUid: options.uid,
            uid: wx.getStorageSync('uid')
        })
        this.storeBindings = createStoreBindings(this, {
            store,
            fields: {
                userInfo: 'userInfo'
            },
        })
        this.storeBindings.updateStoreBindings()
        this.getMsgDet().then(() => {
            this.setData({
                scrollTop: 10000
            })
        })
        this.interval = setInterval(() => {
            this.getNewMsgDet()
        }, 5000)
    },
    getMsgDet() {
        return new Promise(async (reslove) => {
            let pageNo = this.data.pageNo
            if (this.data.totalNo < pageNo) {
                return this.setData({
                    triggered: false,
                })
            }
            let data = await getMsgDet({
                senderUid: this.data.targetUid,
                pageNo,
                pageSize: 10
            })
            let {
                list,
                totalNo
            } = data
            this.setData({
                list: [...list, ...this.data.list],
                totalNo,
                pageNo: totalNo >= pageNo ? ++pageNo : pageNo,
                triggered: false,
            })
            reslove()
        })
    },
    async getNewMsgDet() {
        let res = await getNewMsgDet({
            senderUid: this.data.targetUid,
        })
        let newList = [...this.data.list, ...res]
        this.setData({
            list: newList,
        })
    },
    async sendReply() {
        if (!this.data.value) {
            return
        }
        await sendMsg({
            content: this.data.value,
            type: '1',
            receiverUid: this.data.targetUid
        })
        this.setData({
            value: '',
            scrollTop: this.data.list.length * 1000
        })
        this.getNewMsgDet()
    },
    previewImage(e) {
        var imageUrl = e.currentTarget.dataset.src;
        wx.previewImage({
            urls: [imageUrl]
        })
    },
    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,
            name: '朗读录音',
            header: {
                uid: wx.getStorageSync('uid')
            },
            success: async (res) => {
                await sendMsg({
                    content: JSON.parse(res.data).data,
                    type: '2',
                    receiverUid: this.data.targetUid
                })
                this.getNewMsgDet()
                this.setData({
                    scrollTop: this.data.list.length * 1000
                })
            }
        })
    },
    bindKeyInput(e) {
        this.setData({
            value: e.detail.value
        })
    },
    jumpUserInfo({
        currentTarget
    }) {
        wx.navigateTo({
            url: `/pages/personal/index?uid=${currentTarget.dataset.uid}&type=user`,
        })
    },
    onUnload() {
        clearInterval(this.interval)
    }
})