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

<?php

$ids = $_GET['id'];
    $statement = $pdo->prepare("SELECT * FROM tbl_ids_purity e where e.id=".$ids);
    $statement->execute();
    $datas = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    
    if($totalData>0){
        $name = $datas[0]['name'];
        $product_ids = $datas[0]['product_ids'];
        $value = $datas[0]['value'];
        $short_name = $datas[0]['short_name'];
    }


?>
<div class="content-wrapper">
        <section class="content-header">
            <h1>Update Purity</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Update Purity</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">
                
                <?php if($totalData==1){ ?>
                        <form id="form_details" autocomplete="off">
                            
                            <input type="hidden" name="ids" value="<?=$ids?>">
                                                                 
                        <div class="row">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="name">Purity Name</label>                                    
                                    <input type="text" name="name" value="<?=$name?>" placeholder="Enter the Purity Name" class="form-control tip" id="name"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="short_name">Short Name</label>                                    
                                    <input type="text" name="short_name" value="<?=$short_name?>" placeholder="Enter the Short Name" class="form-control tip" id="short_name"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="product_id">Product Name</label>                                   
                                    <select name="product_id" class="form-control select2" style="width:100%" id="product_id" required="required">
                                        <option value="">Select Product</option>
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_ids_products` where status=1");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>" <?= ($result['id']==$product_ids ? "selected":"")?>><?=$result['name']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="value">Value</label>                                    
                                    <input type="text" name="value" value="<?=$value?>" placeholder="Enter the Value" class="form-control tip" id="value"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label>Status</label>
                                    <select class="form-control" name="status" id="status" required>
                                        <option value="1" <?php if($datas[0]['status']==1){echo "selected";}?>>Active</option>
                                        <option value="0" <?php if($datas[0]['status']==0){echo "selected";}?>>In-Active</option>
                                    </select>
                                </div>
                            </div>
                            
                            
                        </div>
                        
                        
                        

                        <div class="form-group" style="text-align:center;">
                            <input type="submit"  id="submit" value="Update Purity" class="btn btn-success" />
                        </div>
                        
                        </form>
                <?php }else{ ?>
                        <center><b style="color: red;">No Records Found</b></center><br><br>
                <?php } ?>

                    </div>
                    
                        <div class="form-group" style="text-align:center;">
                            <a href="purity-add.php"><button class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</button></a>
                            <a href="purity-list.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;">
        <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;">Purity Updated Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <a href="purity-edit.php?id=<?=$ids?>"><button class="btn btn-info"><i class="fa fa-refresh"></i> Reset Form</button></a>
                            &nbsp;&nbsp;
                            <a href="purity-list.php"><button class="btn btn-warning"><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </center>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>


<?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_purity');
                    $.ajax({
                            url: 'apis/customs.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']);
                                     $("#submit").prop('disabled', false);
                                    //  $('#success-div').show();
                                     $('#loading-div').hide();
                                     setTimeout(function(){ window.location.href="purity-list.php"; },1500);
                                }else{
                                    toastr.error(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</script>