<template>
  <div class="card" :style="{ height: cardHeight }">
    <!-- 头部 -->
    <div :class="type === 'long' ? 'card-long-header' : 'card-header'">
      <div class="header-container">
        <div class="card-title">{{ title }}</div>

        <!-- ↓↓↓  可选下拉  ↓↓↓ -->
        <div v-if="isShowSelect" class="card-select-block" ref="dropdown">
          <div class="select-display" @click="toggleDropdown">
            <span class="display-text">{{ selectedText }}</span>
            <img
                class="arrow-img"
                :class="{ open: showDropdown }"
                src="@/assets/images/icon/icon_down_arrow.png"
                alt="arrow"
            />
          </div>

          <ul v-show="showDropdown" class="select-dropdown">
            <li
                v-for="opt in processedOptions"
                :key="opt.value"
                class="select-option"
                @click="selectOption(opt)"
            >
              {{ opt.label }}
            </li>
          </ul>
        </div>
      </div>
    </div>

    <!-- 内容插槽 -->
    <div class="card-container">
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CardWithCustomSelect',
  props: {
    title:            { type: String,  default: '请填写标题' },
    type:             { type: String,  default: 'short' },
    isShowSelect:     { type: Boolean, default: false },
    heightPercentage: { type: Number,  default: 100 },
    /* 原下拉数据 */
    selectOptions:    { type: Array,   default: () => [] },
    /* 新增：是否要带“全部” */
    includeAll:       { type: Boolean, default: false }
  },
  data() {
    return {
      selected: '',        // 绑定 value
      showDropdown: false
    }
  },

  computed: {
    cardHeight()  { return `${this.heightPercentage}%` },

    /* 归一化 options：保证有 label / value */
    processedOptions() {
      const norm = this.selectOptions.map((o, i) => ({
        label : o.label ?? o.name ?? `选项${i+1}`,
        value : o.value ?? o.id   ?? i
      }));
      return this.includeAll
          ? [{ label: '全部', value: '' }, ...norm]
          : norm;
    },

    /* 当前显示文本 */
    selectedText() {
      const found = this.processedOptions.find(o => o.value === this.selected);
      return found ? found.label : '请选择';
    }
  },

  mounted() {
    document.addEventListener('click', this.handleClickOutside);

    /* ----- 默认值逻辑 ----- */
    if (this.includeAll) {
      this.selected = '';                 // “全部”
    } else if (this.processedOptions.length) {
      this.selected = this.processedOptions[0].value;  // 选首项
      this.$emit('change', this.selected);             // 通知父组件
    }
  },
  beforeDestroy() {
    document.removeEventListener('click', this.handleClickOutside);
  },

  methods: {
    toggleDropdown() { this.showDropdown = !this.showDropdown },

    selectOption(opt) {
      this.selected     = opt.value;
      this.showDropdown = false;
      this.$emit('change', this.selected);   // 抛出事件
    },

    handleClickOutside(e) {
      if (this.$refs.dropdown && !this.$refs.dropdown.contains(e.target)) {
        this.showDropdown = false;
      }
    }
  }
}
</script>

<style scoped lang="scss">
.card {
  position: relative;
  width: 100%;
  height: 280px;
  background: linear-gradient(
          270deg,
          #031B2E 0%,   /* 深午夜蓝：对应原 #052E23 */
          #104266 53%,  /* 深海军蓝：对应原 #124937 */
          #031B2E 100%  /* 回到首色，首尾呼应 */
  );
  border: 1px solid;
  border-image: linear-gradient(180deg, rgba(44, 154, 255, 0), rgba(44, 154, 255, 1)) 1 1;
  display: flex;
  flex-direction: column;
}

.card-header {
  height: 55px;
  background: url("./../../assets/images/bg/card_title_bg3.png") no-repeat;
  background-size: 100% 100%;
  color: white;
  margin-top: -10px;
}

.card-long-header {
  width: 100%;
  height: 15%;
  background: url("./../../assets/images/bg/card_title_bg3.png") no-repeat;
  background-size: 100% 100%;
  color: white;
  margin-top: -10px;
}

.header-container {
  position: relative;
  width: calc(100% - 90px);
  margin-left: 50px;
  height: 100%;
  display: flex;
  justify-content: space-between;
  line-height: 55px;
  font-weight: 600;
  font-size: 22px;
  color: #FFFFFF;
  text-shadow: 0 2px 5px rgba(44, 154, 255, 0.6);
  text-align: left;
}

.card-title {
  flex-shrink: 0;
}

.card-container {
  width: 100%;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
}

/* 选择区域样式 */
.card-select-block {
  position: relative;
  width: 90px;
  height: 26px;
  margin-top: 10px;
  margin-right: 20px;
  cursor: pointer;
  font-size: 14px;
  z-index: 99;
}

.select-display {
  position: relative;
  width: 100%;
  height: 26px;
  line-height: 26px;
  padding-left: 8px;
  padding-right: 20px;
  background: #0D2847;
  border: 1px solid #2C9AFF;
  box-shadow: 0 0 6px rgba(44, 154, 255, 0.20);
  color: #C8D3E0;
  font-weight: bold;
  border-radius: 2px;
  overflow: hidden;
}


.display-text {
  display: inline-block;
  max-width: 70px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: middle;
}

.arrow-img {
  width: 10px;
  height: 6px;
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%) rotate(0deg);
  transition: transform 0.3s ease;
  pointer-events: none;
}

.arrow-img.open {
  transform: translateY(-50%) rotate(180deg);
}

.select-dropdown {
  position: absolute;
  top: 34px;
  left: 0;
  width: 110px;
  background: #0D2847;
  border: 1px solid #2C9AFF;
  box-shadow: 0 2px 8px rgba(44, 154, 255, 0.20);
  list-style: none;
  margin: 0;
  padding: 0;
  border-radius: 2px;
}


.select-option {
  min-height: 26px;
  line-height: 26px;
  padding: 0 10px;
  color: #FFFFFF;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;

  &:hover {
    background-color: #418A7039;
  }

  &:last-child {
    border-bottom: none;
  }
}

</style>
