<!-- 搜索表单 -->
<template>
  <div style="display: flex; justify-content: space-between">
    <a-space style="flex-wrap: wrap">
      <a-button type="primary" class="ele-btn-icon" @click="add">
        <template #icon>
          <PlusOutlined />
        </template>
        <span>添加网址</span>
      </a-button>
      <DictSelect
        dict-code="linkType"
        v-model:value="where.linkType"
        :placeholder="`选择分类`"
        style="width: 200px"
        @change="search"
      />
      <a-input-search
        allow-clear
        placeholder="搜索关键词"
        v-model:value="where.keywords"
        @pressEnter="search"
        @search="search"
      />
      <a-button @click="reset">重置</a-button>
      <a-button
        v-if="hasRole('superAdmin')"
        @click="openUrl(`/website/link/dict`)"
        >设置分类</a-button
      >
    </a-space>
    <a-space :size="10" style="flex-wrap: wrap; margin-right: 20px">
      <a-button
        danger
        v-if="selection?.length"
        class="ele-btn-icon"
        @click="removeBatch"
      >
        <template #icon>
          <DeleteOutlined />
        </template>
        <span>批量删除</span>
      </a-button>
    </a-space>
  </div>
</template>

<script lang="ts" setup>
  import { DeleteOutlined, PlusOutlined } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import { watch } from 'vue';
  import DictSelect from '@/components/DictSelect/index.vue';
  import { LinkParam } from '@/api/cms/link/model';
  import { openUrl } from '@/utils/common';
  import { hasRole } from '@/utils/permission';

  const props = withDefaults(
    defineProps<{
      // 选中的角色
      selection?: [];
    }>(),
    {}
  );

  const emit = defineEmits<{
    (e: 'search', where?: LinkParam): void;
    (e: 'add'): void;
    (e: 'remove'): void;
    (e: 'advanced'): void;
  }>();

  // 表单数据
  const { where, resetFields } = useSearch<LinkParam>({
    linkType: undefined,
    name: undefined,
    keywords: ''
  });

  /* 搜索 */
  const search = () => {
    emit('search', {
      ...where
    });
  };

  // 发布应用
  const add = () => {
    emit('add');
  };

  // 批量删除
  const removeBatch = () => {
    emit('remove');
  };

  /*  重置 */
  const reset = () => {
    resetFields();
    search();
  };

  watch(
    () => props.selection,
    () => {}
  );
</script>
