// pages/select_library/select_library.js
import { routers, viewImage, sharePage, isFn, getGlobalVal, formatDateTime, formateNumber } from '../../utils/util.js'
import { LibList} from '../../utils/api.js'
const { navigateTo, redirectTo, navigateBack } = routers()
const { globalData } = getApp()
const { baseImgUrl, thumbnail } = globalData
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaIndex:0,
    cityName:'',
    nodata: {
      nodataObj: {
        text: '没有搜索结果'
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData(options);
    this.getLibList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const obj = { navigateTo, redirectTo, navigateBack, viewImage }
    for (const i in obj) {
      this[i] = obj[i]
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
  * 跳转
  */
  navigateCtl: app.navigateCtl,
  /**
   * 底部导航跳转
   */
  redirectCtl: app.redirectCtl,
  /**
   * 保存学生姓名
   */
  inputCtl: function (e) {
    const { value } = e.detail
    const { cityName } = this.data
    if (cityName == value) {
      return
    }
    this.setData({ cityName: value })
  },
  /**
  * 重新搜索学生列表
  */
  blurCtl: function () {
    const { cityName } = this.data
    this.setData({ areaIndex:0})
    // if (!cityName) {
    //   wx.showToast({
    //     title: '搜索内容不能为空',
    //     icon: 'none'
    //   })
    //   return
    // }
    this.getLibList()
  },
  /**
  * 点击馆
  */
  getLibList: function (cb) {
    const continuousFn = { fn: this.getLibList, param: { ...arguments } }
    const { areaIndex, libIndex, cityName } = this.data;
    LibList({ data: { cityName}, continuousFn }).then(res => {
      const { cityList } = res.data;
      var temp = {};
      var listTemp=(res=>{
        for (var i in res) {
          for (var j in res[i].libList) {
           res[i].libList[j].status= ''
          }
        }
        return res
      })(cityList)
      this.setData({ cityList: listTemp})
      if (isFn(cb)) cb()
    }).catch(res => {
      if (isFn(cb)) cb()
    })
  },
  /**        
   * 选择城市
   */
  selectCity: function (e) {
    const { index } = e.currentTarget.dataset;
    this.setData({ areaIndex: index })
  },
  /**
   * 选择馆名
   */
  setLibName: function (e) {
    const { item, idx } = e.currentTarget.dataset;
    const { libId, libName } = item;
    const { areaIndex, libIndex, cityList, curatorName='', phone,code=''} = this.data;
    var temp = {};
    for (var i in cityList) {
      for (var j in cityList[i].libList) {
        temp[`cityList[${i}].libList[${j}].status`] = ''
      }
    }
    temp[`cityList[${areaIndex}].libList[${idx}].status`] = cityList[areaIndex].libList[idx].status == '' ? 'active' : '';
    this.setData(temp)
    this.redirectCtl({ url: 'register', method: { libId, libName, curatorName, phone,code,isSlect:1} }, true)
  },
})