<template>
  <el-select
    v-model="selected"
    v-bind="$attrs"
    :loading="loading"
    v-on="$listeners"
  >
    <el-option
      v-for="item in options"
      :key="item.reportId"
      :label="item.reportName"
      :value="item.reportId"
    >
      <el-row :gutter="20">
        <el-col :span="12">{{ item.reportId }}</el-col>
        <el-col :span="12" class="text-right">{{ item.reportName }}</el-col>
      </el-row>
    </el-option>
  </el-select>
</template>

<script>
import { getReportPage } from "@/api/system/menu";

export default {
  props: {
    value: {
      // 允许多种类型
      type: [String, Number, Array],
      default: "",
    },
  },
  data() {
    return {
      options: [],
      loading: false,
    };
  },
  computed: {
    innerValue: {
      get() {
        // 统一转成数组
        if (Array.isArray(this.value)) return this.value;
        if (!this.value) return [];
        return [this.value];
      },
      set(val) {
        // 向外回传：如果是多选就传数组，单选就传字符串
        const toEmit = this.multiple ? val : val[0] || "";
        this.$emit("input", toEmit);
      },
    },
  },
  computed: {
    selected: {
      get() {
        return this.value;
      },
      set(v) {
        this.$emit("input", v);
      },
    },
  },
  created() {
    this.fetchReportList();
  },
  methods: {
    fetchReportList() {
      this.loading = true;
      const params = {
        pageNo: 1,
        pageSize: 100,
      };
      getReportPage(params)
        .then((res) => {
          const list = res.data || {};
          this.options = list;
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style></style>
