const express = require('express');
const db = require('../db');
const { authenticate, requireAdmin } = require('../middleware/auth');

const router = express.Router();

router.get('/my-projects', async (req, res) => {
  try {
    const result = await db.query(`
      SELECT p.*, 
             c.name as customer_name,
             u.name as manager_name
      FROM projects p
      LEFT JOIN customers c ON p.customer_id = c.id
      LEFT JOIN users u ON p.project_manager_id = u.id
      WHERE p.status IN ('active', 'in_progress', 'planning', 'pending')
      ORDER BY p.created_at DESC
    `);
    
    const projects = result.rows.map(project => ({
      ...project,
      latest_log: null,
      progress: 0
    }));
    
    res.json({ success: true, data: projects, count: projects.length });
  } catch (error) {
    console.error('获取施工项目失败:', error);
    res.status(500).json({ success: false, message: '操作失败' });
  }
});

module.exports = router;
