<template>
  <Transition name="modal-fade">
    <div v-if="visible" class="custom-modal-overlay" @click.self="handleOverlayClick">
      <div class="custom-modal el-dialog--center" :class="themeClass">
        <div class="custom-modal-header">
          <h3 class="custom-modal-title">{{ title }}</h3>
          <button class="custom-modal-close" @click="handleClose">
            <svg viewBox="0 0 1024 1024" width="16" height="16">
              <path d="M810.666667 273.493333L750.506667 213.333333 512 451.84 273.493333 213.333333 213.333333 273.493333 451.84 512 213.333333 750.506667 273.493333 810.666667 512 572.16 750.506667 810.666667 810.666667 750.506667 572.16 512z" fill="currentColor"></path>
            </svg>
          </button>
        </div>
        
        <div class="custom-modal-body">
          <div class="algorithm-process-content">
            <p v-if="introText" class="process-intro">{{ introText }}</p>
            <div class="process-text">
              {{ formattedProcessText }}
            </div>
          </div>
        </div>
        
        <div class="custom-modal-footer">
          <button class="custom-modal-button" @click="handleClose">{{ confirmText }}</button>
        </div>
      </div>
    </div>
  </Transition>
</template>

<script>
export default {
  name: 'AlgorithmProcessModal',
  props: {
    // 弹窗显示状态
    modelValue: {
      type: Boolean,
      default: false
    },
    // 弹窗标题
    title: {
      type: String,
      default: '猪手专业算法筛选过程'
    },
    // 介绍文本
    introText: {
      type: String,
      default: ''
    },
    // 确认按钮文本
    confirmText: {
      type: String,
      default: '我知道了'
    },
    // 筛选过程原始文本
    processText: {
      type: String,
      default: ''
    },
    // 是否启用文本格式化（大乐透需要，双色球不需要）
    enableFormatting: {
      type: Boolean,
      default: false
    },
    // 主题类型（ssq: 双色球, dlt: 大乐透）
    theme: {
      type: String,
      default: 'ssq',
      validator: (value) => ['ssq', 'dlt'].includes(value)
    }
  },
  emits: ['update:modelValue', 'close'],
  computed: {
    visible: {
      get() {
        return this.modelValue
      },
      set(value) {
        this.$emit('update:modelValue', value)
      }
    },
    formattedProcessText() {
      if (!this.processText) {
        return '暂无筛选过程说明'
      }
      
      if (this.enableFormatting) {
        return this.formatFilteringText(this.processText)
      }
      
      return this.processText
    },
    themeClass() {
      return `${this.theme}-theme`
    }
  },
  methods: {
    handleVisibilityChange(value) {
      this.$emit('update:modelValue', value)
    },
    
    handleClose() {
      this.visible = false
      this.$emit('close')
    },
    
    handleOverlayClick() {
      // 点击遮罩层关闭弹窗
      this.handleClose()
    },
    
    // 格式化筛选过程文本（只在关键步骤处换行）
    formatFilteringText(text) {
      if (!text) return '暂无筛选过程说明'
      
      return text
        // 先清理可能的重复词汇（只处理字母单词，不处理数字）
        .replace(/\b([a-zA-Z\u4e00-\u9fa5]+)\1+\b/g, '$1') // 清理连续重复的单词，但不处理数字
        .replace(/通过通过/g, '通过') // 特别处理"通过"重复
        .replace(/，，+/g, '，') // 清理连续逗号
        .replace(/。。+/g, '。') // 清理连续句号
        // 在步骤分隔处换行（如：第一步、第二步等关键词后）
        .replace(/(第[一二三四五六七八九十\d]+步[：:，,])/g, '\n$1\n')
        // 在预测结果开始处换行
        .replace(/(最终推荐[：:，,])/g, '\n$1')
        .replace(/(推荐结果[：:，,])/g, '\n$1')
        .replace(/(筛选结果[：:，,])/g, '\n$1')
        // 在重要的算法说明开始处换行
        .replace(/(算法说明[：:，,])/g, '\n$1')
        .replace(/(分析过程[：:，,])/g, '\n$1')
        // 在筛选步骤说明处换行
        .replace(/(筛选步骤[：:，,])/g, '\n$1\n')
        // 在分号后面如果是重要内容开始则换行
        .replace(/；(\s*直接)/g, '；\n$1')
        .replace(/；(\s*筛选)/g, '；\n$1')
        .replace(/；(\s*推荐)/g, '；\n$1')
        // 在"其中"这种重要分段处适当换行（但要更谨慎）
        .replace(/([。；])(\s*其中)/g, '$1\n$2')
        // 在包含多个数字的长列表后换行（但保持在一行内的短序列不变）
        .replace(/(\d+(?:\s*,\s*\d+){4,}[\d\s,]*?)([，。])/g, '$1$2\n')
        // 清理开头和结尾的换行
        .replace(/^\n+/, '')
        .replace(/\n+$/, '')
        // 清理多余的连续换行
        .replace(/\n{3,}/g, '\n\n')
        // 最后再清理一次可能的重复
        .replace(/通过通过/g, '通过')
        .trim()
    }
  }
}
</script>

<style scoped>
/* 弹窗过渡动画 */
.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: opacity 0.3s ease;
}

.modal-fade-enter-from,
.modal-fade-leave-to {
  opacity: 0;
}

.modal-fade-enter-active .custom-modal,
.modal-fade-leave-active .custom-modal {
  transition: transform 0.3s ease;
}

.modal-fade-enter-from .custom-modal,
.modal-fade-leave-to .custom-modal {
  transform: scale(0.9);
}

/* 遮罩层 */
.custom-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 20px;
}

/* 弹窗容器 */
.custom-modal {
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.15);
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 弹窗头部 */
.custom-modal-header {
  padding: 20px 20px 10px;
  border-bottom: 1px solid #ebeef5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.custom-modal-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 0;
  flex: 1;
  text-align: center;
}

.custom-modal-close {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #909399;
  transition: color 0.3s ease;
  position: absolute;
  right: 18px;
  top: 18px;
}

.custom-modal-close:hover {
  color: #409eff;
}

/* 弹窗主体 */
.custom-modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

/* 筛选过程内容样式 */
.algorithm-process-content {
  padding: 0;
}

.process-intro {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 15px;
  text-align: center;
}

.process-text {
  font-size: 16px;
  color: #555;
  background: #f8f9fa;
  padding: 20px;
  border-radius: 8px;
  word-break: break-word;
  word-wrap: break-word;
  overflow-wrap: break-word;
  max-width: 100%;
  text-align: left;
  line-height: 2.2;
  white-space: pre-line;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
}

/* 双色球主题 */
.custom-modal.ssq-theme .process-text {
  background: linear-gradient(135deg, #f8f9fa 0%, #fff5f5 100%);
}

.custom-modal.ssq-theme .process-text:hover {
  box-shadow: 0 4px 12px rgba(229, 62, 62, 0.15);
}

.custom-modal.ssq-theme .custom-modal-button {
  background: linear-gradient(135deg, #e53e3e 0%, #ff6b6b 100%);
  box-shadow: 0 2px 8px rgba(229, 62, 62, 0.3);
}

.custom-modal.ssq-theme .custom-modal-button:hover {
  background: linear-gradient(135deg, #ff6b6b 0%, #e53e3e 100%);
  box-shadow: 0 4px 12px rgba(229, 62, 62, 0.4);
}

/* 大乐透主题 */
.custom-modal.dlt-theme .process-text {
  background: linear-gradient(135deg, #f8f9fa 0%, #f7fcf7 100%);
}

.custom-modal.dlt-theme .process-text:hover {
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.15);
}

.custom-modal.dlt-theme .custom-modal-button {
  background: linear-gradient(135deg, #4caf50 0%, #66bb6a 100%);
  box-shadow: 0 2px 8px rgba(76, 175, 80, 0.3);
}

.custom-modal.dlt-theme .custom-modal-button:hover {
  background: linear-gradient(135deg, #66bb6a 0%, #4caf50 100%);
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.4);
}

/* 弹窗底部 */
.custom-modal-footer {
  padding: 10px 20px 20px;
  text-align: center;
  flex-shrink: 0;
  border-top: 1px solid #ebeef5;
}

.custom-modal-button {
  background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%);
  border: none;
  padding: 12px 24px;
  font-size: 14px;
  font-weight: 500;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
  transition: all 0.3s ease;
  color: #ffffff;
  cursor: pointer;
  min-width: 100px;
}

.custom-modal-button:hover {
  background: linear-gradient(135deg, #66b1ff 0%, #409eff 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
}

.custom-modal-button:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
}

/* 移动端优化 */
@media (max-width: 768px) {
  .custom-modal-overlay {
    padding: 15px;
  }
  
  .custom-modal-header {
    padding: 15px 15px 8px;
  }
  
  .custom-modal-title {
    font-size: 16px;
  }
  
  .custom-modal-close {
    right: 15px;
    top: 15px;
  }
  
  .custom-modal-body {
    padding: 15px;
  }
  
  .process-intro {
    font-size: 15px;
    margin-bottom: 12px;
  }
  
  .process-text {
    font-size: 15px;
    line-height: 2.0;
    padding: 15px;
    border-radius: 6px;
  }
  
  .custom-modal-button {
    padding: 10px 20px;
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  .custom-modal-overlay {
    padding: 10px;
  }
  
  .custom-modal {
    width: 95%;
    max-height: 85vh;
  }
  
  .custom-modal-header {
    padding: 12px 12px 6px;
  }
  
  .custom-modal-title {
    font-size: 15px;
  }
  
  .custom-modal-close {
    right: 12px;
    top: 12px;
  }
  
  .custom-modal-body {
    padding: 12px;
  }
  
  .process-intro {
    font-size: 14px;
    margin-bottom: 10px;
  }
  
  .process-text {
    font-size: 14px;
    line-height: 1.8;
    padding: 12px;
    border-radius: 4px;
  }
  
  .custom-modal-footer {
    padding: 8px 12px 12px;
  }
  
  .custom-modal-button {
    padding: 8px 16px;
    font-size: 12px;
    min-width: 80px;
  }
}
</style>
