const app = getApp();
import HTTP from "../../../../../requestFn/Api"
var isPerview
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shows: false,
    indexs: 0,
    has_face_device: false,
  },

  onShow(options) {
    if (this.data.isPerview) {
      this.setData({
        isPerview: false
      })
      return;
    } else {
      const {
        house_list
      } = app.globalData;
      let garden_list = [];
      let obj = {};
      let garden_id
      for (var i = 0; i < house_list.length; i++) {
        if (!obj[house_list[i].garden_id]) {
          garden_list.push(house_list[i]);
          obj[house_list[i].garden_id] = true;
        }
        if(house_list[i].selected){
          garden_id=house_list[i].garden_id
        }
      }
      if (garden_list.length > 0) {
        this.setData({
          garden_list,
          garden_id
        })
        this.If_Face();
        this.Face();
      }
    }
  },
  selectTaps() {
    this.setData({
      shows: !this.data.shows,
    });
  },
  // 点击下拉列表
  bindPickerChange(e) {
    const indexs = e.detail.value;
    const {
      shows,
      garden_id,
      garden_list
    } = this.data;
    const newID = garden_list[indexs].garden_id
    if (garden_id !== newID) {
      this.setData({
        indexs,
        shows: !shows,
        garden_id: newID,
      });
      this.If_Face();
      this.Face();
    }else{
      this.selectTaps();
    }
  },
  async If_Face() {
    const {
      garden_id
    } = this.data;
    const {
      has_face_device
    } = await HTTP.Has_Face({
      garden_id
    })
    this.setData({
      has_face_device
    })
  },
  async Face() {
    const {
      garden_id
    } = this.data;
    const {
      pic_url,
      approve_status,
      down_status,
      feedback
    } = await HTTP.Face({
      garden_id,
    })
    this.setData({
      pic_url,
      approve_status,
      down_status,
      feedback
    })
    console.log(pic_url);
  },
  goVK() {
    wx.navigateTo({
      url: `/page/message/pages/VKSession?garden_id=${this.data.garden_id}`,
    })
  },
  previewImage() {
    this.setData({
      isPerview: true
    })
    wx.previewImage({
      urls: [this.data.pic_url],
    })
  },
})