//index.js
//获取应用实例
const app = getApp()

const page = require('../../utils/page.js');
const HttpSever = require('../../utils/http.js').HttpSever;
const $api = require('../../config/api.js').$api;

Page({
  onShow() {
    wx.setNavigationBarTitle({
      title: '楼盘详情'
    })
  },
  onLoad: function (options) {
    const id = page.getCurrentPageOptions().id
    const _that = this
    HttpSever.Ajax({
      url: $api.building.detail.url.replace(':id', id),
      method: $api.building.detail.method,
    }).then((data) => {
      const dicts = app.globalData.Dict
      const apart = data.data.buildingApartment.map(x => {
        return {
          ...x,
          imgUrl: (x.buildingImgList[0] || {}).url,
          marketStatus: (dicts.filter(d => d.id == x.marketStatus)[0] || {}).name
        }
      })
      let propertyType = ''
      data.data.propertyType.split(',').map(x => {
        const f = dicts.filter(d => d.id == x)
        if (f.length > 0) {
          propertyType += (propertyType === '' ? '' : ',') + f[0].name
        }
      })
      _that.setData({
        EstateDetail: { ...data.data, propertyType: propertyType, buildingApartment: apart}
      })
    })
  },
  data: {
    UserInfo: app.globalData.UserInfo,
    ScreenBottomHeight: app.globalData.ScreenBottomHeight,
    EstateDetail: {}
  },
  MakeAnAppointment() { // 预约看房
    wx.navigateTo({
      url: '/pages/AppointmentTime/index?id=' + this.data.EstateDetail.buildingId + '&name=' + this.data.EstateDetail.buildingName
    })
  },
  OpenPhone() { // 电话咨询
    let _self = this
    wx.makePhoneCall({
      phoneNumber: _self.data.EstateDetail.PhoneNum
    })
  }
})