<?php 
ob_start();
session_start();
include "../config/config.php";

$condition='';
$month = $_POST['month'];
$scheme_id = $_POST['scheme_id'];


$statement = $pdo->prepare("SELECT * FROM tbl_customer_scheme  WHERE tbl_customer_scheme.id=?");
$statement->execute(array($scheme_id));
$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();
if($totalData>0){
        
        $ids = $datas[0]['id'];
        $customer_id = $datas[0]['customer_id'];
        $scheme_id = $datas[0]['scheme_id'];
        $scheme_amount = $datas[0]['scheme_amount'];
        $scheme_type = $datas[0]['type'];
        
        if($scheme_type==1){
            $colspan="6";
        }else{
            $colspan="8";
        }

}
        
    
        

$today_price = $pdo->prepare("SELECT * FROM tbl_prices  WHERE product_id=? and purity_id=?");
$today_price->execute(array(1,2));
$price_datas = $today_price->fetchAll(PDO::FETCH_ASSOC);
$price_totalData = $today_price->rowCount();

$per_gm_amount = $price_datas[0]['amount'];
$scheme_weight = ($scheme_amount/$price_datas[0]['amount']);


if($_POST['type']=="table_full_structure"){

?>

<table style="width: 100%;"> 
                            <thead>
                                <tr style="background-color: #df4a11f7;color: white;">
                                    <th>Month</th>
                                    <th>Amount</th>
                                <?php if($scheme_type==2){ ?>
                                    <th>Weight (gm)</th>
                                    <th>Gram Weight</th>
                                <?php } ?>
                                    
                                    <th>Payment Type</th>
                                    <th>Reference</th>
                                    <th>Notes</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody id="payment_tbody">
                                <tr class="porow">
                                    
                                    
                                    
                                    <td>
                                        <select name="payment_month[]" class="form-control select2 payment_month" style="width:100%" id="payment_month_1" required onchange="PaymentScheme(1)">
                                        <?php 
                                                for($i=1;$i<=12;$i++){
                                                    if($i==$month){
                                                        echo "<option value='".$i."' selected>".str_pad($i, 2, "0", STR_PAD_LEFT)."-".date("F", mktime(0, 0, 0, $i, 10))."</option>";
                                                    }
                                                }
                                            ?>
                                        </select>
                                    </td>
                                    
                                    
                                    
                                    <td>
                                        <input name="payment_amount[]" class="form-control payment_amount" style="width:100%" id="payment_amount" value="<?=$scheme_amount?>" oninput="PaymentMonth()" required>
                                    </td>
                                    
                                    
                                <?php if($scheme_type==2){ ?>
                                    <td>
                                        <input name="product_weight[]" class="form-control" style="width:100%" id="product_weight" value="<?=number_format($scheme_weight,2)?>" required>
                                    </td>
                                    
                                    <td>
                                        <input type="text" name="per_gm_amount[]" class="form-control" style="width:100%;" id="per_gm_amount" value="<?=$per_gm_amount?>" required>
                                    </td>
                                <?php }else { ?>
                                        <input type="hidden" name="product_weight[]" class="form-control" style="width:100%" id="product_weight" value="0" required>
                                <?php } ?>
                                
                                    
                                    
                                
                                
                                
                                
                                    <td>
                                        <select name="payment_type[]" class="form-control select2" id="payment_type" style="width: 100%;">
                                            <?php 
                                            $statement = $pdo->prepare("SELECT * FROM `tbl_payment_type` WHERE status=1");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                foreach ($location_result as $result) { ?>
                                                    <option value="<?=$result['id']?>"><?=$result['name']?></option>
                                            <?php } ?>
                                        </select>
                                    </td>
                                    <td>
                                        <input type="text" name="reference[]"  placeholder="Reference No" class="form-control"  id="reference" />
                                    </td>
                                    <td>
                                        <input name="notes[]" id="notes" placeholder="Notes" class="form-control">
                                    </td>
                                    <td>
                                        
                                    </td>
                                </tr>
                                <tr style="background: aqua;">
                                    <td colspan="<?=$colspan?>" style="text-align: center;"><b id="payment_span_1"></b></td>
                                </tr>
                            </tbody>
                        </table>
                        
                        
<?php } ?>


<?php if($_POST['type']=="table_row"){ 

$count = $_POST['count'];


?>
        
        <tr class="porow_<?=$count?>">
                
                <td>
                    <select name="payment_month[]" class="form-control select2 payment_month" style="width:100%" id="payment_month_<?=$count?>" required onchange="PaymentScheme(<?=$count?>)">
                    <?php 
                            for($i=1;$i<=12;$i++){
                                if($i==$month){
                                    echo "<option value='".$i."' selected>".str_pad($i, 2, "0", STR_PAD_LEFT)."-".date("F", mktime(0, 0, 0, $i, 10))."</option>";
                                }
                            }
                        ?>
                    </select>
                </td>
                <td>
                    <input name="payment_amount[]" class="form-control payment_amount" value="<?=$scheme_amount?>" style="width:100%" oninput="PaymentMonth()" id="payment_amount" required>
                </td>
                <?php if($scheme_type==2){ ?>
                    <td>
                        <input name="product_weight[]" class="form-control" style="width:100%" id="product_weight" value="<?=number_format($scheme_weight,2)?>" required>
                    </td>
                    <td>
                        <input type="text" name="per_gm_amount[]" class="form-control" style="width:100%;" id="per_gm_amount" value="<?=$per_gm_amount?>" required>
                    </td>
                <?php }else { ?>
                        <input type="hidden" name="product_weight[]" class="form-control" style="width:100%" id="product_weight" value="0" required>
                <?php } ?>
                <td>
                    <select name="payment_type[]" class="form-control select2" id="payment_type" style="width: 100%;">
                        <?php 
                        $statement = $pdo->prepare("SELECT * FROM `tbl_payment_type` WHERE status=1");
                        $statement->execute();
                        $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                            foreach ($location_result as $result) { ?>
                                <option value="<?=$result['id']?>"><?=$result['name']?></option>
                        <?php } ?>
                    </select>
                </td>
                <td>
                    <input type="text" name="reference[]"  placeholder="Reference No" class="form-control"  id="reference" />
                </td>
                <td>
                    <input type="text" name="notes[]" id="notes" placeholder="Notes" class="form-control">
                </td>
                <td style="text-align: center;">
                    <span class="btn btn-danger btn-xs remove_row" data-id="<?=$count?>"><i class="fa fa-times"></i></span>
                </td>
            </tr>
            <tr style="background: aqua;" class="porow_<?=$count?>">
                <td colspan="<?=$colspan?>" style="text-align: center;"><b id="payment_span_<?=$count?>"></b></td>
            </tr>
            
            
<?php } ?>



