<?php

class RecordCounter2 {
    private $conn;
    private $columnPrefix;
    private $countRange;

    public function __construct($conn) {
        $this->conn = $conn;
    }

    public function countRecords2($prefix,$range,$condX) {
        $bCounts = [];
        $cCounts = [];

        foreach ($range as $value) {
            $bCounts[] = 'count(case when `' . $prefix . $value . '` != "" AND `' . $prefix . $value . '` != "98" then record end) as ' . $prefix . 'b' . $value;
            $cCounts[] = 'count(case when `' . $prefix . $value . '` = "1" then record end) as ' . $prefix . 'c' . $value;
        }

        $bSql = implode(', ', $bCounts);
        $cSql = implode(', ', $cCounts);

        $sql = "SELECT $bSql, $cSql, record FROM `upload` WHERE $condX";
        $result = mysqli_query($this->conn, $sql);
        $ALL = mysqli_fetch_array($result);

        $data = [];

        foreach ($range as $value) {
            $bCount = $ALL[$prefix . 'b' . $value];
            $cCount = $ALL[$prefix . 'c' . $value];
            
            $percentage = ($bCount > 0) ? ($cCount / $bCount) * 100 : 0;

            $data[] = [
                'bCount' => $bCount,
                'cCount' => $cCount,
                'percentage' => $percentage,
            ];
        }
        return $data;
    }
}
?>