<!-- 搜索表单 -->
<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-button
      danger
      type="primary"
      class="ele-btn-icon"
      :disabled="selection.length === 0"
      @click="removeBatch"
    >
      <template #icon>
        <DeleteOutlined />
      </template>
      <span>批量删除</span>
    </a-button>
    <a-range-picker
      v-model:value="dateRange"
      @change="search"
      value-format="YYYY-MM-DD"
      class="ele-fluid"
    />
    <a-select
      v-model:value="where.week"
      style="width: 120px"
      placeholder="请选择星期"
      allow-clear
      @change="handleTabs"
    >
      <a-select-option :value="1">星期一</a-select-option>
      <a-select-option :value="2">星期二</a-select-option>
      <a-select-option :value="3">星期三</a-select-option>
      <a-select-option :value="4">星期四</a-select-option>
      <a-select-option :value="5">星期五</a-select-option>
      <a-select-option :value="6">星期六</a-select-option>
      <a-select-option :value="0">星期日</a-select-option>
    </a-select>
    <!--    <a-radio-group v-model:value="where.week" @change="handleTabs">-->
    <!--      <a-radio-button :value="0">星期日</a-radio-button>-->
    <!--      <a-radio-button :value="1">星期一</a-radio-button>-->
    <!--      <a-radio-button :value="2">星期二</a-radio-button>-->
    <!--      <a-radio-button :value="3">星期三</a-radio-button>-->
    <!--      <a-radio-button :value="4">星期四</a-radio-button>-->
    <!--      <a-radio-button :value="5">星期五</a-radio-button>-->
    <!--      <a-radio-button :value="6">星期六</a-radio-button>-->
    <!--    </a-radio-group>-->
    <a-input-search
      allow-clear
      placeholder="请输入关键词"
      v-model:value="where.keywords"
      @pressEnter="search"
      @search="search"
    />
  </a-space>
</template>

<script lang="ts" setup>
  import { PlusOutlined, DeleteOutlined } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import { ref, watch } from 'vue';
  import { BCPlanParam } from '@/api/apps/bc/plan/model';

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

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

  // 表单数据
  const { where } = useSearch<BCPlanParam>({
    keywords: undefined,
    userId: undefined,
    week: undefined,
    dayTime: undefined,
    createTimeStart: '',
    createTimeEnd: ''
  });
  // 下来选项
  const type = ref('keywords');
  // 搜索内容
  const searchText = ref('');
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);

  /* 搜索 */
  const search = () => {
    const [d1, d2] = dateRange.value ?? [];
    if (type.value == 'keywords') {
      where.keywords = searchText.value;
    }
    emit('search', {
      ...where,
      createTimeStart: d1 ? d1 + ' 00:00:00' : '',
      createTimeEnd: d2 ? d2 + ' 23:59:59' : ''
    });
  };

  // 发布菜品
  const add = () => {
    emit('add');
  };

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

  const handleTabs = (index) => {
    where.week = index;
    search();
  };

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