<template>
  <view class="tab-radio-container" :style="{ gridTemplateColumns: `repeat(${colNum}, 1fr)` }">
    <view 
      class="tab-item"
      :class="{ 
        'tab-item--active': isMultiple ? activeValues.includes(item.value) : activeValue === item.value,
        'tab-item--multiple': isMultiple
      }"
      v-for="(item, index) in tabList"
      :key="index"
      @click="handleTabClick(item)"
    >
      <text class="tab-item__text">{{ item.name }}</text>
      <!-- <view v-if="isMultiple" class="tab-item__checkbox" :class="{ 'tab-item__checkbox--checked': activeValues.includes(item.value) }">
        <view v-if="activeValues.includes(item.value)" class="tab-item__checkbox-inner"></view>
      </view> -->
    </view>
  </view>
</template>

<script>
export default {
  name: "TabRadio",
  props: {
    tabList: {
      type: Array,
      required: true,
      default: () => []
    },
    defaultActive: {
      type: [String, Number, Array],
      default: ""
    },
    colNum: {
      type: Number,
      default: 4
    },
    mode: {
      type: String,
      default: "single",
      validator: (value) => ["single", "multiple"].includes(value)
    },
    echoInfo: {
      type: [String, Number, Array],
      default: ""
    },
    isClear: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    isMultiple() {
      return this.mode === "multiple";
    }
  },
  data() {
    // 优先使用 echoInfo，如果 echoInfo 不为空则使用它，否则使用 defaultActive
    const initialValue = this.echoInfo || this.defaultActive;
    return {
      activeValue: this.isMultiple ? '' : initialValue,
      activeValues: this.isMultiple ? (Array.isArray(initialValue) ? initialValue : []) : []
    };
  },
  watch: {
    defaultActive: {
      handler(val) {
        // 只有当 echoInfo 为空时，才使用 defaultActive
        if (!this.echoInfo) {
          if (this.isMultiple) {
            this.activeValues = Array.isArray(val) ? val : [];
          } else {
            this.activeValue = val;
          }
        }
      },
      immediate: true
    },
    echoInfo: {
      handler(val) {
        if (val !== undefined && val !== null) {
          if (this.isMultiple) {
            this.activeValues = Array.isArray(val) ? val : [];
          } else {
            this.activeValue = val;
          }
        }
      },
      immediate: true
    },
    mode: {
      handler(newMode) {
        // 当 mode 变化时，重新初始化选中值
        const initialValue = this.echoInfo || this.defaultActive;
        if (newMode === 'multiple') {
          this.activeValues = Array.isArray(initialValue) ? initialValue : [];
          this.activeValue = '';
        } else {
          this.activeValue = initialValue;
          this.activeValues = [];
        }
      },
      immediate: true
    },
    isClear: {
      handler(val) {
        if (val) {
          this.activeValue = '';
          this.activeValues = [];
        }
      },
      immediate: true
    }
  },
  methods: {
    handleTabClick(item) {
      if (this.isMultiple) {
        this.handleMultipleSelect(item);
      } else {
        this.handleSingleSelect(item);
      }
    },
    handleSingleSelect(item) {
      this.activeValue = item.value;
      this.$emit("tabChange", item.value);
    },
    handleMultipleSelect(item) {
      const index = this.activeValues.indexOf(item.value);
      if (index > -1) {
        this.activeValues.splice(index, 1);
      } else {
        this.activeValues.push(item.value);
      }
      this.$emit("tabChange", [...this.activeValues]);
    }
  }
};
</script>

<style lang="scss" scoped>
@import "./index.scss";
</style>