<!-- 搜索表单 -->
<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 { ref, watch } from 'vue';
  import { AppParam } from '@/api/oa/app/model';
  import DictSelect from '@/components/DictSelect/index.vue';
  import { LinkParam } from '@/api/oa/link/model';
  import { openUrl } from '@/utils/common';
  import { hasRole } from '@/utils/permission';

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

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

  // 表单数据
  const { where, resetFields } = useSearch<LinkParam>({
    linkType: undefined,
    name: undefined,
    keywords: ''
  });
  // 下来选项
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);
  const type = ref<any>('全部');

  /* 搜索 */
  const search = () => {
    const [d1, d2] = dateRange.value ?? [];
    emit('search', {
      ...where,
      createTimeStart: d1 ? d1 + ' 00:00:00' : '',
      createTimeEnd: d2 ? d2 + ' 23:59:59' : ''
    });
  };

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

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

  const handleSearch = () => {
    where.showExpiration = undefined;
    where.appStatus = type.value;
    if (type.value == '全部') {
      where.appStatus = undefined;
      where.showExpiration = undefined;
    } else {
      where.appStatus = type.value;
      if (type.value == '未签续费') {
        where.sort = 'expirationTime';
        where.order = 'asc';
        where.showExpiration = false;
        where.appStatus = '已上架';
      } else {
        where.sort = undefined;
        where.order = undefined;
      }
    }
    emit('search', where);
  };

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

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