<?php $currency_symbol = $this->customlib->getSchoolCurrencyFormat(); ?>

<style type="text/css">
    @media print {
        /* Adjust the .py-4 class to support different layouts */
        .py-4 {
            font-size: 12px; /* Default font size */
            line-height: 1.2;
            padding: 5px; /* Reduce padding for small receipts */
        }

        /* Adjust for different column sizes when printing */
        .col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12 {
            float: left;
            box-sizing: border-box;
        }
        .col-sm-12 {
            width: 100%;
        }
        .col-sm-11 {
            width: 91.66666667%;
        }
        .col-sm-10 {
            width: 83.33333333%;
        }
        .col-sm-9 {
            width: 75%;
        }
        .col-sm-8 {
            width: 66.66666667%;
        }
        .col-sm-7 {
            width: 58.33333333%;
        }
        .col-sm-6 {
            width: 50%;
        }
        .col-sm-5 {
            width: 41.66666667%;
        }
        .col-sm-4 {
            width: 33.33333333%;
        }
        .col-sm-3 {
            width: 25%;
        }
        .col-sm-2 {
            width: 16.66666667%;
        }
        .col-sm-1 {
            width: 8.33333333%;
        }

        /* Adjust for page breaks */
        .col-sm-pull-12,
        .col-sm-push-12 {
            right: 100%;
            left: 100%;
        }

        .col-sm-offset-12 {
            margin-left: 100%;
        }

        /* Make columns responsive for smaller screens */
        .visible-xs {
            display: none !important;
        }

        .hidden-xs {
            display: block !important;
        }

        .hidden-xs.hidden-print {
            display: none !important;
        }

        /* Adjust layout for printers with small paper sizes (e.g., Epson receipt printers) */
        @media print and (max-width: 80mm) {
            .py-4 {
                font-size: 10px; /* Smaller font for smaller printers */
                padding: 3px; /* Adjust padding for small receipts */
            }
            td, th {
                font-size: 9px; /* Smaller text for table cells */
            }
            .col-sm-12, .col-sm-6 {
                width: 100%; /* Full-width for small receipts */
            }
            .col-sm-1 {
                width: 10%; /* Adjust widths */
            }
            .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6 {
                width: 50%; /* Adjusting for small receipt printer widths */
            }
        }

        /* Hide elements for print */
        .hidden-print {
            display: none !important;
        }

        /* Print margins and page size settings */
        @page {
            size: auto; /* Let the printer decide the page size */
            margin: 5mm; /* Reduce margins for compact printing */
        }

        /* Overall table adjustments */
        table {
            width: 100%;
            border-collapse: collapse;
        }
        td, th {
            padding: 2px;
            font-size: 10px; /* Adjust the font size for smaller receipts */
        }
    }
</style>

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper" >

    <section class="content-header">
        <h1>
            <i class="fa fa-mortar-board"></i> Point Of Sale
        </h1>
        <a href="<?php echo base_url(); ?>uniformreceipting" class="col-md pull-right">Receipt a Uniform</a>
        <b style="padding-left:5px;padding-right:5px;" class="pull-right"> | </b>
        <a href="<?php  echo base_url(); ?>uniformpos" class="col-md pull-right">Sale a Uniform</a>
        <b style="padding-left:5px;padding-right:5px;" class="pull-right"> | </b>
        <a href="<?php  echo base_url(); ?>uniformitem" class="col-md pull-right">Add Item</a>
        <b style="padding-left:5px;padding-right:5px;" class="pull-right"> | </b>
        <a href="<?php  echo base_url(); ?>group" class="col-md pull-right">Create Group</a>
        <b style="padding-left:5px;padding-right:5px;" class="pull-right"> | </b>
        <a href="<?php  echo base_url(); ?>values" class="col-md pull-right">Create Values</a>  
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <?php
            if ($this->rbac->hasPrivilege('class', 'can_add')) {
                ?>
            <div class="col-md-4">
                <!-- Horizontal Form -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><?php echo $title;       ?></h3>
                    </div><!-- /.box-header -->
                    <form id="form1" action="<?php echo base_url('Uniformreceipting/add') ?>" method="POST" accept-charset="utf-8">
                        <div class="box-body">
                            <?php if ($this->session->flashdata('msg')) { ?>
                            <?php echo $this->session->flashdata('msg') ?>
                            <?php } ?>
                            <?php
                                if (isset($error_message)) {
                                    echo "<div class='alert alert-danger'>" . $error_message . "</div>";
                                }    
                                ?>
                            <?php echo $this->customlib->getCSRF(); ?>

                            <div class="form-group">
                                <label for="colour">Date</label><small class="req"> *</small>
                                <input autofocus="" id="rec_date" name="rec_date" placeholder="" type="date"
                                    class="form-control" value="<?php echo date("Y-m-d")?>" />
                                <span class="text-danger"><?php echo form_error('inv_date'); ?></span>
                            </div>

                            <div class="form-group">
                                <label for="rec">Receipt</label><small class="req"> *</small>
                                <input autofocus="" id="rec" name="rec" placeholder="" readonly  type="text"
                                    class="form-control" value="<?php echo $rec; ?>" />
                                <span class="text-danger"><?php echo form_error('rec'); ?></span>
                            </div>

                                    <div class="form-group">
                                    <label for="inv_number">Reference</label><small class="req"> *</small>
                                    <select id="inv_number" name="inv_number" class="form-control">
                                        <option value="">Select Reference</option>
                                        <?php 
                                        if (isset($references) && !empty($references)) {
                                            foreach ($references as $reference) { ?>
                                                <option value="<?php echo $reference->batch_num; ?>">
                                                    <?php echo $reference->batch_num . ' : ' . $reference->itemcode . ' - ' . $reference->description; ?>
                                                </option>
                                        <?php 
                                            }
                                        } 
                                        ?>
                                    </select>
                                    <span class="text-danger"><?php echo form_error('inv_number'); ?></span>
                                </div>

                                <div class="form-group">
                                <label for="student_id">Student Id</label><small class="req"> *</small>
                                <select id="student_id" name="student_id" class="form-control">
                                    <option value="select"><?php echo $this->lang->line('select') ?></option>
                                     <?php foreach ($studenID->result() as $student) { ?>
                                    <option value="<?php echo $student->admission_no; ?>" 
                                            data-firstname="<?php echo $student->firstname; ?>" 
                                            data-lastname="<?php echo $student->lastname; ?>">
                                        <?php echo $student->admission_no. ' : '. $student->firstname . ' ' . $student->lastname; ?>
                                    </option>
                                <?php } ?>
                                </select>
                                <span class="text-danger"><?php echo form_error('student_id'); ?></span>
                            </div>

                            <div class="form-group">
                                <label for="name">Name</label><small class="req"> *</small>
                                <input autofocus="" id="name" name="name" readonly type="text" class="form-control" value="<?php echo set_value('name'); ?>" />
                                <span class="text-danger"><?php echo form_error('name'); ?></span>
                            </div>

                            <div class="form-group">
                                <label for="gl_account">Account Name</label><small class="req"> *</small>
                                <select id="gl_account" name="gl_account" class="form-control">
                                    <option value="">Select GL Account</option>
                                    <?php foreach ($glledger_accounts->result() as $grup11) { ?>
                                        <option value="<?php echo $grup11->name; ?>">
                                            <?php echo $grup11->gl_code . ' > ' . $grup11->name; ?>
                                        </option>

                                        <?php
                                        $account_type = $grup11->gl_code;
                                        // Query the database for sub-accounts related to the current account
                                        $this->db->select('*');
                                        $this->db->from('glsub_account');
                                        $this->db->where('account_type', $account_type); // Fixed the WHERE clause
                                        $this->db->order_by('glsub_account.name', 'ASC');
                                        $query1 = $this->db->get();

                                        foreach ($query1->result() as $grup12) {
                                        ?>
                                            <option value="<?php echo $grup12->name; ?>">
                                                <?php echo $grup11->gl_code . ' > ' . $grup12->glsub_code . ' > ' . $grup12->name; ?>
                                            </option>
                                        <?php } ?>
                                    <?php } ?>
                                </select>
                                <span class="text-danger"><?php echo form_error('gl_account'); ?></span>
                            </div>

                          
                            <div class="form-group">
                                <label for="colour">Description</label><small class="req"> *</small>
                                <input autofocus="" id="description" name="description" placeholder="" type="text"
                                    class="form-control" value="<?php echo set_value('description'); ?>" />
                                <span class="text-danger"><?php echo form_error('description'); ?></span>
                            </div>
                             <div class="form-group">
                                <label for="colour">Narration</label><small class="req"> *</small>
                                <input autofocus="" id="narration" name="narration" placeholder="" type="text"
                                    class="form-control" value="<?php echo set_value('narration'); ?>" />
                                <span class="text-danger"><?php echo form_error('narration'); ?></span>
                            </div>
                            
                            <div class="form-group">
                                <label for="amount">Amount</label><small class="req"> *</small>
                                <input autofocus="" id="amount" name="amount" placeholder="" type="text"
                                    class="form-control" value="<?php echo set_value('amount'); ?>" />
                                <span class="text-danger"><?php echo form_error('amount'); ?></span>
                            </div>

                            <div class="form-group">
                                <label for="discount">Discount</label><small > </small>
                                <input autofocus="" id="discount" name="discount" placeholder="0.00" type="text"
                                    class="form-control" value="<?php echo set_value('discount', '0'); ?>" />
                                <span class="text-danger"><?php echo form_error('discount'); ?></span>
                            </div>
                           
                            <div class="form-group">
                                <label for="payment">Payment Method</label><small class="req"> *</small>
                                <select id="payment" name="payment" placeholder="" type="text" class="form-control">
                                    <option value=""><?php echo $this->lang->line('select') ?></option>
                                    <?php 
												foreach ($payment_methods->result() as $method) {
												?>
                                    <option value="<?php echo $method->payment_methods; ?>">
                                        <?php echo $method->payment_methods; ?></option>
                                    <?php }
											?>
                                </select>
                                <span class="text-danger"><?php echo form_error('payment'); ?></span>
                            </div>
                        </div><!-- /.box-body -->
                        <div class="box-footer">
                            <button type="submit"
                                class="btn btn-info pull-right"><?php echo $this->lang->line('save'); ?></button>
                        </div>

                    </form>
                </div>
            </div>
            <!--/.col (right) -->
            <!-- left column -->
            <?php } ?>
            <div class="col-md-<?php
            if ($this->rbac->hasPrivilege('class', 'can_add')) {
                echo "8";
            } else {
                echo "12";
            }
            ?>">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header ptbnull">
                        <h3 class="box-title titlefix"><?php echo $title_list; ?></h3>
                        <div class="box-tools pull-right">
                        </div><!-- /.box-tools -->
                    </div><!-- /.box-header -->
                    <div class="box-body">
                        <div class="table-responsive mailbox-messages">
                            <div class="download_label"><?php echo $title_list; ?></div>
                            <table class="table table-striped table-bordered table-hover example">
                                <thead>
                                    <tr>
                                        <th>Entry Date</th>
                                        <th>Student ID</th>
                                        <th>Name</th>
                                        <th>GL Account</th>
                                        <th>Description</th>
                                        <th>Amount</th>
                                        <th>Discount</th>
                                        <th>Total</th>                                     
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php  
										 foreach ($uniform->result() as $row)  
										 {   
                                        ?>
                                    <tr>
                                        <td class="mailbox-name"><?php echo $row->created_date;?></td>
                                        <td class="mailbox-name"><?php echo $row->student_id;?></td>
                                        <td class="mailbox-name"><?php echo $row->name;?></td>
                                        <td class="mailbox-name"><?php echo $row->gl_account;?></td>
                                        <td class="mailbox-name"><?php echo $row->description;?></td>  
                                     <td class="mailbox-name"><?php echo $row->amount;?></td>
                                        <td class="mailbox-name"><?php echo $row->discount;?></td>
                                        <td class="mailbox-name"><?php echo ($row->amount)- $row->discount;?></td>
                                    </tr>
                                    <?php } ?>
                                    <tr>
										    <td>           
									    		<div class="btn-group pull-left"> 														
									    			<button  class="btn btn-xs btn-info printRec"                  
									    					 title="<?php echo $this->lang->line('print'); ?>"
									    					 ><i class="fa fa-print"></i> Print Receipt</button>
									    		</div> 
									    	</td>
								    </tr>
                                </tbody>
                            </table><!-- /.table  <link rel="stylesheet" href="<?php echo base_url(); ?>backend/dist/css/style.css">-->
                            <div class="py-4" style="display: none">
                            <div class="px-14 py-6">
                                <table class="w-full border-collapse border-spacing-0">
                                <tbody>
                                <tr class="row header text-center">
                            <td>
                                <div class="row header text-center">
                                    <div class="col-sm-12">
                                        <?php if (!empty($settinglist[0]['image'])) { ?>
                                            <div style="display: flex; justify-content: center;">
                                                <img style="height:70px;" src="<?php echo base_url(); ?>/uploads/school_content/logo/<?php echo $settinglist[0]['image']; ?>"> 
                                            </div>
                                            <br/>
                                        <?php } ?>
                                        <strong style="font-size: 20px;"><?php echo $settinglist[0]['name']; ?></strong><br>
                                        <?php echo $settinglist[0]['address']; ?><br>
                                        <?php echo $this->lang->line('phone'); ?>: <?php echo $settinglist[0]['phone']; ?><br>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                    </table>
                </div>

                <div class="bg-slate-100 px-14 py-6 text-sm">
                    <table class="w-full border-collapse border-spacing-0">
                    <tbody>
                        <tr>
                        <td class="w-1/3 align-top">
                            <div class="text-sm text-neutral-600">
                            
                            <p class="font-bold">Receipt No#</p>
                            <p><?php echo $rec ?></p>               
                            </div>
                        </td>
                        <td class="w-1/3 align-top">
                                                <div class="text-sm text-neutral-600">
                                                    <p class="font-bold">Date</p>
                                                    <?php
                            $todayLongDate = (new DateTime())->format('F j, Y');?>
                                                    <p><?php echo  $todayLongDate ?></p>
                                                </div>
                                                </td>
                                                <td class="w-1/3 align-top">
                                                <div class="text-sm text-neutral-600">
                                                    <p class="font-bold">Student ID</p>
                                                    <p><?php echo  $studentID ?></p>
                                                </div>
                                                </td>
                                    <td class="w-1/3 align-top text-right">
                                        <div class="text-sm text-neutral-600">
                                    
                        <p class="font-bold">
                                            <?php 
                                            $this->db->select('*');
                                            $this->db->from('students');
                                            $this->db->where('admission_no', $studentID);
                                            $query = $this->db->get();
                                            $result = $query->row();
                                            if ($result) {
                                            echo $result->firstname.' '.$result->lastname;
                                            } else {
                                echo "No Name found.";
                            }
                                            ?>
                                        </p>
                                        <p>
                                            <?php 
                                            $this->db->select('*');
                                            $this->db->from('students');
                                            $this->db->where('admission_no', $studentID);
                                            $query = $this->db->get();
                                            $result = $query->row();
                                            if ($result) {
                                            echo $result->current_address;
                                            } else {
                                echo "No address found.";
                            }
                                            ?>
                            </p>
                                        
                            <p>
                            <?php 
                            // Use raw SQL query directly
                            $sql = "SELECT * FROM classes WHERE id = (
                                        SELECT class_id 
                                        FROM student_session 
                                        WHERE student_id = (
                                            SELECT id 
                                            FROM students 
                                            WHERE admission_no = '$studentID' 
                                            LIMIT 1
                                        ) 
                                        LIMIT 1
                                    )";
                            
                            $query = $this->db->query($sql);
                            $result = $query->row();
                            
                            // Accessing the 'class' column
                            if ($result) {
                                echo $result->{'class'}; // Use curly braces to avoid the 'class' keyword conflict
                            } else {
                                echo "No class found.";
                            }
                            ?>
                        </p>
                            </div>
                        </td>
                        </tr>
                    </tbody>
                    </table>
                </div>
                <div class="px-14 py-10 text-sm text-neutral-700">
                <table class="w-full border-collapse border-spacing-0">
                <thead>
                    <tr>
                        
                        <td class="border-b-2 border-main pb-3 pl-2 text-center font-bold text-main">Reference</td>
                        <td class="border-b-2 border-main pb-3 pl-2 font-bold text-main">Description</td>
                        <td class="border-b-2 border-main pb-3 pl-2 text-right font-bold text-main">Amount</td>
                        <td class="border-b-2 border-main pb-3 pl-2 text-center font-bold text-main">Disc</td>
                        <td class="border-b-2 border-main pb-3 pl-2 pr-3 text-right font-bold text-main">Total</td>
                    </tr>
                </thead>
                <tbody>
                    <?php  
                    $totaldisc = 0;
                    $total = 0;

                    foreach ($uniform->result() as $row) {  
                        // Calculate total for each row
                        $row_total = $row->amount - $row->discount;
                        $totaldisc += $row->discount;
                        $total += $row_total;
                    ?>
                    <tr>
                    
                        <td class="border-b py-3 pl-2 text-center"><?php echo $row->reference; ?></td>  
                        <td class="border-b py-3 pl-2"><?php echo $row->description; ?></td>
                        <td class="border-b py-3 pl-2 text-right"><?php echo number_format($row->amount, 2); ?></td>
                        <td class="border-b py-3 pl-2 text-center"><?php echo number_format($row->discount, 2); ?></td>
                        <td class="border-b py-3 pl-2 pr-3 text-right"><?php echo number_format($row_total, 2); ?></td>
                    </tr>
                    <?php } ?>
                    
                    <!-- Summary Row -->
                    <tr>
                        <td colspan="6">
                            <table class="w-full border-collapse border-spacing-0">
                                <tbody>
                                    <tr>
                                        <td class="w-full"></td>
                                        <td>
                                            <table class="w-full border-collapse border-spacing-0">
                                                <tbody>
                                                    <tr>
                                                        <td class="p-3">
                                                            <div class="whitespace-nowrap text-slate-400">Total (Disc):</div>
                                                        </td>
                                                        <td class="p-3 text-right">
                                                            <div class="whitespace-nowrap font-bold text-main"><?php echo number_format($totaldisc, 2); ?></div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td class="bg-main p-3">
                                                            <div class="whitespace-nowrap font-bold text-white">Total (Amt) Paid:</div>
                                                        </td>
                                                        <td class="bg-main p-3 text-right">
                                                            <div class="whitespace-nowrap font-bold text-white"><?php echo number_format($total, 2); ?></div>
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </td>
                    </tr>
                </tbody>
            </table>

                </div>
                </div>
                                    </div><!-- /.mail-box-messages -->
                                </div><!-- /.box-body -->
                            </div>
                        </div>
                        <!--/.col (left) -->
                        <!-- right column -->

                    </div>
                    <div class="row">
                        <!-- left column -->

                        <!-- right column -->
                        <div class="col-md-12">

                        </div>
                        <!--/.col (right) -->
                    </div> <!-- /.row -->
                </section><!-- /.content -->
            </div><!-- /.content-wrapper -->
            <script>
                document.getElementById('student_id').addEventListener('change', function() {
                    // Get the selected option
                    var selectedOption = this.options[this.selectedIndex];
                    
                    // Retrieve first and last name from data attributes
                    var firstname = selectedOption.getAttribute('data-firstname') || '';
                    var lastname = selectedOption.getAttribute('data-lastname') || '';
                    
                    // Concatenate first and last name and set it in the name field
                    document.getElementById('name').value = firstname + ' ' + lastname;
                });
            </script>
            <script>
                document.querySelector('.printRec').addEventListener('click', function() {
                    // AJAX request to update the database
                    $.ajax({
                        url: '<?php echo base_url("Uniformreceipting/updatenow"); ?>', // Route to update the DB
                        type: 'POST',
                        data: { batch_num_id: '0' }, // Pass the actual Receipt ID here
                        success: function(response) {
                            // If the database update is successful, trigger the print
                            if (response == 'success') {
                                var printWindow = window.open('', '', 'height=600,width=800');
                                var content = document.querySelector('.py-4').innerHTML;
                                printWindow.document.write('<html><head><title>Print Receipt</title>');
                                printWindow.document.write('<link rel="stylesheet" href="<?php echo base_url(); ?>backend/dist/css/style.css">'); // Optional: include styles
                                printWindow.document.write('</head><body >');
                                printWindow.document.write(content);
                                printWindow.document.write('</body></html>');
                                printWindow.document.close();
                                printWindow.focus();
                                printWindow.print();
                            } else {
                                alert('Failed to update the receipt status.');
                            }
                        },
                        error: function() {
                            alert('Error while updating the receipt status.');
                        }
                    });
                });
            </script>


