<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <a-button type="primary" class="ele-btn-icon" @click="add">
      <template #icon>
        <PlusOutlined />
      </template>
      <span>添加</span>
    </a-button>
    <a-radio-group v-model:value="type" @change="handleSearch">
      <a-radio-button value="全部">全部</a-radio-button>
      <a-radio-button value="已上架">已上架</a-radio-button>
      <a-radio-button value="已下架">已下架</a-radio-button>
    </a-radio-group>
    <a-input-search
      allow-clear
      placeholder="请输入关键词"
      v-model:value="where.keywords"
      @pressEnter="search"
      @search="search"
    />
    <a-button @click="reset">重置</a-button>
  </a-space>
</template>

<script lang="ts" setup>
  import { PlusOutlined } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import { ref, watch } from 'vue';
  import { AppParam } from '@/api/oa/app/model';

  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<AppParam>({
    appId: undefined,
    userId: undefined,
    appStatus: undefined,
    companyId: undefined,
    companyName: '',
    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 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>
