<?php 
  session_start();
  if(empty($_SESSION['branch']) || $_SESSION['branch'] == ''){
    header("Location: index.php");
    die();
  }
  require_once 'system/conn.php';

  // 1. Centralized Date Logic (Fixes Redirect Loop)
  $year  = $_POST['year']  ?? $_SESSION['archive_year'] ?? null;
  $month = $_POST['month'] ?? $_SESSION['archive_month'] ?? null;
  $branch_id = $_SESSION['branch'];

  if(!$year || !$month){
      header("Location: previous-salary.php");
      exit();
  }
  
  $dateObj   = DateTime::createFromFormat('!m', $month);
  $monthName = $dateObj->format('F');
?>
<!doctype html>
<html lang="en" class="semi-dark">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="assets/css/pace.min.css" rel="stylesheet" />
    <script src="assets/js/pace.min.js"></script>
    <link href="assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet" />
    <link href="assets/plugins/simplebar/css/simplebar.css" rel="stylesheet" />
    <link href="assets/plugins/metismenu/css/metisMenu.min.css" rel="stylesheet" />
    <link href="assets/plugins/datatable/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/bootstrap-extended.css" rel="stylesheet">
    <link href="assets/css/style.css" rel="stylesheet">
    <link href="assets/css/icons.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap" rel="stylesheet">
    <link href="assets/css/dark-theme.css" rel="stylesheet" />
    <link href="assets/css/semi-dark.css" rel="stylesheet" />
    <link href="assets/css/header-colors.css" rel="stylesheet" />
    <title>Previous Bank Sheet (<?php echo "$year - $month"; ?>)</title>
  </head>
  <body>
    <div class="wrapper">
    <?php include 'system/header.php'?>
      <div class="page-content-wrapper">
         <div class="page-content">
          <div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
            <div class="breadcrumb-title pe-3">Previous Bank Sheet (<?php echo "$year - $monthName"; ?>)</div>
            <div class="ms-auto">
               <a href="excel-previous-bank-sheet.php?m=<?php echo $month; ?>&y=<?php echo $year; ?>"><button type="button" class="btn btn-primary px-5">Excel Download</button></a>
            </div>
          </div>
            <hr/>
            
                 <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table id="example2" class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>SR No</th>
                                        <th>Employee Name</th>
                                        <th>Designation</th>
                                        <th>Employee Code</th>
                                        <th>Account No.</th>
                                        <th>Net Pay</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                        $cnt = 1;
                                        
                                        // UNION LOGIC APPLIED HERE
                                        // Query 1: Employee Data
                                        // Query 2: Total Sum (Name = 'z_TOTAL')
                                        $sql = "
                                            SELECT name, designation, emp_id, ac_no, net_pay
                                            FROM prv_emp_salary
                                            WHERE branch = :branch1 
                                              AND month = :month1 
                                              AND year = :year1 
                                              AND block = '' 
                                              AND cheque = ''
                                            
                                            UNION
                                            
                                            SELECT 'Z TOTAL', '', '', '', SUM(net_pay)
                                            FROM prv_emp_salary
                                            WHERE branch = :branch2
                                              AND month = :month2
                                              AND year = :year2
                                              AND block = ''
                                              AND cheque = ''
                                            
                                            ORDER BY name ASC
                                        ";
                                        
                                        $stmt = $conn->prepare($sql);
                                        $stmt->execute([
                                            ':branch1' => $branch_id, ':month1' => $month, ':year1' => $year,
                                            ':branch2' => $branch_id, ':month2' => $month, ':year2' => $year
                                        ]);
                                        
                                        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                                            $isTotal = ($row['name'] == 'z_TOTAL');
                                    ?>
                                    <tr>
                                        <td><?php echo $isTotal ? '' : $cnt; ?></td>
                                        
                                        <td><?php echo $isTotal ? '<strong>TOTAL</strong>' : htmlspecialchars($row['name']);?></td>
                                        
                                        <td><?php echo htmlspecialchars($row['designation']);?></td>
                                        <td><?php echo htmlspecialchars($row['emp_id']);?></td>
                                        <td><?php echo htmlspecialchars($row['ac_no']);?></td>
                                        
                                        <td class="<?php echo $isTotal ? 'fw-bold text-primary' : ''; ?>">
                                            <?php echo $row['net_pay'];?>
                                        </td> 
                                    </tr>
                                    <?php 
                                        if(!$isTotal) $cnt++; 
                                        } 
                                    ?>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <th>SR No</th>
                                        <th>Employee Name</th>
                                        <th>Designation</th>
                                        <th>Employee Code</th>
                                        <th>Account No.</th>
                                        <th>Net Pay</th>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>

         </div>
         </div>

<?php include 'system/footer.php'?>