<?php
if(isset($_POST['insert']))
{
    switch ($_POST['insert'])
    {
        case 'sign-up':
            if($user->register($_POST['email'], $_POST['password'], $_POST['repassword'], 1, CLIENT_USER_KEY, $_POST['firstname'],$_POST['lastname']))
            {
                $reg = 0;
                $lastuser = $user->lastuser;
                if(isset($_POST['reginname']))
                {
                    $reg = 1;
                }
                $sql = "insert into client_details (phone,lga_id,street,customer_type,meternumber,
                            registered_in_name,date_created,state_id,user_id)
                        values (:phone,:lga,:street,:type,:meter,:reg,:date,:state,
                        :user)";
                $fields = array(
                    ':phone'=>$_POST['phone'],
                    ':lga'=>$_POST['lga'],
                    ':street'=>$_POST['street'],
                    ':type'=>$_POST['customertype'],
                    ':meter'=>$_POST['meterno'],
                    ':reg'=>$reg,
                    ':date'=>date('d-m-Y'),
                    ':state'=>$_POST['state'],
                    ':user'=>$lastuser,
                );
                $q=$con->insert_query($sql,$fields);
                if($q)
                {
                    $user->login($_POST['email'], $_POST['password']);
                    //send message
                    $body = "<p>Thank you for registering on IWIN. Click the link below to login.</p>";
                    $body .= '<a href="'.APP_URL.'login" style="display: inline-block; color: #fff; text-decoration:none; text-align: center; background-color: #0053a6; padding: 8px;">Login</a>';
                    $sent = SendMessage("Sign Up successful", $_POST['email'], "IWIN", $body, $con);
                    echo '<div class="alert alert-success">Sign up successful.</div>';
                    echo '<script>window.location="client-area"</script>';
                }
            }
            else
            {
                echo '<div class="alert alert-danger">';
                foreach($user->errormsg as $error)
                {
                    echo "<span>".$error."</span>";
                }
                echo '</div>';
            }
            break;
            
        case 'complaint':
            $sql = "insert into complaints (category_id,complaint,user_id,date_created,complaintno,ticket_number)
                    values (:cid,:comp,:user,:date,:no,:tkt)";
            $user_id = GetUserID($_SESSION['user_session'], $con);
            $complaintno = mt_rand(10000, 99999);
            $tktno = 'tx'.mt_rand(100000000,109999999);
            $fields = array(
                ':cid'=>$_POST['category'],
                ':comp'=>$_POST['message'],
                ':user'=>$user_id,
                ':date'=>date('Y-m-d H:i A'),
                ':no'=>$complaintno,
                ':tkt'=>$tktno
            );
            $q=$con->insert_query($sql,$fields);
            if($q)
            {
                if($_POST['change'] == 1)
                {
                    $reg = 0;
                    if(isset($_POST['reginname']))
                    {
                        $reg = 1;
                    }
                    $sql = "update client_details set street=:street,state_id=:state,lga_id=:lga,customer_type=:type,meternumber=:meter,registered_in_name=:reg where user_id=:user";
                    $fields = array(
                        ':street'=>$_POST['street'],
                        ':lga'=>$_POST['lga'],
                        ':state'=>$_POST['state'],
                        ':type'=>$_POST['customertype'],
                        ':meter'=>$_POST['meterno'],
                        ':reg'=>$reg,
                        ':user'=>$user_id,
                    );
                    $con->update_query($sql,$fields);
                }
                echo '<div class="alert alert-success">Complaint has been submitted successfully.</div>';
            }
            break;
            
        case 'schedule_for_later':

            // capture scheduled date
            $schedule_date = $_POST['scheduleDate'];

            // capture participant selection
            $participant_type = isset($_POST['participantType']) ? $_POST['participantType'] : 'all';
            $participant_data = array();

            // gather participant data based on type
            if($participant_type == 'individual'){

                $participant_data['individuals'] = isset($_POST['individuals']) ? $_POST['individuals'] : array();

            } elseif($participant_type == 'department'){

                $participant_data['departments'] = isset($_POST['departments']) ? $_POST['departments'] : array();

            } elseif($participant_type == 'position'){

                $participant_data['positions'] = isset($_POST['positions']) ? $_POST['positions'] : array();

            } elseif($participant_type == 'dob'){

                $participant_data['dob_start'] = isset($_POST['dob_start']) ? $_POST['dob_start'] : null;
                $participant_data['dob_end'] = isset($_POST['dob_end']) ? $_POST['dob_end'] : null;

            }

            // encode participant data as JSON
            $participant_data_json = json_encode($participant_data);

            // insert scheduled mail for later sending
            $sql="insert into scheduled_mails (email_subject, template_type, email_message, send_later, participant_type, participant_data) values (:email_subject, :template_type, :email_messsage, :send_later, :participant_type, :participant_data)";
            $fields=array(':email_subject'=>$_POST['subject'], ':template_type'=>$_POST['templateSelector'], ':email_messsage'=>$_POST['body'], ':send_later'=>$schedule_date, ':participant_type'=>$participant_type, ':participant_data'=>$participant_data_json);

            // execute insertion
            $q=$con->insert_query($sql,$fields);

            // display success message if insertion was successful
            if($q)
            {
                if(isset($_POST['scheduleForLater']))
                {
                    echo '<div class="alert alert-success alert-dismissible" id="msg">
                            <i class="fa fa-check-circle fa-fw fa-lg"></i>
                            <strong>Well done!</strong> ' . ucfirst($_POST['templateSelector']) . ' message template successfully scheduled
                            </div>';
                }
            }

            // end case
            break;
            
        case 'send_now':

            // capture participant selection
            $participant_type = isset($_POST['participantType']) ? $_POST['participantType'] : 'all';
            $participant_data = array();

            // gather participant data based on type
            if($participant_type == 'individual'){

                $participant_data['individuals'] = isset($_POST['individuals']) ? $_POST['individuals'] : array();

            } elseif($participant_type == 'department'){

                $participant_data['departments'] = isset($_POST['departments']) ? $_POST['departments'] : array();

            } elseif($participant_type == 'position'){

                $participant_data['positions'] = isset($_POST['positions']) ? $_POST['positions'] : array();

            } elseif($participant_type == 'dob'){

                $participant_data['dob_start'] = isset($_POST['dob_start']) ? $_POST['dob_start'] : null;
                $participant_data['dob_end'] = isset($_POST['dob_end']) ? $_POST['dob_end'] : null;

            }

            // encode participant data as JSON
            $participant_data_json = json_encode($participant_data);

            // insert scheduled mail for immediate sending
            $sql="insert into scheduled_mails (email_subject, template_type, email_message, send_now, participant_type, participant_data) values (:email_subject, :template_type, :email_messsage, :send_now, :participant_type, :participant_data)";
            $fields=array(':email_subject'=>$_POST['subject'], ':template_type'=>$_POST['templateSelector'], ':email_messsage'=>$_POST['body'], ':send_now'=>1, ':participant_type'=>$participant_type, ':participant_data'=>$participant_data_json);

            // execute insertion
            $q=$con->insert_query($sql,$fields);

            // display success message if insertion was successful
            if($q)
            {
                if(isset($_POST['sendNow']))
                {
                    echo '<div class="alert alert-success alert-dismissible" id="msg">
                            <i class="fa fa-check-circle fa-fw fa-lg"></i>
                            <strong>Well done!</strong> ' . ucfirst($_POST['templateSelector']) . ' message template successfully scheduled
                            </div>';
                }
            }

            // end case
            break;
    }
}
?>