<!-- 快捷方式 -->
<template>
  <a-row :gutter="16" ref="wrapRef">
    <a-col v-for="item in data" :key="item.url" :lg="3" :md="6" :sm="9" :xs="8">
      <a-card :bordered="false" hoverable :body-style="{ padding: 0 }">
        <div class="app-link-block" @click="navTo(item)">
          <component
            :is="item.icon"
            class="app-link-icon"
            :style="{ color: item.color }"
          />
          <div class="app-link-title">{{ item.title }}</div>
        </div>
      </a-card>
    </a-col>
  </a-row>
</template>

<script lang="ts" setup>
  import { ref, onMounted, onBeforeUnmount } from 'vue';
  import SortableJs from 'sortablejs';
  import type { Row as ARow } from 'ant-design-vue';
  import { openNew, openUrl } from '@/utils/common';
  import { getOriginDomain } from '@/utils/domain';
  const CACHE_KEY = 'workplace-links';
  // 当前开发环境
  const env = import.meta.env.MODE;

  interface LinkItem {
    icon: string;
    title: string;
    url: string;
    color?: string;
  }

  // 默认顺序
  const DEFAULT: LinkItem[] = [
    {
      icon: 'settingOutlined',
      title: '网站设置',
      url: '/website/setting'
    },
    {
      icon: 'LayoutOutlined',
      title: '单页管理',
      url: '/cms/design'
    },
    {
      icon: 'FileSearchOutlined',
      title: '文章管理',
      url: '/cms/article'
    },
    {
      icon: 'AntDesignOutlined',
      title: '广告管理',
      url: '/website/ad'
    },
    {
      icon: 'InstagramOutlined',
      title: '图片素材',
      url: '/cms/photo'
    },
    {
      icon: 'ShoppingOutlined',
      title: '表单数据',
      url: '/cms/form-record'
    },
    {
      icon: 'ChromeOutlined',
      title: '友情链接',
      url: '/oa/link'
    },
    {
      icon: 'DesktopOutlined',
      title: '网站首页',
      url: `${localStorage.getItem('TenantId')}.${localStorage.getItem(
        'domain'
      )}`
    }
  ];

  // 获取缓存的顺序
  const cache = (() => {
    const str = localStorage.getItem(CACHE_KEY);
    try {
      return str ? JSON.parse(str) : null;
    } catch (e) {
      return null;
    }
  })();

  const data = ref<LinkItem[]>([...(cache ?? DEFAULT)]);

  const wrapRef = ref<InstanceType<typeof ARow> | null>(null);

  let sortableIns: SortableJs | null = null;

  /* 重置布局 */
  const reset = () => {
    data.value = [...DEFAULT];
    cacheData();
  };

  /* 缓存布局 */
  const cacheData = () => {
    localStorage.setItem(CACHE_KEY, JSON.stringify(data.value));
  };

  const navTo = (item) => {
    if (item.icon == 'DesktopOutlined') {
      // if (env == 'development') {
      //   return openUrl(getOriginDomain());
      // }
      return openNew(`${domain.value}`);
    }
    openUrl(item.url);
  };

  onMounted(() => {
    const isTouchDevice = 'ontouchstart' in document.documentElement;
    if (isTouchDevice) {
      return;
    }
    sortableIns = new SortableJs(wrapRef.value?.$el, {
      animation: 300,
      onUpdate: ({ oldIndex, newIndex }) => {
        if (typeof oldIndex === 'number' && typeof newIndex === 'number') {
          const temp = [...data.value];
          temp.splice(newIndex, 0, temp.splice(oldIndex, 1)[0]);
          data.value = temp;
          cacheData();
        }
      },
      setData: () => {}
    });
  });

  onBeforeUnmount(() => {
    if (sortableIns) {
      sortableIns.destroy();
    }
  });

  defineExpose({ reset });
</script>

<script lang="ts">
  import * as icons from './link-icons';
  import { getSiteInfo } from '@/api/layout';
  import { ref } from 'vue';

  const tenantId = ref<number>();
  const domain = ref<string>();

  getSiteInfo().then((data) => {
    tenantId.value = data.tenantId;
    domain.value = data.domain;
  });

  export default {
    components: icons
  };
</script>

<style lang="less" scoped>
  .app-link-block {
    padding: 12px;
    text-align: center;
    display: block;
    color: inherit;

    .app-link-icon {
      color: #666666;
      font-size: 30px;
      margin: 6px 0 10px 0;
    }
  }
</style>
