<template>
    <div class="demand-hall" :style="{ height: `calc(100vh - ${tabBarHeight}px - 50px)` }">
        <!-- 地图 -->
        <div class="map-container" ref="mapContainer"></div>

        <div class="search-wrap">
            <el-input
                class="search"
                v-model="searchValue"
                placeholder="搜索位置"
                @search="search"
                :prefix-icon="Search"
            />
        </div>

        <floating-panel
            class="floating-panel"
            v-model:height="height"
            :anchors="anchors"
            :content-draggable="false"
            @height-change="handleHeightChange"
        >
            <template #header>
                <div class="header-bar"></div>
                <div class="select-group">
                    <el-select class="select-item" v-model="dateValue" placeholder="Select">
                        <el-option
                            v-for="item in dateOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                    <el-select class="select-item" v-model="areaValue" placeholder="Select">
                        <el-option
                            v-for="item in areaOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                    <el-select class="select-item" v-model="areaValue1" placeholder="Select">
                        <el-option
                            v-for="item in areaOptions1"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="user-btn" v-if="height === anchors[0]" @click.stop="toPage">
                    <img class="user-icon" src="@/assets/img/home/user-icon.png" alt="">
                    新增用户
                </div>
            </template>
            <div class="hall-content">
                <div class="farm-list" ref="cardContentRef" :style="{ height: `${cardContentHeight}px` }">
                    <div class="task-item" v-for="item in 10" :key="item">
                        <task-item></task-item>
                    </div>
                </div>
            </div>
        </floating-panel>
    </div>
</template>

<script setup>
import { FloatingPanel } from "vant";
import { computed, nextTick, onMounted, ref } from "vue";
import { useStore } from "vuex";
import IndexMap from "../map/index";
import taskItem from "@/components/taskItem.vue";
import { Search } from "@element-plus/icons-vue";
const props = defineProps({
    isCapital: {
        type: Boolean,
        default: false
    }
})

const store = useStore();
const tabBarHeight = computed(() => store.state.home.tabBarHeight);

// const tabBarHeight = ref(localStorage.getItem("tabBarHeight") * 1 || 50);

const anchors = ref([310 + tabBarHeight.value, Math.round(1 * window.innerHeight) - 44]);
const height = ref(anchors.value[0]);


const indexMap = new IndexMap();
const mapContainer = ref(null);

onMounted(() => {
    const point = store.state.home.miniUserLocationPoint;
    nextTick(() => {
        indexMap.initMap(point, mapContainer.value, props.isCapital);
    });
});

const cardContentHeight = ref(230);
const searchValue = ref("");
const search = () => {
    console.log(searchValue.value);
};
const dateValue = ref("1");
const dateOptions = [
    { value: "1", label: "农事类型" },
    { value: "2", label: "2" },
    { value: "3", label: "3" },
];
const areaValue = ref("1");
const areaOptions = [
    { value: "1", label: "距离" },
    { value: "2", label: "2" },
    { value: "3", label: "3" },
];
const areaValue1 = ref("1");
const areaOptions1 = [
    { value: "1", label: "区域筛选" },
    { value: "2", label: "2" },
    { value: "3", label: "3" },
];

const cardContentRef = ref(null);
const handleHeightChange = ({ height }) => {
    if (height === anchors.value[0]) {
        cardContentHeight.value = 230;
        cardContentRef.value.scrollTo({ top: 0, behavior: "smooth" });
    } else if (height === anchors.value[1]) {
        cardContentHeight.value = Math.round(1 * window.innerHeight) - (tabBarHeight.value - 40) - 170;
    }
};

function toPage() {
    console.log('topage');
}

</script>

<style lang="scss" scoped>
.demand-hall {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
    .map-container {
        width: 100%;
        height: calc(100% - 290px);
    }

    .search {
        position: absolute;
        top: 8px;
        width: calc(100% - 24px);
        left: 12px;
        ::v-deep {
            .el-input__wrapper {
                box-shadow: none;
                border-radius: 20px;
            }
        }
    }

    .expand-btn-wrap {
        position: absolute;
        bottom: 12px;
        left: 12px;
        width: calc(100% - 24px);
        background-image: linear-gradient(180deg, #d7eafc 0%, #ffffff 100%);
        border-radius: 14px;
        padding: 15px 12px;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-weight: 500;
        font-size: 15px;
        .expand-btn {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 4px;
            font-size: 13px;
            color: #2199f8;
        }
    }

    .hall-content {
        height: 100%;
        position: relative;
        .farm-list {
            overflow: auto;
            height: calc(100% - 60px);
            border-top: 1px solid rgba(0, 0, 0, 0.1);
            padding: 8px 12px 12px 12px;
        }
        .task-item + .task-item {
            margin-top: 10px;
        }
    }
    
    .select-group {
        display: flex;
        padding: 0 12px;
        .select-item {
            width: 100%;
            ::v-deep {
                .el-select__wrapper {
                    text-align: center;
                    gap: 2px;
                    box-shadow: none;
                    justify-content: center;
                    background: none;
                }
                .el-select__selection {
                    flex: none;
                    width: fit-content;
                }
                .el-select__placeholder {
                    position: static;
                    transform: none;
                    width: fit-content;
                    color: rgba(0, 0, 0, 0.2);
                }
                .el-select__caret {
                    color: rgba(0, 0, 0, 0.2);
                }
            }
        }
    }

    .header-bar {
        width: 20px;
        height: 3px;
        margin: 10px auto 10px auto;
        border-radius: 4px;
        background: #969799;
    }
}
        .user-btn {
            position: absolute;
            z-index: 10;
            top: -42px;
            right: 12px;
            background: #fff;
            height: 32px;
            display: flex;
            align-items: center;
            border-radius: 5px;
            padding: 0 8px;
            font-size: 12px;
            color: #2199F8;
            .user-icon {
                width: 16px;
                margin-right: 4px;
            }
        }

.floating-panel {
    width: 100%;
    background: #ffffff;
    ::v-deep {
        .van-floating-panel__content {
            background: transparent;
            overflow: hidden;
        }
        .van-floating-panel__header {
            position: relative;
            &::after {
                content: "";
                height: 100px;
                width: 100%;
                position: absolute;
                top: 0px;
                left: 0;
            }
        }
    }
}
</style>
