<!-- 搜索表单 -->
<template>
  <a-space>
    <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"
      :v-role="`dev`"
      @click="removeBatch"
      :disabled="props.selection.length === 0"
    >
      <template #icon>
        <DeleteOutlined />
      </template>
      <span>批量删除</span>
    </a-button>
    <!--    <a-radio-group v-model:value="listType" @change="handleTabs">-->
    <!--      <a-radio-button value="all">全部</a-radio-button>-->
    <!--      <a-radio-button value="onSale">出售中</a-radio-button>-->
    <!--      <a-radio-button value="offSale">已下架</a-radio-button>-->
    <!--      <a-radio-button value="soldOut">已售罄</a-radio-button>-->
    <!--    </a-radio-group>-->
    <a-select v-model:value="isBind" style="width: 120px; margin: 0px 2px 0px 2px;" defaultValue="-1">
      <a-select-option value="-1">全部</a-select-option>
      <a-select-option value="0">未绑定</a-select-option>
      <a-select-option value="1">已绑定</a-select-option>
    </a-select>
    <a-input-search
      allow-clear
      v-model:value="searchText"
      placeholder="请输入搜索关键词"
      @search="search"
      @pressEnter="search"
    >
      <template #addonBefore>
        <a-select v-model:value="type" style="width: 120px; margin: -5px -12px">
          <a-select-option value="equipmentCode">设备编号</a-select-option>
          <a-select-option value="equipmentName">设备名称</a-select-option>
          <a-select-option value="merchantCode">商户编号</a-select-option>
        </a-select>
      </template>
    </a-input-search>
    <!--    <CustomerSelect-->
    <!--      v-model:value="where.equipmentId"-->
    <!--      :placeholder="`按设备筛选`"-->
    <!--      @select="onSelect"-->
    <!--      @clear="onClear"-->
    <!--    />-->
  </a-space>
</template>

<script lang="ts" setup>
  import {
    PlusOutlined,
    EditOutlined,
    DeleteOutlined
  } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import type { EquipmentParam } from '@/api/apps/equipment/model';
  // import CustomerSelect from '@/components/CustomerSelect/index.vue';
  import { ref, watch } from 'vue';

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

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

  // 表单数据
  const { where } = useSearch<EquipmentParam>({
    equipmentId: undefined,
    equipmentName: '',
    equipmentCode: '',
    merchantCode: '',
    isBind: undefined
  });

  // 下来选项
  const type = ref('equipmentCode');
  const isBind = ref();
  // tabType
  // const listType = ref('all');
  // 搜索内容
  const searchText = ref('');

  /* 搜索 */
  const search = () => {
    where.equipmentName = undefined;
    where.equipmentCode = undefined;
    where.merchantCode = undefined;
    where.isBind = undefined;
    if (type.value == 'equipmentName') {
      where.equipmentName = searchText.value;
    } else if (type.value == 'equipmentCode') {
      where.equipmentCode = searchText.value;
    } else if (type.value == 'merchantCode') {
      where.merchantCode = searchText.value;
    }
    if (isBind.value && isBind.value != '-1') {
      where.isBind = parseInt(isBind.value);
    } else {
      where.isBind = undefined;
    }
    emit('search', where);
  };

  // const handleTabs = (e) => {
  //   listType.value = e.target.value;
  //   if (listType.value == 'all') {
  //     resetFields();
  //   }
  //   if (listType.value == 'onSale') {
  //     where.status = 0;
  //   }
  //   if (listType.value == 'offSale') {
  //     where.status = 1;
  //   }
  //   if (listType.value == 'soldOut') {
  //     where.stockTotal = 0;
  //   }
  //   emit('search', where);
  // };

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

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

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

  // 监听字典id变化
  watch(
    () => props.selection,
    () => {}
  );
</script>
