<?php
ini_set('session.cookie_secure', 1);
session_start(['read_and_close' => true]);
require_once "../../includes/db.php";
header('Content-Type: application/json');

try {
  // $storeid = isset($_GET['storeid']) ? intval($_GET['storeid']) : 0;
  $comp_id = $_GET['comp_id'] ?? 0;

    $stmt3 = $pdo->prepare("
      SELECT 
            d.t_item, 
            r.p_name, 
            'count' AS t_type, 
            SUM(CASE WHEN d.t_date = DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND d.t_type = 'DEL' THEN d.t_qty ELSE 0 END) - 
            SUM(CASE WHEN d.t_date = DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND d.t_type = 'GR' THEN d.t_qty ELSE 0 END) 
            AS total_SALES_yest,
            
               SUM(CASE WHEN d.t_date =  CURRENT_DATE()  AND d.t_type = 'DEL' THEN d.t_qty ELSE 0 END) - 
            SUM(CASE WHEN d.t_date =  CURRENT_DATE() AND d.t_type = 'GR' THEN d.t_qty ELSE 0 END) 
            AS total_SALES_today,
        
            SUM(CASE WHEN (d.t_date >= DATE_FORMAT(CURRENT_DATE, '%Y-%m-01') ) AND d.t_type = 'DEL' THEN d.t_qty ELSE 0 END) - 
            SUM(CASE WHEN (d.t_date >= DATE_FORMAT(CURRENT_DATE, '%Y-%m-01') ) AND d.t_type = 'GR' THEN d.t_qty ELSE 0 END) 
            AS total_SALES_month
        
        FROM transactions d
        JOIN accounts u ON d.t_cr = u.id AND d.co_id = u.co_id
        JOIN products r ON r.id = d.t_item   AND d.co_id = r.co_id
        WHERE d.t_copy = 'O' 
        AND d.t_status <> 'Pending'
        AND r.sale_count = '1'
        AND d.t_date >= DATE_FORMAT(CURRENT_DATE, '%Y-%m-01')
        AND d.co_id = ?
        AND d.t_type IN ('DEL', 'GR')
        
        GROUP BY d.t_item, r.p_name
        ORDER BY d.t_item ASC
    ");

    $stmt3->execute([$comp_id]);
    $data3 = $stmt3->fetchAll(PDO::FETCH_ASSOC);

    if (!empty($data3)) {
        foreach ($data3 as $row) {
            
             
            
             if ($row['t_type'] === 'DEL')
         {
             $style1 =  "text-danger" ;
             
             if ($row['t_status'] === 'Pending')
          {
           $style2 =  "text-secondary" ;   
          }
            if ($row['t_status'] === 'Confirmed')
          {
           $style2 =  "text-danger" ;   
          }
         }
         
            if ($row['t_type'] === 'GR')
         {
             $style1 =  "text-primary" ;
             
             if ($row['t_status'] === 'Pending')
          {
           $style2 =  "text-secondary" ;   
          }
            if ($row['t_status'] === 'Confirmed')
          {
           $style2 =  "text-primary" ;   
          }
         }
         
         $style1 =  "text-dark" ;   
          $style2 =  "text-secondary" ; 
           $style3 =  "text-success" ; 
            $style4 =  "text-primary" ; 

                       echo '
            <tr>
                
                <td class="' . $style1 . '">
                 <a data-toggle="modal" class="item " data-target="#" 
                       data-loc_id="' . htmlspecialchars($row["t_tt_doc"]) . '" 
                       data-usr="' . htmlspecialchars($store_id) . '"
                       data-ord="' . htmlspecialchars($row['t_doc_no']) . '"
                       data-type="' . htmlspecialchars($row['t_type']) . '">
                        ' . htmlspecialchars($row["p_name"]) . '
                        </a>
                        </td>
                <td class="' . $style2 . '">
                    <a data-toggle="modal" class="item " data-target="#" 
                       data-loc_id="' . htmlspecialchars($row["t_tt_doc"]) . '" 
                       data-usr="' . htmlspecialchars($store_id) . '"
                       data-ord="' . htmlspecialchars($row['t_doc_no']) . '"
                       data-type="' . htmlspecialchars($row['t_type']) . '">
                       ' . htmlspecialchars($row["total_SALES_yest"]) . '
                       </a>
                       </td>
                <td class="' . $style3 . '">
                    <a data-toggle="modal" class="item " data-target="#" 
                       data-loc_id="' . htmlspecialchars($row["t_tt_doc"]) . '" 
                       data-usr="' . htmlspecialchars($store_id) . '"
                       data-ord="' . htmlspecialchars($row['t_doc_no']) . '"
                       data-type="' . htmlspecialchars($row['t_type']) . '">
                        ' . number_format($row["total_SALES_today"], 0, '.', ',') . '
                    </a>
                </td>
                
                <td class="' . $style4 . '">
                    <a data-toggle="modal" class="item " data-target="#" 
                       data-loc_id="' . htmlspecialchars($row["t_tt_doc"]) . '" 
                       data-usr="' . htmlspecialchars($store_id) . '"
                       data-ord="' . htmlspecialchars($row['t_doc_no']) . '"
                       data-type="' . htmlspecialchars($row['t_type']) . '">
                        ' . number_format($row["total_SALES_month"], 0, '.', ',') . '
                    </a>
                </td>
            </tr>
            ';

        }
    } else {
        echo '<tr><td colspan="4" class="text-center text-warning">No matching records found.</td></tr>';
    }
} catch (PDOException $e) {
    echo '<tr><td colspan="4" class="text-center text-danger">Error fetching data.</td></tr>';
    error_log("Database Error: " . $e->getMessage()); // Log error for debugging
}
?>
