<!-- 搜索表单 -->
<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="type" @change="handleSearch">
      <a-radio-button value="全部">全部({{ conut.totalNum }})</a-radio-button>
      <a-radio-button value="运行中"
        >运行中({{ conut.totalNum1 }})</a-radio-button
      >
      <a-radio-button value="已到期"
        >已到期({{ conut.totalNum2 }})</a-radio-button
      >
      <a-radio-button value="已销毁"
        >已销毁({{ conut.totalNum3 }})</a-radio-button
      >
    </a-radio-group>
    <SelectCompany
      :placeholder="`按企业筛选`"
      v-model:value="where.companyName"
      @done="chooseCompanyName"
    />
    <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 { AssetsParam } from '@/api/oa/assets/model';
  import { Company } from '@/api/system/company/model';
  import { getCount } from '@/api/oa/assets';

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

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

  // 下来选项
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);
  const type = ref<any>('开发中');

  // 表单数据
  const { where, resetFields } = useSearch<AssetsParam>({
    assetsId: undefined,
    userId: undefined,
    status: undefined,
    companyId: undefined,
    showExpiration: '',
    companyName: '',
    keywords: ''
  });

  // 统计数据
  const conut = ref({
    totalNum: 0,
    totalNum1: 0,
    totalNum2: 0,
    totalNum3: 0,
    totalNum4: 0,
    totalNum5: 0
  });

  /* 搜索 */
  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 handleSearch = () => {
    where.showExpiration = undefined;
    where.status = type.value;
    if (type.value == '全部') {
      where.status = undefined;
      where.showExpiration = undefined;
    } else {
      where.status = type.value;
      if (type.value == '未签续费') {
        where.sort = 'expirationTime';
        where.order = 'asc';
        where.showExpiration = false;
        where.status = '运行中';
      } else {
        where.sort = undefined;
        where.order = undefined;
      }
    }
    emit('search', where);
  };

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

  getCount().then((data: any) => {
    conut.value = data;
  });

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