<?php

require( dirname(__FILE__) . '/wp-load.php' );


function download_form_callback($args) {
  global $wpdb;
  $response = $query_rs = array();
  $response["status"] = false;
  $response["error"] = false;
  if(empty($args["type_of_account"]) && empty($args["type_of_form"]))  {
    $response["download_link"] = "Please select type of account and type of form";
    $response["error"] = true;
  }elseif(empty($args["type_of_account"])){
    $response["download_link"] = "Please select type of account";
    $response["error"] = true;
  }elseif(empty($args["type_of_form"])){
    $response["download_link"] = "Please select type of form";
    $response["error"] = true;
  }
  $result = array('Resident Individual' ,'NRI' ,'HUF' ,'Trust' ,'Corporate' ,'LLP' ,'Resident but Foreign National' ,'Partnership');
  //$results_1 = array('Acount opening','Kotak Closure','Part Redemption ','KYC Change ','Kotak Add Nominee');
  $results_1 = array('Acount opening');
  if(!in_array($args["type_of_account"], $result))  {
    $response["download_link"] = "Please select correct select type of account";
    $response["error"] = true;
  }elseif(!in_array($args["type_of_account"], $result)){
    $response["download_link"] = "Please select correct select type of form";
    $response["error"] = true;
  }

  if(!$response["error"]){  
    $sql_query = 'select id,download_file_name from mr_download_form where type_of_account="'.$args["type_of_account"].'" and type_of_form = "'.$args["type_of_form"].'"';
    $query_rs = $wpdb->get_results($sql_query);
    $query_rs = object_to_array($query_rs);
    if($query_rs){
      $response["status"] = true;
      $response["file_name"] = $query_rs["0"]["download_file_name"];
      $path = site_url("wp-content/uploads/download-form/".$query_rs["0"]["download_file_name"]);
      $link = "<a href='$path' target='_blank'>Click here</a> <span style='color: red'>to download your form</span>";
      $response["download_link"] = $link;
    }
  }
  echo json_encode($response);
  die(); 
}


function object_to_array($data)
{
    if (is_array($data) || is_object($data))
    {
        $result = array();
        foreach ($data as $key => $value)
        {
            $result[$key] = object_to_array($value);
        }
        return $result;
    }
    return $data;
}


download_form_callback($_POST);