<?php 
include "../config/config.php"; 
$ids = $_GET['id'];
    $statement = $pdo->prepare("SELECT * FROM tbl_products_name e where e.id=".$ids);
    $statement->execute();
    $datas = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    
    
    $statement_size = $pdo->prepare("SELECT * FROM tbl_size e where e.product_name_id=".$ids);
    $statement_size->execute();
    $size_datas = $statement_size->fetchAll(PDO::FETCH_ASSOC);
    $size_totalData = $statement_size->rowCount();

if($totalData==1){
?>   
<style>
.modal-open .modal {
    overflow-x: hidden;
    overflow-y: hidden;
}
</style>
<div class="modal-dialog modal-lg" role="document" id="add_modal" style="overflow-x: hidden; overflow-y: hidden;">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title mt-0" id="myLargeModalLabel"><b>Products Details</b></h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="margin-top: -25px;"><i class="fa fa-times"></i></button>
            </div>
            
            <form id="form_details" method="post">
                
                <input type="hidden" name="ids"  value="<?=$datas[0]['id']?>" required>
            <div id="content-div">
            
            
            <div class="modal-body" style="padding: 20px 40px 20px 40px;">
                
                
                <div class="row">
                    
                    
                    <div class="col-md-8">
                        <div class="form-group">
                            <label>Name</label>
                            <input type="text" name="name" id="name" class="form-control" value="<?=$datas[0]['name']?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Product HSN Code</label>
                            <input type="text" name="product_hsn_code" id="product_hsn_code" class="form-control" value="<?=$datas[0]['product_hsn_code']?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Quantity</label>
                            <input type="text" name="quantity" id="quantity" class="form-control" value="<?=$datas[0]['quantity']?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Product Type</label>
                            <select class="form-control" name="product_type" id="product_type" required>
                                <option value="1" <?= ($datas[0]['type']==1) ? "selected":"" ?>>Product Type</option>
                                <option value="2" <?= ($datas[0]['type']==2) ? "selected":"" ?>>Ornament Type</option>
                            </select>
                        </div>
                    </div>
                    
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Product Details</label>
                            <select class="form-control" name="product_details" id="product_details" required>
                                <option value="">Select Product</option>
                                <?php $statement = $pdo->prepare("SELECT * FROM `tbl_ids_products` WHERE status=1");
                                    $statement->execute();
                                    $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                    foreach ($resultss as $resss) { ?>
                                        <option value="<?=$resss['id']?>" <?php if($datas[0]['product_ids']==$resss['id']){ echo "selected";}?> ><?=$resss['name']?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    
                    
                    
                    <div class="col-md-12">
                        <div class="form-group">
                            <span class="btn btn-primary btn-xs" id="add_new_btn"><i class="fa fa-plus"></i> Add Size</span>
                            
                            <div class="row">
                                <div id="append_section" style="padding-top: 10px;">
                                
                                <?php 
                                if($size_totalData>0){
                                    foreach($size_datas as $size){
                                ?>
                                    <div class="col-md-3 row_size">
                                        <div class="form-group">
                                            <label>Size <span class="btn btn-danger btn-xs remove_size" onclick="RemoveSize(<?=$size['id']?>)"><i class="fa fa-times"></i></span></label>
                                            <input type="text" name="size[]" id="" class="form-control" value="<?=$size['size']?>" >
                                            <input type="hidden" name="size_id[]" id="" class="form-control" value="<?=$size['id']?>" >
                                        </div>
                                    </div>
                                
                                
                                <?php  } } ?>
                                    
                                    
                                    
                                    
                                </div>
                            </div>
                            
                        </div>
                    </div>
                    
                    
                    
                    
                <div class="col-md-3">    
                    <div class="form-group">
                        <label>Status</label>
                        <select class="form-control" name="status" id="status" required>
                            <option value="1" <?= ($datas[0]['status']==1) ? "selected":"" ?>>Active</option>
                            <option value="0" <?= ($datas[0]['status']==0) ? "selected":"" ?>>In-Active</option>
                        </select>
                    </div>
                </div>
                
                </div>
                    

            </div>
            
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary">Update</button>
            </div>
            
            
            </div>
            </form>
            
            
            
            <div id="loading-div" style="display: none; padding-bottom: 80px;">
                    <center> <img src="img/loading.gif" style="width: 250px;"><br><b class="blinking">Processing Please Wait...</b></center>
            </div>
            
            
            
        </div>
    </div>
<script>
    $('#form_details').on('submit', function(e){
        console.log("Click And Form Working");
                e.preventDefault();
                $('#loading-div').show();
                $('#content-div').hide();
                var formData = new FormData(this);
                formData.append('type', 'update_products_name');
                            $.ajax({
                            url: 'apis/products.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                if(response['status']==200){
                                     /*alert(response['message']);*/
                                     $("#add_modal .close").click();
                                     $('#add_form').trigger("reset");
                                     emp_datas();
                                     $('#loading-div').hide();
                                     $('#content-div').show();
                                }else{
                                    alert(response['message']);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                                
                               
                            }
                        });
                
    });
</script>
<?php } ?>
<script>

function RemoveSize(id){
            var result = confirm("Confirm Want to Delete This Size?");
            if (result) {
                    $.ajax({
                    type: 'POST',
                    url: 'api/delete.php',
                    data: {type:'hard_delete',delete_id:id, table: 'tbl_size'},                  
                    success: function(response) {
                        console.log(response); 
                        emp_datas();
                    }
                });
            }
}
         
         
$(document).ready(function() {
    
var count = 1;
$("#add_new_btn").click(function(){
    
    var row=`<div class="col-md-3 row_size">
                <div class="form-group">
                    <label>Size <span class="btn btn-danger btn-xs remove_size"><i class="fa fa-times"></i></span></label>
                    <input type="text" name="size[]" id="" class="form-control" value="" >
                    <input type="hidden" name="size_id[]" id="" class="form-control" value="0" >
                </div>
            </div>`;
        
        $("#append_section").append(row);
        count++;
});
});

$(document).on('click', '.remove_size', function() {
        $(this).closest('.row_size').remove();
    });
</script>