import {
  showToast
} from '../../utils/tips.js'
import {
  imgServerUrl
} from '../../config/config.js'

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

  },
  onShow: function () {
    let cityName = wx.getStorageSync('city') || '无锡';
    let cityCode = wx.getStorageSync('cityCode') || '320200';
    this.setData({
      cityName: cityName,
      cityCode: cityCode
    })
    this.fetchData()
  },
  // 返回上级页面
  back() {
    wx.navigateBack({
      delta: 1,
    })
  },
  //获取历史记录
  fetchData() {
    var searchKey = wx.getStorageSync("searchKey");
    if (searchKey) {
      this.setData({
        searchKey: searchKey
      })
    }
  },

  //删除历史记录
  delHistory(e) {
    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
			if (!searchKey.map(value => value.searchVal).includes(this.data.searchVal)) {
				searchKey.push(key);
				wx.setStorageSync("searchKey", searchKey);
			}
    }

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