<!-- 快捷方式 -->
<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 }">
        <router-link :to="item.url" class="app-link-block">
          <component
            :is="item.icon"
            class="app-link-icon"
            :style="{ color: item.color }"
          />
          <div class="app-link-title">{{ item.title }}</div>
        </router-link>
      </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';
  const CACHE_KEY = 'workplace-links';

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

  // 默认顺序
  const DEFAULT: LinkItem[] = [
    {
      icon: 'ChromeOutlined',
      title: '网址导航',
      url: '/apps/link'
    },
    {
      icon: 'LaptopOutlined',
      title: '项目管理',
      url: '/oa/project'
    },
    {
      icon: 'ShoppingOutlined',
      title: '商城系统',
      url: '/shop/goods'
    },
    {
      icon: 'FileSearchOutlined',
      title: 'CMS内容管理系统',
      url: '/cms/article'
    },
    {
      icon: 'settingOutlined',
      title: '系统设置',
      url: '/system/user'
    },
    {
      icon: 'AppstoreAddOutlined',
      title: '扩展插件',
      url: '/system/appstore'
    },
    {
      icon: 'TeamOutlined',
      title: '用户管理',
      url: '/system/user'
    },
    {
      icon: 'DesktopOutlined',
      title: '网站首页',
      url: '/apps/home/index'
    }
  ];

  // 获取缓存的顺序
  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));
  };

  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';

  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>
