<?php  include "header.php"; ?>
<?php
if(isset($_GET['id'])){
    $ids = $_GET['id'];
    
$statement = $pdo->prepare("SELECT * FROM tbl_repair_voucher e where e.id=?");
$statement->execute(array($ids));
$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();

$datas_counts="FALSE";
if($totalData>0){
    $datas_counts="TRUE";
}

    $title = "Update";
    $ajax = "update_repair_voucher";
    $success = "Updated";
}else{
    $datas_counts="TRUE";
    $ids = '';
    $title = "Add";
    $ajax = "add_repair_voucher";
    $success = "Added";
}
?>




<div class="content-wrapper">
        <section class="content-header">
            <h1><?=$title?> Repair Voucher</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active"><?=$title?> Repair Voucher</li>            
            </ol>
        </section>


        <div class="col-lg-12 alerts">
            <div id="custom-alerts" style="display:none;">
                <div class="alert alert-dismissable">
                    <div class="custom-msg"></div>
                </div>
            </div>
            
            <?php if ($error)  { ?>
            <div class="alert alert-danger alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-ban"></i> Error</h4>
                <?= $error; ?>
            </div>
            <?php $error=0;} if ($warning) { ?>
            <div class="alert alert-warning alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-warning"></i> Warning</h4>
                <?= $warning; ?>
            </div>
            <?php  $warning=0;} if ($message) { ?>
            <div class="alert alert-success alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4>    <i class="icon fa fa-check"></i> Success</h4>
                <?= $message; ?>
            </div>
            <?php $message=0; } ?>
        </div>
        <div class="clearfix"></div>







<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary" style="box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 6px, rgba(0, 0, 0, 0.23) 0px 3px 6px;">
                
                <div class="box-body">
                    
            <?php if($datas_counts=="TRUE"){ ?> 
                <div id="content-div">
                    
                    <div class="col-lg-12">

                        <form id="form_details" autocomplete="off">
                            
                            <input type="hidden" name="ids" id="ids" value="<?=$ids?>">
                                                                 
                            <div class="row">
                                    
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label class="required" for="customer_id">Customer Details</label>                                   
                                            <select name="customer_id" class="form-control select2" style="width:100%" id="customer_id" required="required">
                                                <option value="">Select Customer</option>
                                                <?php $statement = $pdo->prepare("SELECT * FROM `tbl_customers` where status=1");
                                                    $statement->execute();
                                                    $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                    foreach ($resultsss as $ress) { ?>
                                                        <option value="<?=$ress['id']?>" <?php if(isset($datas) && $datas[0]['customer_id']==$ress['id']){ echo "selected";}?>><?=$ress['customer_name']?> - <?=$ress['mobile_no']?></option>
                                                <?php  }  ?>
                                            </select>
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-1" style="text-align: center;">
                                            <a href="modal/customers-create.php" data-toggle="ajax-modal" title="Add Customers" class="tip btn btn-success btn-xs" id="" style="margin-top: 23px; padding: 5px;"><i class="fa fa-plus-circle" aria-hidden="true" style="font-size:22px;"></i></a>
                                    </div>
                            
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label class="required" for="date">Repair Date</label>                                   
                                            <input type="date" name="date" class="form-control" style="width:100%" id="date" value="<?php if(isset($datas)){ echo $datas[0]['date'];}else { echo date('Y-m-d');}?>" required="required">
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label class="required" for="repair_type">Type</label>                                   
                                            <select name="repair_type" class="form-control select2" style="width:100%" id="repair_type" required="required">
                                                <option value="1" <?php if(isset($datas) && $datas[0]['type']==1){ echo "selected";}?>>Repair</option>
                                                <option value="2" <?php if(isset($datas) && $datas[0]['type']==2){ echo "selected";}?>>Soliding</option>
                                                <option value="3" <?php if(isset($datas) && $datas[0]['type']==3){ echo "selected";}?>>Others</option>
                                            </select>
                                        </div>
                                    </div>
                                    
                                    
                                    
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label class="required" for="sales_man_id">Sales Man</label>                                   
                                            <select name="sales_man_id" class="form-control select2" style="width:100%" id="sales_man_id" required="required">
                                                <option value="">Select Employees</option>
                                                <?php $statement = $pdo->prepare("SELECT * FROM `tbl_users` where status=1 and id!=0");
                                                    $statement->execute();
                                                    $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                    foreach ($location_result as $result) { ?>
                                                        <option value="<?=$result['id']?>" <?php if(isset($datas) && $datas[0]['sales_man']==$result['id']){ echo "selected";}?>><?=$result['name']?> - <?=$result['emp_code']?></option>
                                                <?php  }  ?>
                                            </select>
                                        </div>
                                    </div>
                                    
                                    
                            </div>
                            
                            <div class="row">
                                    
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label class="required" for="product_ids">Products</label>                                   
                                            <select name="product_ids" class="form-control select2" style="width:100%" onchange="productKarat()" id="product_ids" required="required">
                                                <option value="">Select Product</option>
                                                <?php $statement = $pdo->prepare("SELECT * FROM `tbl_ids_products` WHERE status=1");
                                                    $statement->execute();
                                                    $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                    foreach ($resultsss as $ress) { ?>
                                                        <option value="<?=$ress['id']?>" <?php if(isset($datas) && $datas[0]['product_ids']==$ress['id']){ echo "selected";}?>><?=$ress['name']?></option>
                                                <?php  }  ?>
                                            </select>
                                        </div>
                                    </div>
                                    
                                    
                                    
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label class="required" for="weight_gram">Weight</label>                                   
                                            <input type="text" name="weight_gram" class="form-control weight" oninput="AmountCalc()" style="width:100%" id="weight_gram" value="<?php if(isset($datas)){ echo $datas[0]['weight'];}?>" required="required"  onkeypress="return isNumberKey(this, event);">
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label class="required" for="due_date">Due Date</label>                                   
                                            <input type="date" name="due_date" class="form-control" style="width:100%" id="due_date" value="<?php if(isset($datas)){ echo $datas[0]['due_date'];}?>" required="required"  onkeypress="return isNumberKey(this, event);">
                                        </div>
                                    </div>
                                
                                    
                                    <div class="col-md-3" hidden>
                                        <div class="form-group">
                                            <label class="required" for="product_variant_id">Product Details</label>                                   
                                            <select name="product_variant_id" class="form-control select2" style="width:100%" id="product_variant_id">
                                                <option value="">Select Product</option>
                                                <?php 
                                                    $statement = $pdo->prepare("SELECT tbl_product_variants.*, tbl_products_name.name as products_name 
                                                                                FROM `tbl_product_variants` 
                                                                                LEFT JOIN tbl_products_name ON tbl_products_name.id=tbl_product_variants.product_name
                                                                                WHERE tbl_product_variants.sales_status=1");
                                                    $statement->execute();
                                                    $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                    foreach ($resultsss as $ress) { ?>
                                                        <option value="<?=$ress['id']?>"><?=$ress['products_name']?> - <?=$ress['hsn_code']?></option>
                                                <?php  }  ?>
                                            </select>
                                        </div>
                                    </div>
                                    
                                    
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label class="required" for="product_name">Product Details</label>                                   
                                            <select name="product_name" class="form-control select2" style="width:100%" id="product_name">
                                                <option value="">Select Product</option>
                                                <?php $statement = $pdo->prepare("SELECT tbl_products_name.*, tbl_ids_products.name as products_name 
                                                                                FROM `tbl_products_name` 
                                                                                LEFT JOIN tbl_ids_products ON tbl_ids_products.id=tbl_products_name.product_ids
                                                                                WHERE tbl_products_name.status=1 ORDER BY tbl_ids_products.id ASC");
                                                    $statement->execute();
                                                    $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                    foreach ($resultsss as $ress) { ?>
                                                        <option value="<?=$ress['id']?>" <?php if(isset($datas) && $datas[0]['product_name']==$ress['id']){ echo "selected";}?> ><?=$ress['name']?> (<?=$ress['products_name']?>)</option>
                                                <?php  }  ?>
                                            </select>
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label class="required" for="location_id">Locations</label>                                   
                                            <select name="location_id" class="form-control select2" style="width:100%" id="location_id" required="required">
                                                <?php 
                                                    $statement = $pdo->prepare("SELECT bb.* FROM `tbl_business_branch` bb WHERE bb.status=1 $branch_conditions");
                                                    $statement->execute();
                                                    $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                    foreach ($location_result as $result) { ?>
                                                        <option value="<?=$result['id']?>" <?php if(isset($datas) && $datas[0]['location_id']==$result['id']){ echo "selected";}?> ><?=$result['location_name']?></option>
                                                <?php  }  ?>
                                            </select>
                                        </div>
                                    </div>
                                            
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label class="optional" for="expected_purchase_date">Remarks</label>     
                                            <textarea name="remarks" id="remarks" placeholder="Remarks" rows="3" class="form-control"><?php if(isset($datas)){ echo $datas[0]['remarks'];}?></textarea>
                                        </div>
                                    </div>
                                    <?php if(isset($_GET['id'])){ ?>
                                        <input type="hidden" name="is_verified" id="is_verified" value="0">
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <label class="optional" for="repair_completed_date">Completed Date</label>                                   
                                                <input type="date" name="repair_completed_date" class="form-control" style="width:100%" id="repair_completed_date" value="<?php if(isset($datas)){ echo $datas[0]['repair_completed_date'];}?>" >
                                            </div>
                                        </div>
                                        
                                    <?php } ?>
                                    
                                    <!--</?php if($ids==0){ ?>  -->
                                    <!--    <div class="col-md-2">-->
                                    <!--         <span class="btn btn-primary btn-xs" id="add_new_row" style="margin-top: 20px;"><i class="fa fa-plus"></i> Add Payment</span>-->
                                    <!--    </div>-->
                                    <!--</?php } ?>-->
                                    
                                   
                                </div>
                                
                                
                            <?php if((empty($datas[0]['repair_completed_date']) || $datas[0]['repair_completed_date'] == '0000-00-00') && isset($_GET['id'])){ ?>
                            <div class="row" id="otp_sender_div">
                                
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="optional" for="otp_send_type">Type</label>                                   
                                        <select name="otp_send_type" class="form-control select2" style="width:100%" id="otp_send_type">
                                            <option value="">Select Otp Sender</option>
                                            <option value="1">Customer</option>
                                            <option value="2">User</option>
                                        </select>
                                    </div>
                                </div>
                                
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="optional" for="sender_detail">Sender Details</label>                                   
                                        <select name="sender_detail" class="form-control select2" style="width:100%" id="sender_detail">
                                           
                                            
                                        </select>
                                    </div>
                                </div>
                                
                                <div class="col-md-4">
                                    <div class="form-group" style="margin-top:20px;">
                                        <button type="button" name="send_otp" id="send_otp" class="btn btn-primary"> Send OTP</button>
                                    </div>
                                </div>
                            </div>
                            <?php } ?> 
                            <?php if(isset($_GET['id'])){ ?>
                            <div class="row" id="otp_div" style="display:none">        
                             
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="required" for="otp">Enter OTP</label>                                   
                                        <input type="text" name="otp" class="form-control" style="width:100%" id="otp" value="" maxlength="6" >
                                    </div>
                                </div>
                                
                                <div class="col-md-4">
                                    <div class="form-group" style="margin-top: 20px;">
                                        <button type="button" name="submit_otp" id="submit_otp" class="btn btn-success"> Submit OTP</button>
                                    </div>
                                </div>
                            </div>
                            <?php } ?> 
                        <!--</?php if($ids==0){ ?>    -->
                            <!--<div class="row porow" style="margin-top: 20px;">
                                    
                                    
                                    
                                    
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label class="required" for="paid_amount">Amount</label>        
                                            <input type="hidden" name="payment_id[]" value="0">
                                            <input type="text" name="paid_amount[]"  placeholder="Enter the Amount" class="form-control paid_amount" required="required" id="paid_amount" />
                                        </div>
                                    </div>
                                    
                                    
                                    
                                    <div class="col-md-2">
                                        <div class="form-group">
                                             <label class="required" for="payment_type">Payment Type</label>
                                            <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>
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label class="optional" for="reference">Reference</label>                                   
                                            <input type="text" name="reference[]"  placeholder="Reference No" class="form-control"  id="reference" />
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label class="optional" for="notes">Notes</label>                                   
                                            <textarea name="notes[]" id="notes" placeholder="Notes" rows="3" class="form-control"></textarea>
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-1">
                                    </div>
                                    
                                    
                                   
                                </div>-->
                        <!--</?php } ?>-->
                        
                            <div id="append_sections"></div>
                        

                        <div class="form-group" style="text-align:center; margin-top: 20px;">
                            <input type="submit"  id="submit" value="Submit" class="btn btn-success" />
                        </div>
                        
                        </form>

                    </div>
                    
                    
                        <div class="form-group" style="text-align:center; margin-top: 20px;">
                            <span onclick="location.reload();" class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</span>
                            <a href="repair-voucher-list.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </div>
                        
                </div>
            <?php } else { ?>
                <center style="margin: 50px 0px;"><b style="color: red;">No Records Found</b></center>
            <?php } ?>
            
                
                
<div class="container" id="loading-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
        <center> <img src="img/loading.gif" style="width: 250px;"><br><b class="blinking">Processing Please Wait...</b></center>
</div>
                    
                    
                    
                    
                    <div class="container" id="success-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
                        <center><b class="blinking" style="font-size:22px;">Repair Voucher <?=$success?> Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <span onclick="location.reload();" class="btn btn-info"><i class="fa fa-refresh"></i> Reset Form</span>
                            &nbsp;&nbsp;
                            <a href="repair-voucher-list.php"><button class="btn btn-warning"><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </center>
                    </div>
                        
                        
                        
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</section>


<?php include "footer.php"; ?>


<script>
$(document).ready(function() {
    
    $("#otp_send_type").on("change", function(){
        
        var send_type = $(this).val();
        var row_id = $("#ids").val();
        
        if(send_type != '' && row_id != ''){
            $.ajax({
                url: 'api/get-values-api.php',
                type: 'post',
                dataType: 'json',
                data: {field_type:'get_repair_sender_details',send_type,row_id},
                success: function (response) {
                    // console.log(response);
                    if(response['error']==false){
                       
                     $('#sender_detail').html(response['data']);  
                     $('#sender_detail').trigger('change');
                     
                    }else{
                        toastr.error(response['message']);
                    }
                }
            })
            
        }else{
           $('#sender_detail').trigger('change'); 
        }
        
    });
    
    $("#send_otp").on("click",function(){
        
        var send_type = $("#otp_send_type").val();
        var sender_id = $("#sender_detail").val();
        
        if(send_type != '' && sender_id != ''){
            
            $.ajax({
                url: 'api/get-values-api.php',
                type: 'post',
                dataType: 'json',
                data: {field_type:'repair_send_otp',send_type,sender_id},
                success: function (response) {
                    // console.log(response);
                    if(response['error']==false){
                        toastr.success(response['message']);
                        $('#otp_div').css('display','block');  
                        $('#send_otp').text('Resend OTP');
                        $("#otp").attr('required',false);
                     
                    }else{
                        toastr.error(response['message']);
                    }
                }
            }) 
        }
        
    });
    
    $("#submit_otp").on('click',function(){
        
        var send_type = $("#otp_send_type").val();
        var sender_id = $("#sender_detail").val();
        var otp = $("#otp").val(); 
        
         if(send_type != '' && sender_id != '' && otp != ''){
            
            $.ajax({
                url: 'api/get-values-api.php',
                type: 'post',
                dataType: 'json',
                data: {field_type:'repair_submit_otp',send_type,sender_id,otp},
                success: function (response) {
                    // console.log(response);
                    if(response['error']==false){
                        toastr.success(response['message']);
                        $('#otp_div').css('display','none'); 
                        $('#otp_sender_div').css('display','none');
                        $("#is_verified").val(1);
                        
                    }else{
                        toastr.error(response['message']);
                    }
                }
            }) 
        }

    });
    
   $('#form_details').on('submit', function(e){
                e.preventDefault();
               
                var formData = new FormData(this);
                formData.append('type', '<?=$ajax?>');
                var is_verified = $("#is_verified").val();
                var title = '<?php echo $title ?>';
                if((is_verified == '1' && title == 'Update') || title == 'Add'){
                    
                    $('#loading-div').show();
                    $('#content-div').hide();
                    $("#submit").prop('disabled', true);
                    
                    $.ajax({
                            url: 'apis/repair_voucher.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                console.log(response);
                                if(response['status']==200){
                                     /*alert(response['message']);*/
                                     toastr.success(response['message']);
                                     $('#form_details').trigger("reset");
                                     $("#submit").prop('disabled', false);
                                    //  $('#success-div').show();
                                     $('#loading-div').hide();
                                      setTimeout(function(){ window.location.href="repair-voucher-list.php"; },1500); 
                                }else{
                                    toastr.error(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
                }else{
                   toastr.error('Please verify otp after entering the completed date sumbit...');
                }
   });
   
});
</script>


<style>
    .porow{
        margin-top: 20px;
    background: aliceblue;
    padding: 10px 0px 10px 0px;
    box-shadow: rgba(0, 0, 0, 0.02) 0px 1px 3px 0px, rgba(27, 31, 35, 0.15) 0px 0px 0px 1px
    }
</style>

<script>

$(document).on('click', '.remove_row', function() {
        $(this).closest('.porow').remove();
        grand_total();
});


function AmountCalc(){
    var weight = $("#weight_gram").val() || 0;
    var per_gm_rate = $("#per_product_gram_amount").val() || 0;
    
    Calc = parseFloat(weight)*parseFloat(per_gm_rate);
    
    $("#total_amount").val(Calc.toFixed(2));
}

function DateCalc(){
    var TotalAmount = $("#total_amount").val();
    var PerGramAmount = $("#per_product_gram_amount").val();
    var selectedOption = $("#advance_policy").find("option:selected");
    var days = selectedOption.data("days");
    var percentage = selectedOption.data("percentage");
    var todayDate = new Date($("#date").val());
    var weight = $("#weight_gram").val();
    
    AdvanceAmount = parseFloat(TotalAmount)*parseFloat(percentage)/100;
    
    $("#advance_amount").val(AdvanceAmount.toFixed(2));
    $("#advance_amount_db").val(AdvanceAmount.toFixed(2));
    
    var EnterAdvanceAmount= $("#advance_amount").val();
    Advance_Pro_Weight = parseFloat(EnterAdvanceAmount)/parseFloat(PerGramAmount);
    $("#advance_total_weight").val(weight);
    
    if (!isNaN(todayDate.getTime()) && !isNaN(days)) {
            var finalDate = new Date(todayDate.getTime() + (days * 24 * 60 * 60 * 1000));

            var finalDateString = finalDate.toISOString().split('T')[0];
            
            $("#expected_purchase_date").val(finalDateString);
            
            console.error("Date: "+finalDateString);
        } else {
            
            console.error("Invalid date or days.");
        }
        
        
}

function NewAdvCalc(){
    
    var advance_amount_db = $("#advance_amount_db").val();
    var EnterAdvanceAmount= $("#advance_amount").val();
    var PerGramAmount = $("#per_product_gram_amount").val();
    var weight = $("#weight_gram").val();
    
    BalanceAmount = parseFloat(EnterAdvanceAmount)-parseFloat(advance_amount_db);
    
    
    console.log("Advance DB Amount: "+advance_amount_db);
    console.log("Advance Enter Amount: "+EnterAdvanceAmount);
    console.log("Balance Amount: "+BalanceAmount);
    console.log("Per Gram Amount: "+PerGramAmount);
    
    BalanceGramWeight = BalanceAmount/PerGramAmount;
    console.log("Balance Gram Weight: "+BalanceGramWeight);
    
    Advance_Pro_Weight = parseFloat(EnterAdvanceAmount)/parseFloat(PerGramAmount);
    
    console.log("Total Amount: "+EnterAdvanceAmount+" Per Gram Amount: "+PerGramAmount+" Calculation: "+Advance_Pro_Weight);
    cal = parseFloat(weight) + parseFloat(BalanceGramWeight);
    $("#advance_total_weight").val(cal.toFixed(2));
}

$(document).ready(function() {
    
var count = 1;
$("#add_new_row").click(function(){
    
    var row=`<div class="row porow" style="margin-top: 20px;">
                            
                            
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="paid_amount">Amount</label>                      
                                    <input type="hidden" name="payment_id[]" value="0">
                                    <input type="text" name="paid_amount[]"  placeholder="Enter the Amount" class="form-control paid_amount" required="required" id="paid_amount" />
                                </div>
                            </div>
                            
                            
                            
                            <div class="col-md-2">
                                <div class="form-group">
                                     <label class="required" for="payment_type">Payment Type</label>
                                    <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>
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="optional" for="reference">Reference</label>                                   
                                    <input type="text" name="reference[]"  placeholder="Reference No" class="form-control"  id="reference" />
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="optional" for="notes">Notes</label>                                   
                                    <textarea name="notes[]" id="notes" placeholder="Notes" rows="3" class="form-control"></textarea>
                                </div>
                            </div>
                            
                            <div class="col-md-1">
                                <span class="btn btn-danger btn-sm remove_row" style="margin-top: 25px;"><i class="fa fa-times"></i></span>
                            </div>
                           
                        </div>`;
        
        $("#append_sections").append(row);
        $(".select2").select2();
        count++;
});
});

function productKarat(){
    
    $('#purity').empty();  
    var product_ids = $("#product_ids").val();
    $.ajax({
            url: 'api/get-values-api.php',
            type: 'post',
            dataType: 'json',
            data: {type:'get_purity',product_ids:product_ids},
            success: function (response) {
                console.log(response);
                if(response['error']==false){
                    var m='';
                    for(var n=0;n<response['data'].length;n++){
                        m+='<option value='+response['data'][n]['id']+'>'+response['data'][n]['name']+'</option>';
                    }
                 $('#purity').append(m);  
                 $('#purity').trigger('change');
                 
                 GetTodayAmount();
                }
            }
        });
    
}

function GetTodayAmount(){
    console.log("Get Today Amount Changed");
    var product_ids = $("#product_ids").val();
    var purity_ids = $('#purity').val(); 
   
    $.ajax({
            url: 'api/get-values-api.php',
            type: 'post',
            dataType: 'json',
            data: {type:'get_today_rates',product_ids:product_ids, purity_ids:purity_ids},
            success: function (response) {
                console.log(response);
                if(response['error']==false){
                    console.log(response);
                    $("#per_product_gram_amount").val(response['today_rate']);
                }else{
                    $("#per_product_gram_amount").val("0");
                }
            }
        });
}
</script>