  <?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'] : '';
    $supp_id       = (isset($_GET['supp_id'])) ? $_GET['supp_id'] : 0;
    $search_value_broker_id     = (isset($_GET['search_value_broker_id'])) ? $_GET['search_value_broker_id'] : 0;

  ?>
    <script>
        var link = "purchase_section";
        var sub_link = "purchase";
    </script>
    
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1> PURCHASE</h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li><a href="#">Purchase</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('purchase/purchasectrl/purchase_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'], $supp_id,'id="supp_id" name="supp_id" class="form-control floating-select flexselect" ');?>
                                        <label>SUPPLIER</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('purchase/purchasectrl/purchase_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('purchase/purchasectrl/purchase_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>SUPPLIER</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>ACTION</th>  
                                </tr>
                            </thead>
                            <tbody>
                        <?php 
                                if(!empty($data['purchase_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['purchase_data'] as $key => $value): 
                                    $purchase_id      = encrypt_decrypt("encrypt", $value['per_id'], SECRET_KEY);
                                    $entry_no   = $value['per_entry_no'];
                                    $entry_no_unregi   = $value['per_unregi_entry_no'];
                                    $entry_date = date('d-m-Y', strtotime($value['per_entry_date']));
                                    $customer           = strtoupper($value['acc_contact']);
                                    $qty                = $value['total_qty'];
                                    $amt                = $value['total_amt'];
                                    $total_pc_amt       = $value['total_pc_amt'];
                                    $total_price_ct     = $value['total_price_ct'];

                                    $total_dollar       = $value['total_dollar'];
                                    $total_dollar_amt   = $value['total_dollar_amt'];

                                    // Add to grand totals
                                    $grand_pc_amt       += $total_pc_amt;
                                    $grand_qty          += $qty;
                                    $grand_amt          += $amt;

                                    $grand_price_ct     = $grand_pc_amt / $grand_qty;

                                    $grand_dollar_amt   += $total_dollar_amt;

                                    $grand_dollar_ct    = $grand_dollar_amt / $grand_qty;
                        ?>
                            <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><a href="<?php echo base_url('purchase/purchasectrl/purchase_master?action=print&id='.$purchase_id); ?>" class="glyphicon glyphicon-print" target="_blank" width="1024" height="768"></a></td> -->
                                <td>
                                    <a href="<?php echo base_url('purchase/purchasectrl/purchase_master?action=edit&id='.$purchase_id); ?>" class="glyphicon glyphicon-pencil"></a>
                                    <span class="glyphicon glyphicon-trash" style="cursor: pointer;" onClick="delete_purchase(<?php echo $value['per_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>
                            </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/purchase.js')?>"></script> 
  