<?php  include "header.php";


$id = $_GET['id'];

if(isset($id)){
    
    $ajax = 'update_model';
    $title = 'Update';
   
    $edit_statement = $pdo->prepare("SELECT * FROM tbl_models WHERE id=?");
    $edit_statement->execute(array($id));
    $edit_data = $edit_statement->fetchAll(PDO::FETCH_ASSOC);
    
     foreach($edit_data as $data){
         $model_name = $data['model_name'];
         $model_hsn_code = $data['model_hsn_code'];
         $category_id = $data['category_id'];
         $subcategory_id = $data['subcategory_id'];
     }
    
}else{
    $title = 'Add';
    $ajax = 'create_model'; 
    $id = 0;
}


?>


<div class="content-wrapper">
        <section class="content-header">
            <h1><?=$title?> Model</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active"><?=$title?> Model</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">
                    
                    <div id="content-div">
                    
                    <div class="col-lg-12">

                        <form id="form_details" autocomplete="off">
                            
                        <input type="hidden" name="row_id" id="row_id" value="<?=$id?>" >
                                                                 
                        <div class="row">
                            
                             <div class="col-md-2"></div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="category_id">Category</label>                                   
                                    <select name="category_id" class="form-control select2" style="width:100%" id="category_id" required="required">
                                        <option value="">Select Category</option>
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_category` where status=1");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>" <?= (!empty($category_id) && $category_id == $result['id'] ? "selected" : "") ?> ><?=$result['name']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="subcategory_id"> Sub Category</label>                                   
                                    <select name="subcategory_id" class="form-control select2" style="width:100%" id="subcategory_id" required="required">
                                        <option value="">Select Sub Category</option>
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_sub_category` where status=1");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>" <?= (!empty($subcategory_id) && $subcategory_id == $result['id'] ? "selected" : "") ?> ><?=$result['name']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                             <div class="col-md-2"></div>
                         
                        </div>   
                            
                        <div class="row">    
                            <div class="col-md-2"></div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="model_name">Model Name</label>                                    
                                    <input type="text" name="model_name" value="<?=$model_name?>" placeholder="Enter the Model Name" class="form-control tip" id="model_name"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="model_hsn_code">Model HSN Code</label>                                    
                                    <input type="text" name="model_hsn_code" value="<?=$model_hsn_code?>" placeholder="Enter the Model HSN Code" class="form-control tip" id="model_hsn_code"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-2"></div>
                        </div>
                        
                        
                        

                        <div class="form-group" style="text-align:center;margin-top:20px">
                            <input type="submit"  id="submit" value="<?=$title?> Model" 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="model-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;">Model Added 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="sub-category-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"; ?>

<style>

</style>

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