<!-- 搜索表单 -->
<template>
  <a-space>
    <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"
      @click="removeBatch"
      :disabled="props.selection.length === 0"
    >
      <template #icon>
        <DeleteOutlined />
      </template>
      <span>批量删除</span>
    </a-button>
    <SelectCompany
      :placeholder="`请选择所属企业`"
      v-model:value="where.companyName"
      @done="chooseCompanyName"
    />
    <DictSelect
      dict-code="serverBrand"
      :placeholder="`品牌厂商`"
      v-model:value="where.brand"
      style="width: 100px"
      @change="onBrand"
    />
    <DictSelect
      dict-code="serverStatus"
      :placeholder="`主机状态`"
      v-model:value="where.status"
      style="width: 100px"
      @change="onStatus"
    />
    <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 type { AssetsParam } from '@/api/oa/assets/model';
  // import SelectUser from '@/components/SelectUser/index.vue';
  import { ref, watch } from 'vue';
  import { assignObject } from 'ele-admin-pro';
  import { Company } from '@/api/system/company/model';

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

  const props = defineProps<{
    // 勾选的项目
    selection?: [];
  }>();

  // 表单数据
  const { where, resetFields } = useSearch<AssetsParam>({
    keywords: '',
    name: '',
    code: '',
    isExpire: '',
    companyName: '',
    userId: undefined
  });
  // 日期范围选择
  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 chooseCompanyName = (data: Company) => {
    where.companyName = data.companyName;
    where.companyId = data.companyId;
    emit('search', where);
  };

  const onStatus = (text) => {
    where.status = text;
    emit('search', where);
  };

  const onBrand = (text) => {
    where.brand = text;
    emit('search', where);
  };

  // 新增
  const add = () => {
    emit('add');
  };

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

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

  // 监听字典id变化
  watch(
    () => props.selection,
    () => {}
  );
</script>
