<?php
require("inc/include.php");
if(isset($_GET['id']) && intval($_GET['id']) > 0)
{
    $userProfile = $db->getUser($_GET['id']);
    if($userProfile == null)
    {
        header("Location:index.php");
        exit;
    }
}
elseif(isset($user))
{
    $userProfile = $user;
}
else
{
    header("Location:index.php");
    exit;
}
$err = "";
if(isset($_POST['tradeUrl']) && isset($_POST['token']))
{
    if(isset($user))
    {
        if($userProfile->getId() == $user->getId())
        {
            if(checkToken(600, "updateTradeUrl"))
            {
                $tradeUrl = $_POST['tradeUrl'];
                $tradeUrlParams = parse_url($tradeUrl);
                if($tradeUrlParams === false || !isset($tradeUrlParams['query']))
                {
                    $err .= $l->getString("This trade URL isn't correct.");
                }
                else
                {
                    parse_str($tradeUrlParams['query'], $arrayParams);
                    if(!isset($arrayParams['token']))
                    {
                        $err .= $l->getString("This trade URL isn't correct.") . "<br>";
                    }
                    else
                    {
                        $token = $arrayParams['token'];
                        if($db->updateUserToken($user->getId(), $token))
                        {
                            $user->setTradeToken($token);
                            $succ = $l->geTString("Trade URL successfully updated !");
                        }
                        else
                        {
                            $err .= $l->geTString("SQL query failed, please try again later") . '<br>';
                        }
                    }
                }
            }
            else
            {
                $err .= $l->geTString("Your security token expired, please refresh and try again") . '<br>';
            }
        }
        else
        {
            $err .= $l->geTString("You can't change someone else's trade URL") . '<br>';
        }
    }
    else
    {
        $err .= $l->geTString("You need to be logged in to update your trade URL") . '<br>';
    }
}
if(isset($_POST['actualPassword']) && isset($_POST['newPassword']) && isset($_POST['passwordCheck']) && isset($_POST['token']))
{
    if(isset($user))
    {
        if($userProfile->getId() == $user->getId())
        {
            if(checkToken(600, "updatePassword"))
            {
                $hash = $db->getUserHash($user->getId());
                $password = $_POST['actualPassword'];
                if($hash != null && password_verify($password, $hash))
                {
                    $newPassword = $_POST['newPassword'];
                    if(strlen($newPassword) < $config['forms']['passwordMin'] || strlen($newPassword) > $config['forms']['passwordMax'])
                    {
                        $err .= $l->getString("Your password must be between %d and %d characters.", $config['forms']['passwordMin'], $config['forms']['passwordMax']) . "<br>";
                    }
                    else
                    {
                        $passwordCheck = $_POST['passwordCheck'];
                        if($newPassword !== $passwordCheck)
                        {
                            $err .= $l->getString("Your passwords don't match.") . "<br>";
                        }
                        else
                        {
                            $newPasswordHash = password_hash($newPassword, PASSWORD_DEFAULT, ['cost' => 12]);
                            if($db->updateUserPassword($user->getId(), $newPasswordHash))
                            {
                                $succ = $l->geTString("Password successfully updated !");
                            }
                        }
                    }
                }
                else
                {
                    $err .= $l->getString("Your password is incorrect.") . "<br>";
                }
            }
            else
            {
                $err .= $l->geTString("Your security token expired, please refresh and try again") . '<br>';
            }
        }
        else
        {
            $err .= $l->geTString("You can't change someone else's password") . '<br>';
        }
    }
    else
    {
        $err .= $l->geTString("You need to be logged in to update your password") . '<br>';
    }
}
require "inc/header.php";
?>

<div class="b-container">
    <div style="display: flex;">
        <div class="b-items-group b-items-5 b-items-cases" style="background-color:#18191d;<?php echo (!isset($user) || $user->getId() != $userProfile->getId() ? "width: 100%;" : "width: 50%;"); ?>">
            <div class="b-inner">
                <div class="b-case-container">
                    <div class="b-canvas-container">
                        <br>
                        <h2 class="b-case-items-text"><?php echo $l->getString('<span class="b-user-profile-name"><b>%s</b></span>\'s history', $userProfile->getUsername()); ?></h2>
                        <br>
                        <div class="b-profile-left">
                            <div class="b-profile-avatar">
                                <img alt="Avatar" src="<?php echo $userProfile->getAvatar(); ?>">
                            </div>
                            <a target="_blank" href="http://steamcommunity.com/profiles/<?php echo $userProfile->getSteamId(); ?>/" class="b-steam-profile-btn"><span>Steam profile</span></a>
                        </div>
                        <br>
                    </div>
                </div>
            </div>
        </div>
        <?php
        if(isset($user) && $user->getId() == $userProfile->getId())
        {
            ?>
        <div class="b-items-group b-items-5 b-items-cases">
        
			<nav class="b-breadcrumbs">

<h1><i class="fa fa-user" aria-hidden="true"></i> <?php echo $l->getString("Edit your information"); ?></h1>
</nav>
           
            
            <div class="b-profile-right">
                <?php
                    if(isset($err) && !empty($err))
                    {
                        ?>
                        <div class="alert alert-danger"><?php echo $err; ?></div>
                        <?php
                    }
                    if(isset($succ) && !empty($succ))
                    {
                        ?>
                        <div class="alert alert-success"><?php echo $succ; ?></div>
                        <?php
                    }
                ?>
                <div class="b-profile-settings">
                    <div>
                        <div class="b-trade-url-input-wrapper">
                            <form action="profile.php" method="post">
                                <label for="tradeUrl">- <?php echo $l->getString("Trade URL"); ?></label> ( <a href="https://steamcommunity.com/id/me/tradeoffers/privacy"><?php echo $l->getString("Where to find my trade URL ?"); ?></a>) <br><br><input value="<?php echo $user->getTradeUrl(); ?>" name="tradeUrl" id="tradeUrl" type="url">
                                <input type="hidden" name="token" value="<?php echo generateToken("updateTradeUrl"); ?>">
                                <br><br>
                                <button type="submit"><?php echo $l->getString("Update"); ?></button>
                                <br><br>
                            </form>
                            <button id="changePassword"><?php echo $l->getString("Change password"); ?></button>
                            <form method="post" action="profile.php" class="hidden" id="formChangePassword">
                                <label for="actualPassword">- <?php echo $l->getString("Actual password"); ?></label><br>
                                <input name="actualPassword" id="actualPassword" type="password">
                                <br><br>
                                <label for="newPassword">- <?php echo $l->getString("New password"); ?></label> ( <?php echo $l->getString("Between %d and %d characters", $config['forms']['passwordMin'], $config['forms']['passwordMax']); ?> ) <br>
                                <input  value="" name="newPassword" id="newPassword" type="password">
                                <br><br>
                                - <?php echo $l->getString("Retype password"); ?> <br>
                                <input id="passwordCheck" name="passwordCheck" type="password">
                                <br><br>
                                <input type="hidden" name="token" value="<?php echo generateToken("updatePassword"); ?>">
                                <button type="submit"><?php echo $l->getString("Update"); ?></button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php
        }
        ?>
    </div>
    <div class="b-footer-wrapper">
        <div class="b-container">
            <div class="b-case-items-wrapper">
                <h2 class="b-case-items-text"><?php echo $l->getString("Last <span>cases</span> opened : "); echo "<span>".$userProfile->getUsername()."</span>";?></h2>
                <div class="b-gradient-line"></div>
                <div class="b-case-items">
                    <?php
                    $items = $db->getOpenedCasesForUser($userProfile->getId());
                    foreach($items as $item)
                    {
                        ?>
                        
                    <div class="b-case-item">
                        <div class="b-picture">
                            <img alt="<?php echo $item['name']; ?>" src="https://steamcommunity-a.akamaihd.net/economy/image/<?php echo $item['image']; ?>">
                            <div class="b-descr">
                                <span>
                                <?php
                                $itemName = str_replace(["(Field-Tested)", "(Factory New)", "(Battle-Scarred)", "(Minimal Wear)", "(Well-Worn)"], "", $item['name']);
                                echo $itemName . (!empty($item['exterior']) ? " | " . $item['exterior'] : "");
                                ?>
								
				</span>
                                <br>
                                <span><?php echo " " . ($item['status'] == 6 ? '<i class="fa fa-money" style="color:green"></i>' : "") . ($item['status'] > 0 && $item['status'] < 4 ? '<i class="fa fa-paper-plane" style="color:green"></i>' : "") . (($item['status'] == 0 || $item['status'] == 0) && isset($user) && $user->getId() == $userProfile->getId() ? '<a href="javascript:void(0);" onclick="tradeItem(' . $item['itemUniqueId'] . ')"><font color="#e44f1b">' . $l->getString("Trade for %d keys", $item['price']*100) . '</font></a> or <a href="javascript:void(0);" onclick="requestItem(' . $item['itemUniqueId'] . ')"><font color="#e44f1b">' . $l->getString("Request") . '</font></a>' : ""); ?> </span>
                            </div>
                        </div>
                    </div>
                        <?php
                    }
                    ?>
                    <div class="b-clear"></div>
                </div>
            </div>
            <script>
                var lSkinSent = "<?php echo $l->getString("Skin sent successfully"); ?>";
                var lSkinTraded = "<?php echo $l->getString("Skin traded successfully"); ?>";
                var lSkinSendFailed = "<?php echo $l->getString("Unable to send the skin"); ?>";
                var lSkinTradeFailed = "<?php echo $l->getString("Unable to trade the skin"); ?>";
            </script>
<?php
$additionnalScripts = '<script src="templates/default/js/profile.js"></script>';

require "inc/footer.php";