<!-- 搜索表单 -->
<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-button class="ele-btn-icon" @click="openImport">
      <template #icon>
        <UploadOutlined />
      </template>
      <span>导入</span>
    </a-button>
    <a-button class="ele-btn-icon" @click="handleExport">
      <template #icon>
        <DownloadOutlined />
      </template>
      <span>导出</span>
    </a-button>
    <a-radio-group v-model:value="status" @change="handleStatus">
      <a-radio-button :value="1">闲置</a-radio-button>
      <a-radio-button :value="2">在用</a-radio-button>
    </a-radio-group>
    <a-input-search
      allow-clear
      placeholder="请输入关键词"
      v-model:value="searchText"
      @pressEnter="search"
      @search="search"
    />
    <!-- 导入弹窗 -->
    <Import v-model:visible="showImport" @done="search" />
  </a-space>
</template>

<script lang="ts" setup>
  import {
    PlusOutlined,
    UploadOutlined,
    DownloadOutlined,
    DeleteOutlined
  } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import { ref, watch } from 'vue';
  import { utils, writeFile } from 'xlsx';
  import { TowerFall } from '@/api/tower/fall/model';
  import { TowerFallParam } from '@/api/tower/fall/model';
  import Import from './import.vue';
  import { message } from 'ant-design-vue';

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

  const emit = defineEmits<{
    (e: 'search', where?: TowerFallParam): void;
    (e: 'add'): void;
    (e: 'remove'): void;
    (e: 'update', status?: number): void;
    (e: 'import'): void;
  }>();

  // 表单数据
  const { where, resetFields } = useSearch<TowerFallParam>({
    id: undefined,
    name: undefined,
    keywords: ''
  });
  // 下来选项
  const type = ref('keywords');
  // 搜索内容
  const searchText = ref('');
  // 状态
  const status = ref<number>();
  // 是否显示导入弹窗
  const showImport = ref(false);

  /* 搜索 */
  const search = () => {
    resetFields();
    if (type.value == 'keywords') {
      where.keywords = searchText.value;
    }
    emit('search', {
      ...where
    });
  };

  // 导出
  const handleExport = () => {
    if (!props.selection?.length) {
      message.error('请至少选择一条数据');
      return;
    }
    const array: (string | number)[][] = [
      [
        '防坠器状态',
        '出厂编号',
        '防坠器型号',
        '制造厂商',
        '产权单位',
        '出厂日期',
        '报废日期'
      ]
    ];

    props.selection?.forEach((d: TowerFall) => {
      array.push([
        `${['闲置', '在用'][Number(d.status)]}`,
        `${d.code}`,
        `${d.model}`,
        `${d.factory}`,
        `${d.companyName}`,
        `${d.factoryDate}`,
        `${d.discardDate}`

      ]);
    });
    const sheetName = '防坠器导出列表';
    const workbook = {
      SheetNames: [sheetName],
      Sheets: {}
    };
    const sheet = utils.aoa_to_sheet(array);
    workbook.Sheets[sheetName] = sheet;
    // 设置列宽
    sheet['!cols'] = [
      { wch: 10 },
      { wch: 10 },
      { wch: 10 },
      { wch: 10 },
      { wch: 20 },
      { wch: 40 },
      { wch: 10 }
    ];
    writeFile(workbook, '导出防坠器表.xlsx');
  };

  // 发布设备
  const add = () => {
    emit('add');
  };

  // 导入
  const openImport = () => {
    showImport.value = true;
  };

  const handleStatus = () => {
    emit('update', status.value);
  };

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

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