<!-- 搜索表单 -->
<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"
      v-if="selection.length > 0"
      @click="removeBatch"
    >
      <template #icon>
        <DeleteOutlined />
      </template>
      <span>批量删除</span>
    </a-button>
    <!--    <a-button @click="batchMove" v-if="selection.length > 0">-->
    <!--      <template #icon>-->
    <!--        <UserSwitchOutlined />-->
    <!--      </template>-->
    <!--      批量转移-->
    <!--    </a-button>-->
    <a-input-search
      allow-clear
      placeholder="请输入关键词"
      v-model:value="searchText"
      @pressEnter="search"
      @search="search"
    />
  </a-space>
</template>

<script lang="ts" setup>
  import {
    PlusOutlined,
    DeleteOutlined,
    UploadOutlined,
    DownloadOutlined
  } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import type { CustomerParam } from '@/api/oa/customer/model';
  import { ref, watch } from 'vue';
  import { utils, read } from 'xlsx';
  // import { assignObject } from 'ele-admin-pro';
  import { message } from 'ant-design-vue/es';

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

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

  // 表单数据
  const { where } = useSearch<CustomerParam>({
    customerName: '',
    customerCode: '',
    nickname: '',
    keywords: '',
    userId: undefined
  });
  // 下来选项
  // 搜索内容
  const searchText = ref('');

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

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

  // 导入数据的列
  const importTitle = ref<string[]>(['A', 'B', 'C', 'D', 'E', 'F', 'G']);
  // 导入的数据
  const importData = ref<Record<string, any>[]>([]);
  // 导入数据二维数组形式
  const importDataAoa = ref<(string | number)[][]>([]);

  /* 导入本地 excel 文件 */
  const importBatch = (file: File) => {
    if (
      ![
        'application/vnd.ms-excel',
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
      ].includes(file.type)
    ) {
      message.error('只能选择 excel 文件');
      return false;
    }
    if (file.size / 1024 / 1024 > 20) {
      message.error('大小不能超过 20MB');
      return false;
    }
    const reader = new FileReader();
    reader.onload = (e) => {
      const data = new Uint8Array(e.target?.result as any);
      const workbook = read(data, { type: 'array' });
      const sheetNames = workbook.SheetNames;
      const worksheet = workbook.Sheets[sheetNames[0]];
      // 解析成二维数组
      const aoa = utils.sheet_to_json<string[]>(worksheet, { header: 1 });
      // 生成表格需要的数据
      let list: Record<string, any>[] = [];
      let maxCols = 0;
      let title: string[] = [];
      aoa.forEach((d) => {
        if (d.length > maxCols) {
          maxCols = d.length;
        }
        const row = {};
        for (let i = 0; i < d.length; i++) {
          const key = getCharByIndex(i);
          row[key] = d[i];
          row['__colspan__' + key] = 1;
          row['__rowspan__' + key] = 1;
        }
        list.push(row);
      });
      for (let i = 0; i < maxCols; i++) {
        title.push(getCharByIndex(i));
      }
      importTitle.value = title;
      importData.value = list;
      importDataAoa.value = aoa;
    };
    console.log(importData.value);
    console.log(importDataAoa.value);
    reader.readAsArrayBuffer(file);
    return false;
  };
  /* 生成Excel列字母序号 */
  const getCharByIndex = (index: number) => {
    const chars = [
      'A',
      'B',
      'C',
      'D',
      'E',
      'F',
      'G',
      'H',
      'I',
      'J',
      'K',
      'L',
      'M',
      'N',
      'O',
      'P',
      'Q',
      'R',
      'S',
      'T',
      'U',
      'V',
      'W',
      'X',
      'Y',
      'Z'
    ];
    if (index < chars.length) {
      return chars[index];
    }
    const n = parseInt(String(index / chars.length));
    const m = index % chars.length;
    return chars[n] + chars[m];
  };

  // 转移
  // const batchMove = () => {
  //   emit('batchMove');
  // };

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

  const onClear = () => {
    where.userId = undefined;
    search();
  };

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