<template>
    <LayHeader :class="getHeaderClass">
        <!-- left start -->

        <div :class="`${prefixCls}-left`">
            <!-- logo -->
            <AppLogo v-if="getShowHeaderLogo || getIsMobile" :class="`${prefixCls}-logo`" :theme="getHeaderTheme" :style="getLogoWidth" :show-title="!getCollapsed" />
            <LayoutTrigger v-if="(getShowContent && getShowHeaderTrigger && !getSplit && !getIsMixSidebar) || getIsMobile" :theme="getHeaderTheme" :sider="false" />
        </div>
        <!-- left end -->
        <div :class="`${prefixCls}-menu`" v-if="getShowContent && getShowBread">
            <LayoutBreadcrumb :theme="getHeaderTheme" />
        </div>
        <!-- menu start -->
        <div :class="`${prefixCls}-menu`" v-if="getShowTopMenu && !getIsMobile">
            <LayoutMenu :isHorizontal="true" :theme="getHeaderTheme" :splitType="getSplitType" :menuMode="getMenuMode" />
        </div>
        <!-- menu-end -->

        <!-- action  -->
        <div :class="`${prefixCls}-action`">
            <LayoutBreadcrumb :theme="getHeaderTheme" v-if="getShowTopMenu && !getIsMobile" />
            <AppSearch :class="`${prefixCls}-action__item `" v-if="getShowSearch" />
            <ErrorAction v-if="getUseErrorHandle" :class="`${prefixCls}-action__item error-action`" />

            <Notify v-if="getShowNotice" :class="`${prefixCls}-action__item notify-item`" />

            <UserTenantChange v-if="getAppEnvConfig().VITE_TENANT_ENABLED" />
            <SettingDrawer v-if="getShowSetting" :class="`${prefixCls}-action__item`" ref="drawer" />
            <UserDropDown @menu-click="onMenuClick" :theme="getHeaderTheme" :show-settings="getShowSetting" />
        </div>
    </LayHeader>
</template>
<script lang="ts">
    import { defineComponent, unref, computed, ref } from 'vue';

    import { propTypes } from '/@/utils/propTypes';

    import { Layout } from 'ant-design-vue';
    import { AppLogo } from '/@/components/Application';
    import LayoutMenu from '../menu/index.vue';
    import LayoutTrigger from '../trigger/index.vue';

    import { AppSearch } from '/@/components/Application';

    import { useHeaderSetting } from '/@/hooks/setting/useHeaderSetting';
    import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
    import { useRootSetting } from '/@/hooks/setting/useRootSetting';

    import { MenuModeEnum, MenuSplitTyeEnum } from '/@/enums/menuEnum';
    import { SettingButtonPositionEnum } from '/@/enums/appEnum';
    import { UserTenantChange } from '/@/components/Application';

    import { LayoutBreadcrumb, Notify, ErrorAction } from './components';
    import UserDropDown from '/@/layouts/default/header/components/user-dropdown/DropDown.vue';
    import { useAppInject } from '/@/hooks/web/useAppInject';
    import { useDesign } from '/@/hooks/web/useDesign';

    import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
    import { useLocale } from '/@/locales/useLocale';
    import {getAppEnvConfig} from "/@/utils/env";

    export default defineComponent({
        name: 'LayoutHeader',
        methods: {getAppEnvConfig},
        components: {
            LayHeader: Layout.Header,
            AppLogo,
            LayoutTrigger,
            LayoutBreadcrumb,
            LayoutMenu,
            UserDropDown,
            Notify,
            AppSearch,
            ErrorAction,
            SettingDrawer: createAsyncComponent(() => import('/@/layouts/default/setting/index.vue'), {
                loading: true
            }),
            UserTenantChange
        },
        props: {
            fixed: propTypes.bool
        },
        setup(props) {
            const { prefixCls } = useDesign('layout-header');
            const { getShowTopMenu, getShowHeaderTrigger, getSplit, getIsMixMode, getMenuWidth, getIsMixSidebar, getCollapsed } = useMenuSetting();
            const { getUseErrorHandle, getShowSettingButton, getSettingButtonPosition } = useRootSetting();

            const { getHeaderTheme, getShowFullScreen, getShowNotice, getShowContent, getShowBread, getShowHeaderLogo, getShowHeader, getShowSearch } = useHeaderSetting();

            const { getShowLocalePicker } = useLocale();

            const { getIsMobile } = useAppInject();

            const getHeaderClass = computed(() => {
                const theme = unref(getHeaderTheme);
                return [
                    prefixCls,
                    {
                        [`${prefixCls}--fixed`]: props.fixed,
                        [`${prefixCls}--mobile`]: unref(getIsMobile),
                        [`${prefixCls}--${theme}`]: theme
                    }
                ];
            });

            const getShowSetting = computed(() => {
                if (!unref(getShowSettingButton)) {
                    return false;
                }
                const settingButtonPosition = unref(getSettingButtonPosition);

                if (settingButtonPosition === SettingButtonPositionEnum.AUTO) {
                    return unref(getShowHeader);
                }
                return settingButtonPosition === SettingButtonPositionEnum.HEADER;
            });

            const getLogoWidth = computed(() => {
                if (!unref(getIsMixMode) || unref(getIsMobile)) {
                    return {};
                }
                const width = unref(getMenuWidth) < 180 ? 180 : unref(getMenuWidth);
                return { width: `${width}px` };
            });

            const getSplitType = computed(() => {
                return unref(getSplit) ? MenuSplitTyeEnum.TOP : MenuSplitTyeEnum.NONE;
            });

            const getMenuMode = computed(() => {
                return unref(getSplit) ? MenuModeEnum.HORIZONTAL : null;
            });

            const drawer = ref();
            function onMenuClick(name) {
                if (name === 'themeSetting') {
                    drawer.value.openDrawer(true);
                }
            }

            return {
                prefixCls,
                getHeaderClass,
                getShowHeaderLogo,
                getHeaderTheme,
                getShowHeaderTrigger,
                getIsMobile,
                getShowBread,
                getShowContent,
                getSplitType,
                getSplit,
                getMenuMode,
                getShowTopMenu,
                getShowLocalePicker,
                getShowFullScreen,
                getShowNotice,
                getUseErrorHandle,
                getLogoWidth,
                getIsMixSidebar,
                getShowSettingButton,
                getShowSetting,
                getShowSearch,
                getCollapsed,
                onMenuClick,
                drawer
            };
        }
    });
</script>
<style lang="less">
    @import url('./index.less');
</style>
