<template>
    <view class="mainWrapper">
        <view class="headSearch">
            <view class="search-view">
                <view class="search-item">
                    <image src="../../static/img/search.svg" style="width: 32rpx;height: 32rpx;" mode=""></image>
                    <input type="text" confirm-type="搜索" v-model="keywords" @confirm="getList"
                        placeholder="搜零工岗位/搜全职岗位/搜政策" />
                    <view @click="getList"
                        style="width: 80rpx;height: 50rpx;color:#fff;background-color: #0091ff;font-size:24rpx;text-align: center;line-height: 50rpx;border-radius: 25rpx;;">
                        搜索</view>
                </view>
            </view>
        </view>
        <view class="search-history">
            <view class="title">搜索记录</view>
            <view class="clear" @click="clearKeyWords">
                <image src="../../static/img/delete.png" style="width: 24rpx;height: 24rpx;" mode=""></image>
                <view style="margin-left: 5rpx;">清空</view>
            </view>
            <view class="wrapper" v-if="searchHistoryList.length > 0">
                <view class="item" v-for="(item, index) in searchHistoryList" @click="keyWordsClick(item.keywords)">
                    {{ item.keywords }}
                </view>
            </view>
            <view v-else class="empty">暂无历史记录</view>
            <v-tabs :tabs="tabList" height="45px" v-model="activeTab" color="#999" activeColor="#000" fontSize="30rpx"
                activeFontSize="31rpx" @change='changeTab' />

        </view>
        <block v-if="activeTab === 0">
            <view v-if="companyList.length ">
                <block v-for="(item, index) in companyList" :key="item.id">
                    <companyList :companyitem="item" :location="userLocation"></companyList>
                </block>
            </view>
            <view v-else>
                <image src="../../static/img/pic_notask.svg" mode="" class="nothing"></image>
                <view class="nothingContnt">
                    暂无信息
                </view>
            </view>
        </block>
        <block v-if="activeTab === 1">
            <view v-if="newList.length">
                <block v-for="(item, index) in newList" :key="item.id">
                    <workList :companyitem="item" :near="activeTab === 0" :location="userLocation"></workList>
                </block>
            </view>
            <view v-else>
                <image src="../../static/img/pic_notask.svg" class="nothing" mode=""></image>
                <view class="nothingContnt">暂无信息</view>
            </view>
        </block>



        <block v-if="activeTab == 2">
            <view v-if="recommendList.length">
                <!--    <block v-for="(item, index) in recommendList" :key="item.id">
                    <companyList :companyitem="item"></companyList>
                </block> -->
                <view v-for="(item, index) in recommendList" :key="index"
                    style="width: 90%;height: 100rpx;background-color: #fff;border-radius: 10rpx;margin:20rpx 5% 20rpx 5%;display: flex;align-items: center"
                    @click="goPolicyInfo(item.id)">
                    <image :src="item.headPic" style="width: 60rpx;height: 60rpx;margin: 0 20rpx;"></image>
                    <view style="font-size: 26rpx;">{{ item.articleTitle }}</view>
                </view>
            </view>


            <view v-else>
                <image src="../../static/img/pic_notask.svg" mode="" class="nothing"></image>
                <view class="nothingContnt">
                    暂无信息
                </view>
                <!-- <view class="btn" @click="goResume">
        			完善技能
        		</view> -->
            </view>
        </block>

        <block v-if="activeTab == -1">
            <view>
                <image src="../../static/img/pic_notask.svg" mode="" class="nothing"></image>
                <view class="nothingContnt">
                    没有更多数据
                </view>
            </view>
        </block>
    </view>
</template>

<script>
    import {
        cloneDeep
    } from 'lodash';
    import {
        newMissionAll,
        getSearchKeyWordsList,
        clearSearchKeyWords,
        getListByKeyWords,
        nearMission,
        getPolicyContentList,
        userWorkSearchListAll
    } from '@/api/mission.js';
    import {
        mapGetters
    } from 'vuex'
    import companyList from '@/components/companyList/companyList.vue';
    import workList from '@/components/companyList/workList.vue'
    const titleStatus = {
        'mission': {
            title: '零工岗位',
            val: 0,
        },
        'work': {
            title: '全职岗位',
            val: 1,
        },
        'policy': {
            title: '政策',
            val: 2,
        },
    }
    export default {
        components: {
            companyList,
            workList
        },
        data() {
            return {
                activeTab: 0,
                keywords: '',
                searchHistoryList: [],
                companyList: [],
                newList: [],
                recommendList: [],
                tabList: ['零工岗位', '全职岗位', '政策']
            }
        },
        computed: {
            ...mapGetters(['userLocation']),
        },
        onShow: function() {
            this.getKeyWordsList()
        },
        methods: {
            goPolicyInfo(data) {
                if (data) {
                    uni.navigateTo({
                        url: `/pages/user/policyContent?id=${data}`
                    })
                }
            },
            // 获取搜索历史记录
            getKeyWordsList() {
                getSearchKeyWordsList().then(res => {
                    let response = res.data
                    if (response.code == 200) {
                        this.searchHistoryList = response.data
                    }
                })
            },
            keyWordsClick(keyWords) {
                // console.log(keyWords)
                this.searchResultShow = true;
                this.keywords = keyWords;
            },
            // 清空搜索历史
            clearKeyWords() {
                clearSearchKeyWords().then(res => {
                    if (res.data.code == 200) {
                        this.getKeyWordsList()
                    }
                })
            },
            changeTab: function(e) {
                this.activeTab = e;
                // const tab = cloneDeep(this.tabList[e])
                // Object.keys(titleStatus).map((key) => {
                //     if (titleStatus[key].title === tab) {
                //         this.activeTab = titleStatus[key].val;
                //     }
                // })
            },
            async getList() {
                const params = {
                    keywords: this.keywords
                }
                let resData = await userWorkSearchListAll(params)
                if (resData.data.code === 200) {
                    const {
                        mission,
                        policy,
                        work
                    } = resData.data.data

                    const tabVal = []
                    Object.keys(titleStatus).map((key) => {
                        if (resData.data.data[key].length) {
                            tabVal.push(titleStatus[key].title)
                        }
                    })
                    if (tabVal.length) {
                        let count = 0
                        Object.keys(titleStatus).map((key) => {
                            if (!count && titleStatus[key].title === tabVal[0]) {
                                this.activeTab = titleStatus[key].val;
                                count++
                            }
                        })
                    }
                    this.companyList = mission
                    this.recommendList = policy
                    this.newList = work
                }
            }
        }
    }
</script>
<style scoped>
    .mainWrapper {
        background-color: #f3f4f8;
        min-height: calc(100vh - var(--window-top) - var(--status-bar-height) - var(--window-bottom));
    }

    .baddd {
        background-color: #f6f6f6;
        height: 20rpx;
    }

    .close {
        position: absolute;
        right: 24rpx;
        top: 6rpx;
        height: 60rpx;
        padding: 5rpx 0;
        line-height: 60rpx;
        font-family: PingFangSC-Regular;
        font-size: 32rpx;
        color: #333333;
    }

    .headSearch .search-view {
        border-bottom: 1rpx solid #dddddd;
        padding: 14rpx 32rpx;
        background: rgba(255, 255, 255, 1);
    }

    .headSearch .search-item {
        display: flex;
        padding: 0 10rpx;
        height: 60rpx;
        line-height: 60rpx;
        background: rgba(249, 249, 249, 1);
        border-radius: 200px;
        align-items: center;
        border: 1px solid #0091ff;
    }

    .headSearch .search-item input {
        padding: 0 24rpx;
        height: 80rpx;
        font-weight: 400;
        flex: 1;
        text-align: start;
        font-size: 28rpx;
        font-family: PingFang-SC-Bold, PingFang-SC;
        color: rgba(51, 51, 51, 1);
    }

    .headSearch .search-item .placeholderClass {
        color: #CCCCCC;
    }

    .headSearch .search-item image {
        height: 32rpx;
        width: 32rpx;
    }

    .nothing {
        width: 400rpx;
        height: 200rpx;
        display: block;
        margin: 0 auto;
        margin-top: 50%;
    }

    .nothingContnt {
        font-family: PingFangSC-Regular;
        font-size: 28rpx;
        color: #333333;
        margin-top: 30rpx;
        text-align: center;
    }

    .search-history {
        background-color: #fff;
        padding: 10rpx 40rpx;
        position: relative;
    }

    .search-history .title {
        font-size: 28rpx;
        font-weight: bold;
    }

    .search-history .clear {
        position: absolute;
        right: 40rpx;
        top: 10rpx;
        font-size: 24rpx;
        display: flex;
        align-items: center;
        color: #707070;
    }

    .search-history .empty {
        font-size: 24rpx;
        text-align: center;
        color: #707070;
    }

    .search-history .wrapper {
        display: flex;
        flex-wrap: wrap;
        margin-top: 10rpx;
    }

    .search-history .wrapper .item {
        font-size: 24rpx;
        background-color: #f6f6f6;
        padding: 8rpx 12rpx;
        border-radius: 20rpx;
        margin-right: 10rpx;
        position: relative;
    }

    .search-result {
        padding: 20rpx 30rpx;
        background-color: #fff;
        height: 100vh;
    }

    .search-result .title {
        font-size: 26rpx;
        font-weight: bold;
        border-bottom: 1rpx solid #ddd;
        height: 60rpx;
        line-height: 60rpx;
    }

    .search-result .search-item {
        display: flex;
        align-items: center;
        font-size: 26rpx;
        border-bottom: 1rpx solid #ddd;
        height: 80rpx;
        line-height: 80rpx;

    }

    .search-result .search-item .type {
        font-size: 20rpx;
        color: #707070;
        background-color: #dddddd;
        border-radius: 15rpx;
        height: 30rpx;
        line-height: 30rpx;
        padding: 0 20rpx;
    }
</style>