<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <a-button type="primary" class="ele-btn-icon" @click="add">
      <template #icon>
        <plus-outlined />
      </template>
      <span>添加工单</span>
    </a-button>
    <a-button
      danger
      type="primary"
      class="ele-btn-icon"
      :disabled="selection.length === 0"
      @click="removeBatch"
    >
      <template #icon>
        <delete-outlined />
      </template>
      <span>批量删除</span>
    </a-button>
    <a-radio-group v-model:value="where.status" @change="handleSearch">
      <a-radio-button :value="0">{{ `待处理(${count1})` }}</a-radio-button>
      <a-radio-button :value="1">{{ `已完成(${count2})` }}</a-radio-button>
    </a-radio-group>
    <SelectCompany
      v-model:value="where.promoterName"
      placeholder="选择客户"
      @done="onPromoter"
    />
    <SelectStaff
      v-model:value="where.commanderName"
      placeholder="选择用户"
      @done="onCommander"
    />
    <a-input-search
      allow-clear
      placeholder="请输入关键词"
      v-model:value="where.keywords"
      @pressEnter="search"
      @search="search"
    />
    <a-button @click="reload">重置</a-button>
  </a-space>
</template>

<script lang="ts" setup>
  import useSearch from '@/utils/use-search';
  import { ref, watch } from 'vue';
  import { TaskParam } from '@/api/oa/task/model';
  import {
    PlusOutlined,
    EditOutlined,
    SearchOutlined,
    DeleteOutlined,
    UpSquareOutlined,
    DownSquareOutlined
  } from '@ant-design/icons-vue';
  import { Company } from '@/api/system/company/model';
  import { User } from '@/api/system/user/model';
  import { getCount } from '@/api/oa/task';

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

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

  const count1 = ref(0);
  const count2 = ref(0);
  // 表单数据
  const { where } = useSearch<TaskParam>({
    taskId: undefined,
    name: undefined,
    userId: undefined,
    status: undefined,
    nickname: undefined,
    keywords: undefined,
    promoterName: undefined
  });

  // 搜索内容
  // const searchText = ref('');

  /* 搜索 */
  const search = () => {
    emit('search', where);
  };

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

  const onPromoter = (data: Company) => {
    where.promoterName = data.companyName;
    where.promoter = data.userId;
    emit('search', where);
  };

  const onCommander = (data: User) => {
    where.commanderName = data.nickname;
    where.commander = data.userId;
    emit('search', where);
  };

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

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

  const reload = () => {
    // 刷新当前路由
    emit('search', {});
  };

  /* 查询 */
  const query = () => {
    getCount().then((result) => {
      count1.value = result[0].count;
      count2.value = result[1].count;
    });
  };

  query();

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