  <?php 
    $this->load->view('templates/header');
    $action                     = (isset($_GET['action'])) ? $_GET['action'] : "";
    $entry_no      = (isset($_GET['entry_no'])) ? $_GET['entry_no'] : 0;
    $entry_date    = (isset($_GET['entry_date'])) ? $_GET['entry_date'] : '';
    $cust_id       = (isset($_GET['cust_id'])) ? $_GET['cust_id'] : 0;
    $search_value_broker_id     = (isset($_GET['search_value_broker_id'])) ? $_GET['search_value_broker_id'] : 0;

  ?>
    <script>
        var link = "sales_section";
        var sub_link = "sales";
    </script>
    
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1> SALES</h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li><a href="#">Sales</a></li>
        <li class="active">View</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-md-12 col-sm-12">
                <div class="box box-primary">
                    <!-- <div class="box-body"> -->
                        <form class="form-horizontal" id="search_form" action="<?php echo base_url('sales/salesctrl/sales_master/')?>" method="get">
                            <div class="panel panel-body remove-bottom">
                                <div class="form-group floating-form">
                                    
                                    <div class="col-md-1 col-sm-1 floating-label">
                                        <?php echo form_dropdown('', $data['entry_no_drop_down'], $entry_no,'id="entry_no" name="entry_no" class="form-control floating-select flexselect" ');?>
                                        <label>ENTRY NO</label>
                                    </div>

                                    <div class="col-md-1 col-sm-1 floating-label">
                                        <input type="date" class="form-control floating-input" name="entry_date" id="entry_date" value="<?php echo $entry_date ?>" placeholder=" " autocomplete="off"/>
                                        <label>ENTRY DATE</label>
                                    </div>

                                    <div class="col-md-2 col-sm-1 floating-label">
                                        <?php echo form_dropdown('', $data['account_data'], $cust_id,'id="cust_id" name="cust_id" class="form-control floating-select flexselect" ');?>
                                        <label>CUSTOMER</label>
                                    </div>
                                    
                                    <div class="col-md-1 col-sm-1 ">
                                        <button type="submit" class="btn btn-primary"><i class="fa fa-search" aria-hidden="true"></i>Search</button>                                   
                                        <input type="hidden" class="form-control" id="action" name="action" value="<?php echo $action ?>" />     
                                    </div>

                                    <div class="col-md-1 col-sm-1">
                                        <a href="<?php echo base_url('sales/salesctrl/sales_master?action=view'); ?>" class="btn btn-primary"><i class="fa fa-undo" aria-hidden="true"></i>
                                            Reset
                                        </a>
                                    </div>
                                    <div class="col-md-1 col-sm-1">
                                        <a href="<?php echo base_url('sales/salesctrl/sales_master?action=add'); ?>" class="btn btn-primary">Add New</a>
                                    </div>
                                    <div class="col-md-2 col-sm-2 ">
                                        <label for="inputEmail3" class="col-sm-1 control-label control-label_custom" style="width:90%;">
                                            Cnt :
                                            <span id="count_reload">                                                 
                                                <i id="total_rows"><?php echo $total_rows;?></i>
                                            </span>
                                        </label>                                        
                                    </div>                                    
                                </div>
                            </div>
                        </form>
                    <!-- </div> -->
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">     
                <div class="box">
                    <div class="box-body" id="table_reload">
                        <table class="table table-bordered table-striped hoverTable">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>ENTRY NO(REG)</th>
                                    <th>ENTRY NO(UNREG)</th>
                                    <th>ENTRY DATE</th>    
                                    <th>CUSTOMER</th>
                                    <th>PRICE/CT</th>
                                    <th>PC AMT</th>
                                    <th>DOLLAR/CT</th>
                                    <th>DOLLAR AMT</th>
                                    <th>TOTAL QTY</th>
                                    <th>TOTAL AMT</th>
                                    <th>PRINT</th> 
                                    <th>EXCEL</th>       
                                    <th>ACTION</th>  
                                </tr>
                            </thead>
                            <tbody>
                        <?php 
                                if(!empty($data['sales_data'])):
			                        // echo "<pre>"; print_r($data); exit;

                                    // Initialize grand totals
                                    $grand_price_ct     = 0;
                                    $grand_pc_amt       = 0;
                                    $grand_qty          = 0;
                                    $grand_amt          = 0;
                                    $grand_dollar_ct    = 0;
                                    $grand_dollar_amt   = 0;
                                foreach ($data['sales_data'] as $key => $value): 
                                    $sales_id      = encrypt_decrypt("encrypt", $value['sale_id'], SECRET_KEY);
                                    $sale_type   = $value['sale_type'];
                                    $entry_no   = $value['sale_entry_no'];
                                    $entry_no_unregi   = $value['sale_unregi_entry_no'];
                                    $entry_date = date('d-m-Y', strtotime($value['sale_entry_date']));
                                    $customer           = strtoupper($value['acc_name']);
                                    $qty                = (float) $value['total_qty'];
                                    $amt                = (float) $value['total_amt'];
                                    $total_pc_amt       = (float) $value['total_pc_amt'];
                                    $total_price_ct     = (float) $value['total_price_ct'];

                                    $total_dollar       = (float) $value['total_dollar'];
                                    $total_dollar_amt   = (float) $value['total_dollar_amt'];

                                    // Add to grand totals
                                    $grand_pc_amt       += $total_pc_amt;
                                    $grand_qty          += $qty;
                                    $grand_amt          += $amt;
                                    $grand_dollar_amt   += $total_dollar_amt;
                                    
                                    // ✅ SAFE division
                                    if ($grand_qty > 0) {
                                        $grand_price_ct  = $grand_pc_amt / $grand_qty;
                                        $grand_dollar_ct = $grand_dollar_amt / $grand_qty;
                                    } else {
                                        $grand_price_ct  = 0;
                                        $grand_dollar_ct = 0;
                                    }
                        ?>
                            <tr>
                                <td><?php echo $key+1?></td>                                
                                <td><?php echo $entry_no; ?></td>
                                <td><?php echo $entry_no_unregi; ?></td>
                                <td><?php echo $entry_date; ?></td>
                                <td><?php echo $customer; ?></td>
                                <td><?php echo number_format($total_price_ct, 2); ?></td>
                                <td><?php echo number_format($total_pc_amt, 2); ?></td>
                                <td><?php echo number_format($total_dollar, 2); ?></td>
                                <td><?php echo number_format($total_dollar_amt, 2); ?></td>
                                <td><?php echo number_format($qty, 2); ?></td>
                                <td><?php echo number_format($amt, 2); ?></td>
                                <td>
                                    <?php 
                                        if($sale_type == 1){
                                    ?>
                                        <a href="<?php echo base_url('sales/salesctrl/sales_master?action=print&type=export&id='.$sales_id); ?>" class="glyphicon glyphicon-print" title="Print" target="_blank" width="1024" height="768"></a>

                                    <?php
                                        } else { 
                                    ?>
                                        <a href="<?php echo base_url('sales/salesctrl/sales_master?action=print&id='.$sales_id); ?>" class="glyphicon glyphicon-print" title="Print" target="_blank" width="1024" height="768"></a>
                                    <?php
                                        }
                                    ?>
                                </td>
                                <?php 
                                    if($sale_type == 1){
                                ?>
                                    <td>
                                        <a href="<?= base_url('sales/salesctrl/export_excel?sales_id='.$value['sale_id']); ?>" title="Export to Excel">
                                            <i class="fa fa-file-excel-o" style="font-size: medium;"></i> 
                                        </a>
                                    </td>
                                <?php
                                    } else { 
                                ?>
                                    <td></td>
                                <?php      
                                    }
                                ?>
                                <td>
                                    <a href="<?php echo base_url('sales/salesctrl/sales_master?action=edit&id='.$sales_id); ?>" class="glyphicon glyphicon-pencil"></a>
                                    <span class="glyphicon glyphicon-trash" style="cursor: pointer;" onClick="delete_sales(<?php echo $value['sale_id']?>);"></span></td>
                            </tr> 
                        <?php
                            endforeach;
                            ?>

                            <!-- ✅ Grand Total Row -->
                            <tr style="background:#f2f2f2; font-weight:bold;">
                                <td colspan="5" align="right">GRAND TOTAL:</td>
                                <td><?php echo number_format($grand_price_ct, 2); ?></td>
                                <td><?php echo number_format($grand_pc_amt, 2); ?></td>
                                <td><?php echo number_format($grand_dollar_ct, 2); ?></td>
                                <td><?php echo number_format($grand_dollar_amt, 2); ?></td>
                                <td><?php echo number_format($grand_qty, 2); ?></td>
                                <td><?php echo number_format($grand_amt, 2); ?></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                        <?php
                            else:
                        ?>
                            <tr>
                                <td colspan="12" style="color:red;" align="center">No Record Found!</td>
                            </tr>
                        <?php
                            endif;
                        ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="text-right">
                <?= $this->pagination->create_links(); ?>
            </div>
        </div>
    </section>
    <?php 
    $this->load->view('templates/footer');
   ?>
    <script src="<?php echo assets('js/sales.js')?>"></script> 
  