<!-- 搜索表单 -->
<template>
  <div style="display: flex; justify-content: space-between"> </div>
</template>

<script lang="ts" setup>
  import { DeleteOutlined } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import { ref, watch } from 'vue';
  import { utils, writeFile } from 'xlsx';
  import { Company } from '@/api/system/company/model';
  import { CompanyParam } from '@/api/system/company/model';
  import { message } from 'ant-design-vue';

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

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

  // 表单数据
  const { where, resetFields } = useSearch<CompanyParam>({
    companyId: undefined,
    companyName: undefined,
    keywords: '',
    authentication: undefined,
    version: undefined,
    province: '',
    city: '',
    region: ''
  });
  const tenantId = ref<number>();
  if (localStorage.getItem('TenantId')) {
    tenantId.value = Number(localStorage.getItem('TenantId'));
  }

  // 搜索内容
  const searchText = ref('');
  // 所在城市
  const city = ref<string[]>([]);

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

  // 导出
  const handleExport = () => {
    if (!props.selection?.length) {
      message.error('请至少选择一条数据');
      return;
    }
    const array: (string | number)[][] = [
      [
        'ID',
        '企业名称',
        '企业简称',
        '所在城市',
        '详细地址',
        '联系电话',
        '企业域名',
        '实名认证',
        '版本',
        '注册时间',
        '到期时间',
        '备注'
      ]
    ];

    props.selection?.forEach((d: Company) => {
      let version = '';
      if (d.version === 10) {
        version = '体验版';
      }
      if (d.version === 20) {
        version = '授权版';
      }
      array.push([
        `${d.companyId}`,
        `${d.companyName}`,
        `${d.shortName}`,
        `${d.city}`,
        `${d.address}`,
        `${d.phone}`,
        `${d.domain}`,
        `${d.authentication == 1 ? '已认证' : ''}`,
        `${version}`,
        `${d.createTime}`,
        `${d.expirationTime}`,
        `${d.users}`,
        `${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 handleSearch = () => {
    console.log(where);
    emit('search', where);
  };

  /*  重置 */
  const reset = () => {
    resetFields();
    search();
  };

  const handleCity = (text) => {
    where.city = text[1];
    emit('search', where);
  };

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

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