// pages/search/search.js
const app = getApp()
import {doctorList} from "../../../api/consultExpert"
import {hotSearch} from "../../../api/common"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword:'',
     page:1,
     pageNumber:10,
     toggle:false,
     lock:false,
     doctorList:[],
     seachList:[],
     img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/patient/static',
     hotList:[]
  },
  goSearch(){
    this.handleSearch(this.data.keyword)
  },
  changeIpt(event){
   this.setData({
    keyword:event.detail.value
   })
  },
  getIptvalue(event){
    this.setData({
      keyword:event.detail.value
    });
    this.handleSearch(event.detail.value)
  },
  handleSearch(value){
    if(!value) {
      wx.showToast({
        title: '搜索内容不能为空',
        icon:"none"
      })
      return false
    };
    let serachList=wx.getStorageSync('serachList');
    let serachArr=[];
    if(serachList){
      serachArr=JSON.parse(serachList);
    };
     var index=serachArr.indexOf(value);
    
    if(index!=-1){
      serachArr.splice(index,1);
    };
    if(serachArr.length>=30){
      serachArr.pop();
    };
    serachArr.unshift(value);
    wx.setStorageSync('serachList',JSON.stringify(serachArr));
    this.setData({
     seachList:serachArr
   })
    this.onfresh();
  },
  handlehotSearch(){
    hotSearch({
      client_type:1,
      keyword_place:1
    }).then(data=>{
     this.setData({
      hotList:data
     })
    })
  },
  selectHistroy(event){
    let value=event.currentTarget.dataset.id;
    this.setData({
      keyword:value,
      toggle:true
    });
    this.handleSearch(value)
  },
  getSearchList(){
    let serachList=wx.getStorageSync('serachList');
    let serachArr=[];
    if(serachList){
      serachArr=JSON.parse(serachList);
    };
    this.setData({
      seachList:serachArr
    })
  },
  goBack(){
    wx.navigateBack({
      delta: 1,
    })
  },
  goExpertDetail(event){
    app.method.navigateTo({
      url: '/patient/pages/expertDetail/expertDetail?doctor_id='+event.currentTarget.dataset.doctorid
    })
  },
  delSearch(){
    wx.setStorageSync('serachList','');
    this.getSearchList();
    wx.showToast({
      title: '搜索历史已清空',
      icon:'none'
    })
  },
  getDoctorList(){
   let {keyword,page,pageNumber}=this.data;
   doctorList({
     keyword:keyword,
     page:page,
     per_page:pageNumber
   }).then((res)=>{
     let {doctorList}=this.data;
      if(res.data.length==0){
       this.setData({
         lock:true
       });
       return false;
      }else{
         this.setData({
           toggle:true,
           doctorList:doctorList.concat(res.data)
         })
      }
  

   })
  },
  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) {
    this.getSearchList();
    this.handlehotSearch();
    if(options.keyword){
      this.setData({
       keyword:options.keyword
      })
     }
    //this.getDoctorList();
  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  
})