<link rel="stylesheet" href="css/font.css" type="text/css" />
<script>
function media_report_print(url) {
    window.open(url, 'onmouseover650', 'height=580,width=1200,scrollbars=yes,left=50,top=50,toolbar=no,location=no,directories=no,status=no,menubar=no');
}

function media_usage_report_overall_excel(from_date, to_date, jobcard_from, jobcard_to, buyer_name, roll_size, type_1, jobtype,job_card_no) {
    window.location.href =
        "report/media_report/usage_excel.php?from_date=" + from_date +
        "&to_date=" + to_date +
        "&jobcard_from=" + jobcard_from +
        "&jobcard_to=" + jobcard_to +
        "&buyer_name=" + buyer_name +
        "&roll_size=" + roll_size +
        "&type_1=" + type_1 +
        "&jobtype=" + jobtype+
        "&job_card_no=" + job_card_no;
}
</script>

<?php
error_reporting(0);
include("../../include/db.php");
date_default_timezone_set("Asia/Kolkata");

// ==============================
// Utility
// ==============================
function get_item_id_media($roll_size) {
    $id = "";
    $sql_query11 = "SELECT * FROM consumable_creation WHERE particulars = '$roll_size' AND item = 'Media'";
    $rs11 = mysql_query($sql_query11);
    if ($rs11 && mysql_num_rows($rs11) > 0) {
        while ($rsdata11 = mysql_fetch_object($rs11)) {
            $id = $rsdata11->id;
        }
    }
    return $id;
}

// ==============================
// GET Parameters
// ==============================
$type_1        = isset($_GET['type_1']) ? $_GET['type_1'] : "";

$jobcard_from  = isset($_GET['jobcard_from']) ? $_GET['jobcard_from'] : "";
$job_card_no = isset($_GET['job_card_no']) ? $_GET['job_card_no'] : "";
$jobcard_to    = isset($_GET['jobcard_to']) ? $_GET['jobcard_to'] : "";
$buyer_name    = isset($_GET['buyer_name']) ? $_GET['buyer_name'] : "";
$roll_size     = isset($_GET['roll_size']) ? $_GET['roll_size'] : "";
$jobtype       = isset($_GET['jobtype']) ? $_GET['jobtype'] : "";
$from_date     = isset($_GET['from_date']) ? $_GET['from_date'] : "";
$to_date       = isset($_GET['to_date']) ? $_GET['to_date'] : "";

$cur_date = date("Y-m-d");

// ==============================
// Date Conditions
// ==============================
if ($from_date != "") {
    $from_date1 = "DATE_FORMAT(media_date,'%Y-%m-%d') >= '$from_date'";
} else {
    $from_date1 = "DATE_FORMAT(media_date,'%Y-%m-%d') = '$cur_date'";
}
if ($to_date != "") {
    $to_date1 = "DATE_FORMAT(media_date,'%Y-%m-%d') <= '$to_date'";
} else {
    $to_date1 = "DATE_FORMAT(media_date,'%Y-%m-%d') = '$cur_date'";
}

// ==============================
// WHERE Conditions
// ==============================
$conditions = [];
if ($from_date1) $conditions[] = $from_date1;
if ($to_date1) $conditions[] = $to_date1;
if ($buyer_name) $conditions[] = "in_cust_name = '$buyer_name'";
if ($roll_size) $conditions[] = "roll_name = '$roll_size'";
if ($jobtype) $conditions[] = "job_types = '$jobtype'";
if ($job_card_no) $conditions[] = "jobcard_no = '$job_card_no'";

if ($jobcard_from != "" && $jobcard_to != "") {
    $conditions[] = "jobcard_no BETWEEN '$jobcard_from' AND '$jobcard_to'";
} else if ($jobcard_from != "") {
    $conditions[] = "jobcard_no >= '$jobcard_from'";
} else if ($jobcard_to != "") {
    $conditions[] = "jobcard_no <= '$jobcard_to'";
}

$where_sql = (count($conditions) > 0) ? " WHERE " . implode(" AND ", $conditions) : "";
// echo $where_sql;
// ==============================
// Main Query
// ==============================
$sql = "SELECT * FROM media_sub_list $where_sql ORDER BY jobcard_no ASC";
// echo "SELECT * FROM media_sub_list $where_sql ORDER BY jobcard_no ASC";
$rs = mysql_query($sql);
$rscount = ($rs) ? mysql_num_rows($rs) : 0;
?>

<link rel="stylesheet" href="css/font.css" type="text/css" />
<style>
body { font-family: Arial, sans-serif; font-size: 13px; }
.table-container { margin-top: 10px; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ccc; padding: 5px; text-align: left; }
th { background: #f0f0f0; font-weight: bold; }
tr:nth-child(even) { background: #fafafa; }
.total-row th { background: #e8e8e8; }
.header-table td { border: none; }
.header-icons { float: right; margin-right: 10px; }
.summary-table { width: 40%; margin-top: 25px; }
.summary-table th { background: #d9edf7; }
</style>

<script>
function media_report_print(url) {
    window.open(url, 'media_report', 'height=580,width=1200,scrollbars=yes,left=50,top=50,toolbar=no,location=no,status=no,menubar=no');
}
function media_usage_report_overall_excel(from_date, to_date, jobcard_from, jobcard_to, buyer_name, roll_size, type_1, jobtype,job_card_no) {
    window.location.href =
        "report/media_report/usage_excel.php?from_date=" + from_date +
        "&to_date=" + to_date +
        "&jobcard_from=" + jobcard_from +
        "&jobcard_to=" + jobcard_to +
        "&buyer_name=" + buyer_name +
        "&roll_size=" + roll_size +
        "&type_1=" + type_1 +
        "&jobtype=" + jobtype+
        "&job_card_no=" + job_card_no;
}
</script>

<!-- ============================== -->
<!-- HEADER (Excel / Print Buttons) -->
<!-- ============================== -->
<table class="header-table" width="100%">
  <tr>
    <td>
      <a onclick="media_usage_report_overall_excel(
        '<?php echo $from_date; ?>',
        '<?php echo $to_date; ?>',
        '<?php echo $jobcard_from; ?>',
        '<?php echo $jobcard_to; ?>',
        '<?php echo $buyer_name; ?>',
        '<?php echo $roll_size; ?>',
        '<?php echo $type_1; ?>',
        '<?php echo $jobtype; ?>',
        '<?php echo $job_card_no; ?>'

      )">
        <img src="img/excel-icon.png" width="30" height="30" class="header-icons" title="Export to Excel" />
      </a>
      <a href="javascript:media_report_print(
        'report/media_report/media_usages_report_print.php?from_date=<?php echo $from_date; ?>&to_date=<?php echo $to_date; ?>&jobcard_from=<?php echo $jobcard_from; ?>&jobcard_to=<?php echo $jobcard_to; ?>&job_card_no=<?php echo $job_card_no; ?>&buyer_name=<?php echo $buyer_name; ?>&roll_size=<?php echo $roll_size; ?>&type_1=<?php echo $type_1; ?>&jobtype=<?php echo $jobtype; ?>'
      )">
        <img src="img/PrinterBlack.png" width="35" height="30" class="header-icons" title="Print" />
      </a>
    </td>
  </tr>
</table>

<!-- ============================== -->
<!-- USAGE LIST TABLE -->
<!-- ============================== -->
<div class="table-container">
<table>
  <thead>
    <tr>
      <th>S.No</th>
      <th>Date</th>
      <th>Job Type</th>
      <th>Jobcard No</th>
      <th>Buyer Name</th>
      <th>Particulars</th>
      <th>Width</th>
      <th>Height</th>
      <th>Order Qty</th>
      <th>Planed Qty</th>
      <th>Roll Size</th>
      <th>Batch No</th>
      <th>Req By</th>
      <th>Steps Used</th>
      <th>Steps</th>
      <th>Total Repeats</th>
      <th>Gap</th>
      <th>Gap Mtr</th>
      <th>Total Req</th>
    </tr>
  </thead>
  <tbody>
<?php
if ($rscount > 0) {
    $i = 0; $sum_tot2 = 0;
    while ($rsdata = mysql_fetch_array($rs)) {
        $i++;
        $media_date   = $rsdata['media_date'];
        $jobtype      = $rsdata['job_types'];
        $jobcard_no   = $rsdata['jobcard_no'];
        $customer     = $rsdata['in_cust_name'];
        $media_part   = $rsdata['media_particular'];
        $width        = $rsdata['width'];
        $height       = $rsdata['height'];
        $order_qty    = $rsdata['order_qty'];
        $plan_qty     = $rsdata['plan_qty'];
        $roll_name    = $rsdata['roll_name'];
        $batch_no     = $rsdata['batch_no'];
        $req_by       = $rsdata['req_by'];
        $steps_used   = $rsdata['steps_used'];
        $steps        = $rsdata['steps'];
        $total_steps  = $rsdata['total_steps'];
        $gap          = $rsdata['gap'];
        $gap_mtr      = $rsdata['gap_mtr'];
        $tot_req_by   = $rsdata['tot_req_by'];
        $sum_tot2 += $tot_req_by;

        echo "<tr>
          <td>$i</td>
          <td>".date("d-m-Y", strtotime($media_date))."</td>
          <td>$jobtype</td>
          <td>$jobcard_no</td>
          <td>$customer</td>
          <td>$media_part</td>
          <td>$width</td>
          <td>$height</td>
          <td align='right'>".number_format($order_qty,2)."</td>
          <td align='right'>".number_format($plan_qty,2)."</td>
          <td>$roll_name</td>
          <td>$batch_no</td>
          <td align='right'>$req_by</td>
          <td align='right'>$steps_used</td>
          <td align='right'>$steps</td>
          <td align='right'>$total_steps</td>
          <td align='right'>$gap</td>
          <td align='right'>$gap_mtr</td>
          <td align='right'>".number_format($tot_req_by,2)."</td>
        </tr>";
    }
    echo "<tr class='total-row'>
            <th colspan='18' align='right'>Total</th>
            <th align='right'>".number_format($sum_tot2,2)."</th>
          </tr>";
} else {
    echo "<tr><td colspan='19' align='center'>No records found.</td></tr>";
}
?>
  </tbody>
</table>
</div>

<!-- ============================== -->
<!-- SUMMARY TABLE (Roll Size Totals) -->
<!-- ============================== -->
<?php
$summary_conditions = $conditions;
$summary_where = (count($summary_conditions) > 0) ? " WHERE " . implode(" AND ", $summary_conditions) : "";
$sql_summary = "SELECT roll_name, SUM(tot_req_by) AS tot FROM media_sub_list $summary_where GROUP BY roll_name ORDER BY roll_name ASC";
$rs_sum = mysql_query($sql_summary);
?>

<table class="summary-table">
  <thead>
    <tr>
      <th>Roll Size</th>
      <th>Sum of Total Req</th>
    </tr>
  </thead>
  <tbody>
<?php
$grand_total = 0;
if ($rs_sum && mysql_num_rows($rs_sum) > 0) {
    while ($row = mysql_fetch_array($rs_sum)) {
        $grand_total += $row['tot'];
        echo "<tr>
                <td>{$row['roll_name']}</td>
                <td align='right'>".number_format($row['tot'],2)."</td>
              </tr>";
    }
    echo "<tr class='total-row'>
            <th align='right'>Grand Total</th>
            <th align='right'>".number_format($grand_total,2)."</th>
          </tr>";
} else {
    echo "<tr><td colspan='2' align='center'>No records found.</td></tr>";
}
?>
  </tbody>
</table>


        </div> <!-- CSSTableGenerator -->
      </div>
    </div>
  </div>
</div>
