import AgentApi from '../../apis/agent'
import HouseApi from '../../apis/house'
import { loginByUserInfo } from '../../utils/login'
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    agentDetail: {},
    showCard: false,
    house_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { agency_id } = options
    this.fetchAgentDetail(agency_id)
    this.fetchRecommendList()
  },
  fetchAgentDetail (agent_user_id) {
    AgentApi.fetchAgentDetail(agent_user_id).then(res => {
      this.setData({
        agentDetail: res.data
      })
    })
  },
  onShow () {
    this.setData({
      loginStatus: getApp().globalData.token ? true : false
    })
  },
  
  showWorkCard () {
    this.setData({
      showCard: true
    })
  },
  hideWorkCard () {
    this.setData({
      showCard: false
    })
  },
  makePhone () {
    wx.makePhoneCall({
      phoneNumber: this.data.agentDetail.phone,
    })
  },
  fetchRecommendList () {
    HouseApi.houseList({saleState: 1, city: app.globalData.city}).then(res => {
      this.setData({
        house_list: res.data.list
      })
    })
  },
  toChat () {
    if (!getApp().globalData.token) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    wx.navigateTo({
      url: `/pages/chat/chat?toId=${this.data.agentDetail.agency_user_id}&toName=${this.data.agentDetail.name}`,
    })
  },
  bindgetuserinfo (e) {
    const { action } = e.currentTarget.dataset
    loginByUserInfo().then(res => {
      this.setData({
        loginStatus: app.globalData.token ? true : false,
        userinfo: app.globalData.userinfo,
      })
      this[action] && this[action]()
    })
  }
})