import HouseApi from './../../apis/house'
import agentApi from '../../apis/agent'
import { randomString } from './../../utils/tools'
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
    console.log(index, this.data.clients)
    this.setData({
      cardShowStatus: true,
      selectedCustomer: this.data.clients[index]
    })
  },
  submit (e) {
    if (e.detail) {
      ImSend.sendTextMsg(e.detail,  this.data.selectedCustomer.user_id)
    }
    this.sendVrMsg()
    
    this.setData({
      cardShowStatus: false
    })
  },
  cancle () {
    this.setData({
      cardShowStatus: false
    })
  },
  getMessageList () {
    this.setData({
      'fetcherData.agency_user_id': app.globalData.userinfo.agency_user_id
    })
    app.wxshowloading('拼命加载中...');
    agentApi.fetchCustomers(this.data.fetcherData).then(res => {
        this.setData({
            clients: res.data.list
        })
        console.log(res.data)
    }).finally(() => {
        wx.hideLoading()
    })
  },
  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
        return item
    }).sort((a,b) => new Date(b.latestMsgTime) - new Date(a.latestMsgTime))
},
  sendVrMsg () {
    const room_id = randomString(18)
    const { house } = this.data
    this.postDataToOpen(room_id).then(res => {
      const vr_link = res.data.vrLink
      ImSend.sendVrMsg(house, res.data.roomId, this.data.selectedCustomer.user_id, true)
    })
  },
  postDataToOpen (room_id) {
    return HouseApi.postDataToOpen({house_id: this.data.house_id, room_id: '', type: 'customer'})
  },
  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
    }
    return getApp().getIMHandler().newFriendSendMsg({content: defaultContent})
  },
  bindinput (e) {
    const { value } = e.detail
    this.setData({
      'fetcherData.user_name': value
    })
    // this.getMessageList()
  },
  searchbtnclick () {

  }
})