<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap; margin-bottom: 16px">
    <a-button type="primary" class="ele-btn-icon" @click="add">
      <template #icon>
        <PlusOutlined />
      </template>
      <span>添加</span>
    </a-button>
    <!--    <a-radio-group v-if="hasRole('superAdmin') || hasRole('admin')" v-model:value="where.appStatus" @change="handleAppStatus">-->
    <!--      <a-radio-button value="all">全部({{ conut.totalNum }})</a-radio-button>-->
    <!--      <a-radio-button value="开发中"-->
    <!--      >开发中({{ conut.totalNum2 }})-->
    <!--      </a-radio-button-->
    <!--      >-->
    <!--      <a-radio-button value="已上架"-->
    <!--      >运行中({{ conut.totalNum3 }})-->
    <!--      </a-radio-button-->
    <!--      >-->
    <!--      <a-radio-button value="已下架"-->
    <!--      >已下线({{ conut.totalNum5 }})-->
    <!--      </a-radio-button-->
    <!--      >-->
    <!--    </a-radio-group>-->
    <a-radio-group v-model:value="where.sceneType" @change="handleSceneType">
      <a-radio-button value="myProject">我的项目</a-radio-button>
      <a-radio-button value="involved">我的参与</a-radio-button>
      <a-radio-button value="collection">我的收藏</a-radio-button>
    </a-radio-group>
    <a-select
      v-model:value="where.appType"
      style="width: 150px"
      placeholder="产品类型"
      @change="search"
    >
      <a-select-option value="云·企业官网">云·企业官网</a-select-option>
      <a-select-option value="小程序开发">小程序开发</a-select-option>
      <a-select-option value="企业商城">企业商城</a-select-option>
      <a-select-option value="办公协同(OA)">办公协同(OA)</a-select-option>
    </a-select>
    <a-select
      v-model:value="where.appStatus"
      style="width: 150px"
      :placeholder="`开发进度(${conut.totalNum})`"
      @change="handleAppStatus"
    >
      <a-select-option value="开发中">开发中({{conut.totalNum2}})</a-select-option>
      <a-select-option value="已上架">已上架({{conut.totalNum3}})</a-select-option>
      <a-select-option value="已下架">已下线({{conut.totalNum5}})</a-select-option>
    </a-select>
    <a-input-search
      allow-clear
      placeholder="请输入关键词"
      v-model:value="where.keywords"
      @pressEnter="search"
      @search="search"
    />
    <a-button @click="reset">重置</a-button>
  </a-space>
</template>

<script lang="ts" setup>
  import { PlusOutlined } from '@ant-design/icons-vue';
  import {ProjectParam} from "@/api/project/project/model";
  import {ref, watch} from 'vue';
  import useSearch from "@/utils/use-search";
  import {getData} from "@/api/project/project";
  import {hasRole} from "@/utils/permission";

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

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

  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);
  // 表单数据
  const {where,resetFields} = useSearch<ProjectParam>({
    appId: undefined,
    userId: undefined,
    appStatus: undefined,
    companyId: undefined,
    keywords: ''
  });

  // 统计数据
  const conut = ref({
    totalNum: 0,
    totalNum2: 0,
    totalNum3: 0,
    totalNum4: 0,
    totalNum5: 0,
    totalNum6: 0,
    totalNum7: 0
  });

  const handleSceneType = () => {
    where.appStatus = undefined;
    emit('search', where);
  };

  const handleAppStatus = () => {
    where.sceneType = undefined;
    if(where.appStatus === 'all'){
      where.appStatus = undefined;
    }
    emit('search', where);
  };

  /* 搜索 */
  const search = () => {
    const [d1, d2] = dateRange.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 reset = () => {
    resetFields();
    search();
  };

  getData().then((data: any) => {
    conut.value = data;
  });

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