import {
  searchHistory,
  deleteHistory,
  getIndexList
} from '../../services/index.js'
import {
  showToast
} from '../../utils/tips.js'
import {
  updataStorageData
} from '../../utils/storage.js'
import {
  imgServerUrl,
  imgServerUrl_new
} from '../../config/config.js'

Page({
  data: {
    historyList: [],
    searchVal: '',
    searchKey: [],
    imgServerUrl: imgServerUrl,
    hotSearch:[
      {
        id:0,
        searchVal:'返费'
      },
      {
        id: 1,
        searchVal: '工资日结'
      }
    ]
  },
  onLoad: function(options) {

  },
  onShow: function() {
    let cityName = wx.getStorageSync('city') || '无锡';
    let cityCode = wx.getStorageSync('cityCode') || '320200';
    console.log(cityName)
    console.log(cityCode)
    this.setData({
      cityName: cityName,
      cityCode: cityCode
    })
    this.fetchData()
  },

  //获取历史记录
  fetchData() {
    var searchKey = wx.getStorageSync("searchKey");
    console.log("ll", searchKey);
    if (searchKey) {
      this.setData({
        searchKey: searchKey
      })
    }
  },

  //删除历史记录
  delHistory(e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    let title = "删除提示"
    let content = "";
    content = "是否清除历史记录"
    wx.showModal({
      title: title,
      content: content,
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync("searchKey");
          showToast("删除成功");
          this.setData({
            searchKey: []
          })
        }
      }
    })
  },

  // 搜索框输入值
  bindKeyInput(e) {
    this.data.searchVal = e.detail.value;
  },

  // 点击搜索记录
  selectMsg(e) {
    this.data.searchVal = e.currentTarget.dataset.value
    this.toSearchPage(1);
  },
  //搜索
  toSearchPage(type) {
    if (!this.data.searchVal) {
      showToast("请输入搜索内容");
      return;
    }
    if (type == 1) {

    } else {
      var key = {};
      key.id = this.data.searchKey.length;
      key.searchVal = this.data.searchVal;

      let searchKey = this.data.searchKey
      searchKey.push(key);
      wx.setStorageSync("searchKey", searchKey);
    }

    wx.navigateTo({
      url: '../search-result/search-result?searchVal=' + this.data.searchVal,
    })
  },
})