<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <!--    <SelectCompany-->
    <!--      :placeholder="`按企业筛选`"-->
    <!--      v-model:value="where.companyName"-->
    <!--      @done="chooseCompany"-->
    <!--    />-->
    <SelectApp
      :placeholder="`按应用筛选`"
      v-model:value="where.appName"
      @done="chooseApp"
    />
    <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 useSearch from '@/utils/use-search';
  import { ref, watch } from 'vue';
  import { App, AppParam } from '@/api/oa/app/model';
  import { Company } from '@/api/system/company/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,
    keywords: ''
  });
  // 下来选项
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);

  /* 搜索 */
  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 chooseCompany = (data: Company) => {
    where.companyName = data.companyName;
    where.companyId = data.companyId;
    emit('search', where);
  };

  const chooseApp = (data: App) => {
    where.appName = data.appName;
    where.appId = data.appId;
    emit('search', where);
  };

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

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