<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require '../quiklrn_app/config.php';
include '../quiklrn_app/curl.php';

// require 'external_api.php';
header( 'Access-Control-Allow-Origin: *' );

$method_name = '';
if ( isset( $_REQUEST[ 'method' ] ) && $_REQUEST[ 'method' ] != '' ) {
    $method_name = $_REQUEST[ 'method' ];
}

switch ( $method_name ) {
    case 'test_function':
     json_output( test_function() );
    break;

    case 'downloadCSV':
        json_output( downloadCSV() );
       break;

       case 'workFlow':
        ( workFlow() );
       break;

}

function downloadCSV(){
    $array = ['department_name'=> 'Gen', 'course_name'=> 'Maths', 'course_code' => 123];
    array_to_csv_download([$array]);
  }

function array_to_csv_download() {

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    $delimiter = ",";
    $filename = "reports_" . date('Y-m-d') . ".csv";
   
    $tmpDir = '../../tmp/output.csv';
    
    // $csvFile = fopen('/var/www/html/tmp/xyz.csv', 'w');
    $csvFile = fopen($tmpDir, 'w');
   
    if (!flock($csvFile, LOCK_EX)) {
        error_log('Cannot get lock!');
        return false;
    } 
    die;
    $fields = array('Dept Name', 'Course Name', 'Course Code');
    fputcsv($csvFile, $fields, $delimiter);
    $array = array(['department_name'=> 'Gen', 'course_name'=> 'Maths', 'course_code' => '123']);
    $csvData = array();
    foreach($array as $data){
        $csvData =  array($data['department_name'],$data['course_name'], $data['course_code']); 
        fputcsv($csvFile, $csvData);
    }
    fclose($csvFile);
    return true;
}   

function workFlow(){
    $csvDownload = array_to_csv_download();
    echo "<pre>";
    var_dump($csvDownload);die;
}

function test_function() {
    $email_type = isset($_REQUEST['email_type']) ? $_REQUEST['email_type']  : 0;
    if($email_type == 0){
        return get_generic_response(404, null, "Email Type not found!");
    }
    if($email_type == 2){

    }

    $notification_type="3";
        $fromEmail = isset($_GET['fromEmail']) && ($_GET['fromEmail']) ? ($_GET['fromEmail']) : 0;
    
        if ($fromEmail == 0) {
            return get_generic_response(404, null, "fromEmail not found!");
        }
        $fromName = isset($_GET['fromName']) && ($_GET['fromName']) ? ($_GET['fromName']) : 0;
        if ($fromName == 0) {
            return get_generic_response(404, null, "fromName not found!");
        }
        $fromUserId = isset($_GET['fromUserId']) && ($_GET['fromUserId']) ? ($_GET['fromUserId']) : 0;
        if ($fromUserId == 0) {
            return get_generic_response(404, null, "fromUserId not found!");
        }
        $toEmail = isset($_GET['toEmail']) && count($_GET['toEmail']) ? $_GET['toEmail'] : 0;
        if ($toEmail == 0) {
            return get_generic_response(404, null, "toEmail not found!");
        }
        $toName = isset($_GET['toName']) && count($_GET['toName']) ? $_GET['toName'] : 0;
        if ($toName == 0) {
            return get_generic_response(404, null, "toName not found!");
        }

        $toUserId = isset($_GET['toUserId']) && count($_GET['toUserId']) ? $_GET['toUserId'] : 0;
        if ($toUserId == 0) {
            return get_generic_response(404, null, "toUserId not found!");
        }
        
        $subject = isset($_GET['subject']) && ($_GET['subject']) ? $_GET['subject'] : 0;
        if ($subject == 0) {
            return get_generic_response(404, null, "subject not found!");
        }
        $message = isset($_GET['message']) && ($_GET['message']) ? $_GET['message'] : 0;
        if ($message == 0) {
            return get_generic_response(404, null, "message not found!");
        }

        $action_value = isset($_GET['action_value']) && ($_GET['action_value']) ? $_GET['action_value'] : '';
        
        $action_url = isset($_GET['action_url']) && ($_GET['action_url']) ? $_GET['action_url'] : '';
  
        $notification_type = 1;
        $notifier_history_id = 1;

        $toEmailCount = count($toEmail);
        for($i = 0; $i<($toEmailCount); $i++)
        {
            // echo $i+1;
            $params = array(    'method' => 'sendEmail', 
                'fromEmail' => $fromEmail,
                'fromName' => $fromName, 
                'fromUserId' => $fromUserId,
                'toEmail' => $toEmail[$i], 
                'toName' => $toName[$i],
                'toUserId' => $toUserId[$i], 
                'subject' => $subject,
                'message' => $message, 
                'action_value' => $action_value,
                'action_url' => $action_url,
                'notification_type' => 1                        
            );
            $callAPI = json_decode( CallAPI( 'GET', QUIKLRN_API_URL, $params ), true );
            var_dump($callAPI);
        }

        die;

    echo "<pre>";
    var_dump($aa);die;
}

