import HouseApi from './../../apis/house'
import ImApi from './../../apis/im'
import { randomString } from './../../utils/tools'
import { fotmatDate } from './../../utils/date'
import ImSend from '../../utils/imSend'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    house: {},
    house_id: '',
    customers: [],
    cardShowStatus: false,
    selectedCustomer: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getHouseDetail(options.house_id)
    this.getMessageList()
  },
  toCustomerList () {
    wx.navigateTo({
      url: `/pages/customer-list/customer-list?house_id=${this.data.house_id}`,
    })
  },
  getHouseDetail (house_id) {
    HouseApi.houseDetail(house_id).then(res => {
      res.data.detail_images = JSON.parse(res.data.detail_images)
      this.setData({
        house: res.data,
        house_id: house_id
      })
    })
  },
  confirmInvite (e) {
    const { index } = e.currentTarget.dataset
    this.setData({
      cardShowStatus: true,
      selectedCustomer: this.data.customers[index]
    })
  },
  submit (e) {
    if (e.detail) {
      ImSend.sendTextMsg(e.detail,  this.data.selectedCustomer.user_id)
    }
    const room_id = randomString(18)
    const { house } = this.data
    console.log(this.data.selectedCustomer)
    this.postDataToOpen(room_id).then(res => {
      // const vr_link = res.data.vrLink
      ImSend.sendVrMsg(house, res.data.roomId, this.data.selectedCustomer.id, true)
    })
    this.setData({
      cardShowStatus: false
    })
  },
  sendVrMsg () {
   
  },
  postDataToOpen (room_id) {
    return HouseApi.postDataToOpen({house_id: this.data.house_id, room_id: '', type: 'customer'})
  },
  cancle () {
    this.setData({
      cardShowStatus: false
    })
  },
  getMessageList () {
    return ImApi.getContacts().then(res => {
      this.customers = this.dealConversations(res.data.friends)
      this.setData({
        customers: this.customers
      })
      
    })
  },
  dealConversations (conversations) {
    return conversations.map(item => {
        let content = item.latestMsgContent
        try {
            let parseContent = JSON.parse(content)
            if (parseContent.house_name) {
                content = `【云带看】${JSON.parse(content).house_name}`
            } else if (parseContent.duration) {
                content = '【语音】'
            } else if (parseContent.content) {
                content = '【图片】'
            }
            
        } catch (err) {
        }
        item.latestMsgContent = content
        item.latestMsgTime = fotmatDate(item.latestMsgTime, 'yyyy-MM-dd hh:mm:ss')
        return item
    }).sort((a,b) => new Date(b.latestMsgTime) - new Date(a.latestMsgTime))
},
  sendVrMsg () {
    const room_id = randomString(18)
    const { house } = this.data
    ImSend.sendVrMsg(house, room_id, this.data.selectedCustomer.id, true)
  },
  sendMsg ({content, msgType}) {
    let defaultContent = {
      fromId: app.globalData.userinfo.user_id,
      fromName: app.globalData.userinfo.phone,
      toId: this.data.selectedCustomer.id, //this.properties.agent_user.agency_user_id,
      toName: this.data.selectedCustomer.name,
      type: 'TYPE_ONE',
      msgType,
      content: content
    }
    console.log(getApp().getIMHandler())
    return getApp().getIMHandler().newFriendSendMsg({content: defaultContent})
  },
  bindinput (e) {
    const { value } = e.detail
    this.keyword = value
  },
  filterMessage () {
    this.setData({
      customers: this.customers.filter(item => item.name.indexOf(this.keyword) !== -1)
    })
  }
})