<?php
defined( 'ABSPATH' ) or die( 'Direct access is not permitted' );
global $wpdb;
$table_name = $wpdb->prefix."custody_icici_forms";
include_once('includes/functions.php');

if(isset($_POST['but_submit']) && $_POST['but_submit'] == 'Submit'){
  $check_data = check_empty_file($_POST);
  if($check_data["status"]){
    $error = $check_data["error"];
    echo "<script>location.replace('admin.php?page=custodylist&err=$error');</script>";
    exit();
  }else{
    include_once('includes/post_variable.php');
    $date_time = date('Y-m-d H:i:s');
    $data ="INSERT INTO $table_name (type_of_account,type_of_form,slug,download_file_name,created_on,ordering) VALUES('$type_of_account','$type_of_form','$slug','$image_url','$date_time','$ordering')";
    $wpdb->query($data);
    echo "<script>location.replace('admin.php?page=custodylist&msg=1');</script>";
    exit();
  }
  
}

if(isset($_GET['action']) && $_GET['action']=="new" || $_GET['action']=="update" || $_GET['action']=="ETOF" || $_GET['action']=="ETOA"){
  $sql = "select type_of_account from $table_name group by type_of_account order by type_of_account";
  $results = $wpdb->get_results($sql);
  foreach ($results as $value) {
    $result[] = $value->type_of_account;
  }

  $sql_1 = "select type_of_form from $table_name group by type_of_form order by type_of_form";
  $results_1 = $wpdb->get_results($sql_1);
  foreach ($results_1 as $value) {
    $result_1[] = $value->type_of_form;
  }
}


if ($_GET['action']=="delete" && isset($_GET['del'])) {
    $del_id = $_GET['del'];
    $wpdb->query("DELETE FROM $table_name WHERE id='$del_id'");
    echo "<script>location.replace('admin.php?page=custodylist');</script>";
    exit();
}

if ($_GET['action']=="status" && isset($_GET['upt'])) {
    $upt = $_GET['upt'];
    $status = $_GET['status'];
    $wpdb->query("update $table_name set status ='$status'  WHERE id='$upt'");
    echo "<script>location.replace('admin.php?page=custodylist&msg=2');</script>";
    exit();
}

if(isset($_POST['but_submit']) && $_POST['but_submit'] == 'Update' && isset($_POST['action'])){

    if($_POST['action'] == 'update_main'){
      $check_data = check_empty_file($_POST);
      if($check_data["status"]){
        $error = $check_data["error"];
        echo "<script>location.replace('admin.php?page=custodylist&err=$error');</script>";
        exit();
      }else{
        $id = $_POST['id'];
        include_once('includes/post_variable.php');        
        $update_query = "update $table_name set type_of_account ='$type_of_account',type_of_form ='$type_of_form',slug= '$slug',download_file_name ='$image_url',ordering = '$ordering' WHERE id=".$id;  
        $wpdb->query($update_query);
        echo "<script>location.replace('admin.php?page=custodylist&msg=2');</script>";
        exit();
      }
    }elseif($_POST['action'] == 'update_tof'){
      $check_data = check_type_of_form($_POST);
       if($check_data["status"]){
          $error = $check_data["error"];
          echo "<script>location.replace('admin.php?page=custodylist&err=$error');</script>";
          exit();
       }else{
         $type_of_form_other = trim($_POST['type_of_form_other']);
         $type_of_form =$_POST['type_of_form'];
         $wpdb->query("update $table_name set type_of_form ='$type_of_form_other'  WHERE type_of_form='$type_of_form'");
         echo "<script>location.replace('admin.php?page=custodylist&msg=4');</script>";
         exit();
        }
    }elseif($_POST['action'] == 'update_toa'){
       $check_data = check_type_of_account($_POST);
       if($check_data["status"]){
          $error = $check_data["error"];
          echo "<script>location.replace('admin.php?page=custodylist&err=$error');</script>";
          exit();
       }else{
          $type_of_account_other = trim($_POST['type_of_account_other']);
         $type_of_account =$_POST['type_of_account'];
         $wpdb->query("update $table_name set type_of_account ='$type_of_account_other'  WHERE type_of_account='$type_of_account'");
         echo "<script>location.replace('admin.php?page=custodylist&msg=5');</script>";
         exit();
        }
    }
}

if(isset($_GET['msg'])){
  echo "<br><p class='sccuess_message'>".sccuess_message($_GET['msg'])."</p>";  
}
if(isset($_GET['err'])){
  echo "<br><p class='error_message'>".error_message($_GET['err'])."</p>";  
} 
?>
<!-- Form -->
<?php if(isset($_GET['action']) && $_GET['action']=="new"){
  include_once('includes/add.php');
} elseif(isset($_GET['action']) && $_GET['action']=="update"){ 
        $upt_id = $_GET['upt'];
        $query ="SELECT * FROM $table_name WHERE id='$upt_id'";
        $new_result = $wpdb->get_results($query);
        foreach($new_result as $print) {
          $id = $print->id;
          $type_of_form = $print->type_of_form;
          $type_of_account = $print->type_of_account;
          $download_file_name = $print->download_file_name;
          $slug = $print->slug;
          $ordering = $print->ordering;
        }?>
<table class="form-table"> 
  <form method='post' action='' name='myform' enctype='multipart/form-data'>
    <input type="hidden" name="id" id="id" value="<?php echo $id?>">
    <input type="hidden" name="action" id="action" value="update_main">
      <tr class="form-field term-name-wrap">
        <th scope="row"><label for="name">Type of Form</label></th>
        <td>
          <select name="type_of_form" id="type_of_form" required>
                <option value="">Select Type of Form</option>
                    <?php
                    foreach($result_1 as $value ) { ?>
                      <option value="<?php echo $value; ?>" <?php if($type_of_form == $value){?>selected="selected"<?php } ?> ><?php echo $value; ?></option>
                    <?php
                    }
                  ?>
           </select>
        </td>
      </tr>
      <tr class="form-field term-name-wrap">
        <th scope="row"><label for="name">Select Type of Account</label></th>
        <td>
            <select name="type_of_account" id="type_of_account" required>
                <option value="">Select Type of Account</option>
                    <?php
                    foreach( $result as $value ) { ?>
                      <option value="<?php echo $value; ?>" <?php if($type_of_account == $value){?>selected="selected"<?php } ?>><?php echo $value; ?></option>
                    <?php
                    }
                  ?>
           </select>
        </td>
      </tr>
      <tr class="form-field term-slug-wrap">
          <th scope="row"><label for="name">Slug (URL Name)</label></th>
          <td>
              <input name="slug" id="tag-slug" type="text" value="<?php echo $slug?>" size="40" required>
          </td>
      </tr>
     <tr class="form-field term-name-wrap">
        <th scope="row"><label for="name">Select File Path</label></th>
        <td><input type="text" name="image_url" id="image_url" value="<?php echo $download_file_name?>" required></a>
        </td>
      </tr>
     <tr>
          <th scope="row"><label for="name">Ordering</label></th>
          <td>
              <input name="ordering" id="ordering" type="number" max="100" min="0" size="10" value="<?php echo $ordering?>" required>
          </td>
      </tr>      
      <tr>
        <td>&nbsp;</td>
        <td><input type='submit' class='button button-primary' name='but_submit' value='Update'></td>
      </tr>
  </form>
</table> 
  
<?php
} elseif(isset($_GET['action']) && $_GET['action']=="view"){ 
        $upt_id = $_GET['upt'];
        $new_result = $wpdb->get_results("SELECT * FROM $table_name WHERE id='$upt_id'");
        foreach($new_result as $print) {
          $id = $print->id;
          $type_of_form = $print->type_of_form;
          $type_of_account = $print->type_of_account;
          $download_file_name = $print->download_file_name;
          $slug = $print->slug;
          $ordering = $print->ordering;
       }
   include_once('includes/view.php');
}elseif(isset($_GET['action']) && $_GET['action']=="ETOF"){ 
   include_once('includes/type_of_form.php');     
}elseif(isset($_GET['action']) && $_GET['action']=="ETOA"){ 
   include_once('includes/type_of_account.php');    
}else{
   include_once('includes/listing.php' );
}
?>
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>  
<script type="text/javascript">
  $("#type_of_form_div").hide();
  $("#type_of_account_other").hide();

  $("#type_of_form").change(function(){
    $("#type_of_form_div").hide();
    if($(this).val()=='Other'){
      $("#type_of_form_div").show();
    }
  });
  $("#type_of_account").change(function(){
    $("#type_of_account_other").hide();
    if($(this).val()=='Other'){
      $("#type_of_account_other").show();
    }
  });

  $('.upload_image_button').click(function() {
    var send_attachment_bkp = wp.media.editor.send.attachment;
    var button = $(this);
    wp.media.editor.send.attachment = function(props, attachment) {
        $(button).parent().prev().attr('src', attachment.url);
        $(button).prev().val(attachment.id);
        wp.media.editor.send.attachment = send_attachment_bkp;
    }
    wp.media.editor.open(button);
    return false;
 });   
</script>