/*
 * 手太欠
 * 肥督督商城
 */

const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchHistory   : []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
        if (wx.getStorageSync('searchHistory')) {
            this.setData({
                searchHistory: wx.getStorageSync('searchHistory')
            })
        }
    },

    /**
     * 搜索
     */
    searchForm(e) {
        // 存入历史搜索
        let searchHistory = this.data.searchHistory,
            searchValue   = e.detail.value.search,
            newsearchHistory

        if (searchValue != '') {
            searchHistory.push(searchValue)
            newsearchHistory = Array.from(new Set(searchHistory))
            // 写入缓存
            wx.setStorageSync('searchHistory', newsearchHistory)
            // 转跳搜索结果页
            wx.navigateTo({
                url: '/pages/mall/goods/goods?title=' + searchValue
            })
        }
    },

    /**
     * 历史记录搜索
     */
    bindSearch(e) {
        wx.navigateTo({
            url: '/pages/mall/goods/goods?title=' + e.currentTarget.dataset.value
        })
    },

    /**
     * 清理搜索结果
     */
    removeSearchHistory() {
        wx.removeStorage({
            key: 'searchHistory',
            success: res => {
                if (res.errMsg == "removeStorage:ok") {
                    this.setData({
                        searchHistory: []
                    })
                }
            }
        })
    }
})