<div class="popup_inner_element">
<?php
$form = $this->beginWidget('CActiveForm', array(
    'id' => 'inventoryprice-form',
    'enableAjaxValidation' => false,
    'enableClientValidation' => false,
        ));
?>

    <div class="tab-content">
        <div class="tab-pane active" id="inventoryprice_tab1">
            <ul class="clearfix form-ul material">

                <?php echo $form->hiddenField($model, 'id'); ?>
            
                <li class="col-sm-3">
            
                    <?php echo $form->textField($model, 'from_date', array('class','datetimepicker','data-label' => 'Effective From <span style="color:red">*</span>', 'data-validation' => 'required')); ?>
            
                </li>
                <li  class="col-sm-3">
                    <?php  echo $form->textField($model, 'lic_percent', array('class' => 'tooltipster','data-label' => 'L.I.C Percentage <span style="color:red">*</span>')); ?>
                </li>
                <li  class="col-sm-3">
                    <?php  echo $form->dropDownList($model, 'currency_code', ['USD'=>"USD","EURO"=>"EURO"], array('data-label' => 'Currency <span style="color:red"></span>', 'empty'=>'-Select-','data-search'=>'true')); ?>
                </li>
            </ul>
            <div class="table-responsive">
                <?php
                // SQL to fetch all categories and inventory
            $sql_category = "SELECT C.id AS category_id, C.product_category, I.model_no, I.id, I.gmc_code,I.base_code,I.gmc_colour,I.base_category_name,
                            I.description,I.length,I.width,I.height,I.weight,I.barcode,I.origin,I.category_type_id,I.order_lot_to_ycj
                             FROM {{temp_inventory_price}} T
                             LEFT JOIN {{inventory}} I ON I.id = T.product_id
                             LEFT JOIN {{mas_product_category}} C ON C.id = I.product_category_id
                             WHERE T.upload_id=".$model->id."
                             GROUP BY I.id
                             ORDER BY C.sort_order ASC";
            $model_inventory = Yii::app()->db->createCommand($sql_category)->queryAll();
             // Fetch all categry in a single query
            $modMasCategoryType = MasCategoryType::model()->findAll();
            $arr_categry = [];
            foreach ($modMasCategoryType as $d) {
                $arr_categry[$d->id] = $d->category_type_name;
            }
            // Fetch all regions
            $modMasRegion = MasRegion::model()->findAll(array('order' => 'id ASC'));
            $sql_latest_price = "SELECT product_id, region_id, price,cost_price,lic_percentage,price_percentage
                             FROM {{temp_inventory_price}} P
                             LEFT JOIN {{inventory}} I ON I.id = P.product_id
                             WHERE P.upload_id=".$model->id."
                             GROUP BY product_id,region_id
                             ORDER BY P.id DESC";// Ensures the latest price
            
            $prices = Yii::app()->db->createCommand($sql_latest_price)->queryAll();

            // Index prices for quick lookup
            $priceLookup = [];
            foreach ($prices as $p) {
                $priceLookup[$p['product_id']][$p['region_id']]['price'] = $p['price'];
                $priceLookup[$p['product_id']][$p['region_id']]['costprice'] = $p['cost_price'];
                $priceLookup[$p['product_id']][$p['region_id']]['lic_percentage'] = $p['lic_percentage'];
                $priceLookup[$p['product_id']][$p['region_id']]['price_percentage'] = $p['price_percentage']*100;
            }
            $modelMasSettings = new MasSettings();
                $currencyRates = [
                    'AED' => (float)$modelMasSettings->GetCurrenctAmt('AED'),
                    'EUR' => (float)$modelMasSettings->GetCurrenctAmt('EUR'),
                ];
            ?>
            <table class="table table-hover table-bordered personal-task" id="price_details">        
                <tr>            
                    <th width="4%">No</th>
                    <th width="10%">Category</th>        
                    <th width="10%">GMC</th>        
                    <th >Basecode</th>        
                    <th >Model No</th>        
                    <!--<th >Colour</th>    -->    
                    <th >Cost Price</th>     
                    <th >L.I.C Price</th>
                    <?php 
                    foreach ($modMasRegion as $i=>$region) {?>
                    <th><?php echo $region->region_name;?></th>
                    <th><?php echo $region->region_name.' %';?></th>
                    <?php }?>
                    <th >Description</th>
                    <th >Origin</th>  
                    <th >Category</th>  
                </tr>
                <?php 
                $no=1;
                foreach ($model_inventory as $row){ ?>
                <tr>
                    <td><?php echo $no;?></td>
                    <td><?php echo $row['product_category'];?></td>
                    <td><?php echo $row['gmc_code'];?></td>
                    <td><?php echo $row['base_code'];?></td>
                    <td><?php echo $row['model_no'];?></td>
                    <!--<td><?php //echo $row['gmc_colour'];?></td>-->
                    <?php
                    $i=0;
                        foreach ($modMasRegion as $i=>$region) { 
                            $exchange=0;
                            if($region->parent_id==''||(int)$region->parent_id==0){
                                $price = isset($priceLookup[$row['id']][$region->id]['price']) ? $priceLookup[$row['id']][$region->id]['price'] : '0';
                                $cprice= isset($priceLookup[$row['id']][$region->id]['costprice']) ? $priceLookup[$row['id']][$region->id]['costprice'] : '0';
                                $lprice= isset($priceLookup[$row['id']][$region->id]['lic_percentage']) ? $priceLookup[$row['id']][$region->id]['lic_percentage'] : '0';
                                $price_percentage=isset($priceLookup[$row['id']][$region->id]['price_percentage']) ? $priceLookup[$row['id']][$region->id]['price_percentage'] : '0';
                            }else{
                                $exchange = $currencyRates[$region->currency_type];
                                $price = isset($priceLookup[$row['id']][$region->parent_id]['price']) ? ($priceLookup[$row['id']][$region->parent_id]['price']*$exchange) : '0';
                                $price_percentage=isset($priceLookup[$row['id']][$region->parent_id]['price_percentage']) ? $priceLookup[$row['id']][$region->parent_id]['price_percentage'] : '0';
                            }
                            if($i==0){?>
                                <td class="text-right"><?php echo number_format($cprice, '2', '.', '');;?></td>
                                <td class="text-right"><?php echo number_format($cprice * $lprice, '2', '.', '');;?></td>
                            <?php } ?>
                            <td class="text-right"><?php echo number_format($price, '2', '.', '');?></td>
                            <td class="text-right"><?php echo number_format($price_percentage, '2', '.', '').'%';?></td>
                    <?php  } ?>
                <td><?php echo $row['description'];?></td>
                <td><?php echo $row['origin'];?></td>
                <td><?php echo $arr_categry[$row['category_type_id']];?></td>
                </tr>
            <?php 
            $no++;
            } ?>
            </table>
            </div> 
        </div> 
    
    </div>

 <div class="modal-footer">
    <button type="button" class="btn btn-default" id="inventoryprice_close" data-dismiss="modal">Close</button>
    
    <button type="button" class="btn btn-default" id="inventoryprice_close" onclick="">Update</button>
</div>
<?php $this->endWidget(); ?>