<!-- 搜索表单 -->
<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 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-button
      danger
      type="primary"
      class="ele-btn-icon"
      v-if="selection.length > 0"
      @click="removeBatch"
    >
      <template #icon>
        <DeleteOutlined />
      </template>
      <span>批量删除</span>
    </a-button>
    <a-button> 批量生成二维码 </a-button>
    <a-button> 打印配件码 </a-button>
    <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 type { AccessoryParam } from '@/api/tower/accessory/model';
  import { ref, watch } from 'vue';
  import { utils, writeFile } from 'xlsx';
  import { message } from 'ant-design-vue/es';
  import { Accessory } from '@/api/tower/accessory/model';
  import Import from './import.vue';

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

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

  // 表单数据
  const { where } = useSearch<AccessoryParam>({
    accessoryId: undefined,
    accessoryName: '',
    status: undefined,
    keywords: ''
  });
  // 下来选项
  // 搜索内容
  const searchText = ref('');
  // 是否显示导入弹窗
  const showImport = ref(false);

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

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

  // 导出
  const handleExport = () => {
    if (!props.selection?.length) {
      message.error('请至少选择一条数据');
      return;
    }
    const array: (string | number)[][] = [
      [
        '配件名称',
        '配件分类',
        '适用设备型号',
        '配件规格',
        '配件单位',
        '重量',
        '单价',
        '自编号',
        '产权单位编码',
        '产权单位名称',
        '制造厂家',
        '出厂日期',
        '备注'
      ]
    ];

    props.selection?.forEach((d: Accessory) => {
      array.push([
        `${d.accessoryName}`,
        `${d.accessoryCategory}`,
        `${d.accessoryModel}`,
        `${d.accessorySpecs}`,
        `${d.accessoryUnit}`,
        `${d.accessoryWeight}`,
        `${d.accessoryPrice}`,
        `${d.accessoryNo}`,
        `${d.companyNo}`,
        `${d.companyName}`,
        `${d.manufactor}`,
        `${d.factoryDate}`,
        `${d.comments}`
      ]);
    });
    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 removeBatch = () => {
    emit('remove');
  };

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

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