<template>
  <setting-outlined
    v-bind="$attrs"
    class="ant-pro-dropdown ant-pro-dropdown-action"
    @click="handleSettingBtnClick"
  />
  <h-modal
    v-model:visible="state.visible"
    :width="700"
    :title="t('global.setting.title')"
    :confirm-loading="state.confirmLoading"
    @ok="handleOk"
  >
    <h-row :gutter="16">
      <h-col :span="12">
        <list label="Basic Layout">
          <list-item label="Layout Mode">
            <h-select :value="layout" @change="handleLayoutChange" style="width: 150px">
              <h-select-option value="side">Side</h-select-option>
              <h-select-option value="left">Left</h-select-option>
              <h-select-option value="top">Top</h-select-option>
              <h-select-option value="mix">Mix</h-select-option>
            </h-select>
          </list-item>
          <list-item label="Content Width">
            <h-radio-group
              :value="contentWidth"
              @change="handleContentWidthChange"
              style="width: 150px"
            >
              <h-radio-button value="Fluid">Fluid</h-radio-button>
              <h-radio-button value="Fixed">Fixed</h-radio-button>
            </h-radio-group>
          </list-item>
          <list-item label="Nav Theme">
            <h-radio-group
              :disabled="hasMix"
              :value="navTheme"
              @change="handleNavThemeChange"
              style="width: 150px"
            >
              <h-radio-button value="dark">Dark</h-radio-button>
              <h-radio-button value="light">Light</h-radio-button>
            </h-radio-group>
          </list-item>
          <list-item label="Split Menu">
            <h-switch
              checked-children="开"
              un-checked-children="关"
              :disabled="layout !== 'mix'"
              :checked="splitMenus"
              @change="handleSplitMenusChange"
            />
          </list-item>
        </list>

        <list v-if="false" label="Box">
          <list-item label="Fixed Header">
            <h-switch :checked="fixedHeader" />
          </list-item>
          <list-item label="Fixed Sidebar">
            <h-switch :checked="fixedSidebar" />
          </list-item>
        </list>

        <list label="Router Animation">
          <h-select
            :value="transitionName || 'null'"
            @change="handleChangeTransitionName"
            style="width: 200px"
          >
            <h-select-option value="null">Null</h-select-option>
            <h-select-option value="slide-fadein-up">Slide Up</h-select-option>
            <h-select-option value="slide-fadein-right">Slide Right</h-select-option>
            <h-select-option value="fadein">Fade In</h-select-option>
            <h-select-option value="zoom-fadein">Zoom</h-select-option>
          </h-select>
        </list>
      </h-col>
    </h-row>
  </h-modal>
</template>

<script lang="ts">
import { computed, defineComponent, reactive } from 'vue';
import { useStore } from 'vuex';
import { useI18n } from 'vue-i18n';
import { SettingOutlined } from '@ant-design/icons-vue';
import {
  SET_CONTENT_WIDTH,
  SET_LAYOUT,
  SET_NAV_THEME,
  SET_SPLIT_MENUS,
  SET_TRANSITION_NAME,
} from '@/store/modules/app/mutations';

import List from './list.vue';
import ListItem from './list-item.vue';
import type { RadioChangeEvent } from 'ant-design-vue';

export default defineComponent({
  name: 'SettingDialog',
  inheritAttrs: false,
  setup() {
    const { t } = useI18n();
    const store = useStore();
    const state = reactive({
      visible: false,
      confirmLoading: false,
    });
    const handleOk = () => {
      state.visible = false;
    };
    const handleSettingBtnClick = () => {
      state.visible = true;
    };

    const layout = computed(() => store.getters['app/layout']);
    const navTheme = computed(() => store.getters['app/navTheme']);
    const contentWidth = computed(() => store.getters['app/contentWidth']);
    const splitMenus = computed(() => store.getters['app/splitMenus']);
    const fixedHeader = computed(() => store.getters['app/fixedHeader']);
    const fixedSidebar = computed(() => store.getters['app/fixedSidebar']);
    const transitionName = computed(() => store.getters['app/transitionName']);

    const hasMix = computed(() => layout.value === 'mix');

    const handleLayoutChange = (value: string) => {
      if (value !== 'mix') {
        // 强制停止使用分割菜单
        store.commit(`app/${SET_SPLIT_MENUS}`, false);
      } else {
        // 如果是 mix 模式，则需要将主题色设置为 dark
        store.commit(`app/${SET_NAV_THEME}`, 'light');
      }
      store.commit(`app/${SET_LAYOUT}`, value);
    };
    const handleContentWidthChange = (e: RadioChangeEvent) => {
      const value = (e.target as HTMLInputElement).value;
      store.commit(`app/${SET_CONTENT_WIDTH}`, value);
    };
    const handleNavThemeChange = (e: RadioChangeEvent) => {
      const value = (e.target as HTMLInputElement).value;
      store.commit(`app/${SET_NAV_THEME}`, value);
    };
    const handleSplitMenusChange = (checked: boolean) => {
      store.commit(`app/${SET_SPLIT_MENUS}`, checked);
    };
    const handleChangeTransitionName = (value: string) => {
      store.commit(`app/${SET_TRANSITION_NAME}`, value === 'null' ? '' : value);
    };

    return {
      t,

      state,
      layout,
      navTheme,
      contentWidth,
      splitMenus,
      fixedHeader,
      fixedSidebar,
      transitionName,
      hasMix,

      handleOk,
      handleSettingBtnClick,

      handleLayoutChange,
      handleContentWidthChange,
      handleNavThemeChange,
      handleSplitMenusChange,
      handleChangeTransitionName,
    };
  },
  components: {
    SettingOutlined,
    List,
    ListItem,
  },
});
</script>
