<?php
session_start();

if (isset($_SESSION['userid'])) {
    require_once('../../db.php');
    require_once('../../shared/functionsinc.php');
    $userId = $_SESSION['userid'];
    $userLayer = $_SESSION['layer'];
    $property_id = $_SESSION['property'];
    if (isset($_POST["my_call_flow_data"]) && $_POST["my_call_flow_data"] == "call_flow") {
        $update_call_flow = $_POST['update_call_flow'];
        $is_first_default_message = 0;
        $is_first_upload_audio = 0;
        $is_first_record_audio = 0;
        $is_first_textto_speech = 0;
        $first_audio_file = "";
        $first_text = "";

        $uploadDir = '../../../../data/audio_messages/';

        $first_message_vocie_select = "man";
        $no_response_vocie_select = "man";
        $press2_vocie_select = "man";
        $press2_auth_vocie_select = "man";
        $press2_auth_ack_vocie_select = "man";
        $press2_auth_access_vocie_select = "man";
        $press2_auth_denied_vocie_select = "man";
        $advertisement_vocie_select = "man";
        $connected_no_answer_vocie_select = "man";
        $connected_hangup_vocie_select = "man";
        $is_beep_vocie_select = "man";
        $is_access_vocie_select = "man";

        $call_flow_first_message = $_POST['call_flow_first_message'];
        if($call_flow_first_message == "is_first_default_message"){
        	$is_first_default_message = 1;
        	$first_audio_file = "Welcome-to-Our-Home.wav";
        }
        else if($call_flow_first_message == "is_first_upload_audio"){
        	$is_first_upload_audio = 1;
        	$file_name = $_FILES['call_flow_first_audio_field'];
            $fileName = $file_name['name'];
            if(empty($fileName) && $update_call_flow == 1){
                $first_audio_file = $_POST['is_first_upload_audio_field_hidden'];
            }
            else{
                $first_audio_file = processSingleFile($file_name);
            }
        }
        else if($call_flow_first_message == "is_first_record_audio"){
        	$is_first_record_audio = 1;
        	$audioData = $_POST['call_flow_first_audio_audio_data'];
            if(empty($audioData) && $update_call_flow == 1){
                $first_audio_file = $_POST['update_first_record_audio_field'];
            }
            else{
    		    $audioData = str_replace('data:audio/wav;base64,', '', $audioData);
    		    $audioData = base64_decode($audioData);
    		    $first_audio_file = 'recording_' . bin2hex(random_bytes(16)) . '.wav';
    		    $filePath = $uploadDir . $first_audio_file;

    		    file_put_contents($filePath, $audioData);
            }
        }
        else if($call_flow_first_message == "is_first_textto_speech"){
        	$is_first_textto_speech = 1;
        	$first_text = $_POST['is_first_textto_speech_field'];
            $first_message_vocie_select = $_POST['first_message_vocie_select'];
        }

        $call_flow_first_end_message = $_POST['call_flow_first_end_message'];
        
        $is_no_response_default_message = 0;
        $is_no_response_upload_audio = 0;
        $is_no_response_record_audio = 0;
        $is_no_response_textto_speech = 0;
        $no_response_audio_file = "";
        $no_response_text = "";

        if($call_flow_first_end_message == "is_no_response_default_message"){
        	$is_no_response_default_message = 1;
        	$no_response_audio_file = "good_bye.wav";
        }
        else if($call_flow_first_end_message == "is_no_response_upload_audio"){
        	$is_no_response_upload_audio = 1;
        	$file_name = $_FILES['is_no_response_upload_audio_field'];
            $fileName = $file_name['name'];
        	if(empty($fileName) && $update_call_flow == 1){
                $no_response_audio_file = $_POST['is_no_response_upload_audio_field_hidden'];
            }
            else{
                $no_response_audio_file = processSingleFile($file_name);
            }
        }
        else if($call_flow_first_end_message == "is_no_response_record_audio"){
        	$is_no_response_record_audio = 1;
        	$audioData = $_POST['call_flow_first_end_audio_audio_data'];
            if(empty($audioData) && $update_call_flow == 1){
                $no_response_audio_file = $_POST['is_no_response_record_audio_field_hidden'];
                
            }
            else{
    		    $audioData = str_replace('data:audio/wav;base64,', '', $audioData);
    		    $audioData = base64_decode($audioData);
    		    $no_response_audio_file = 'recording_' . bin2hex(random_bytes(16)) . '.wav';
    		    $filePath = $uploadDir . $no_response_audio_file;
    		    file_put_contents($filePath, $audioData);
            }

        }
        else if($call_flow_first_end_message == "is_no_response_textto_speech"){
        	$is_no_response_textto_speech = 1;
        	$no_response_text = $_POST['is_no_response_textto_speech_field'];
            $no_response_vocie_select = $_POST['no_response_vocie_select'];
        }

        $is_hang_up_message = $_POST['is_hang_up_message'];
        
        $press2_message = $_POST['press2_message'];
        $press2_default_message = 0;
        $press2_upload_audio = 0;
        $press2_record_audio = 0;
        $press2_textto_speech = 0;
        $press2_audio_file = "";
        $press2_text = "";

        if($press2_message == "press2_default_message"){
            $press2_default_message = 1;
            $press2_audio_file = "enter_your_code_now.wav";
        }
        else if($press2_message == "press2_upload_audio"){
            $press2_upload_audio = 1;
            $file_name = $_FILES['press2_upload_audio_field'];
            $fileName = $file_name['name'];
            if(empty($fileName) && $update_call_flow == 1){
                $press2_audio_file = $_POST['press2_upload_audio_field_hidden'];
            }
            else{
                $press2_audio_file = processSingleFile($file_name);
            }
        }
        else if($press2_message == "press2_record_audio"){
            $press2_record_audio = 1;
            $audioData = $_POST['press2_audio_audio_data'];
            if(empty($audioData) && $update_call_flow == 1){
                $press2_audio_file = $_POST['press2_record_audio_field_hidden'];
                
            }
            else{
                $audioData = str_replace('data:audio/wav;base64,', '', $audioData);
                $audioData = base64_decode($audioData);
                $press2_audio_file = 'recording_' . bin2hex(random_bytes(16)) . '.wav';
                $filePath = $uploadDir . $press2_audio_file;
                file_put_contents($filePath, $audioData);
            }

        }
        else if($press2_message == "press2_textto_speech"){
            $press2_textto_speech = 1;
            $press2_text = $_POST['press2_textto_speech_field'];
            $press2_vocie_select = $_POST['press2_vocie_select'];
        }

        $press2_auth_code_message = $_POST['press2_auth_code_message'];
        $press2_auth_code_default_message = 0;
        $press2_auth_code_upload_audio = 0;
        $press2_auth_code_record_audio = 0;
        $press2_auth_code_textto_speech = 0;
        $press2_auth_code_audio_file = "";
        $press2_auth_code_text = "";

        if($press2_auth_code_message == "press2_auth_code_default_message"){
            $press2_auth_code_default_message = 1;
            $press2_auth_code_audio_file = "please_hold.wav";
        }
        else if($press2_auth_code_message == "press2_auth_code_upload_audio"){
            $press2_auth_code_upload_audio = 1;
            $file_name = $_FILES['press2_auth_code_upload_audio_field'];
            $fileName = $file_name['name'];
            if(empty($fileName) && $update_call_flow == 1){
                $press2_auth_code_audio_file = $_POST['_auth_code'];
                $press2_auth_code_audio_file = $_POST['press2_auth_code_upload_audio_field_hidden'];
            }
            else{
                $press2_auth_code_audio_file = processSingleFile($file_name);
            }
        }
        else if($press2_auth_code_message == "press2_auth_code_record_audio"){
            $press2_auth_code_record_audio = 1;
            $audioData = $_POST['press2_auth_code_audio_audio_data'];
            if(empty($audioData) && $update_call_flow == 1){
                $press2_auth_code_audio_file = $_POST['press2_auth_code_record_audio_field_hidden'];
                
            }
            else{
                $audioData = str_replace('data:audio/wav;base64,', '', $audioData);
                $audioData = base64_decode($audioData);
                $press2_auth_code_audio_file = 'recording_' . bin2hex(random_bytes(16)) . '.wav';
                $filePath = $uploadDir . $press2_auth_code_audio_file;
                file_put_contents($filePath, $audioData);
            }

        }
        else if($press2_auth_code_message == "press2_auth_code_textto_speech"){
            $press2_auth_code_textto_speech = 1;
            $press2_auth_code_text = $_POST['press2_auth_code_textto_speech_field'];
            $press2_auth_vocie_select = $_POST['press2_auth_vocie_select'];
        }

        $press2_auth_code_ack_message = $_POST['press2_auth_code_ack_message'];
        $press2_auth_code_ack_default_message = 0;
        $press2_auth_code_ack_upload_audio = 0;
        $press2_auth_code_ack_record_audio = 0;
        $press2_auth_code_ack_textto_speech = 0;
        $press2_auth_code_ack_audio_file = "";
        $press2_auth_code_ack_text = "";

        if($press2_auth_code_ack_message == "press2_auth_code_ack_default_message"){
            $press2_auth_code_ack_default_message = 1;
            $press2_auth_code_ack_audio_file = "open_command_sent.wav";
        }
        else if($press2_auth_code_ack_message == "press2_auth_code_ack_upload_audio"){
            $press2_auth_code_ack_upload_audio = 1;
            $file_name = $_FILES['press2_auth_code_ack_upload_audio_field'];
            $fileName = $file_name['name'];
            if(empty($fileName) && $update_call_flow == 1){
                $press2_auth_code_ack_audio_file = $_POST['_auth_code'];
                $press2_auth_code_ack_audio_file = $_POST['press2_auth_code_ack_upload_audio_field_hidden'];
            }
            else{
                $press2_auth_code_ack_audio_file = processSingleFile($file_name);
            }
        }
        else if($press2_auth_code_ack_message == "press2_auth_code_ack_record_audio"){
            $press2_auth_code_ack_record_audio = 1;
            $audioData = $_POST['press2_auth_code_ack_audio_audio_data'];
            if(empty($audioData) && $update_call_flow == 1){
                $press2_auth_code_ack_audio_file = $_POST['press2_auth_code_ack_record_audio_field_hidden'];
                
            }
            else{
                $audioData = str_replace('data:audio/wav;base64,', '', $audioData);
                $audioData = base64_decode($audioData);
                $press2_auth_code_ack_audio_file = 'recording_' . bin2hex(random_bytes(16)) . '.wav';
                $filePath = $uploadDir . $press2_auth_code_ack_audio_file;
                file_put_contents($filePath, $audioData);
            }

        }
        else if($press2_auth_code_ack_message == "press2_auth_code_ack_textto_speech"){
            $press2_auth_code_ack_textto_speech = 1;
            $press2_auth_code_ack_text = $_POST['press2_auth_code_ack_textto_speech_field'];
            $press2_auth_ack_vocie_select = $_POST['press2_auth_ack_vocie_select'];
        }

        $press2_auth_code_access_message = $_POST['press2_auth_code_access_message'];

        $press2_auth_code_access_default_message = 0;
        $press2_auth_code_access_upload_audio = 0;
        $press2_auth_code_access_record_audio = 0;
        $press2_auth_code_access_textto_speech = 0;
        $press2_auth_code_access_audio_file = "";
        $press2_auth_code_access_text = "";

        if($press2_auth_code_access_message == "press2_auth_code_access_default_message"){
            $press2_auth_code_access_default_message = 1;
            $press2_auth_code_access_audio_file = "access_granted.wav";
        }
        else if($press2_auth_code_access_message == "press2_auth_code_access_upload_audio"){
            $press2_auth_code_access_upload_audio = 1;
            $file_name = $_FILES['press2_auth_code_access_upload_audio_field'];
            $fileName = $file_name['name'];
            if(empty($fileName) && $update_call_flow == 1){
                $press2_auth_code_access_audio_file = $_POST['_auth_code'];
                $press2_auth_code_access_audio_file = $_POST['press2_auth_code_access_upload_audio_field_hidden'];
            }
            else{
                $press2_auth_code_access_audio_file = processSingleFile($file_name);
            }
        }
        else if($press2_auth_code_access_message == "press2_auth_code_access_record_audio"){
            $press2_auth_code_access_record_audio = 1;
            $audioData = $_POST['press2_auth_code_access_audio_audio_data'];
            if(empty($audioData) && $update_call_flow == 1){
                $press2_auth_code_access_audio_file = $_POST['press2_auth_code_access_record_audio_field_hidden'];
                
            }
            else{
                $audioData = str_replace('data:audio/wav;base64,', '', $audioData);
                $audioData = base64_decode($audioData);
                $press2_auth_code_access_audio_file = 'recording_' . bin2hex(random_bytes(16)) . '.wav';
                $filePath = $uploadDir . $press2_auth_code_access_audio_file;
                file_put_contents($filePath, $audioData);
            }

        }
        else if($press2_auth_code_access_message == "press2_auth_code_access_textto_speech"){
            $press2_auth_code_access_textto_speech = 1;
            $press2_auth_code_access_text = $_POST['press2_auth_code_access_textto_speech_field'];
            $press2_auth_access_vocie_select = $_POST['press2_auth_access_vocie_select'];
        }

        $press2_auth_code_denied_message = $_POST['press2_auth_code_denied_message'];
        $press2_auth_code_denied_default_message = 0;
        $press2_auth_code_denied_upload_audio = 0;
        $press2_auth_code_denied_record_audio = 0;
        $press2_auth_code_denied_textto_speech = 0;
        $press2_auth_code_denied_audio_file = "";
        $press2_auth_code_denied_text = "";

        if($press2_auth_code_denied_message == "press2_auth_code_denied_default_message"){
            $press2_auth_code_denied_default_message = 1;
            $press2_auth_code_denied_audio_file = "access_denied.wav";
        }
        else if($press2_auth_code_denied_message == "press2_auth_code_denied_upload_audio"){
            $press2_auth_code_denied_upload_audio = 1;
            $file_name = $_FILES['press2_auth_code_denied_upload_audio_field'];
            $fileName = $file_name['name'];
            if(empty($fileName) && $update_call_flow == 1){
                $press2_auth_code_denied_audio_file = $_POST['_auth_code'];
                $press2_auth_code_denied_audio_file = $_POST['press2_auth_code_denied_upload_audio_field_hidden'];
            }
            else{
                $press2_auth_code_denied_audio_file = processSingleFile($file_name);
            }
        }
        else if($press2_auth_code_denied_message == "press2_auth_code_denied_record_audio"){
            $press2_auth_code_denied_record_audio = 1;
            $audioData = $_POST['press2_auth_code_denied_audio_audio_data'];
            if(empty($audioData) && $update_call_flow == 1){
                $press2_auth_code_denied_audio_file = $_POST['press2_auth_code_denied_record_audio_field_hidden'];
                
            }
            else{
                $audioData = str_replace('data:audio/wav;base64,', '', $audioData);
                $audioData = base64_decode($audioData);
                $press2_auth_code_denied_audio_file = 'recording_' . bin2hex(random_bytes(16)) . '.wav';
                $filePath = $uploadDir . $press2_auth_code_denied_audio_file;
                file_put_contents($filePath, $audioData);
            }

        }
        else if($press2_auth_code_denied_message == "press2_auth_code_denied_textto_speech"){
            $press2_auth_code_denied_textto_speech = 1;
            $press2_auth_code_denied_text = $_POST['press2_auth_code_denied_textto_speech_field'];
            $press2_auth_denied_vocie_select = $_POST['press2_auth_denied_vocie_select'];
        }

        $is_authorized_message = $_POST['is_authorized_message'];
        $is_pre_notify_message = $_POST['is_pre_notify_message'];

        $advertisement_first_message = $_POST['advertisement_first_message'];
        $is_advertisement_default_message = 0;
        $is_advertisement_upload_audio = 0;
        $is_advertisement_record_audio = 0;
        $is_advertisement_textto_speech = 0;
        $advertisement_audio_file = "";
        $advertisement_text = "";

        if($advertisement_first_message == "is_advertisement_default_message"){
        	$is_advertisement_default_message = 1;
        	$advertisement_audio_file = "You-are-using-invisible-intercom-ADVERTSING.wav";
        }
        else if($advertisement_first_message == "is_advertisement_upload_audio"){
        	$is_advertisement_upload_audio = 1;
        	$file_name = $_FILES['is_advertisement_upload_audio_field'];
            $fileName = $file_name['name'];
            if(empty($fileName) && $update_call_flow == 1){
                $advertisement_audio_file = $_POST['adv_aud_file_hidden'];
            }
            else{
                $advertisement_audio_file = processSingleFile($file_name);
            }
        }
        else if($advertisement_first_message == "is_advertisement_record_audio"){
        	$is_advertisement_record_audio = 1;
        	$audioData = $_POST['is_advertisement_record_audio_field'];
            if(empty($audioData) && $update_call_flow == 1){
                $advertisement_audio_file = $_POST['adv_rec_hidden'];
                
            }
            else{
    		    $audioData = str_replace('data:audio/wav;base64,', '', $audioData);
    		    $audioData = base64_decode($audioData);
    		    $advertisement_audio_file = 'recording_' . bin2hex(random_bytes(16)) . '.wav';
    		    $filePath = $uploadDir . $advertisement_audio_file;
    		    file_put_contents($filePath, $audioData);
            }

        }
        else if($advertisement_first_message == "is_advertisement_textto_speech"){
        	$is_advertisement_textto_speech = 1;
        	$advertisement_text = $_POST['is_advertisement_textto_speech_field'];
            $advertisement_vocie_select = $_POST['advertisement_vocie_select'];
        }

        $connect_to_me_message = $_POST['connect_to_me_message'];
        $is_connected_no_answer_default_message = 0;
        $is_connected_no_answer_upload_audio = 0;
        $is_connected_no_answer_record_audio = 0;
        $is_connected_no_answer_textto_speech = 0;
        $connected_no_answer_audio_file = "";
        $connected_no_answer_text = "";

        if($connect_to_me_message == "is_connected_no_answer_default_message"){
            $is_connected_no_answer_default_message = 1;
            $connected_no_answer_audio_file = "no_one_available.wav";
        }
        else if($connect_to_me_message == "is_connected_no_answer_upload_audio"){
            $is_connected_no_answer_upload_audio = 1;
            $file_name = $_FILES['is_connected_no_answer_upload_audio_field'];
            $fileName = $file_name['name'];
            if(empty($fileName) && $update_call_flow == 1){
                $connected_no_answer_audio_file = $_POST['connect_aud_file_hidden'];
            }
            else{
                $connected_no_answer_audio_file = processSingleFile($file_name);
            }
        }
        else if($connect_to_me_message == "is_connected_no_answer_record_audio"){
            $is_connected_no_answer_record_audio = 1;
            $audioData = $_POST['is_connected_no_answer_record_audio_field'];
            if(empty($audioData) && $update_call_flow == 1){
                $connected_no_answer_audio_file = $_POST['connect_rec_hidden'];
            }
            else{
                $audioData = str_replace('data:audio/wav;base64,', '', $audioData);
                $audioData = base64_decode($audioData);
                $connected_no_answer_audio_file = 'recording_' . bin2hex(random_bytes(16)) . '.wav';
                $filePath = $uploadDir . $connected_no_answer_audio_file;
                file_put_contents($filePath, $audioData);
            }

        }
        else if($connect_to_me_message == "is_connected_no_answer_textto_speech"){
            $is_connected_no_answer_textto_speech = 1;
            $connected_no_answer_text = $_POST['is_connected_no_answer_textto_speech_field'];
            $connected_no_answer_vocie_select = $_POST['connected_no_answer_vocie_select'];
        }

        $is_connected_hangeup_message = 0;
        $is_connected_hangeup_message = $_POST['is_connected_hangeup_message'];
        
        if($is_connected_hangeup_message == "hang_up_message_play"){
            $is_connected_hangeup_message = 1;
        }
        else if($is_connected_hangeup_message == "hang_up_call_terminate"){
            $is_connected_hangeup_message = 0;
        }

        $hang_up_record_audio = $_POST['hang_up_record_audio'];
        $is_connected_hangeup_default_message = 0;
        $is_connected_hangeup_upload_audio = 0;
        $is_connected_hangeup_record_audio = 0;
        $is_connected_hangeup_textto_speech = 0;
        $connected_hangeup_audio_file = "";
        $connected_hangeup_text = "";

        if($hang_up_record_audio == "is_connected_hangeup_default_message"){
            $is_connected_hangeup_default_message = 1;
            $connected_hangeup_audio_file = "access_denied.wav";
        }
        else if($hang_up_record_audio == "is_connected_hangeup_upload_audio"){
            $is_connected_hangeup_upload_audio = 1;
            $file_name = $_FILES['is_connected_hangeup_upload_audio_field'];
            $fileName = $file_name['name'];
            if(empty($fileName) && $update_call_flow == 1){
                $connected_hangeup_audio_file = $_POST['hang_aud_file_hidden'];
            }
            else{
                $connected_hangeup_audio_file = processSingleFile($file_name);
            }
        }
        else if($hang_up_record_audio == "is_connected_hangeup_record_audio"){
            $is_connected_hangeup_record_audio = 1;
            $audioData = $_POST['is_connected_hangeup_record_audio_field'];
            if(empty($audioData) && $update_call_flow == 1){
                $connected_hangeup_audio_file = $_POST['hang_rec_aud_hidden'];
            }
            else{
                $audioData = str_replace('data:audio/wav;base64,', '', $audioData);
                $audioData = base64_decode($audioData);
                $connected_hangeup_audio_file = 'recording_' . bin2hex(random_bytes(16)) . '.wav';
                $filePath = $uploadDir . $connected_hangeup_audio_file;
                file_put_contents($filePath, $audioData);
            }

        }
        else if($hang_up_record_audio == "is_connected_hangeup_textto_speech"){
            $is_connected_hangeup_textto_speech = 1;
            $connected_hangeup_text = $_POST['is_connected_hangeup_textto_speech_field'];
            $connected_hangup_vocie_select = $_POST['connected_hangup_vocie_select'];
        }

        $open_command_first_check = $_POST['open_command_first_check'];
        $is_beep_default_message = 0;
        $is_beep_upload_audio = 0;
        $is_beep_record_audio = 0;
        $is_beep_textto_speech = 0;
        $beep_audio_file = "";
        $beep_text = "";

        if($open_command_first_check == "is_beep_default_message"){
            $is_beep_default_message = 1;
            $beep_audio_file = "open_command_sent.wav";
        }
        else if($open_command_first_check == "is_beep_upload_audio"){
            $is_beep_upload_audio = 1;
            $file_name = $_FILES['is_beep_upload_audio_field'];
            $fileName = $file_name['name'];
            if(empty($fileName) && $update_call_flow == 1){
                $beep_audio_file = $_POST['beep_aud_file_hidden'];
            }
            else{
                $beep_audio_file = processSingleFile($file_name);
            }
        }
        else if($open_command_first_check == "is_beep_record_audio"){
            $is_beep_record_audio = 1;
            $audioData = $_POST['is_beep_record_audio_field'];
            if(empty($audioData) && $update_call_flow == 1){
                $beep_audio_file = $_POST['beep_aud_rec_hidden'];
            }
            else{
                $audioData = str_replace('data:audio/wav;base64,', '', $audioData);
                $audioData = base64_decode($audioData);
                $beep_audio_file = 'recording_' . bin2hex(random_bytes(16)) . '.wav';
                $filePath = $uploadDir . $beep_audio_file;
                file_put_contents($filePath, $audioData);
            }

        }
        else if($open_command_first_check == "is_beep_textto_speech"){
            $is_beep_textto_speech = 1;
            $beep_text = $_POST['is_beep_textto_speech_field'];
            $is_beep_vocie_select = $_POST['is_beep_vocie_select'];
        }
        $access_first_options = $_POST['access_first_options'];
        $is_access_default_message = 0;
        $is_access_upload_audio = 0;
        $is_access_record_audio = 0;
        $is_access_textto_speech = 0;
        $access_audio_file = "";
        $access_text = "";

        if($access_first_options == "is_access_default_message"){
            $is_access_default_message = 1;
            $access_audio_file = "You-may-now-access-the-property.wav";
        }
        else if($access_first_options == "is_access_upload_audio"){
            $is_access_upload_audio = 1;
            $file_name = $_FILES['is_access_upload_audio_field'];
            $fileName = $file_name['name'];
            if(empty($fileName) && $update_call_flow == 1){
                $access_audio_file = $_POST['access_aud_file_hidden'];
            }
            else{
                $access_audio_file = processSingleFile($file_name);
            }
        }
        else if($access_first_options == "is_access_record_audio"){
            $is_access_record_audio = 1;
            $audioData = $_POST['is_access_record_audio_field'];
            if(empty($audioData) && $update_call_flow == 1){
                $access_audio_file = $_POST['access_rec_hidden'];
            }
            else{
                $audioData = str_replace('data:audio/wav;base64,', '', $audioData);
                $audioData = base64_decode($audioData);
                $access_audio_file = 'recording_' . bin2hex(random_bytes(16)) . '.wav';
                $filePath = $uploadDir . $access_audio_file;
                file_put_contents($filePath, $audioData);
            }
        }
        else if($access_first_options == "is_access_textto_speech"){
            $is_access_textto_speech = 1;
            $access_text = $_POST['is_access_textto_speech_field'];
            $is_access_vocie_select = $_POST['is_access_vocie_select'];
        }

        $is_grant_access_detail_message = $_POST['is_grant_access_detail_message'];
        $is_hang_up_call_terminate = $_POST['is_hang_up_call_terminate'];
        $end_call_time = $_POST['end_call_time'];
        $call_duration_after_user_connect = $_POST['call_duration_after_user_connect'];
        $call_flow_id = $_POST['call_flow_id'];

        $first_number = $_POST['first_number'];
        $second_number = $_POST['second_number'];
        $third_number = $_POST['third_number'];
        $first_number_disturb = 0;
        $second_number_disturb = 0;
        $third_number_disturb = 0;
        $door_key_option = 0;
                
        if(isset($_POST['first_number_disturb']) && $_POST['first_number_disturb'] == 1)
        {
            $first_number_disturb = 1;    
        }
        if(isset($_POST['second_number_disturb']) && $_POST['second_number_disturb'] == 1)
        {
            $second_number_disturb = 1;    
        }
        if(isset($_POST['third_number_disturb']) && $_POST['third_number_disturb'] == 1)
        {
            $third_number_disturb = 1;
        }
        if(isset($_POST['door_key_option']) && $_POST['door_key_option'] == 1)
        {
            $door_key_option = 1;
        }
        $a = uploadCallFlowData($pdo, $userId, $property_id, $userLayer, $is_first_default_message, $is_first_upload_audio, $is_first_record_audio, $is_first_textto_speech, $first_audio_file, $first_text, $is_no_response_default_message, $is_no_response_upload_audio, $is_no_response_record_audio, $is_no_response_textto_speech, $no_response_audio_file, $no_response_text, $is_hang_up_message, $press2_default_message, $press2_upload_audio, $press2_record_audio, $press2_textto_speech, $press2_audio_file, $press2_text, $press2_auth_code_default_message, $press2_auth_code_upload_audio, $press2_auth_code_record_audio, $press2_auth_code_textto_speech, $press2_auth_code_audio_file, $press2_auth_code_text, $press2_auth_code_ack_default_message, $press2_auth_code_ack_upload_audio, $press2_auth_code_ack_record_audio, $press2_auth_code_ack_textto_speech, $press2_auth_code_ack_audio_file, $press2_auth_code_ack_text, $press2_auth_code_access_default_message, $press2_auth_code_access_upload_audio, $press2_auth_code_access_record_audio, $press2_auth_code_access_textto_speech, $press2_auth_code_access_audio_file, $press2_auth_code_access_text, $press2_auth_code_denied_default_message, $press2_auth_code_denied_upload_audio, $press2_auth_code_denied_record_audio, $press2_auth_code_denied_textto_speech, $press2_auth_code_denied_audio_file, $press2_auth_code_denied_text, $is_authorized_message, $is_pre_notify_message, $is_advertisement_default_message, $is_advertisement_upload_audio, $is_advertisement_record_audio, $is_advertisement_textto_speech, $advertisement_audio_file, $advertisement_text, $is_connected_no_answer_default_message, $is_connected_no_answer_upload_audio, $is_connected_no_answer_record_audio, $is_connected_no_answer_textto_speech, $connected_no_answer_audio_file, $connected_no_answer_text, $is_connected_hangeup_message, $is_connected_hangeup_default_message, $is_connected_hangeup_upload_audio, $is_connected_hangeup_record_audio, $is_connected_hangeup_textto_speech, $connected_hangeup_audio_file, $connected_hangeup_text, $door_key_option, $is_beep_default_message, $is_beep_upload_audio, $is_beep_record_audio, $is_beep_textto_speech, $beep_audio_file, $beep_text, $is_access_default_message, $is_access_upload_audio, $is_access_record_audio, $is_access_textto_speech, $access_audio_file, $access_text, $is_grant_access_detail_message, $is_hang_up_call_terminate,$end_call_time, $call_duration_after_user_connect, $update_call_flow, $call_flow_id, $first_number, $second_number, $third_number, $first_number_disturb, $second_number_disturb, $third_number_disturb, $first_message_vocie_select, $no_response_vocie_select, $press2_vocie_select, $press2_auth_vocie_select, $press2_auth_ack_vocie_select, $press2_auth_access_vocie_select, $press2_auth_denied_vocie_select, $advertisement_vocie_select, $connected_no_answer_vocie_select, $connected_hangup_vocie_select, $is_beep_vocie_select, $is_access_vocie_select);
          
        if (!headers_sent()) {
            header( "Location: ../../dashboard.php?tab=property_builder&active=5");
        }

    }
} else {
    if (!headers_sent()) {
        header("location: ../../login.php");
    }
    exit();
}

