const fs = require('fs');
const path = require('path');

// 定义需要修复的文件路径
const filesToFix = [
  {
    path: path.join(__dirname, 'src', 'app', 'shared', 'components', 'designer-nav', 'designer-nav.html'),
    type: 'html'
  },
  {
    path: path.join(__dirname, 'src', 'app', 'pages', 'admin', 'admin-layout', 'admin-layout.ts'),
    type: 'ts'
  },
  {
    path: path.join(__dirname, 'src', 'app', 'pages', 'customer-service', 'customer-service-layout', 'customer-service-layout.html'),
    type: 'html'
  },
  {
    path: path.join(__dirname, 'src', 'app', 'pages', 'customer-service', 'consultation-order', 'consultation-order.ts'),
    type: 'ts'
  },
  {
    path: path.join(__dirname, 'src', 'app', 'pages', 'customer-service', 'project-detail', 'project-detail.html'),
    type: 'html'
  },
  {
    path: path.join(__dirname, 'src', 'app', 'pages', 'login', 'set-admin-role.html'),
    type: 'html'
  },
  {
    path: path.join(__dirname, 'src', 'app', 'pages', 'team-leader', 'knowledge-base', 'knowledge-base.ts'),
    type: 'ts'
  },
  {
    path: path.join(__dirname, 'src', 'app', 'services', 'auth.service.ts'),
    type: 'ts'
  }
];

// 修复HTML文件
function fixHtmlFile(filePath) {
  try {
    let content = fs.readFileSync(filePath, 'utf8');
    
    // 修复img标签内包含div的问题
    const fixedContent = content.replace(/<img\s+src="<div\s+style="([^"]*)">([^<]*)<\/div>"\s+alt="([^"]*)"\s+class="([^"]*)"\s*\/?>/g,
      '<div style="$1" class="$4" title="$3">$2</div>');
    
    if (content !== fixedContent) {
      fs.writeFileSync(filePath, fixedContent, 'utf8');
      console.log(`已修复HTML文件: ${filePath}`);
      return 1;
    }
  } catch (error) {
    console.error(`修复HTML文件失败: ${filePath}`, error);
  }
  return 0;
}

// 修复TypeScript文件
function fixTsFile(filePath) {
  try {
    let content = fs.readFileSync(filePath, 'utf8');
    
    // 修复avatar属性的格式问题
    const fixedContent = content.replace(/avatar:\s*'\{\s*width:\s*(\d+),\s*height:\s*(\d+),\s*bgColor:\s*'(#[0-9A-Fa-f]+)',\s*textColor:\s*'(#[0-9A-Fa-f]+)',\s*text:\s*'([^']*)'\s*\}'/g,
      (match, width, height, bgColor, textColor, text) => {
        // 生成SVG data URL
        const svg = `data:image/svg+xml,%3Csvg width='${width}' height='${height}' xmlns='http://www.w3.org/2000/svg'%3E%3Crect width='100%25' height='100%25' fill='${encodeURIComponent(bgColor)}'/%3E%3Ctext x='50%25' y='50%25' font-family='Arial' font-size='${Math.min(parseInt(width), parseInt(height)) / 3}' font-weight='bold' text-anchor='middle' fill='${encodeURIComponent(textColor)}' dy='0.3em'%3E${encodeURIComponent(text)}%3C/text%3E%3C/svg%3E`;
        return `avatar: '${svg}'`;
      });
    
    if (content !== fixedContent) {
      fs.writeFileSync(filePath, fixedContent, 'utf8');
      console.log(`已修复TypeScript文件: ${filePath}`);
      return 1;
    }
  } catch (error) {
    console.error(`修复TypeScript文件失败: ${filePath}`, error);
  }
  return 0;
}

// 执行修复
console.log('开始直接修复placeholder链接问题...');
let totalFixed = 0;

filesToFix.forEach(file => {
  if (file.type === 'html') {
    totalFixed += fixHtmlFile(file.path);
  } else if (file.type === 'ts') {
    totalFixed += fixTsFile(file.path);
  }
});

console.log(`直接修复完成！总共修复了 ${totalFixed} 个文件。`);