<template>
  <view class="main">
	<view class="head" :style="{'padding-top':headerHeight+4+'px'}">
		<view class="head_top">
			<view class="head_back" @click="handleBack()" :style="{'width': statusHeight+'px','height':statusHeight+'px'}">
				<image class="head_back_img" src="https://zhkj1.oss-cn-shanghai.aliyuncs.com/newPD/zhBack.png" mode="widthFix"></image>
			</view>
			<view class="head_search" :style="{'width':searchWidth+'px','height':statusHeight+5+'px'}">
				<image class="head_search_img" src="../../static/search_img.png" mode="widthFix"></image>
				<input class="head_search_input" :focus="focus" type="text" confirm-type="search" placeholder="请输入搜索内容" v-model="searchtext" @input="onKeyInput" @confirm="confirm"/>
			</view>
		</view>
	</view>
	<view class="cont" :style="{'padding-top':statusHeight+headerHeight+19+'px'}">
		<block v-if="searchList.length">
			<view class="cont_ul">
				<view class="cont_li" v-for="(item,index) in searchList" :key="index" @click="onList(item.title)">{{item.title}}</view>
			</view>
		</block>
		<block v-else>
			<view class="cont_title">历史搜索</view>
			<view class="cont_keyword">
				<view class="cont_keyword_item">排骨</view>
				<view class="cont_keyword_item">猪肉炖粉条</view>
				<view class="cont_keyword_item">蚂蚁上树</view>
			</view>
			
			<view class="cont_title">搜索发现</view>
			<view class="cont_keyword">
				<view class="cont_keyword_item">买1赠1起</view>
				<view class="cont_keyword_item">水果半价日</view>
				<view class="cont_keyword_item">海鲜5折抢</view>
				<view class="cont_keyword_item">春菜</view>
				<view class="cont_keyword_item">青团</view>
				<view class="cont_keyword_item">虾</view>
				<view class="cont_keyword_item">牛奶</view>
				<view class="cont_keyword_item">矿泉水</view>
			</view>
		</block>
		
	</view>
  </view>
</template>

<script setup>
import { computed,ref,onMounted,onUnmounted,getCurrentInstance,nextTick } from 'vue';
import { useCounterStore } from '@/store/counter'; // 引入 Pinia Store
import { storeToRefs } from 'pinia';//实现解构付值
import { onLoad,onShow,onPullDownRefresh,onPageScroll,onReachBottom,onReady } from "@dcloudio/uni-app"
const counterStore = useCounterStore(); // 使用 Store
const { proxy } = getCurrentInstance();
//使用pinia：storeToRefs方法包裹(保持响应式更新，不使用视图无法更新)
const {statusHeight,headerHeight,statusBartop,ButtonWidth,ButtonHeight } = storeToRefs(counterStore);
// 定义响应式数据，存储页面宽度
const pageWidth = ref(0);
const focus = ref(false);
const searchtext = ref('');
const searchList= ref([]);
// 获取页面宽度
const updatePageWidth = () => {
  const systemInfo = uni.getSystemInfoSync();
  pageWidth.value = systemInfo.windowWidth;
};

const searchWidth = computed(() => {
	return pageWidth.value - statusHeight.value - ButtonWidth.value - 35;
});
// 判断是否有上一页
const getPages = () => {
  const pages = getCurrentPages();
  return pages.length > 1;
};
const handleBack = () => {
  if (getPages()) {
    // 如果有上一页，返回上一页
    uni.navigateBack();
  } else {
    // 如果没有上一页，返回首页
    uni.switchTab({
      url: '/pages/index/index',
    });
  }
};

const onKeyInput=()=>{
	if(searchtext.value){
		const list = [{
			title:'啤酒'
		},
		{
			title:'雪花啤酒'
		},
		{
			title:'青岛啤酒装罐'
		},
		{
			title:'百威啤酒'
		},
		{
			title:'原浆啤酒'
		},
		{
			title:'进口啤酒'
		},
		{
			title:'燕京啤酒'
		}]
		searchList.value = list;
	}else{
		searchList.value = [];
	}
};
const onList=(name)=>{
	uni.navigateTo({
		url:`/userserve/SearchList/SearchList?text=${name}`
	})
};
const confirm=(e)=>{
	uni.navigateTo({
		url:`/userserve/SearchList/SearchList?text=${searchtext.value}`
	})
};
onLoad((options) => {
	focus.value = true;
	updatePageWidth();
});
onShow(() => {});
onReady(()=>{})
onPullDownRefresh(()=>{})
onReachBottom(()=>{})
</script>

<style lang="scss">
	@import './style.scss';
</style>