import HTTP from "../../../../../requestFn/Api"
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: 'ZZFBZ-RK6L6-FPZS5-E3MBZ-63LS2-7WBJE' // 必填
});
Page({
  data: {
    garden_info: {},
    tabs: [{
        key: 1,
        text: '在租房源'
      },
    ],
    build_type: {
      0: "未填写",
      1: "塔楼",
      2: "板楼",
      3: "塔板结合",
      4: "其他"
    },
    status: 1,
    chooseItems: {
      0: [{
        name: '床',
        value: '1',
        selected: false
      }, {
        name: '天然气',
        value: '2',
        selected: false
      }, {
        name: '暖气',
        value: '3',
        selected: false
      }, {
        name: '宽带',
        value: '4',
        selected: false
      }, {
        name: '冰箱',
        value: '5',
        selected: false
      }, {
        name: '衣柜',
        value: '6',
        selected: false
      }, {
        name: '沙发',
        value: '7',
        selected: false
      }, {
        name: '空调',
        value: '8',
        selected: false
      }, {
        name: '电视',
        value: '9',
        selected: false
      }, {
        name: '热水器',
        value: '10',
        selected: false
      }, {
        name: '洗衣机',
        value: '11',
        selected: false
      }, ],
      1: [{
        name: '智能门锁',
        value: '1',
        selected: false
      }, {
        name: 'WIFI',
        value: '2',
        selected: false
      }, {
        name: '近地铁',
        value: '3',
        selected: false
      }, {
        name: '停车场',
        value: '4',
        selected: false
      }, {
        name: '独立卫生间',
        value: '5',
        selected: false
      }, {
        name: '私人阳台',
        value: '6',
        selected: false
      }, {
        name: '首次出租',
        value: '7',
        selected: false
      }, ],
    },
    tabList: [{
      text: '交通',
      key: "交通"
    }, {
      text: '商超',
      key: "商超"
    }, {
      text: '教育',
      key: "教育"
    }, {
      text: '餐饮',
      key: "餐饮"
    }, {
      text: '金融',
      key: "金融"
    }, {
      text: '医疗',
      key: "医疗"
    }, {
      text: '外卖',
      key: "外卖"
    }, ],
    keyword:"交通",
    longitude:0,
    latitude:0
  },
  onLoad(){
  },
  onShow(options) {
    const that = this;
    const eventChannel = that.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      const {
        garden_id,id,
        lat:latitude,
        lnt:longitude
      } = data.garden_info;
      that.setData({
        garden_info: {
          ...data.garden_info
        },
        latitude,longitude
      })
      that.getRenthouse(garden_id,id);
      that.nearby_search()
    })
  },
  onTabChange(e) {
    const {
      value
    } = e.detail;
    this.setData({
      status: value,
    });
  },
  getRenthouse(garden_id,id) {
    HTTP.RentHouse({
      page: 1,
      page_size: 5,
      garden_id:garden_id?garden_id:id,
      approve_status: 2,
    }).then(res => {
      this.setData({
        house_list: res.list,
      })
    })
  },
  tabChangeHandle(e) {
    const {value:keyword} = e.detail;
    this.setData({
      keyword,
    })
    this.nearby_search();
  },
  phoneManager() {
    wx.makePhoneCall({
      phoneNumber: this.data.garden_info.property_phone,
    })
  },
  appointmentHouse() {
    const {
      garden_info
    } = this.data;
    wx.navigateTo({
      url: '../appointment/appointment',
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          detail: garden_info
        })
      }
    })
  },
  goRenthouse(){
    const {garden_id,garden_name} =this.data.garden_info;
    wx.navigateTo({
      url: `../renthouse/renthouse?garden_id=${garden_id}&garden_name=${garden_name}`,
    })
  },
  nearby_search(){
    var _this = this;
    const {keyword,latitude,longitude} = _this.data;
    // 调用接口
    qqmapsdk.search({
       keyword,  //搜索关键词
       location: {
        latitude,longitude
       },
       success: function (res) { //搜索成功后的回调
         var mks = []
         for (var i = 0; i < res.data.length; i++) {
           mks.push({ // 获取返回结果，放到mks数组中
             title: res.data[i].title,
             id: i,
             latitude: res.data[i].location.lat,
             longitude: res.data[i].location.lng,
             width: 20,
             height: 20,
             callout: {
              content: res.data[i].title,
              color: '#333',
              fontSize: 14,
              padding: 8,
              display: 'BYCLICK',
              textAlign: 'center'
            }
           })
         }
         _this.setData({ //设置markers属性，将搜索结果显示在地图中
           markers: mks
         })
       },
       fail: function (res) {
         wx.showToast({
           title: '查询失败',
           icon:"none"
         })
       },
   });
 },
 goMapPlan() {
  const {garden_addr,lat,lnt} = this.data.garden_info;
 wx.openLocation({
   latitude:lat,
   longitude:lnt,
   address:garden_addr,
   scale: 18,
   fail(err){
     wx.showToast({
       title: '请再次尝试',
       icon:"none"
     })
   }
 })
},
})