<!-- 搜索表单 -->
<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-radio-group v-model:value="type" @change="handleSearch">
      <a-radio-button value="出售中"
        >出售中({{ goodsCount?.totalNum }})</a-radio-button
      >
      <a-radio-button value="待上架"
        >待上架({{ goodsCount?.totalNum2 }})</a-radio-button
      >
      <a-radio-button value="已售罄"
        >已售罄({{ goodsCount?.totalNum3 }})</a-radio-button
      >
    </a-radio-group>
    <a-input-search
      allow-clear
      placeholder="请输入关键词"
      v-model:value="where.keywords"
      @pressEnter="reload"
      @search="reload"
    />
    <a-button @click="reset">重置</a-button>
  </a-space>
</template>

<script lang="ts" setup>
  import { PlusOutlined } from '@ant-design/icons-vue';
  import type { GradeParam } from '@/api/user/grade/model';
  import { ref, watch } from 'vue';
  import { getCount } from '@/api/shop/goods';
  import type { GoodsCount, GoodsParam } from '@/api/shop/goods/model';
  import useSearch from '@/utils/use-search';
  import { useRouter } from 'vue-router';
  const { currentRoute } = useRouter();

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

  const type = ref<string>();
  // 统计数据
  const goodsCount = ref<GoodsCount>();

  // 表单数据
  const { where, resetFields } = useSearch<GoodsParam>({
    goodsId: undefined,
    status: undefined,
    isShow: undefined,
    stock: undefined,
    keywords: ''
  });

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

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

  const handleSearch = (e) => {
    const text = e.target.value;
    resetFields();
    if (text === '出售中') {
      where.isShow = 1;
    }
    if (text === '待上架') {
      where.isShow = 0;
    }
    if (text === '已售罄') {
      where.stock = 0;
    }
    emit('search', where);
  };

  const reload = () => {
    getCount().then((data: any) => {
      goodsCount.value = data;
    });
    emit('search', where);
  };

  /*  重置 */
  const reset = () => {
    resetFields();
    type.value = '';
    reload();
  };

  // watch(
  //   () => props.selection,
  //   () => {}
  // );
  watch(
    currentRoute,
    () => {
      reload();
    },
    { immediate: true }
  );
</script>
