<style type="text/css">
    .error{
        color: red;
    }
</style>
<main class="common_margin" id="main"> 
    <div class="main-wrap">
        <div class="dashboard-cover"> 
            <div class="subsec_sec">
                <div class="subject_r">
                    <div class="subject_lsec">
                        <a href="<?php echo base_url()?>buses">
                            <button class="btn btn-dark btn-sm">Back</button>
                        </a>                            
                        <div class="common-heading">
                            <h2><?php echo $title ?></h2>
                        </div> 

                        <?php $reg_no=$brand=$year= $image=$seat=''; 
                    if ($bus_id > 0) {
                        // echo"<pre>";print_r($detailsHere);
                        if (isset($detailsHere) && !empty($detailsHere)) {
                          foreach ($detailsHere as $key => $value) {
                                $reg_no = $value['reg_no'] ;
                                $brand = $value['bus_brand'];
                                $year = $value['year'];
                                $image = str_replace('assets-bustracking/images/bus_image/', '', $value['image']);  
                                $seat = $value['seats'];                          

                            }
                        }

                    } ?>
                        <form action="<?php echo base_url() ?>add-edit-bus/<?php echo $bus_id ?>"  method="post" id="addBus" enctype="multipart/form-data">
                            <input type="hidden" name="bus_id" value="<?php echo $bus_id;?>">
                       
                        <div class="subject_lformarea book-form-area">
                            <div class="form-group"> 
							    <label for="">Register Number<span class="text-danger font-weight-bold">*</span></label>
                                <input type="text" name="reg_no" class="form-control" placeholder="Reg No " style="text-transform:uppercase;" value="<?php echo $reg_no ? $reg_no : ''; ?>"> 
                            </div>
                            <div class="form-group"> 
							    <label for="">Brand<span class="text-danger font-weight-bold">*</span></label>
                                <input type="text" name="brand" class="form-control" placeholder="Brand" value="<?php echo $brand ? $brand : ''; ?>">
                            </div>
                            <div class="form-group"> 
                                <label for="">No of Seats<span class="text-danger font-weight-bold">*</span></label>
                                <input type="text" name="seat" class="form-control" placeholder="Number of seats" maxlength="4" value="<?php echo $seat ? $seat : ''; ?>">
                            </div>
                            <div class="form-group"> 
                                <label for="">Year of Purchase<span class="text-danger font-weight-bold">*</span></label>
                                <input type="text"  name="year" class="form-control" placeholder="Year of Purchase" maxlength="4" value="<?php echo $year ? $year : ''; ?>">
                            </div>
                            <div class="form-group">      
                                <label for="">Add Bus Photo<span class="text-danger font-weight-bold">*</span></label>
                                <input type="file" class="file-upload" name="file" id="upload">
                                <input type="text" class="form-control file-attach" id="upload_link" name="profile_image" placeholder="Add Bus Photo " value="<?php echo $image ? $image : ''; ?>">
                                <i class="fas fa-paperclip"></i>
                            </div>
                        </div>
                        <button type="submit" name="submit" class="subject_addbtn" id="uploadBtn"><?php echo $title; ?></button>
                        </form>
                          
                    </div>
                </div>
            </div> 
        </div>
    </div>
</main> 
<!--End right-top side-->  
 
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
<style type="text/css">
    #upload_link{
    text-decoration:none;
    }
    #upload{
        display:none
    }

</style>
<script>
    $(document).ready(function(){
        
        jQuery.validator.addMethod("inputregx", function(value, element) {
            return this.optional(element) || /^[a-zA-Z0-9 ]{3,50}$/.test(value);
        }, 'Please enter alpha and numeric characters only ');

                
        jQuery.validator.addMethod("patternregx", function(value, element) {
            return this.optional(element) || /^[A-Za-z]{2}[0-9]{2}[A-Za-z]{2,3}[0-9]{4}$/.test(value);
            }, 'Please Enter Alpha Numeric Value');

        jQuery.validator.addMethod("seatregix", function(value, element) {
            return this.optional(element) || /^[0-9]{2,4}$/.test(value);
            }, 'Please Enter Numeric Value');

        jQuery.validator.addMethod("yearregix", function(value, element) {
            return this.optional(element) || /^[0-9]{4}$/.test(value);
            }, 'Please Enter Numeric Value');

        $("#addBus").validate({
            rules: {
                reg_no: {
                    required: true,
                },
                brand:{
                    required:true,
                    inputregx: true,
                    minlength: 3
                },
                seat:{
                    required:true,
                    seatregix:true,                   
                    minlength: 1,
                    maxlength: 4
                    
                },
                year:{
                    required:true,                    
                    yearregix:true
                },
                profile_image:{
                    required: function(element){
                        var name = $('.subject_addbtn').text();
                        if (name == 'Add Bus') {
                            return true;
                        }else{
                            return false;
                        }
                    }  
                }
            },
            messages: {

                reg_no: {
                    required: "Please enter Register Number",
                    minlength : "Please enter more than 4 Characters"
                },
                brand:{
                    required: "Please enter Brand Name",
                    minlength : "Please enter more than 3 Characters"
                },
                seat:{
                    required: "Please enter Number of Seat"
                },
                year:{
                    required:"Plase Enter Year"
                }
                

            },
            submitHandler: function(form) {

                form.submit();
            }
        });



        $("#upload_link").on('click', function(e){
            e.preventDefault();
            $("#upload:hidden").trigger('click');
        });
   
    });

   $('input[type="file"]').change(function (e) {
        $('#upload_link').val(e.target.files[0].name); 
   });

</script>