<!-- 搜索表单 -->
<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-button
      danger
      type="primary"
      class="ele-btn-icon"
      :disabled="selection.length === 0"
      @click="removeBatch"
    >
      <template #icon>
        <DeleteOutlined />
      </template>
      <span>批量删除</span>
    </a-button>
    <a-radio-group v-model:value="where.appStatus" @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-button value="已下架">已下架</a-radio-button>
    </a-radio-group>
    <SelectCompany
      :placeholder="`请选择业主单位`"
      v-model:value="where.companyName"
      @done="chooseCompanyName"
    />
    <SelectCompany
      :placeholder="`请选择开发商`"
      v-model:value="where.developer"
      @done="chooseDeveloper"
    />
    <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, DeleteOutlined } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import { ref, watch } from 'vue';
  import { 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: '',
    companyId: undefined,
    companyName: '',
    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 chooseCompanyName = (data: Company) => {
    where.companyName = data.companyName;
    where.companyId = data.companyId;
    emit('search', where);
  };

  const chooseDeveloper = (data: Company) => {
    where.developer = data.companyName;
    emit('search', where);
  };

  const handleSearch = () => {
    emit('search', where);
  };

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

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