<?php
include("lib/config.php");

$fileID = isset($_GET["fid"])?$_GET["fid"]:'';

if(isset($fileID) && $fileID!='')
{
	$query_main = "SELECT * FROM ".TBL_FILES." WHERE id = '$fileID' ";
	$msInfo = $con->getSingleRow($query_main);
}
?>
<script src="vendor/ckeditor/ckeditor.js"></script>

<div class="box-content">
  <form method="post" name="frmFiles" id="frmFiles">
    <input type="hidden" name="action" value="update">
    <input type="hidden" name="file_id" value="<?php echo(isset($fileID))?$fileID:'';?>">
    
    <div class="form-group">
      <label for="title">Department</label>
      <select class="form-control custom-select-value" name="department_id" id="department_id" value="" required style="width:350px;">
      <option value="">Select Department</option>
      <?php 
        $selQry_depts = "SELECT * FROM ".TBL_DEPARTMENTS." WHERE status='1' ORDER BY name";
        $deptsInfo =  $con->getMultiRow($selQry_depts, __FILE__,__LINE__);
        $totDepts = count($deptsInfo);	
            for($d=0;$d<$totDepts;$d++)
            {
        ?>
      <option value="<?php echo $deptsInfo[$d]['id'];?>" <?php if(isset($msInfo['department_id']) && $msInfo['department_id']==$deptsInfo[$d]['id']){?>selected="selected"<?php }?> ><?php echo $deptsInfo[$d]['name'];?></option>
      <?php }?>
    </select>
    </div>
    <div class="form-group">
      <label for="title">Title</label>
      <input type="text" class="form-control" name="name" id="name" value="<?=(isset($msInfo['name']))?$msInfo['name']:''?>" placeholder="" style="width:350px;" required>
    </div>
    <div class="form-group">
      <table class="table table-striped table-bordered dataTable no-footer">
        <thead>
          <tr>
            <th width="100%" colspan="3">Detail</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td colspan="3"><textarea name="description" id="editor1"><?=(isset($msInfo['description']))?$msInfo['description']:''?></textarea></td>
          </tr>
        </tbody>
      </table>
    </div>
    <!-- <div class="form-group">
      <label for="title">Display Order</label>
      <input type="text" class="form-control" name="display_order" id="display_order" value="<?=(isset($msInfo['display_order']))?$msInfo['display_order']:''?>" placeholder="" style="width:350px;">
    </div>-->
    <div class="control-group">
      <label class="control-label" for="selectError">Status</label>
      <div class="controls">
        <select name="status" id="status" data-rel="chosen">
          <option value="1" <?php if(isset($msInfo['status']) && $msInfo['status']==1){?> selected="selected" <?php }?>>Active</option>
          <option value="0"  <?php if(isset($msInfo['status']) && $msInfo['status']==0){?> selected="selected" <?php }?>>In Active</option>
        </select>
      </div>
    </div>
    <div style="margin-top:30px;text-align:right;">
      <!--<input type="button" class="btn btn-primary" value="<?=(isset($_GET['pid']))?'Update':'Save'?>">-->
      <button data-loading-text="Saving..." type="submit" class="btn btn-success"> <i class="fa fa-floppy-o"></i> Update</button>
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </form>
</div>
<script>
$(document).ready(function(e){
    
	$("#frmFiles").on('submit', function(e){
	  
        e.preventDefault();
		
		for (instance in CKEDITOR.instances) {
			CKEDITOR.instances[instance].updateElement();
		}
		
        $.ajax({
            type: 'POST',
            url: 'files-config.php',
            data: new FormData(this),
            //data: userData,
            contentType: false,
            cache: false,
            processData:false,
			dataType: 'JSON',
            beforeSend: function(){
                //$('.submitBtn').attr("disabled","disabled");
                //$('#fupForm').css("opacity",".5");
            },
            success: function(msg){
			   				
                //$('.statusMsg').html('');
				
                if(msg[0].message == 'ok'){
				
                    $('#frmFiles')[0].reset();
					
					//$('.statusMMsg').html('<span style="font-size:18px;color:#34A853">Product(s) added successfully.</span>');
					$('#myModal').modal('hide');
					
					getFiles(); 
					
					Lobibox.notify('success', {
						title: 'Success',
						msg: 'Record updated successfully.'
					});
					
                }
				else
				{   
					//$('.statusMMsg').html('<span style="font-size:18px;color:#EA4335">Some problem occurred, please try again.</span>');
					$('#myModal').modal('hide');
					
					Lobibox.notify('error', {
						title: 'Error',
						msg: 'Some problem occurred, please try again.'
					});
                }
            }
        });
    });
	
});
</script>
<script>

			CKEDITOR.replace( 'editor1', {
				fullPage: true,
				allowedContent: true,
				extraPlugins: 'wysiwygarea'
			});

		</script>
