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' // 必填
});
const app = getApp();
import openIM from "../../../../../utils/openim"
Page({
  data: {
    detail: {},
    current: 0,
    autoplay: true,
    duration: 500,
    interval: 5000,
    garden_id: null,
    pay_time_type: {
      1: '月付',
      2: '季付',
      3: '半年付',
      4: '年付'
    },
    direction: {
      1: '东',
      2: '南',
      3: '西',
      4: '北'
    },
    rent_type: {
      1: '整租',
      2: '合租',
      3: '转租'
    },
    special_conf: {
      1: '智能门锁',
      2: 'WIFI',
      3: '近地铁',
      4: '停车位',
      5: '独卫',
      6: '私人阳台',
      7: '首次出租'
    },
    base_conf: {
      1: {
        name: "床",
        icon: "/static/base_conf/ic_bedroom.png"
      },
      2: {
        name: "天然气",
        icon: "/static/base_conf/meiqitianranqi.png"
      },
      3: {
        name: "暖气",
        icon: "/static/base_conf/nuanqi.png"
      },
      4: {
        name: "宽带",
        icon: "/static/base_conf/kuandai.png"
      },
      5: {
        name: "冰箱",
        icon: "/static/base_conf/bingxiang.png"
      },
      6: {
        name: "衣柜",
        icon: "/static/base_conf/yigui.png"
      },
      7: {
        name: "沙发",
        icon: "/static/base_conf/shafa.png"
      },
      8: {
        name: "空调",
        icon: "/static/base_conf/kongtiao.png"
      },
      9: {
        name: "电视机",
        icon: "/static/base_conf/dianshiji-.png"
      },
      10: {
        name: "热水器",
        icon: "/static/base_conf/reshuiqi.png"
      },
      11: {
        name: "洗衣机",
        icon: "/static/base_conf/xiyiji.png"
      },
    },
    manager_list: [],
    garden_info: {},
    total: 0,
    tabList: [{
      text: '交通',
      key: "交通"
    }, {
      text: '商超',
      key: "商超"
    }, {
      text: '教育',
      key: "教育"
    }, {
      text: '餐饮',
      key: "餐饮"
    }, {
      text: '金融',
      key: "金融"
    }, {
      text: '医疗',
      key: "医疗"
    }, {
      text: '外卖',
      key: "外卖"
    }, ],
    renthouse_list: [],
    bottom: 0,
    keyword: "交通",
    longitude: 0,
    latitude: 0
  },
  onLoad() {
    const {
      screenHeight,
      safeArea
    } = app.globalData.systemInfo;
    const {
      bottom
    } = safeArea;
    let safeHeight = screenHeight - bottom;
    this.setData({
      bottom: safeHeight
    });
  },
  onShow(options) {
    const that = this;
    const eventChannel = that.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      const strData = JSON.stringify(data);
      const {
        garden_id,
        id,
        lat: latitude,
        lnt: longitude
      } = data.data[0]
      that.setData({
        detail: {
          ...data.data[0],
          rent_id: data.data[0].id
        },
        latitude,
        longitude,
        strData
      })
      that.getHouse_manager(garden_id, id);
      that.getGardenInfo(garden_id);
      that.getRentHouse(garden_id);
      that.nearby_search()
    })
  },
  previewImage(e) {
    wx.previewImage({
      current: this.data.detail.house_pic[e.currentTarget.dataset.index],
      urls: this.data.detail.house_pic,
    });
  },
  getHouse_manager(garden_id, rent_id) {
    HTTP.HouseManager({
      garden_id,
      rent_id
    }).then(res => {
      this.setData({
        manager_list: res.list
      })
    })
  },
  callManager(e) {
    const {
      id
    } = e.currentTarget;
    const {
      name,
      phone
    } = this.data.manager_list.filter(item => item.id == id)[0]
    wx.navigateTo({
      url: `../manager/manager?name=${name}&phone=${phone}`,
    })
  },
  getGardenInfo(garden_id) {
    HTTP.GardenInfo({
      garden_id
    }).then(res => {
      this.setData({
        garden_info: res
      })
    })
  },
  async getRentHouse(garden_id) {
    let data = await HTTP.RentHouse({
      garden_id,
      approve_status: 2,
      page: 1,
      page_size: 9999
    })
    const {
      list,
      total
    } = data;
    let newarr = list.slice(0, 5)
    this.setData({
      total: total,
      renthouse_list: newarr
    })
  },
  goRentHouse() {
    // const {
    //   garden_id,
    //   garden_name
    // } = this.data.detail;
    // let pages = getCurrentPages();
    // let prevPage = pages[pages.length - 2];
    // prevPage.setData({
    //   searchData: {
    //     garden_id
    //   }
    // })
    wx.navigateTo({
      url: '../renthouse/renthouse',
    })
  },
  tabChangeHandle(e) {
    const {
      value: keyword
    } = e.detail;
    this.setData({
      keyword,
    })
    this.nearby_search();
  },
  phoneManager() {
    wx.makePhoneCall({
      phoneNumber: this.data.detail.contact_phone,
    })
  },
  CallManager(e) {
    const {
      phone: phoneNumber
    } = e.currentTarget.dataset;
    wx.makePhoneCall({
      phoneNumber,
    })
  },
  garden_detail() {
    const {
      garden_info,
      detail
    } = this.data;
    wx.navigateTo({
      url: '../garden-detail/garden-detail',
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          garden_info: {
            ...detail,
            ...garden_info,
          }
        })
      }
    })
  },
  appointmentHouse() {
    const that = this;
    wx.navigateTo({
      url: '../appointment/appointment',
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          detail: {
            ...that.data.detail,
            rent_id: that.data.detail.id
          }
        })
      }
    })
  },
  goHouseDetail(e) {
    const {
      house
    } = e.currentTarget.dataset;
    let data = [house]
    wx.navigateTo({
      url: '../detail/detail',
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data
        })
      }
    })
  },
  goMapPlan() {
    const {
      addr,
      lat,
      lnt
    } = this.data.detail;
    // let plugin = requirePlugin('routePlan');
    // let key = 'ZZFBZ-RK6L6-FPZS5-E3MBZ-63LS2-7WBJE'; //使用在腾讯位置服务申请的key
    // let referer = '易小居'; //调用插件的app的名称
    // let endPoint = JSON.stringify({ //终点
    //   'name': addr,
    //   'latitude': lat,
    //   'longitude': lnt
    // });
    // wx.navigateTo({
    //   url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
    // })
    // wx.navigateTo({
    //   url: `../map/map?lat=${lat}&lnt=${lnt}`,
    // })
    wx.openLocation({
      latitude: lat,
      longitude: lnt,
      address: addr,
      scale: 18,
      fail(err) {
        wx.showToast({
          title: '请再次尝试',
          icon: "none"
        })
      }
    })
  },
  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"
        })
      },
    });
  },
  goChat() {
    const that =this;
    const {
      manager_list,
      strData: cardDesc,
    } = that.data;
    const itemList = manager_list.map(item => item.name);
    wx.showActionSheet({
      itemList,
      itemColor: "#03BB7A",
      success(res) {
       app.sendCard(cardDesc,that,res);
      }
    })
  }
})