<?php 
session_start();
include_once '../config.php';
$user_id = $_SESSION['Admin']['id'];
 if(file_exists('../json_files/description.json')){ 
 $json = file_get_contents('../json_files/description.json');
 //Decode JSON
 $json_data2 = json_decode($json,true);
 $results2 = array();
 }
if($_REQUEST['action'] == 'Save'){
$Description = addslashes(trim($_REQUEST['Description']));
$ModalValue = addslashes(trim($_REQUEST['ModalValue']));
$CreatedDate = date('Y-m-d');
$Status = 1;
$ModifiedBy = 0;
$ModifiedDate = null;

/*$sql = "INSERT INTO tbl_decriptions SET Description='$Description',Type='$ModalValue',CreatedBy='$user_id',CreatedDate='$CreatedDate'";
    $conn->query($sql);*/

try {
   $sql = "INSERT INTO tbl_decriptions (Description,Type,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate) VALUES (:Description,:Type,:CreatedBy,:CreatedDate,:ModifiedBy,:ModifiedDate)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':Description', $Description);
$stmt->bindParam(':Type', $ModalValue);
$stmt->bindParam(':CreatedBy', $user_id);
$stmt->bindParam(':CreatedDate', $CreatedDate);
$stmt->bindParam(':ModifiedBy', $ModifiedBy);
$stmt->bindParam(':ModifiedDate', $ModifiedDate);
if ($stmt->execute()) {
 include 'incdescriptionjson.php';
 echo 1;
}
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
} 

}

if($_REQUEST['action'] == 'fetch_record'){
 $id = $_REQUEST['id'];
    $query = "SELECT * FROM tbl_decriptions WHERE id = '$id'";
    $row = getRecord($query);
    echo json_encode($row);
}

 if($_REQUEST['action'] == 'delete') {
    $id = $_REQUEST['id'];
      $query = "DELETE FROM tbl_decriptions WHERE id = '$id'";
      $conn->query($query);
      include 'incdescriptionjson.php';
      echo "Delete Successfully";
}

if($_REQUEST['action'] == 'Edit'){
    $id = $_REQUEST['id'];
$Description = addslashes(trim($_REQUEST['Description']));
$ModalValue = addslashes(trim($_REQUEST['ModalValue']));
$CreatedDate = date('Y-m-d');

$sql = "UPDATE tbl_decriptions SET Description='$Description',ModifiedBy='$user_id',ModifiedDate='$CreatedDate' WHERE id='$id'";
$conn->query($sql);

include 'incdescriptionjson.php';
 echo 1;
}

 if($_REQUEST['action']=='view'){
if(file_exists('../json_files/description.json')){ 
 $json = file_get_contents('../json_files/description.json');
 //Decode JSON
 $json_data = json_decode($json,true);
 $results = array();
 }
    ?>                         
<div style="overflow:auto;">                         
<table id="myTable" class="table table-striped table-bordered dt-responsive nowrap scrolldown" style="width:100%;">
        <thead>
            <tr>
            
             <th style="width : 100px;">Sr.No.</th>
             <th style="width : 700px;">Description</th>
             <th style="width : 200px;">Action</th>
            </tr>
        </thead>
        <tbody>
          <?php 
 $srno = 1;
 $ModalValue = addslashes(trim($_REQUEST['ModalValue']));
foreach($json_data['details'] as $result) {                                     
 /* $sql = "SELECT * FROM tbl_decriptions WHERE Type='$ModalValue' ORDER BY id DESC";
   $rx = $conn->query($sql);
  while($nx = $rx->fetch_assoc()){*/
    if($result['Type'] == $ModalValue){
  ?>
           <tr>
            
             <td style="width : 100px;"><?php echo $srno; ?></td>
             <td style="width : 700px;"><?php echo $result['Description']; ?></td>
             <td style="width : 200px;"><a data-id="<?php echo $result['id']; ?>" href='javascript:void(0);' data-toggle="tooltip" data-placement="top" title="Edit" data-original-title="Edit" class="update"><i class="lnr lnr-pencil mr-2"></i></a>&nbsp;&nbsp;<a data-id="<?php echo $result['id']; ?>" href='javascript:void(0);' data-toggle="tooltip" data-placement="top" title="Delete" data-original-title="Delete" class="delete" id="bootbox-confirm"><i class="lnr lnr-trash text-danger"></i></a>
             </td>
            </tr>
             <?php $srno++;} } ?>
        </tbody>
    </table>
</div>
<script type="text/javascript">

  $(document).ready(function(){
    function get_filter(class_name)
    {
        var filter = [];
        $('.'+class_name+':checked').each(function(){
            filter.push($(this).val());
        });
        return filter;
    }

    $('.common_selector').click(function(){
        var ChkValue = get_filter('ChkValue');
        var ValueId = $('#ValueId').val();

        if(ChkValue == ''){
              $('#'+ValueId).val('');
         }

        else{
        //$('#'+ValueId).val(ChkValue);
        //var OldValue = $('#'+ValueId).val();
        $('#OldValue').val(ChkValue);
         }
        //alert(OldValue);
        
        /*else if(ChkValue == ''){
              $('#'+ValueId).val('');
         }*/
         /*var oldval2 = $('#OldValue').val();
        if(oldval2 != ''){
            var ConcatValue =  oldval2.concat(ChkValue);
        }*/
         
        /* else if(OldValue != ''){
          var ConcatValue =  OldValue.concat(ChkValue);
             $('#'+ValueId).val(ConcatValue);
        }*/
        });
    });
</script>
<?php } 

 if($_REQUEST['action']=='search'){?>
  <div style="overflow:auto;">                         
<table id="myTable" class="table table-striped table-bordered dt-responsive nowrap scrolldown" style="width:100%;">
        <thead>
            <tr>
            
             <th style="width : 100px;">Sr.No.</th>
             <th style="width : 700px;">Description</th>
             <th style="width : 200px;">Action</th>
            </tr>
        </thead>
 <?php          
 $search = mysqli_real_escape_string($conn, $_REQUEST["query"]);
 $ModalValue = addslashes(trim($_REQUEST['ModalValue']));
  $output = "";
  if($search!=''){
  $query = "SELECT * FROM tbl_decriptions 
  WHERE Type='$ModalValue' AND Description LIKE '%".$search."%'";
  }
  else{
    $query = "SELECT * FROM tbl_decriptions WHERE Type='$ModalValue'";
  }
  $rncnt = getRow($query);
  if($rncnt > 0){?>
    <tbody>
    <?php   
    $srno = 1;     
    $row = getList($query);
    foreach($row as $result){?>
        <tr>
            
             <td style="width : 100px;"><?php echo $srno; ?></td>
             <td style="width : 700px;"><?php echo $result['Description']; ?></td>
             <td style="width : 200px;"><a data-id="<?php echo $result['id']; ?>" href='javascript:void(0);' data-toggle="tooltip" data-placement="top" title="Edit" data-original-title="Edit" class="update"><i class="lnr lnr-pencil mr-2"></i></a>&nbsp;&nbsp;<a data-id="<?php echo $result['id']; ?>" href='javascript:void(0);' data-toggle="tooltip" data-placement="top" title="Delete" data-original-title="Delete" class="delete" id="bootbox-confirm"><i class="lnr lnr-trash text-danger"></i></a>
             </td>
            </tr>
<?php $srno++;} } ?>
 </tbody>
    </table>
</div>
<?php } 

 if($_REQUEST['action']=='view2'){
 $ModalValue = addslashes(trim($_REQUEST['ModalValue']));
 foreach($json_data2['details'] as $result) {
  if($result['Type'] == $ModalValue){  
?>   
  <option value="<?php echo $result['Description']; ?>"><?php echo $result['Description']; ?></option>
<?php } }
}
?>
