<?

switch ($_GET["action"]) {
    case 'list':
        require_once("mail_log_listform.inc.php");
        break;
    case 'card':
        mail_log_card();
        break;
    case 'send':
        send_mail_log(TRUE);
        break;
    case 'send_list' :
        send_mail_log(FALSE);
        break;
    case 'filter' :
        require_once("mail_log_listform.inc.php");
        break;
    case 'show_survey_contactform':
            survey_card();
            break;
    case 'loadStats':
        require_once(CMS_PATH . "admin/statistic.inc.php");  
        break; 
    case 'load_suboption':
            load_suboption();
            break;
    case 'load_filtered_data':
        load_filtered_data();
        break;  
    case 'show_person_mail':
        show_person_mail();
        break;          
    default:
        require_once("mail_log_listform.inc.php");
        break;
}

function mail_log_card( $messages = array() ) {
    if ($_POST["input_id"] <> '') {
        $query  = "SELECT *, DATE_FORMAT(send_date,'%d.%m.%Y') AS format_send_date, DATE_FORMAT(send_date,'%H:%i') AS format_send_time FROM main_mail_log WHERE id = '" . $_POST["input_id"] . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        if (@mysqli_num_rows($result) == 1) {
            $input_mail_log = @mysqli_fetch_array($result);
            require_once("mail_log_cardform.inc.php");
        }
    } else {
        require_once("mail_log_listform.inc.php");
    }
}
function survey_card( ) {
    if ($_POST["data-inputid"] <> '') {
        $query  = "SELECT * FROM contactform_header WHERE id = '" . $_POST["data-inputid"] . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        if (@mysqli_num_rows($result) == 1) {
            $contact_header = @mysqli_fetch_array($result);
            require_once("survey_log_cardform.inc.php");
        }
    } else {
        require_once("mail_log_listform.inc.php");
    }
}
function show_person_mail(){
    if ($_POST["input_line_id"] <> '') {
        $query  = "SELECT ml.*, DATE_FORMAT(send_date,'%d.%m.%Y') AS format_send_date, DATE_FORMAT(send_date,'%H:%i') AS format_send_time FROM main_mail_log as ml INNER JOIN survey_persons as sp on ml.spid = sp.id WHERE sp.id = '" . $_POST["input_line_id"] . "' LIMIT 1";
        print_r($_REQUEST['header_id']);
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        if (@mysqli_num_rows($result) == 1) {
            $input_mail_log = @mysqli_fetch_array($result);
            require_once("mail_log_cardform.inc.php");
        }
    } else {
        require_once(CMS_PATH . "admin/statistic.inc.php");  
    }

}
function load_filtered_data(){
     if(isset($_POST['reportrange']) && isset($_POST['reportrange'])){
        $header_id = $_POST['header_id'];
        $range = explode("-", $_POST['reportrange']);
        $date1 = $range[0];
        $date2 = $range[1];
        $date1 = date('Y-m-d', strtotime($date1));
        $date2 = date('Y-m-d', strtotime($date2));
         $line_id = '';
         if(isset($_POST['select_option'])){
            $line_id = " AND line_id = '".$_POST['select_option']."' ";
         }
         $clo_id = '';
         if(isset($_POST['select_sub_option'])){
            $clo_id = " AND clo_id = '".$_POST['select_sub_option']."' ";
         }
         $sql = "SELECT MONTH(created_date) as month, count(*) NUM  FROM survey_answers WHERE header_id = '" . $header_id . "' AND  DATE(created_date) BETWEEN '".$date1."' AND '".$date2."' ".$line_id.$clo_id."  GROUP BY month ";
         
         $label = array(); 
            $dataArrayPie = array();
            $bgArray = array();
            for($i = 1;$i<=12;$i++){
                $dataArrayPie[$i] = 0;
                 
            }

            $result =  @mysqli_query($GLOBALS['mysql_con'], $sql);

            while($row = @mysqli_fetch_array($result)){
                $dataArrayPie[$row['month']] = $row['NUM'];
                
            }
            print_r(json_encode($dataArrayPie));   
     }
}
function load_suboption(){
    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
    $html = '';    

    ?>
    <div class="label"><label><?= $translation->get('select_sub_option');?></label></div>
        <div class="input">
            <select class="bigselect" name="select_sub_option" id="select_sub_option">
                <option disabled selected>----</option>
        </div>
        <?php 
            $queryOpt = "SELECT * FROM contactform_line_option WHERE  line_id = '".$_POST['line_id']."'";
            $resultOpt = @mysqli_query($GLOBALS['mysql_con'], $queryOpt);
            echo $queryOpt;
            while($row = @mysqli_fetch_array($resultOpt)){
                echo '<option value="'. $row['id'].'" data-type="'. $row['type'].'" >'.$row['option_string'].'</option>';
            }
            
        ?>
        </select>
<? }

function send_mail_log( $_card = TRUE ) {
    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
    $messages    = array();
    if ($_POST["input_id"] <> '') {
        $query  = "SELECT * FROM main_mail_log WHERE id = '" . $_POST["input_id"] . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        if (@mysqli_num_rows($result) == 1) {
            $mail = @mysqli_fetch_array($result);
            if (mail_create($mail["subject"], $mail["message"], $mail["from_adress"], $mail["to_adress"], $mail["to_name"], $mail["from_name"], $mail["html_mail"], $mail["attachment"], 0, '')) {
                mail_send();
                $messages[] = '<div class="successbox">' . $translation->get("send_again_successfull") . '</div>';
            }
        }
        if ($_card === TRUE) {
            mail_log_card($messages);
        } else {
            require_once("mail_log_listform.inc.php");
        }

    }
}

function mail_filter( $site_id, $language_id, $from_level = 1, $to_level = 3 ) {
    $current_filter = (isset($_POST['filter_days']) ? $_POST['filter_days'] : 7);
    $translation    = \DynCom\mysyde\common\classes\Registry::get("translation");

    echo "<div class=\"input\"><select onchange=\"filter_mails(this.value);\" class=\"bigselect\" name=\"page_filter\" id=\"page_filter\">";
    echo "<option value='1 WEEK' " . ($current_filter == '1 WEEK' ? 'selected="selected"' : '') . ">" . $translation->get("last_week") . "</option>";
    echo "<option value='1 MONTH' " . ($current_filter == '1 MONTH' ? 'selected="selected"' : '') . ">" . $translation->get("last_month") . "</option>";
    echo "<option value='1 YEAR' " . ($current_filter == '1 YEAR' ? 'selected="selected"' : '') . ">" . $translation->get("last_year") . "</option>";
    echo "<option value='100 YEAR' " . ($current_filter == '100 YEAR' ? 'selected="selected"' : '') . ">" . $translation->get("show_all") . "</option>";
    echo "</select>";
    echo "</div>";
}
