<?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(); ?>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('Uniformpos/add') ?>" method="POST" accept-charset="utf-8">
                        <div class="box-body">
                            <?php if ($this->session->flashdata('msg')) { ?>
                                <div id="flash-msg">
                                    <?php echo $this->session->flashdata('msg'); ?>
                                </div>
                            <?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="inv">Invoice</label><small class="req"> *</small>
                                <input autofocus="" id="inv" name="inv" placeholder="" readonly  type="text"
                                    class="form-control" value="<?php echo $inv; ?>" />
                                <span class="text-danger"><?php echo form_error('inv'); ?></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="itemcode">Item Code</label><small class="req"> *</small>
                                <select id="itemcode" name="itemcode" placeholder="" type="text" class="form-control">
                                    <option value="select"><?php echo $this->lang->line('select') ?></option>
                                    <?php 
												foreach ($code->result() as $item) {
												?>
                                  <option value="<?php echo $item->itemcode; ?>" data-amount="<?php echo $item->sellingprice; ?>" data-warehouse="<?php echo $item->warehouse; ?>" data-costprice="<?php echo $item->costprice; ?>" data-disc="<?php echo $item->itemdescript; ?>"data-quantity="<?php echo $item->quantity; ?>"> <?php echo $item->itemcode; ?>
                                    <?php echo $item->itemdescript?>
                                  </option>
                                    <?php }
											?>
                                </select>
                                <span class="text-danger"><?php echo form_error('itemcode'); ?></span>
                            </div>
                            <div class="form-group">
                                <label for="description">Description</label><small class="req"> *</small>
                                <input autofocus="" id="description" name="description" placeholder="" readonly  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="amount">Cost Price</label><small class="req"> *</small>
                                <input autofocus="" id="cost" name="cost" placeholder="" readonly type="text"
                                    class="form-control" value="<?php echo set_value('cost'); ?>" />
                                <span class="text-danger"><?php echo form_error('cost'); ?></span>
                            </div>
                            <div class="form-group">
                                <label for="amount">Selling Price</label><small class="req"> *</small>
                                <input autofocus="" id="amount" name="amount" placeholder="" readonly  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="amount">Warehouse</label><small class="req"> *</small>
                                <input autofocus="" id="warehouse" name="warehouse" placeholder="" readonly  type="text"
                                    class="form-control" value="<?php echo set_value('warehouse'); ?>" />
                                <span class="text-danger"><?php echo form_error('warehouse'); ?></span>
                            </div>

                            <div class="form-group">
                                <label for="amount">Quantity Available</label><small class="req"> *</small>
                                <input autofocus="" id="originalquantity" name="originalquantity" placeholder="" readonly  type="text"
                                    class="form-control" value="<?php echo set_value('originalquantity'); ?>" />
                                <span class="text-danger"><?php echo form_error('originalquantity'); ?></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="quantity">Quantity</label><small class="req"> *</small>
                                <input autofocus="" id="quantity" name="quantity" placeholder="1" type="text"
                                    class="form-control" value="<?php echo set_value('quantity', '1'); ?>" />
                                <span class="text-danger"><?php echo form_error('quantity'); ?></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>Date</th>
                                        <th>Student-Id</th>
                                        <th>Name</th>
                                        <th>Item Code</th>
                                        <th>Description</th>
                                        <th>Narration</th>
                                        <th>Quantity</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->itemcode;?></td>
                                        <td class="mailbox-name"><?php 

                                                $itemcode2= $row->itemcode;


                                                $this->db->select('*');
                                                $this->db->from('uniformitem');
                                                $this->db->where('itemcode', $itemcode2);
                                                $query = $this->db->get();
                                                $result = $query->row();
echo $result->itemgroups.'/'.$result->groupsize.'-'.$result->valuesize.'/'.$result->groupstyle.'-'.$result->stylevalue.'/'.$result->colourgroup.'-'.$result->colourvalue.'/'.$result->gendergroup.'-'.$result->gendervalue;

                                            ?></td>
                                        <!--<td class="mailbox-name"><?php 

                                        $itemcode2= $row->itemcode;


                                        $this->db->select('*');
                                        $this->db->from('uniformitem');
                                        $this->db->where('itemcode', $itemcode2);
                                        $query = $this->db->get();
                                        $result = $query->row();
                                        echo $result->quantity;
                                        ?></td>-->
                                        <td class="mailbox-name"><?php echo $row->narration;?></td>
                                        <td class="mailbox-name"><?php echo $row->quantity;?></td>
                                       
                                        <td class="mailbox-name"><?php echo $row->discount;?></td>
                                        <td class="mailbox-name"><?php echo ($row->quantity*$row->amount)-$row->discount;?></td>
                                        
                                    </tr>
                                    <?php } ?>

                                    <tr>
													 <td>           
															<div class="btn-group pull-left"> 														
																<button  class="btn btn-xs btn-info printInv"                  
																		 title="<?php echo $this->lang->line('print'); ?>"
																		 ><i class="fa fa-print"></i> Print Invoice</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">Invoice No#</p>
                  <p><?php echo $inv ?></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">
                <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-left font-bold text-main">Item Code</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">Price</td>
              <td class="border-b-2 border-main pb-3 pl-2 text-center font-bold text-main">Qnty</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;
          $totalexcl=0;
										 foreach ($uniform->result() as $row)  
										 {  

                                            $total=$total+($row->quantity*$row->amount)-$row->discount;
                                            $totaldisc=$totaldisc+$row->discount;
                                            $totalexcl=$totalexcl+($row->quantity*$row->amount);


                                        ?>
       
            <tr>
            <td class="border-b py-3 pl-2"><?php echo $row->itemcode;?></td>
                                    
             
              <td class="border-b py-3 pl-2"><?php 

$itemcode2= $row->itemcode;


$this->db->select('*');
$this->db->from('uniformitem');
$this->db->where('itemcode', $itemcode2);
$query = $this->db->get();
$result = $query->row();
echo $result->itemgroups.'/'.$result->groupsize.'-'.$result->valuesize.'/'.$result->groupstyle.'-'.$result->stylevalue.'/'.$result->colourgroup.'-'.$result->colourvalue.'/'.$result->gendergroup.'-'.$result->gendervalue;

?></td>
              
              <td class="border-b py-3 pl-2 text-right"><?php echo $row->amount;?></td>
              <td class="border-b py-3 pl-2 text-center"><?php echo $row->quantity;?></td>
              <td class="border-b py-3 pl-2 text-center"><?php echo $row->discount;?></td>
              
              <td class="border-b py-3 pl-2 pr-3 text-right"><?php echo ($row->quantity*$row->amount);?></td>

            </tr>
            <?php } ?>


            <tr>
              <td colspan="9">
                <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 (Excl):</div>
                              </td>
                              <td class="p-3 text-right">
                                <div class="whitespace-nowrap font-bold text-main"><?php echo $totalexcl;?></div>
                              </td>
                            </tr>
                          
                            <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 $totaldisc;?></div>
                              </td>
                            </tr>
                            <tr>
                              <td class="bg-main p-3">
                                <div class="whitespace-nowrap font-bold text-white">Total (Amt):</div>
                              </td>
                              <td class="bg-main p-3 text-right">
                                <div class="whitespace-nowrap font-bold text-white"><?php echo $total;?></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.getElementById('itemcode').addEventListener('change', function() {
        // Get the selected option
        var selectedOption = this.options[this.selectedIndex];
        var amount = selectedOption.getAttribute('data-amount'); 
        document.getElementById('amount').value = amount ? amount : '';
        var cost = selectedOption.getAttribute('data-costprice');
        document.getElementById('cost').value = cost ? cost : '';
        var description = selectedOption.getAttribute('data-disc');
        document.getElementById('description').value = description ? description : '';
        var originalQuantity = selectedOption.getAttribute('data-quantity');
        document.getElementById('originalquantity').value = originalQuantity ? originalQuantity : '';
        var warehouse = selectedOption.getAttribute('data-warehouse');
        document.getElementById('warehouse').value = warehouse ? warehouse : '';
        
    });
</script>
<script>
    document.querySelector('.printInv').addEventListener('click', function() {
        // AJAX request to update the database
        $.ajax({
            url: '<?php echo base_url("Uniformpos/updatenow"); ?>', // Route to update the DB
            type: 'POST',
            data: { batch_num_id: '0' }, // Pass the actual invoice 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 Invoice</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 invoice status.');
                }
            },
            error: function() {
                alert('Error while updating the invoice status.');
            }
        });
    });
</script>
<script>
$(document).ready(function() {
    // Hide flash message after 3 seconds (3000ms)
    setTimeout(function() {
        $('#flash-msg').fadeOut('slow');
    }, 3000);
});
</script>

<script>
$(document).ready(function() {

    $('#student_id').select2({
        placeholder: "Search student by name or ID",
        allowClear: true,
        width: '100%'
    });


// Populate the name field when student is selected
    $('#student_id').on('select2:select', function (e) {
        var selectedOption = $(this).find(':selected');
        var firstname = selectedOption.data('firstname');
        var lastname  = selectedOption.data('lastname');

        // Fill the name input
        $('#name').val(firstname + ' ' + lastname);
    });

    // Clear name input if selection is cleared
    $('#student_id').on('select2:unselect', function (e) {
        $('#name').val('');
    });

});
</script>

