<?php  include "header.php"; 

error_reporting(E_ALL);

$id = $_GET['id'];
$statement = $pdo->prepare("SELECT * FROM tbl_advance_voucher  WHERE tbl_advance_voucher.id=?");
$statement->execute(array($id));
$edit_data = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();


if($totalData>0){
    foreach($edit_data as $data){
        
        
        $customer_id = $data['customer_id'];
        $advance_date = $data['date'];
        $expected_date = $data['expected_date'];
        $product_variant_id = $data['product_variant_id'];
        $per_gram_amount = $data['per_gram_amount'];



?>


<div class="content-wrapper">
        <section class="content-header">
            <h1>Update Advance Voucher</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Update Advance 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-header">
                    <!--<h3 class="box-title">Please fill in the information below</h3>-->
                </div>
                <div class="box-body">
                    
                    
                <div id="content-div">
                    
                    <div class="col-lg-12">

                        <form id="form_details" autocomplete="off">
                        <input type="hidden" name="ids" value="<?=$id?>" >                                
                        <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 id=?");
                                            $statement->execute(array($customer_id));
                                            $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($resultsss as $ress) { ?>
                                                <option value="<?=$ress['id']?>" <?php echo (isset($customer_id) && $customer_id == $ress['id'] ? 'selected' : ''); ?>><?=$ress['customer_name']?> - <?=$ress['mobile_no']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label class="required" for="date">Date</label>                                   
                                    <input type="date" name="date" class="form-control" style="width:100%" id="date" value="<?=$advance_date?>" required="required">
                                </div>
                            </div>
                            
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label class="required" for="expected_purchase_date">Expected Purchase Date</label>                                   
                                    <input type="date" name="expected_purchase_date" class="form-control" style="width:100%" id="expected_purchase_date" value="<?=$expected_date?>" required="required">
                                </div>
                            </div>
                            
                            
                    </div>
                    
                    <div class="row">
                            
                            
                            
                            
                            <div class="col-md-4">
                                <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 AND tbl_product_variants.id=? ");
                                            $statement->execute(array($product_variant_id));
                                            $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($resultsss as $ress) { ?>
                                                <option value="<?=$ress['id']?>" <?php echo (isset($product_variant_id) && $product_variant_id == $ress['id'] ? 'selected' : ''); ?>><?=$ress['products_name']?> - <?=$ress['hsn_code']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label class="required" for="per_product_gram_amount">Per Gram Amount</label>                                   
                                    <input type="text" name="per_product_gram_amount" class="form-control" style="width:100%" id="per_product_gram_amount" value="<?=$per_gram_amount?>" required="required">
                                </div>
                            </div>
                            
                            <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>
                            
                           
                        </div>
                        
                        
                        <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-3">
                                <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>
                        
                        <div id="append_sections"></div>
                        

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

                    </div>
                    
                    
                        <div class="form-group" style="text-align:center; margin-top: 20px;">
                            <a href="advance-voucher-add.php"><button class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</button></a>
                            <a href="advance-voucher-list.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </div>
                        
                </div>
                
                
<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;">Advance Voucher Payment Added Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <a href="advance-voucher-add.php"><button class="btn btn-info"><i class="fa fa-refresh"></i> Reset Form</button></a>
                            &nbsp;&nbsp;
                            <a href="advance-voucher-list.php"><button class="btn btn-warning"><i class="fa fa-chevron-left"></i> Back to List</button></a>
                            &nbsp;&nbsp;
                            <a href="advance-voucher-print.php?id='.$id.'" target="_blank" title="Print Advance Voucher"><button class="btn btn-warning"><i class="fa fa-print"></i> Print</button></a>
                        </center>
                    </div>
                        
                        
                        
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</section>


<?php include "footer.php"; ?>
<!--<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>-->
<style>
/*.select2-container--default .select2-selection--multiple .select2-selection__choice {
    background-color: rgb(228, 228, 228);
    box-sizing: border-box;
    display: inline-block;
    margin-left: 5px;
    margin-top: 5px;
    position: relative;
    max-width: 100%;
    text-overflow: ellipsis;
    vertical-align: bottom;
    white-space-collapse: collapse;
    text-wrap: nowrap;
    border-width: 1px;
    border-style: solid;
    border-color: rgb(170, 170, 170);
    border-image: initial;
    border-radius: 4px;
    padding: 0px 10px 0px 20px;
    overflow: hidden;
    color: black;
    font-weight: 800;
}
.select2-container--default .select2-selection--multiple {
    background-color: white;
    border: 1px solid #aaa;
    border-radius: 4px;
    cursor: text;
    height: 100px;
    padding-bottom: 5px;
    padding-right: 5px;
    position: relative;
}*/
</style>

<script>

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

$(document).ready(function() {
   $('#form_details').on('submit', function(e){
                e.preventDefault();
                $('#loading-div').show();
                $('#content-div').hide();
                $("#submit").prop('disabled', true);
                var formData = new FormData(this);
                formData.append('field_type', 'update_advance_voucher');
                    $.ajax({
                            url: 'apis/advance_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){
                                    toastr.success(response['message']);
                                     $('#form_details').trigger("reset");
                                     $("#submit").prop('disabled', false);
                                     $('#success-div').show();
                                     $('#loading-div').hide();
                                }else{
                                    toastr.error(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</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).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++;
});
});
</script>


<?php }} ?>
