var inSpareProModule = (function () {
    
    $('.add-inSparePro').on('click',function(){
        
        //var supplierdetails  =   $("#supplierdetails").val();

        var spareID,spareText,eqty;

        //if(supplierdetails) {
            spareID     =   $('#sparedetails').val();
            spareText   =   $('.spare-select-box option:selected').text();            
            eqty        =   $('.eqty-input').val();
            
            if(spareID && spareText && eqty) {

                var trlength    =   $('#inSpareProDetailsAdd tbody tr:last').length;
                
            if(trlength == 0) {

                trlength = 1;

            } else {

                trlength = $('#inSpareProDetailsAdd tbody tr:last').attr('id');
                
                trlength = parseInt(trlength)+parseInt(1);
            }

            var tripDetails =  '<td class="product_name">'+spareText+'<input class="form-control" name="spareID[]"  value="'+spareID+'" type="hidden"></td>';
            
            tripDetails +='<td>'+eqty+'<input class="form-control" name="spareqty[]" type="hidden" value="'+eqty+'" ></td>';

            tripDetails +='<td><input type="button" class="btn btn-default removeDetails" id="removeDetails" value="-"></td>';

            $("#inSpareProDetailsAdd tbody").append("<tr id='"+trlength+"'></tr>");
            $("#inSpareProDetailsAdd tbody tr:last").append(tripDetails);   
            
            $('.eqty-input').val('');

            } else {
                console.log("Please enter Spare details....!");    
                $('.eqty-input').val('');
            }
        // }
        // else {
        //     //alert("Please Select supplier Details & Remove the Spare details values....!");
        //     console.log("Please select supplier Name & Remove the spare details values....!");    
        //     $('.eqty-input').val('');
        // }    
    });

    $('body').on('click', 'input.removeDetails', function(){

        console.log("Remove Row:"+$(this).closest('tr').attr('id'));
        var removeTrip = $(this).closest('tr').attr('id');
        $("#"+removeTrip).remove();

    
    });

    $('body').on('change','select.spare-select-box',function(){
        console.log($(this).val());
        var spareid    =   $(this).val();
        if(spareid) {
            $.ajax({
                url: base_url+'fetchAjaxData/fetchSelectSpareQty.php?spareid='+spareid,
                dataType: "json",
                type: 'GET',        
                success: function(data) {
                    $("#aqty").val('');
                    $("#aqty").val(data[0].spareqty);                   
                },
                error: function() {
                    console.log('An error has occurred');
                }                
            });
        }
    });





    
    

})();