import {Search} from '@nutui/icons-react-taro'
import {Button, Input} from '@nutui/nutui-react-taro'
import {useState} from "react";
import Taro from '@tarojs/taro';

function MySearch() {
  const [keywords, setKeywords] = useState<string>('')

  const onKeywords = (keywords: string) => {
    setKeywords(keywords)
  }

  const onQuery = () => {
    if(!keywords.trim()){
      Taro.showToast({
        title: '请输入关键字',
        icon: 'none'
      });
      return false;
    }
    // 跳转到搜索页面
    Taro.navigateTo({
      url: `/shop/search/index?keywords=${encodeURIComponent(keywords.trim())}`
    });
  }

  // 点击搜索框跳转到搜索页面
  const onInputFocus = () => {
    Taro.navigateTo({
      url: '/shop/search/index'
    });
  }


  return (
    <div className={'z-50 left-0 w-full'}>
      <div className={'px-2 hidden'}>
        <div
          style={{
            display: 'flex',
            alignItems: 'center',
            background: '#ffffff',
            padding: '0 5px',
            borderRadius: '20px',
            marginTop: '100px',
          }}
        >
          <Search size={18} className={'ml-2 text-gray-400'}/>
          <Input
            placeholder="搜索商品"
            value={keywords}
            onChange={onKeywords}
            onConfirm={onQuery}
            onFocus={onInputFocus}
            style={{ padding: '9px 8px'}}
          />
          <div
            className={'flex items-center'}
          >
            <Button type="success" style={{background: 'linear-gradient(to bottom, #1cd98a, #24ca94)'}} onClick={onQuery}>
              搜索
            </Button>
          </div>
        </div>
      </div>
    </div>
  );
}

export default MySearch;
