<template>
  <div style="display: flex; justify-content: space-between">
    <a-space style="flex-wrap: wrap">
      <a-button type="primary" class="ele-btn-icon" @click="openUrl('/system/my-task/add')">
        <template #icon>
          <plus-outlined />
        </template>
        <span>提交工单</span>
      </a-button>
      <a-radio-group v-model:value="where.status" @change="search">
        <a-radio-button value="0">待处理</a-radio-button>
        <a-radio-button value="1">已完成</a-radio-button>
      </a-radio-group>
      <a-button
        danger
        v-if="selection?.length"
        class="ele-btn-icon"
        @click="removeBatch"
      >
        <template #icon>
          <DeleteOutlined />
        </template>
        <span>批量删除</span>
      </a-button>
    </a-space>
    <a-space :size="10" style="flex-wrap: wrap; margin-right: 20px">
      <a-input-search
        allow-clear
        placeholder="工单号|关键词"
        v-model:value="where.keywords"
        @pressEnter="search"
        @search="search"
      />
      <a-button @click="reset">重置</a-button>
    </a-space>
  </div>
</template>

<script lang="ts" setup>
  import { TaskParam } from '@/api/oa/task/model';
  import useSearch from '@/utils/use-search';
  import { DeleteOutlined } from '@ant-design/icons-vue';
  import { App } from '@/api/oa/app/model';
  import DictSelect from '@/components/DictSelect/index.vue';
  import { User } from '@/api/system/user/model';
  import { openUrl } from "@/utils/common";

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

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

  // 表单数据
  const { where, resetFields } = useSearch<TaskParam>({
    keywords: '',
    status: undefined,
    commander: undefined
  });

  const removeBatch = () => {
    emit('remove');
  };

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

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

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

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

  if (props.status == undefined) {
    where.status = '0';
  }
</script>
