<template>
  <el-dialog
    title="新增规则组"
    v-model="innerVisible"
    width="560px"
    :close-on-click-modal="false"
    @close="reset"
  >
    <el-form ref="form" :model="form" label-width="140px" label-position="right">
      <el-form-item label="规则组卡种类型" required>
        <el-select
          v-model="form.cardType"
          placeholder="请选择"
          style="width: 100%"
          @change="form.ruleGroupId = ''"
        >
          <el-option label="借记卡圈客规则" value="debitCard" />
          <el-option label="信用卡圈客规则" value="creditCard" />
        </el-select>
      </el-form-item>

      <el-form-item label="规则组" required>
        <el-select
          v-model="form.ruleGroupId"
          placeholder="请选择"
          filterable
          :filter-method="filterRuleGroup"
          style="width: 100%"
          no-data-text="暂无可选规则组"
          no-match-text="未找到匹配的规则组"
        >
          <el-option
            v-for="rg in filteredGroups"
            :key="rg.id"
            :label="`${rg.id}(${rg.name})`"
            :value="rg.id"
          />
        </el-select>
        <div class="hint">仅展示「已上线」状态且与卡种类型匹配的规则组</div>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSave">保 存</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { ElMessage } from 'element-plus'
import ruleGroupStore from '@/store/ruleGroupStore'

export default {
  name: 'AddRuleGroupDialog',
  props: {
    modelValue: { type: Boolean, default: false },
    excludeIds: { type: Array, default: () => [] }
  },
  emits: ['update:modelValue', 'save'],
  data() {
    return {
      form: { cardType: '', ruleGroupId: '' },
      filterKeyword: ''
    }
  },
  computed: {
    innerVisible: {
      get() { return this.modelValue },
      set(v) { this.$emit('update:modelValue', v) }
    },
    filteredGroups() {
      const all = ruleGroupStore.list.filter((rg) => rg.status === 'online')
      return all.filter((rg) => {
        if (this.form.cardType && rg.type !== this.form.cardType) return false
        if (this.excludeIds.includes(rg.id)) return false
        if (this.filterKeyword) {
          const k = this.filterKeyword.toLowerCase()
          if (!rg.id.toLowerCase().includes(k) && !rg.name.toLowerCase().includes(k)) {
            return false
          }
        }
        return true
      })
    }
  },
  watch: {
    modelValue(v) {
      if (v) this.reset()
    }
  },
  methods: {
    reset() {
      this.form = { cardType: '', ruleGroupId: '' }
      this.filterKeyword = ''
    },
    filterRuleGroup(k) {
      this.filterKeyword = k
    },
    handleSave() {
      if (!this.form.cardType) {
        ElMessage.error('请选择规则组卡种类型')
        return
      }
      if (!this.form.ruleGroupId) {
        ElMessage.error('请选择规则组')
        return
      }
      const rg = ruleGroupStore.getById(this.form.ruleGroupId)
      if (!rg) {
        ElMessage.error('所选规则组不存在')
        return
      }
      this.$emit('save', {
        id: rg.id,
        name: rg.name,
        description: rg.description || '-',
        logic: rg.logic,
        ruleCount: (rg.rules || []).length
      })
      this.innerVisible = false
    },
    handleCancel() {
      this.innerVisible = false
    }
  }
}
</script>

<style scoped>
.hint { color: #909399; font-size: 12px; margin-top: 4px; }
.dialog-footer { text-align: center; }
</style>
