const { chromium } = require('playwright');
const fs = require('fs');
const path = require('path');

async function solveCaptchaAndSavePdf() {
  // 启动手机模式浏览器
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext({
    viewport: { width: 375, height: 812 },
    userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/1AE148',
    deviceScaleFactor: 3,
    isMobile: true,
    hasTouch: true
  });
  
  const page = await context.newPage();
  
  try {
    // 访问目标网页
    console.log('正在访问网页...');
    await page.goto('https://www.schrss.org.cn/scggfwApp/cbzmyz/toPage.do?code=Au4JWUwufsRpfGrEp42G');
    await page.waitForTimeout(3000);
    
    console.log('页面标题:', await page.title());
    
    // 获取页面快照，查看验证码元素
    const snapshot = await page.content();
    console.log('页面内容长度:', snapshot.length);
    
    // 查找验证码图片
    const captchaImg = await page.locator('img').first();
    if (captchaImg) {
      const captchaPath = path.join(__dirname, 'captcha_verify.png');
      await captchaImg.screenshot({ path: captchaPath });
      console.log('验证码已保存到:', captchaPath);
    }
    
    // 获取验证码图片的base64
    const captchaBase64 = await page.evaluate(() => {
      const img = document.querySelector('img');
      if (img) {
        const canvas = document.createElement('canvas');
        const ctx = canvas.getContext('2d');
        canvas.width = img.width;
        canvas.height = img.height;
        ctx.drawImage(img, 0, 0);
        return canvas.toDataURL('image/png');
      }
      return null;
    });
    
    if (captchaBase64) {
      const base64Data = captchaBase64.replace(/^data:image\/\w+;base64,/, '');
      fs.writeFileSync(path.join(__dirname, 'captcha_base64.png'), Buffer.from(base64Data, 'base64'));
      console.log('验证码base64图片已保存');
    }
    
    // 打印页面中所有图片的src
    const imgSrcs = await page.evaluate(() => {
      return [...document.querySelectorAll('img')].map(img => ({
        src: img.src,
        alt: img.alt,
        width: img.width,
        height: img.height
      }));
    });
    console.log('页面中的图片:', JSON.stringify(imgSrcs, null, 2));
    
    // 查找输入框和提交按钮
    const inputs = await page.locator('input').all();
    console.log('输入框数量:', inputs.length);
    
    const buttons = await page.locator('button').all();
    console.log('按钮数量:', buttons.length);
    
    // 查找表单元素
    const formInfo = await page.evaluate(() => {
      const form = document.querySelector('form');
      if (form) {
        return {
          action: form.action,
          method: form.method
        };
      }
      return null;
    });
    console.log('表单信息:', formInfo);
    
    // 等待一下，确保页面完全加载
    await page.waitForTimeout(2000);
    
    // 获取完整的页面快照
    const bodyHtml = await page.evaluate(() => document.body.innerHTML);
    console.log('Body HTML (前2000字符):', bodyHtml.substring(0, 2000));
    
  } catch (error) {
    console.error('错误:', error.message);
  } finally {
    await browser.close();
  }
}

solveCaptchaAndSavePdf();