<!-- eslint-disable vue/require-prop-type-constructor -->
<template>
  <el-button :type="button_type" @click="dataExport" size="default">
    <Icon v-if="is_show_icon" name="document-arrow-up" className="w-4 h-4 mr-1 -ml-1" />
    导出
  </el-button>
</template>

<script lang="ts" setup>
import { exportExcel } from '@/utils/exportExcel';
import moment from 'moment';
import http from '@/support/http'
const props = defineProps({
  api: {
    type: String,
    require: true,
  },
  extro_params: {
    type: Object,
  },
  title_obj: {
    type: Object,
    require: true,
  },
  sheet_name: {
    type: String,
    require: true,
  },
  api_type: {
    type: String,
    default: 'get'
  },
  is_show_icon: {
    type: Boolean,
    default: true
  },
  button_type: {
    type: String,
    default: "primary"
  }
});

const dataExport = () => {
  const exportTableData = ref([])
  http[props.api_type](props.api, props.extro_params).then(res => {
    const r = res.data;
    const code = r.code;
    if (code === 1e4) {
      exportTableData.value = r.data;
      if (exportTableData.value.length > 0) {
        exportExcel(exportTableData.value, `${props.sheet_name}${moment().format('YYYY-MM-DD HH:mm:ss')}`, props.title_obj, props.sheet_name);
      } else {
        ElMessage.warning('暂无数据')
      }
    }
  });
}

onMounted(() => {
});
</script>

<style scoped lang="scss"></style>
