import HouseApi from '../../apis/house'


const app = getApp();
const BASE = require('../../utils/basic');
Page({
  data: {
    agent_user: {},
    house_id: ''
  },
  makePhoneCall(e){
    app.makePhoneCall(e.currentTarget.dataset.phone);
  },
  ShureAppointment(e){
  },
  make_call(e){
    if(!app.globalData.user_id){
      this.ShowToast("请先完成授权");
      return false
    }
    app.makePhoneCall(e.currentTarget.dataset.phone);
  },

  swiperchange: function (detail) {
    if (detail.detail.source == "touch") {
      //当页面卡死的时候，current的值会变成0
      if(detail.detail.current == 0){
        //有时候这算是正常情况，所以暂定连续出现3次就是卡了
        let swiperError = this.data.swiperError;
        swiperError += 1;
        this.setData({swiperError: swiperError });
        if (swiperError >= 3) { //在开关被触发3次以上
          this.setData({ currentIndex: this.data.preIndex });//，重置current为正确索引
          this.setData({swiperError: 0})
        }
      }else {//正常轮播时，记录正确页码索引
        this.setData({ preIndex: detail.detail.current });
        //将开关重置为0
        this.setData({swiperError: 0})
      }
    }
  },
  HandleGetDone(res){
    wx.hideLoading()
  },
  onShareAppMessage: function (res) {
    var path ='/pages/detail/detail?house_id=' + this.data.house_id;
    var arr = getApp().globalData.last_house.detail_images;
    var imageurl = arr[0];
    console.log(imageurl)
    if (getApp().globalData.token) {
      this.addSharedHistory()
    }
    return {
      title: getApp().globalData.last_house.title,
      path: path,
      imageUrl: imageurl, // 分享的封面图
      success: () => {
        
        // 转发成功
      },
      fail: function(err) {
        console.log(err)
        app.ShowModel('网络错误', '转发失败~');
        // 转发失败
      }
    }
    
  },
  ReturnClick(){
    wx.navigateBack({//返回
      delta: 1
    })
  },
  onShow:function(e){
  },
  onLoad: function (options) {
    app.wxshowloading('拼命加载中...');
    const house_id = options.house_id
    this.setData({
      house_id
    })
    this.getHouseAgent(house_id)
    
  },
  getHouseAgent (house_id) {
    HouseApi.getAgentByHouseId(house_id).then(res => {
      if (res.data && res.data[0]) {
        this.setData({
          agent_user: res.data[0]
        })
      }
    })
  },

  // 增加分享记录
  addSharedHistory () {
    HouseApi.addSharedHistory({house_id: this.data.house_id, sale_type: getApp().globalData.last_house.sale_type})
  }
});
