<!-- 搜索表单 -->
<template>
  <div style="display: flex; justify-content: space-between">
    <a-space 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
        danger
        type="primary"
        class="ele-btn-icon"
        v-if="selection?.length > 0"
        @click="removeBatch"
      >
        <template #icon>
          <DeleteOutlined />
        </template>
        <span>批量删除</span>
      </a-button>
      <a-button
        class="ele-btn-icon"
        v-if="selection?.length > 0"
        @click="handleExport"
      >
        <span>导出</span>
      </a-button>
    </a-space>
    <a-space :size="10" style="flex-wrap: wrap; margin-right: 20px">
      <regions-select
        v-model:value="city"
        type="provinceCity"
        valueField="label"
        placeholder="请选择省市区"
        class="ele-fluid"
        @change="handleCity"
      />
      <a-input-search
        allow-clear
        placeholder="请输入关键词"
        v-model:value="searchText"
        @pressEnter="search"
        @search="search"
      />
      <a-button @click="reset">重置</a-button>
    </a-space>
  </div>
</template>

<script lang="ts" setup>
  import {
    PlusOutlined,
    UploadOutlined,
    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 type = ref('keywords');
  // 搜索内容
  const searchText = ref('');
  // 所在城市
  const city = ref<string[]>([]);
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);
  // 是否显示导入弹窗
  const showImport = ref(false);

  /* 搜索 */
  const search = () => {
    console.log(where);
    resetFields();
    const [d1, d2] = dateRange.value ?? [];
    if (type.value == 'keywords') {
      where.keywords = searchText.value;
    }
    emit('search', {
      ...where,
      createTimeStart: d1 ? d1 + ' 00:00:00' : '',
      createTimeEnd: d2 ? d2 + ' 23:59:59' : ''
    });
  };

  // 导出
  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 add = () => {
    // if (tenantId.value == 5) {
    //   return window.open('https://www.gxwebsoft.com/register');
    // }
    emit('add');
  };

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

  const handleSearch = () => {
    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>
