<?php  include "header.php";

$ids = $_GET['id'];

    
    $statement = $pdo->prepare("SELECT * FROM tbl_suppliers e where e.id=?");
    $statement->execute(array($ids));
    $emp_data = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();



?>
<div class="content-wrapper">
        <section class="content-header">
            <h1>Edit Suppliers</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Edit Suppliers</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>





<?php if($totalData==1){ ?>

<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary">
          
          
          
                <div class="box-body" id="content-div">
                    <div class="col-lg-12">

                        <form id="form_details">
                                    <input type="hidden" name="ids" value="<?=$emp_data[0]['id']?>" class="form-control tip" id="id"  required="required" />
                                    
                        

                                                                 
                        <div class="row">
                            
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="name">Name</label>                                    
                                    <input type="text" name="name" value="<?=$emp_data[0]['name']?>" placeholder="Enter the Name" class="form-control tip" id="name"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="phone">Mobile Number</label>                                   
                                     <input type="number" name="phone" placeholder="Enter the Mobile Number" value="<?=$emp_data[0]['mobile_no']?>"  class="form-control tip" id="phone"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="optional" for="alter_mobile_no">Alter Mobile Number</label>                                   
                                     <input type="number" name="alter_mobile_no" placeholder="Enter the Alter Mobile Number" value="<?=$emp_data[0]['alter_mobile_no']?>"  class="form-control tip" id="alter_mobile_no"  />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="product_ids">Product</label>                                   
                                    <select name="product_ids" class="form-control select2" style="width:100%" id="product_ids" required="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 $ress) { ?>
                                                <option value="<?=$ress['id']?>" <?= ($emp_data[0]['product_ids']==$ress['id']) ? "selected":""; ?>><?=$ress['name']?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="optional" for="gst_no">GST No</label>                                    
                                    <input type="gst_no" name="gst_no" value="<?=$emp_data[0]['gst_no']?>" placeholder="Enter the GST No" class="form-control tip" id="gst_no" />
                                </div>
                            </div>
                            
                            
                            
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="optional" for="address">Address</label>                                   
                                     <textarea type="text" name="address" placeholder="Enter the Address" value=""  class="form-control tip" id="address"><?=$emp_data[0]['address']?></textarea>
                                </div>
                            </div>
                            
                            
                        </div>
                        
                        
      
                        

                        <div class="form-group" style="text-align:center;">
                            <input type="submit" name="submit" value="Submit"  class="btn btn-success" />
                        </div>
                        
                        
                        </form>

                    </div>
                    
                    
                        <div class="form-group" style="text-align:center;">
                            <span onclick="location.reload();"  class='btn btn-primary'><i class='fa fa-refresh'></i> Reset Form</span> 
                            <a href="suppliers.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </div>
                        
                        
                    <div class="clearfix"></div>
                </div>
                
<div class="container" id="loading-div" style="display: none; margin-top: 40px; margin-bottom: 40px; padding-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; padding-bottom: 40px;">
                        <center><b class="blinking" style="font-size:22px;">Supplier Details Updated Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <span onclick="location.reload();" class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</span>
                            &nbsp;&nbsp;
                            <a href="suppliers.php"><button class="btn btn-warning"><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </center>
                    </div>
                    
                    
            </div>
        </div>
    </div>
</section>




<?php } else { ?>

<?=$no_records_found?>

<?php } ?>


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




<script>
$(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('type', 'update_suppliers');
                    $.ajax({
                            url: 'apis/suppliers.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']);*/
                                     $('#form_details').trigger("reset");
                                     $("#submit").prop('disabled', false);
                                     $('#content-div').hide();
                                     $('#loading-div').hide();
                                     $('#success-div').show();
                                }else{
                                    alert(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</script>