<?php

if(isset($_POST['update']))
{
    switch ($_POST['update'])
    {            
        case 'rate':
            $sql = "update complaints set service_rating=:rate where id=:id";
            $q=$con->update_query($sql,array(':rate'=>$_POST['rate'], ':id'=>$_POST['id']));
            if($q)
            {
                echo '<script>window.location="view-complaint?id='.$_POST['id'].'"</script>';
            }
            break;
            
        case 'edit-profile':
            $reg = 0;
            if(isset($_POST['reginname']))
            {
                $reg = 1;
            }
            if($_POST['oldemail'] != $_POST['email'])
            {
                $sql = "select email from users where email=:email AND id != :userid";
                $q=$con->select_query($sql,array(':email'=>$_POST['email'],':userid'=>$_POST['userid']));
                if(count($q) > 0)
                {
                    echo '<div class="alert alert-danger">Email already exist</di>';
                    return;
                }
            }
            
            $sql = "update client_details set phone=:phone,lga_id=:lga,street=:street,customer_type=:type,
                meternumber=:meter,registered_in_name=:reg,state_id=:state where user_id=:user";
            $fields = array(
                ':phone'=>$_POST['phone'],
                ':lga'=>$_POST['lga'],
                ':street'=>$_POST['street'],
                ':type'=>$_POST['customertype'],
                ':meter'=>$_POST['meterno'],
                ':reg'=>$reg,
                ':state'=>$_POST['state'],
                ':user'=>$_POST['userid'],
            );
            $q=$con->update_query($sql,$fields);
            if($q)
            {
                $sql = "update users set firstname=:fname,lastname=:lname,email=:email where id=:user";
                $fields = array(
                    ':fname'=>$_POST['firstname'],
                    ':lname'=>$_POST['lastname'],
                    ':email'=>$_POST['email'],
                    ':user'=>$_POST['userid']
                );
                $q=$con->update_query($sql,$fields);
                if($q)
                {
                    $_SESSION['user_session'] = $_POST['email'];
                    echo '<div class="alert alert-success">Profile update successful.</div>';
                    echo '<script>window.location="client-area"</script>';
                }
            }
            break;
            
        case 'message_template_setup':
            $status=0;
            
            if(isset($_POST['status']))
            {
                $status=1;
            }
            
            $sql="update template set subject=:subject, body=:body, status=:st where id=:id";
            $fields=array(
                ':subject'=>$_POST['subject'], ':body'=>$_POST['body'], ':st'=>$status,
                ':id'=>$_SESSION['templateid']
            );
            if($con->update_query($sql,$fields))
            {
                echo '<div class="alert alert-success" id="msg">
                            <i class="fa fa-check-circle fa-fw fa-lg"></i>
                            <strong>Well done!</strong> Template successfully saved.
                            </div>';
                // if($_POST['templatetype'] == STANDARD_TEMPLATE)
                // {
                //     echo '<script>window.location="../admin/standard_template_list"</script>';
                // }
                // else
                // {
                //     echo '<script>window.location="../admin/message_template_list"</script>';
                // }
            }
            break;
    }
}
?>