// pages/expertConsult/expertConsult.js
const app = getApp()
import {doctorList} from "../../../api/consultExpert"
import {AllKepuList} from "../../../api/common"
import {throttle} from "../../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    pageNumber:10,
    isTriggered:false,
    show:false,
    expertise_id:21,
    province_id:'',
    city_id:'',
    keyword:'',
    articeList:[],
    lock:false,
    is_online:0,
    sort_order:1,
    inquiry_type:'1,3',
    inquiry_mode:'1,2',
    navName:"专家问诊",
    scrollHeight:53,
    sickList:[],
    img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/patient/static',
    option: [
      { text: '综合', value: 1 },
      { text: '响应时间快', value: 2 },
      { text: '价格从低到高', value: 3 },
      { text: '价格从高到低', value: 4 },
      { text: '服务数从多到少', value: 5},
    ],
    overlay:true,
    showBg:false,
    selectkind:false,
    selectkarea:false,
    position:"全国",
    doctorList: [],
    triggered: false,
    scrollTop: 0,
    offsetTop: 0,
    columns: [
      {
        values:[],
        className: 'column1',
      },
      {
        values: [],
        className: 'column2',
        defaultIndex: 1,
      }
    ],
    isLoding:false,
    tabId:-1
  },
  goBack(){
    wx.navigateBack({
      delta: 1,
    })
  },
  getArticleList(){
  
    AllKepuList({
        basic_class_id:1
    }).then((res)=>{
     console.log(res);
      this.setData({
        articeList:res
      });
 
    })
   },
   goArticle:throttle(function(event){
    app.method.navigateTo({
        url:"/patient/pages/linkPage/linkPage?url="+encodeURIComponent(event.currentTarget.dataset.url)
      })
  }),
  goHivList:throttle(function(){
    app.method.navigateTo({
        url:"/patient/pages/kepuList/kepuList?title="+encodeURIComponent('关于HIV你知道多少？')+"&id=1"
      })
  }),
  toggleOnline(){
    this.setData({
      is_online:this.data.is_online==0?1:0
    })
    this.onfresh();
  },
  transforpage(arr) {
    const pages = [];
    let L= Math.ceil(arr.length / 2);
    arr.forEach((item, index) => {
      const page = Math.floor(index / L)
      if (!pages[page]) {
        pages[page] = []
      }
      pages[page].push(item)
    });
    return pages
  },
  goSearch:throttle(function(){
    wx.hideKeyboard();
   app.method.navigateTo({
     url: '/patient/pages/search/search',
   })
  }),
  switchTab(event){
    this.setData({
      tabId:event.target.dataset.id,
      expertise_id:event.target.dataset.id
    })
    this.onfresh();
  },
  changeSorder({ detail }){
    this.setData({ 
      sort_order: detail,
      selectkind:true
    });
    this.onfresh();
  },
  getIptvalue(event){
    this.setData({
      keyword:event.detail.value
    })
    this.onfresh();
  },
  goExpertDetail:throttle(function(event){
    app.method.navigateTo({
      url: '/patient/pages/expertDetail/expertDetail?doctor_id='+event.currentTarget.dataset.doctorid
    })
  }),
  openCity(){
    this.setData({
      show:true,
      overlay:false
    })
  },
  closeCity(){
    this.setData({
      show:false,
      overlay:true
    })
  },
  onChange(event) {
    const { picker, value, index } = event.detail;
   if(index===0){
      let currentIndex=picker.getIndexes()[0];
      if(provinceArr[currentIndex].area_id){
        this.cityList(provinceArr[currentIndex].area_id);
      }else{
        this.setData({
         'columns[1].values':[]
        })
      }
     
   }
  },

  onConfirm(event) {
    const { picker, value, index } = event.detail;
    let currentPosition=''
    for (let index = 0; index < value.length; index++) {
      if(!value[0].area_id){
        currentPosition="全国";
        this.setData({
          province_id:'',
          city_id:''
        })
      }else{
          currentPosition+=value[index].area_name;
        if(index===0){
          this.setData({
            province_id:value[index].area_id
          })
        }else if(index==1){
          this.setData({
            city_id:value[index].area_id
          })
        }
    }
  }
   this.setData({
     show:false,
     selectarea:true,
     position:currentPosition
   })
   this.selectComponent('#item').toggle();
   this.onfresh();
  },

  onCancel() {
    this.setData({
      show:false
    })
    this.selectComponent('#item').toggle(false);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  
   handleRefresher(){

    this.setData({
      lock:false,
      page:1,
      scrollHeight:53,
      doctorList:[]
    });
    this.getDoctorList()
   },
   getDoctorList(){
     let {expertise_id,province_id, city_id,sort_order,keyword,page,pageNumber,is_online,inquiry_type,inquiry_mode}=this.data;
    doctorList({
      expertise_id:expertise_id,
      province_id:province_id,
      city_id:city_id,
      sort_order: sort_order,
      is_first_online:is_online,
      keyword:keyword,
      inquiry_type,
      inquiry_mode,
      page:page,
      per_page:pageNumber
    }).then((res)=>{
      let {doctorList}=this.data;
       if(res.data.length==0){
        this.setData({
          lock:true,
          showBg:true,
          isTriggered:false
        });
        this.getArticleList();
        return false;
       }else{
       
          this.setData({
            showBg:true,
            isTriggered:false,
            doctorList:doctorList.concat(res.data)
          })
       }
   

    })
   },
   onScroll(event) {
   
    wx.createSelectorQuery().select('#scroller')
      .boundingClientRect((res) => {
          console.log(event.detail.scrollTop)
       if(event.detail.scrollTop>1600){
           this.lower();
       }
        
        
      }).exec();
    },
  onfresh(){
    this.setData({
      page:1,
      doctorList:[],
      lock:false
    })
    this.getDoctorList();
  },
  lower(e) {
    let {lock}=this.data;
    let addPage=this.data.page+1;
    if(!lock){
      this.setData({
        page:addPage
      });
      this.getDoctorList();
    }  
  },
  onLoad(options) {
    if(options.type=="free"){
      wx.setNavigationBarTitle({
        title: "特惠问诊"
      });
      this.setData({
        navName:"特惠问诊",
        inquiry_type:3,
        inquiry_mode:1
      })
    }else if(options.type=="buyYao"){
      wx.setNavigationBarTitle({
        title: "问诊购药"
      });
      this.setData({
        navName:"问诊购药",
        inquiry_type:4,
        inquiry_mode:1
      })
    }
 
    this.getDoctorList();
 
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  
  },


  onRefresh() {
    if (this._freshing) return
    this._freshing = true
    setTimeout(() => {
      this.setData({
        triggered: false,
      })
      this._freshing = false
    }, 3000)
  },
  onRestore(e) {
    console.log('onRestore:', e)
  },

  onAbort(e) {
    console.log('onAbort', e)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      img_host:app.hostConfig().imghost
    });
  },

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

  },

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

  },

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

  },

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

  },
  onShareTimeline(){
    return {
        title:'恐艾咨询',           // 分享出去的标题
        path:'patient/pages/hivConsult/hivConsult'           // 分享出去的页面路径
     }
  },
// 分享到微信好友
onShareAppMessage(res) {
    return {
       title:'恐艾咨询',           // 分享出去的标题
       path:'patient/pages/hivConsult/hivConsult'           // 分享出去的页面路径
    }
   },
   
  /**
   * 用户点击右上角分享
   */
  
})