<script setup lang="ts">
    import $bus from '@/utils/bus'
    const route = useRoute()
    const router = useRouter()

    // 搜索查询绑定值
    const keywordIptValue = ref('')

    // 输入框 dom
    const inputRef: any = ref(null)

    // 输入框下方历史搜索记录
    const searchHistoryList = ref([
        { name: '220E履带挖掘机' },
        { name: '3443挖掘机' },
        { name: '塔式起重机' },
        { name: '轮式牵引铲运机' }
    ])

    // 搜索按钮
    const searchKeywordBtn = () => {
        if (route.path == '/equipList') {
            $bus.emit('search', keywordIptValue.value)
        } else {
            router.push({
                name: 'equipList',
                state: { keyWord: keywordIptValue.value }
            })
        }
    }

    // 点击下方搜索记录时
    const handleHistory = (hisValue: any) => {
        keywordIptValue.value = hisValue
        inputRef.value.focus()
    }
    //页面刷新回显模糊搜索词
    $bus.on('callBackText', (val) => {
        nextTick(() => {
            keywordIptValue.value = val
        })
    })
</script>

<template>
    <!-- 头部个人信息  收藏夹  手机版等图标 -->
    <div class="header-container">
        <div class="header-user-info">
            <div class="header-box wapper">
                <div class="header-item">
                    <a class="a-border-none" @click="$router.push('/login')">登录</a>
                    <span style="margin: 0 3px">/</span>
                    <a>注册</a>
                </div>
                <div class="header-item">
                    <a @click="$router.push({ name: 'myuser' })">个人中心</a>
                </div>
                <div class="header-item">
                    <img src="../assets/img/home/2023_12_01_beijing2/shoucang.png" alt="" />
                    <a @click="$router.push('/collect')">收藏夹</a>
                </div>
                <div class="header-item last-item">
                    <img src="../assets/img/home/2023_12_01_beijing2/shouji.png" alt="" />
                    <a class="a-border-none">手机版</a>

                    <!-- 二维码弹框 -->
                    <div class="qr-code">
                        <el-image
                            src="https://img.xjishu.com/img/zl/2017/10/212257159687020.gif"
                            fit="cover" />

                        扫二维码查看xxx
                    </div>
                </div>
            </div>
        </div>

        <!-- 中间logo 部分 -->

        <div class="logo-ipt-container wapper">
            <img
                src="@/assets/img/home/2023_12_01_beijing2/logo.png"
                alt="首页"
                title="首页"
                @click="$router.push('/home')" />

            <input
                placeholder="输入设备关键词"
                type="text"
                v-model="keywordIptValue"
                @keydown.enter="searchKeywordBtn"
                ref="inputRef" />
            <button class="search-btn" @click="searchKeywordBtn">搜索</button>

            <div class="ipt-down">
                <a
                    v-for="item in searchHistoryList"
                    :key="item.name"
                    @click="handleHistory(item.name)">
                    {{ item.name }}
                </a>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
    .header-container {
        position: sticky;
        top: 0;
        left: 0;
        z-index: 999;

        .header-user-info {
            background-color: #f5f5f5;
        }

        /* 头部个人信息部分 */
        .header-box {
            height: 37px;
            display: flex;
            align-items: center;
            justify-content: flex-end;
        }

        .header-item {
            margin-left: 15px;
            height: 37px;
            display: flex;
            align-items: center;
            padding-left: 18px;
            color: #6d6d6d;
            font-size: 14px;
            cursor: pointer;

            img {
                vertical-align: middle;
            }

            a {
                border-right: 1px solid #666655;
                padding-right: 33px;
            }

            a:hover {
                color: #2282ff;
            }

            .a-border-none {
                border: none;
                padding: 0;
            }
        }

        .last-item {
            position: relative;
            z-index: 998;
            .qr-code {
                position: absolute;
                left: -70px;
                top: -140px;
                width: 140px;
                height: 140px;
                background-color: #fff;
                visibility: hidden;
                padding: 10px;
                box-shadow: 0 2px 2px 2px #ccc;
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
                color: #333;
                .el-image {
                    width: 120px;
                    height: 120px;
                    margin-bottom: 5px;
                }
            }
        }

        .last-item:hover > .qr-code {
            visibility: visible;
            transform: translateY(177px);
            transition: all 0.6s;
        }

        /* 中间logo  搜索框部分 */
        .logo-ipt-container {
            position: relative;
            display: flex;
            background-color: #eeeff6;
            padding: 30px 0;

            img {
                width: 176px;
                height: 58px;
                cursor: pointer;
            }

            input {
                width: 700px;
                height: 45px;
                border: 1px solid #5ca4ff;
                border-radius: 45px;
                margin-left: 70px;
                line-height: 45px;
                color: transparent;
                text-shadow: 0 0 0 #333;
                padding-left: 30px;
            }

            input:focus {
                outline: 1px solid #5ca4ff;
            }

            input::-webkit-input-placeholder {
                color: #949494;
            }
            .search-btn {
                width: 100px;
                height: 39px;
                transform: translate(-110px, 4px);
                border-radius: 39px;
                background: linear-gradient(132deg, #6de7ff 0%, #348cff 100%);
                box-shadow: 0px 2px 4px 0px rgba(20, 175, 255, 0.5);
                border: none;
                color: #fff;
                cursor: pointer;
                font-size: 18px;
            }

            .ipt-down {
                position: absolute;
                bottom: 20px;
                left: 21%;
                overflow: hidden;

                a {
                    padding: 0 10px;
                    color: #727272;
                    font-size: 14px;
                    cursor: pointer;
                }
            }
        }
    }
</style>
