<script lang="ts" setup>
import { IconCaretUp } from '@computing/opendesign-icons';
import { reactive, ref } from 'vue';
import Date from '@/utils/date';
import { getThemeMode } from '@/utils/utils';
import { showSuccessMessage } from '@/utils/composition';
import { setStoreData } from '@/utils/composition';
import { useRouter } from 'vue-router';
import Dialog from '@/components/Dialog/Dialog.vue';
import Home from '@/pages/Home/Home.vue';

const emits = defineEmits(['back', 'save']);

const router = useRouter();
const componentKey = ref(0);
const options = [
  {
    label: '模板一',
    value: 'ibma',
    show: true,
  },
  {
    label: '模板二',
    value: 'simple',
    show: true,
  },
  {
    label: '模板三',
    value: 'beautiful',
    show: true,
  },
  {
    label: '模板四',
    value: 'standard',
    show: true,
  },
];
const oldData = ref(JSON.parse(localStorage.getItem('customizeStyle') || '{}')?.pageStyle);
const select = ref(JSON.parse(localStorage.getItem('customizeStyle') || '{}')?.pageStyle);

const resetRef = ref();
const cancelRef = ref();
const confirmRef = ref();

// 恢复默认弹窗
const resetConfig = reactive({
  title: '恢复默认首页',
  content: '执行该操作将恢复默认首页，请确认是否继续？',
  okButton: { id: 'resetConfig_ok_btn', label: '确认', hide: false },
  cancelButton: { id: 'resetConfig_cancel_btn', label: '取消', hide: false },
});
// 取消弹窗
const cancelConfig = reactive({
  title: '确认',
  content: '是否取消当前所有操作并退出？',
  okButton: { id: 'cancelConfig_ok_btn', label: '确认', hide: false },
  cancelButton: { id: 'cancelConfig_cancel_btn', label: '取消', hide: false },
});
// 确认弹窗
const confirmConfig = reactive({
  title: '确认',
  type: 'success',
  content: '保存成功！系统即将自动刷新并返回至首页。',
  okButton: { id: 'confirmConfig_ok_btn', label: '确认', hide: false },
  cancelButton: { id: 'confirmConfig_cancel_btn', label: '取消', hide: true },
});

function closeResetModal(value: boolean): void {
  if (value) {
    select.value = '';
    oldData.value = '';
    saveConfig('default');
    componentKey.value += 1;
    reloadPage();
  }
  resetRef.value.hide();
}

function reset(): void {
  resetRef.value.show();
}

function selectChange(item: { label: string; value: string; show: boolean }): void {
  saveConfig(item.value);
  componentKey.value += 1;
}

function saveConfig(pageStyle: string) {
  // 设置第三步标识位，用于标记定制工具已设置
  localStorage.setItem('ptc2', 'true');
  setStoreData('glob', 'webStyle', pageStyle);
  const customizeStyle = JSON.parse(localStorage.getItem('customizeStyle') || '{}');
  customizeStyle['pageStyle'] = pageStyle === 'default' ? undefined : pageStyle;
  customizeStyle['time'] = new Date().pattern('yyyy/MM/dd HH:mm:ss');
  localStorage.setItem('customizeStyle', JSON.stringify(customizeStyle));
}

function closeConfirmModal(value: boolean): void {
  reloadPage();
}

function save(): void {
  confirmRef.value.show();
}

function closeCancelModal(value: boolean): void {
  if (value) {
    saveConfig(oldData.value || 'default');
    emits('back', null);
  }
  cancelRef.value.hide();
}

function back(): void {
  cancelRef.value.show();
}

function reloadPage(): void {
  router.push('/navigate/home').then(() => {
    window.location.reload();
  });
}
</script>

<template>
  <teleport to="#customizedHomepage">
    <div class="container">
      <div class="header">
        <div class="title" style="padding-left: 4px">openUBMC</div>
        <div class="title" style="padding-left: 16px">首页卡片自定义工具</div>
        <div class="select">
          <el-select
            v-model="select"
            v-addId.select="'templateSelected'"
            popper-class="template-select"
            placeholder="请选择首页模板"
            :teleported="false"
            :suffix-icon="IconCaretUp"
          >
            <template v-for="item in options" :key="item.value">
              <el-option
                v-if="item.show"
                :label="item.label"
                :value="item.value"
                @click="selectChange(item)"
              >
                <div class="template-item">{{ item.label }}</div>
              </el-option>
            </template>
          </el-select>
        </div>
      </div>
      <div class="content">
        <div class="overlay"></div>
        <Home :key="componentKey" />
      </div>
      <div class="footer">
        <el-button id="dz-save" type="primary" :disabled="oldData === select" @click="save()">
          保存
        </el-button>
        <el-button id="dz-cancel" @click="back()">取消</el-button>
        <el-button id="dz-reset" @click="reset()">恢复默认首页</el-button>
      </div>
      <!-- 恢复默认 -->
      <Dialog ref="resetRef" :config="resetConfig" @close="closeResetModal"></Dialog>
      <!-- 取消 -->
      <Dialog ref="cancelRef" :config="cancelConfig" @close="closeCancelModal"></Dialog>
      <!-- 确认 -->
      <Dialog ref="confirmRef" :config="confirmConfig" @close="closeConfirmModal"></Dialog>
    </div>
  </teleport>
</template>

<style lang="scss" scoped>
.container {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 2100;
  width: 100vw;
  height: 100vh;

  .header {
    width: 100%;
    height: 48px;
    background: var(--o-bg-color-base);
    box-shadow: inset 0 -2px 0 0 #f4f6fa;
    padding: 12px;

    .title {
      display: inline-block;
      font-size: 16px;
      color: var(--o-text-color-primary);
      line-height: 24px;
      font-weight: 600;
    }
    #templateSelected {
      position: absolute;
      top: 7px;
      left: 50%;
      right: 50%;
      width: 160px;
      background: var(--o-border-color-base);
      border-radius: 4px;

      :deep(.el-input__wrapper) {
        box-shadow: none;
      }

      :deep(.el-input__wrapper:hover) {
        box-shadow: none;
        background: var(--o-border-color-base);
      }
    }
  }

  .content {
    width: 100%;
    height: calc(100% - 112px);
    overflow-y: scroll;
    background: var(--o-bg-color-light);
    position: relative;

    .overlay {
      width: 100%;
      height: 100%;
      position: absolute;
      z-index: 1;
    }
  }

  .footer {
    width: 100%;
    height: 64px;
    background: var(--o-bg-color-base);
    box-shadow: 0 8px 16px 0 rgba (0, 0, 0, 0.1);
    text-align: center;
    padding: 16px;
  }
}
</style>
