<template>
  <el-dialog :visible.sync="open" :title="title" width="300px">
    <el-radio-group v-model="exportType">
      <el-radio v-for="item in ExportOptions" :key="item.type" :label="item.type"
        style="display: block; margin-bottom: 10px">{{ item.label }}
      </el-radio>
    </el-radio-group>
    <div slot="footer">
      <el-button type="primary" @click="exportFile">导 出
      </el-button>
      <el-button plain @click="closeExport">取 消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { ExportType, ExportTypes } from "@/enums/common";

export default {
  name: "ExportTable",
  props: {
    openExport: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '选中导出类型'
    },
    options: {
      type: Array,
      default: () => []
    },
    // 是否有模糊查询框
    hasQuery: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      exportType: ExportTypes[0].type
    }
  },
  computed: {
    open: {
      get() {
        return this.openExport;
      },
      set(value) {
        this.$emit('update:openExport', value);
      }
    },

    // 导出选项
    ExportOptions() {
      if (!this.hasQuery) {
        ExportTypes.splice(-1);
        return ExportTypes;
      }
      return ExportTypes;
    }
  },
  methods: {
    exportFile() {
      this.$emit('export', (exportOptions = [], fileName) => {
        const result = exportOptions.find(item => item.type === this.exportType);
        console.log('result', result);
        if (result) {
          // 添加校验
          if (result.path && this.isSelectedData(result.path)) {
            this.download(result.path, { ...result.params }, fileName);
          }
          this.open = false;
        }
      })
    },
    closeExport() {
      this.open = false;
    },

    // 添加选中导出判断
    isSelectedData(path) {
      console.log('path', path)
      if (this.exportType === ExportType.SELECT) {
        const params = path.slice(path.lastIndexOf("/") + 1);
        if (!params) {
          this.$modal.msgWarning('当前未选中数据，无法正常导出');
          return false;
        }
      }
      return true;
    }

  }
}
</script>

<style scoped></style>
