<!-- 搜索表单 -->
<template>
  <a-space>
    <a-button type="primary" class="ele-btn-icon" @click="add">
      <template #icon>
        <plus-outlined />
      </template>
      <span>新建</span>
    </a-button>
    <template v-if="!merchantId">
      <a-button type="dashed" class="ele-btn-icon" @click="expandAll">
        展开全部
      </a-button>
      <a-button type="dashed" class="ele-btn-icon" @click="foldAll">
        折叠全部
      </a-button>
    </template>
    <SelectMerchant
      :placeholder="`选择商户`"
      class="input-item"
      v-if="!merchantId"
      v-model:value="where.merchantName"
      @done="chooseMerchantId"
    />
    <!--    <SelectGoodsCategory-->
    <!--      v-if="!merchantId"-->
    <!--      class="input-item"-->
    <!--      :placeholder="`请选择商品分类`"-->
    <!--      v-model:value="where.categoryId"-->
    <!--      @done="chooseGoodsCategory"-->
    <!--    />-->
    <a-input-search
      allow-clear
      placeholder="请输入关键词"
      v-model:value="where.keywords"
      @pressEnter="reload"
      @search="reload"
    />
  </a-space>
  <!--  <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>-->
  <!--    <SelectMerchant-->
  <!--      :placeholder="`选择商户`"-->
  <!--      class="input-item"-->
  <!--      v-if="!merchantId"-->
  <!--      v-model:value="where.merchantName"-->
  <!--      @done="chooseMerchantId"-->
  <!--    />-->
  <!--    <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';
  import { Merchant } from '@/api/shop/merchant/model';
  import { GoodsCategory } from '@/api/shop/goodsCategory/model';
  const { currentRoute } = useRouter();

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

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

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

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

  const expandAll = () => {
    emit('expand');
  };

  const foldAll = () => {
    emit('fold');
  };

  // 新增
  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 chooseMerchantId = (item: Merchant) => {
    where.merchantName = item.merchantName;
    where.merchantId = item.merchantId;
    reload();
  };

  const chooseGoodsCategory = (
    category: GoodsCategory,
    data: GoodsCategory
  ) => {
    where.categoryName = data[1].label;
    where.categoryId = data[1].value;
    reload();
  };

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

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