<template>
  <v-btn
    v-if="!isHome"
    :loading="loading"
    color="primary"
    small
    @click="handleAddToHome"
  >添加到工作台 {{ type === 2 ? '快捷' : '' }}</v-btn>
</template>

<script>
import { saveWorkbench } from '@/api/menu'
export default {
  name: 'add-to-home',
  props: {
    // 1: 动态添加组件模块，2：动态添加快捷路径
    type: {
      type: Number,
      default: 1
    },
    label: {
      type: String,
      default: '标题'
    },
    en: {
      type: String,
      default: 'Title'
    },
    routeName: {
      type: String,
      default: 'addToHome'
    },
    // 组件模块导入路径 输入/代表/views/ 不需输入.vue
    // 快捷方式 输入路由地址
    path: {
      type: String,
      default: ''
    },
    itemObjContent: { // 低代码图表参数
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      loading: false,
      isHome: false
    }
  },
  created () {
    this.isHome = this.$route.path === '/home'
  },
  methods: {
    async handleAddToHome () {
      this.loading = true
      const query = {
        active: '1',
        name: this.routeName, // 组件路由
        label: this.label,
        enName: this.en,
        sort: 999,
        meta: {}
      }
      if (this.type) { // null
        Object.assign(query.meta, {
          homeType: this.type, // 添加首页模块类型 1：添加组件模块 2：快捷路径 3：自定义第三方地址
          componentsPath: this.path
        })
      }
      if (this.itemObjContent) Object.assign(query.meta, { itemObjContent: this.itemObjContent }) // 自动图表（定制图表）
      try {
        await saveWorkbench(query)
        // await this.$store.dispatch('menu/saveMenu', query)
        this.$snackbar.success('保存成功')
      } catch (error) {
        this.$snackbar.error(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
