<!-- 搜索表单 -->
<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-button :value="0">恢复</a-radio-button>
      <a-radio-button :value="3">转卖</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 { TowerEquipment } from '@/api/tower/equipment/model';
  import { TowerEquipmentParam } from '@/api/tower/equipment/model';
  import Import from './import.vue';
  import { message } from 'ant-design-vue';

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

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

  // 表单数据
  const { where, resetFields } = useSearch<TowerEquipmentParam>({
    equipmentId: 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)[][] = [
      [
        '设备状态',
        '设备名称',
        '出厂编号',
        '设备型号',
        '制造厂商',
        '采购日期',
        '备案编号',
        '出厂日期',
        '产权单位',
        '设备来源',
        '所属仓库',
        '所属人员',
        '自编号',
        '额定载重',
        '最大起升高度(m)',
        '臂长',
        '当前位置',
        '楼号'
      ]
    ];

    props.selection?.forEach((d: TowerEquipment) => {
      array.push([
        `${['闲置', '报废', '注销', '转卖'][Number(d.status)]}`,
        `${d.name}`,
        `${d.factoryNo}`,
        `${d.model}`,
        `${d.manufactor}`,
        `${d.buyDate ? d.buyDate : ''}`,
        `${d.filingNo}`,
        `${d.factoryDate ? d.factoryDate : ''}`,
        `${d.company}`,
        `${d.source}`,
        `${d.warehouse}`,
        `${d.users}`,
        `${d.equipmentNo}`,
        `${d.ratedLoad}`,
        `${d.maxLiftingHeight}`,
        `${d.armLength}`,
        `${d.currentLocation}`,
        `${d.machineNo}`
      ]);
    });
    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>
