<template>
    <div ref="tableHeader" class="cn-table-header">
        <!-- 通用搜索 -->
        <transition name="el-zoom-in-bottom" mode="out-in">
            <div id="table-com-search1" class="table-com-search" v-show="showSelect"></div>
        </transition>
        <div class="table-header ba-scroll-style">
            <div id="table-com-search2" style="flex: 1; height: 32px; overflow: hidden; margin-right: 20px">
                <el-form
                    id="header-form"
                    @submit.prevent=""
                    @keyup.enter="onComSearch"
                    label-position="left"
                    :inline="true"
                    class="table-com-search-form"
                    :label-width="90"
                >
                    <el-form-item label="日期" v-if="datePicker" style="grid-column: span 2; max-width: unset">
                        <DatePicker ref="Picker"></DatePicker>
                    </el-form-item>
                    <slot name="select"></slot>
                </el-form>
            </div>
            <template v-if="$slots.select || datePicker">
                <el-button @click="onComSearch" type="primary">查询</el-button>
                <el-button @click="onResetForm">重置</el-button>
                <div class="table-search-button-group" v-if="showUnfoldButton">
                    <el-button class="table-search-button-item" color="#dcdfe6" plain @click="showSelectChange">
                        <Icon size="14" name="el-icon-Search" />
                    </el-button>
                </div>
            </template>
            <slot name="operation"></slot>
        </div>
    </div>
</template>

<script setup lang="ts">
import { inject, ref, onMounted, nextTick, onUnmounted } from 'vue'
import type TableStore from '@/utils/tableStore'
import DatePicker from '@/components/form/datePicker/index.vue'
import { mainHeight } from '@/utils/layout'

const tableStore = inject('tableStore') as TableStore
const tableHeader = ref()
const Picker = ref()
const date = ref([
    window.XEUtils.toDateString(new Date(), 'yyyy-MM-dd'),
    window.XEUtils.toDateString(new Date(), 'yyyy-MM-dd')
])

interface Props {
    datePicker?: boolean
}

const props = withDefaults(defineProps<Props>(), {
    datePicker: false
})
if (props.datePicker) {
    // tableStore.table.params.searchBeginTime = Picker.value.timeValue[0]
    // tableStore.table.params.searchEndTime = Picker.value.timeValue[1]
}
// 动态计算table高度
const resizeObserver = new ResizeObserver(entries => {
    for (const entry of entries) {
        handlerHeight()
    }
})
const showUnfoldButton = ref(false)
onMounted(() => {
    if (props.datePicker) {
        tableStore.table.params.searchBeginTime = Picker.value.timeValue[0]
        tableStore.table.params.searchEndTime = Picker.value.timeValue[1]
        tableStore.table.params.timeFlag = Picker.value.timeFlag
    }
    nextTick(() => {
        resizeObserver.observe(tableHeader.value)
        const dom = document.getElementById('header-form') as HTMLElement
        if (dom.offsetHeight > 50) {
            showUnfoldButton.value = true
        }
    })
})
onUnmounted(() => {
    resizeObserver.disconnect()
})

const handlerHeight = () => {
    tableStore.table.height = mainHeight(
        tableStore.table.publicHeight + tableHeader.value.offsetHeight + (tableStore.showPage ? 58 : 0) + 20
    ).height as string
}
const showSelect = ref(false)
const showSelectChange = () => {
    showSelect.value = !showSelect.value
    // 把dom(id=header-form)插入到table-com-search1或者table-com-search2
    const dom = document.getElementById('header-form') as HTMLElement
    if (showSelect.value) {
        const dom1 = document.getElementById('table-com-search1') as HTMLElement
        dom1.appendChild(dom)
    } else {
        const dom2 = document.getElementById('table-com-search2') as HTMLElement
        dom2.appendChild(dom)
    }
}
const onComSearch = async () => {
    if (props.datePicker) {
        tableStore.table.params.searchBeginTime = Picker.value.timeValue[0]
        tableStore.table.params.searchEndTime = Picker.value.timeValue[1]
        tableStore.table.params.timeFlag = Picker.value.timeFlag
    }

    await tableStore.onTableAction('search', {})
}
const onResetForm = () => {
    tableStore.onTableAction('reset', {})
}
const dateChange = () => {
    // tableStore.table.params.searchBeginTime = date.value[0]
    // tableStore.table.params.searchEndTime = date.value[1]
}
</script>

<style scoped lang="scss">
.table-header {
    position: relative;
    overflow-x: auto;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    width: 100%;
    max-width: 100%;
    background-color: var(--ba-bg-color-overlay);
    border: 1px solid var(--ba-border-color);
    border-bottom: none;
    padding: 13px 15px;
    font-size: 14px;

    .table-header-operate-text {
        margin-left: 6px;
    }
}

.table-com-search {
    box-sizing: border-box;
    width: 100%;
    max-width: 100%;
    background-color: var(--ba-bg-color-overlay);
    border: 1px solid var(--ba-border-color);
    border-bottom: none;
    padding: 13px 15px 20px 15px;
    font-size: 14px;
}

.table-com-search-form {
    display: grid;
    grid-gap: 20px;
    grid-template-columns: repeat(5, 1fr);

    :deep(.el-select),
    :deep(.el-cascader),
    :deep(.el-input) {
        width: 100%;
    }

    :deep(.el-form-item) {
        max-width: 600px;
        margin-right: 0;
        margin-bottom: 0;
    }
}

@media screen and (max-width: 2650px) {
    .table-com-search-form {
        grid-template-columns: repeat(6, 1fr);
    }
}

@media screen and (max-width: 2300px) {
    .table-com-search-form {
        grid-template-columns: repeat(5, 1fr);
    }
}

@media screen and (max-width: 1950px) {
    .table-com-search-form {
        grid-template-columns: repeat(4, 1fr);
    }
}

@media screen and (max-width: 1600px) {
    .table-com-search-form {
        grid-template-columns: repeat(3, 1fr);
    }
}

@media screen and (max-width: 1300px) {
    .table-com-search-form {
        grid-template-columns: repeat(2, 1fr);
    }
}

#table-com-search2 {
    .table-com-search-form {
        display: flex;
        flex-wrap: wrap;
    }
}

.mlr-12 {
    margin-left: 12px;
}

.mlr-12 + .el-button {
    margin-left: 12px;
}

.table-search {
    display: flex;
    margin-left: auto;

    .quick-search {
        width: auto;
    }
}

.table-search-button-group {
    display: flex;
    margin-left: 12px;
    border: 1px solid var(--el-border-color);
    border-radius: var(--el-border-radius-base);
    overflow: hidden;

    button:focus,
    button:active {
        background-color: var(--ba-bg-color-overlay);
    }

    button:hover {
        background-color: var(--el-color-info-light-7);
    }

    .table-search-button-item {
        height: 30px;
        border: none;
        border-radius: 0;
    }

    .el-button + .el-button {
        margin: 0;
    }

    .right-border {
        border-right: 1px solid var(--el-border-color);
    }
}

html.dark {
    .table-search-button-group {
        button:focus,
        button:active {
            background-color: var(--el-color-info-dark-2);
        }

        button:hover {
            background-color: var(--el-color-info-light-7);
        }

        button {
            background-color: var(--ba-bg-color-overlay);

            el-icon {
                color: white !important;
            }
        }
    }
}
</style>
