<?php include("includes/header.php");

	require("includes/function.php");
	require("language/language.php");

	require_once("thumbnail_images.class.php");
	
	
  $protocol = strtolower( substr( $_SERVER[ 'SERVER_PROTOCOL' ], 0, 5 ) ) == 'https' ? 'https' : 'http'; 

  $file_path_img = $protocol.'://'.$_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']).'/';
	
   //Get Category
	$cat_qry="SELECT * FROM tbl_category ORDER BY category_name";
	$cat_result=mysqli_query($mysqli,$cat_qry); 
	
	$qry="SELECT * FROM tbl_ringtone where id='".$_GET['ringtone_id']."'";
  $result=mysqli_query($mysqli,$qry);
  $row=mysqli_fetch_assoc($result);
	
 
	

	
		if(isset($_POST['submit']))
	{
        
        
        if ($_POST['mp3_type']=='server_url')
        {
              $mp3_url=$_POST['mp3_url'];
 

            if($_FILES['mp3_thumbnail']['name']!="")
            { 
              $mp3_thumbnail=rand(0,99999)."_".$_FILES['mp3_thumbnail']['name'];
       
            }
            else
            {
              $mp3_thumbnail=$_POST['mp3_thumbnail_name'];
            }

 
        }     
        
         

        if ($_POST['mp3_type']=='local')
        {
            if($_POST['mp3_file_name']!="")
            {
                $file_path = 'http://'.$_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']).'/uploads/';
              
                $mp3_url=$file_path.$_POST['mp3_file_name'];

            }
            else
            {
                $mp3_url=$_POST['mp3_url'];
            }
            
           


        } 


        
          
           $data = array( 
        'cat_id'  =>  $_POST['cat_id'],
        'title'  =>  $_POST['title'],
        'url'  =>  $mp3_url,
        'mp3_type'  =>  $_POST['mp3_type']
			    );		
  		 		 
         $qry=Update('tbl_ringtone', $data, "WHERE id = '".$_POST['ringtone_id']."'");

         
		$_SESSION['msg']="11"; 
		header( "Location:edit_ringtone.php?ringtone_id=".$_POST['ringtone_id']);
		exit;	

		 
	}
	

?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
            $(function () {
                $('#btn').click(function () {
                    $('.myprogress').css('width', '0');
                    $('.msg').text('');
                    var mp3_local = $('#mp3_local').val();
                    if (mp3_local == '') {
                        alert('Please enter file name and select file');
                        return;
                    }
                    var formData = new FormData();
                    formData.append('mp3_local', $('#mp3_local')[0].files[0]);
                    $('#btn').attr('disabled', 'disabled');
                     $('.msg').text('Uploading in progress...');
                    $.ajax({
                        url: 'uploadscript.php',
                        data: formData,
                        processData: false,
                        contentType: false,
                        type: 'POST',
                        // this part is progress bar
                        xhr: function () {
                            var xhr = new window.XMLHttpRequest();
                            xhr.upload.addEventListener("progress", function (evt) {
                                if (evt.lengthComputable) {
                                    var percentComplete = evt.loaded / evt.total;
                                    percentComplete = parseInt(percentComplete * 100);
                                    $('.myprogress').text(percentComplete + '%');
                                    $('.myprogress').css('width', percentComplete + '%');
                                }
                            }, false);
                            return xhr;
                        },
                        success: function (data) {
                         
                            $('#mp3_file_name').val(data);
                            $('.msg').text("File uploaded successfully!!");
                            $('#btn').removeAttr('disabled');
                        }
                    });
                });
            });
        </script>
<script type="text/javascript">
$(document).ready(function(e) {
           $("#mp3_type").change(function(){
          
           var type=$("#mp3_type").val();
              
              if(type=="server_url")
              {
                 
                 $("#mp3_url_display").show();
                 $("#thumbnail").show();
                 $("#mp3_local_display").hide();
              }
              else
              {   
                     
                $("#mp3_url_display").hide();               
                $("#mp3_local_display").show();
                $("#thumbnail").show();

              }    
              
         });
        });
</script>
<div class="row">
      <div class="col-md-12">
        <div class="card">
        <div class="page_title">Edit Radio</div>
          <div class="row mrg-top">
            <div class="col-md-12">
               
              <div class="col-md-12 col-sm-12">
                <?php if(isset($_SESSION['msg'])){?> 
               	 <div class="alert alert-success alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
                	<?php echo $client_lang[$_SESSION['msg']] ; ?></a> </div>
                <?php unset($_SESSION['msg']);}?>	
              </div>
            </div>
          </div>
          <div class="card-body mrg_bottom"> 
            <form action="" name="addeditcategory" method="post" class="form form-horizontal" enctype="multipart/form-data">
            	<input  type="hidden" name="ringtone_id" value="<?php echo $_GET['ringtone_id'];?>" />

              <div class="section">
                <div class="section-body">


                <div class="section-body">
                  <div class="form-group">
                    <label class="col-md-3 control-label">Category</label>
                    <div class="col-md-6">
                      <select name="cat_id" id="cat_id" class="select2">
                        <option value="">--Select Category--</option>
          							<?php
          									while($cat_row=mysqli_fetch_array($cat_result))
          									{
          							?>          						 
          							<option value="<?php echo $cat_row['cid'];?>" <?php if($cat_row['cid']==$row['cat_id']){?>selected<?php }?>><?php echo $cat_row['category_name'];?></option>	          							 
          							<?php
          								}
          							?>
                      </select>
                    </div>
                  </div>
                 
                
                  <div class="form-group">
                    <label class="col-md-3 control-label">Title</label>
                    <div class="col-md-6">
                      <input type="text" name="title" id="title" value="<?php echo $row['title']?>" class="form-control" required>
                    </div>
                  </div>   
                  
         
                 

              <div class="form-group">
                    <label class="col-md-3 control-label">Upload From :-</label>
                    <div class="col-md-6">                       
                      <select name="mp3_type" id="mp3_type" style="width:280px; height:25px;" class="select2" required>
                            <option value="">--Select Category--</option>                            
                            <option value="server_url" <?php if($row['mp3_type']=='server_url'){?>selected<?php }?>>From Server(URL)</option>
                            <option value="local" <?php if($row['mp3_type']=='local'){?>selected<?php }?>>Browse From Device</option>
                      </select>
                    </div>
                  </div>
                  <div id="mp3_url_display" class="form-group" <?php if($row['mp3_type']=='local'){?>style="display:none;"<?php }else{?>style="display:block;"<?php }?>>
                    <label class="col-md-3 control-label">Song URL :-</label>
                    <div class="col-md-6">
                      <input type="text" name="mp3_url" id="mp3_url" value="<?php echo $row['url']?>" class="form-control">
                    </div>
                  </div>
                  <div id="mp3_local_display" class="form-group" <?php if($row['mp3_type']=='local'){?>style="display:block;"<?php }else{?>style="display:none;"<?php }?>>
                    <label class="col-md-3 control-label">Song Upload :-</label>
                    <div class="col-md-6">
                    
                    <input type="hidden" name="mp3_file_name" id="mp3_file_name" value="" class="form-control">
                      <input type="file" name="mp3_local" id="mp3_local" value="" class="form-control">
                      <div><label class="control-label">Current URL :-</label><?php echo $row['url']?></div><br>
                      <div class="progress">
                            <div class="progress-bar progress-bar-success myprogress" role="progressbar" style="width:0%">0%</div>
                        </div>

                        <div class="msg"></div>
                        <input type="button" id="btn" class="btn-success" value="Upload" />
                    </div>
                  </div><br>



      
                  <div class="form-group">
                    <div class="col-md-9 col-md-offset-3">
                      <button type="submit" name="submit" class="btn btn-primary">Save</button>
                    </div>
                  </div>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
        
<?php include("includes/footer.php");?>       
