<?php
session_start();
require_once '../include/Constants.php';
require_once "../vendor/autoload.php";
require_once "../include/DatabaseConn.php";
require_once "../models/User.php";
require_once '../models/USettings.php';
require_once '../include/GoogleAuthenticator.php';
require_once '../models/Document.php';

$db = new DatabaseConn();
$link = $db->connect();

$user = User::getCurrentUser($link);
if (is_null($user)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ../login?" . RETURN_URL . "=" . $url);
    die();
}

$settings = USettings::getSystemSettings($link, $user->getEmail());
if ($settings->getTwofa() && is_null($user->getFa())) {
    header("location: add-2fa");
    die();
}

if (isset($_FILES['proof'])) {
    $package = filter_input(INPUT_POST, "op", FILTER_SANITIZE_STRING);
    $id = filter_input(INPUT_POST, "action", FILTER_SANITIZE_STRING);

    if (strcmp($package, "id") == 0) {
        $doc = "Identity Document";
    } else if (strcmp($package, "por") == 0) {
        $doc = "Proof of Residence";
    }
    if ($_FILES['proof']['size'] > 0) {
        $target_dir = IMG_UPLOAD_DIR;
        $s = substr(str_shuffle(str_repeat("abcdefghijklmnopqrstuvwxyz", 5)), 0, 5);
        $target_file = $target_dir . $s . filter_var(basename($_FILES["proof"]["name"]), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
        $check = getimagesize($_FILES["proof"]["tmp_name"]);
        if ($check !== false) {
            $uploadOk = 1;
        } else {
            $error = "File is not an image.";
            $uploadOk = 0;
        }
// Check if file already exists
        if (file_exists($target_file)) {
            $error = "Sorry, file already exists.";
            $uploadOk = 0;
        }
// Check file size
        if ($_FILES["proof"]["size"] > 50000000) {
            $error = "Sorry, your file is too large.";
            $uploadOk = 0;
        }
// Allow certain file formats
        if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
            $error = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;
        }
// Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
// $error = "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($_FILES["proof"]["tmp_name"], $target_file)) {
                $success = "Document has been uploaded. Pending verification";
                $time = time();
                $status = PENDING;
                if(isset($id) && !is_null($id)) {
                    Document::deleteDocument($id, $link);
                }
                $rss = mysqli_query($link, "insert INTO documents (`user`,`path`,status,`type`,`time`) VALUES ('{$user->getEmail()}', '$target_file', '$status','$package','$time')");
                header("location: documents");
                die();
            } else {
                $error = "Sorry, there was an error uploading your file.";
            }
        }
    } else {
        $error = "Please upload document.";
    }
}
$docs = Document::getDocuments($link, $user->getEmail());
if (!is_null($docs)) {
    if(count($docs) == 1){
        $doc = $docs[0];
        if(strcmp($doc->getType(), "id") == 0) {
             $doc1 = $docs[0];
        }
        if(strcmp($doc->getType(), "por") == 0) {
             $doc2 = $docs[0];
        }
    }
    else if(count($docs) == 2){
        $doc = $docs[0];
        $docc = $docs[1];
        if(strcmp($doc->getType(), "id") == 0) {
             $doc1 = $docs[0];
        }
        if(strcmp($doc->getType(), "por") == 0) {
             $doc2 = $docs[0];
        }
        if(strcmp($docc->getType(), "id") == 0) {
             $doc1 = $docs[1];
        }
        if(strcmp($docc->getType(), "por") == 0) {
             $doc2 = $docs[1];
        }
    }
   
}
//$doc = $docs[0];
?>
<!DOCTYPE html>
<html lang="en">

    <head>
        <?php require_once './partials/head.php'; ?>
        <link href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" rel="stylesheet" type="text/css" media="all" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.3/css/intlTelInput.min.css" />

    </head>

    <body class="ltr en">
        <div ui-view="" id="app" ng-class="{'app-mobile' : app.isMobile, 'app-invest' : app.isInvest, 'app-navbar-fixed' : app.layout.isNavbarFixed,
     'app-sidebar-fixed' : app.layout.isSidebarFixed, 'app-footer-fixed':app.layout.isFooterFixed}" class="ng-scope app-navbar-fixed app-sidebar-fixed" style="">
             <?php require_once './partials/sidebar.php'; ?>
            <div class="app-content ng-scope" ng-class="{loading: loading}">
                <!-- ngInclude: 'assets/views/partials/top-navbar.html' -->
                <?php require_once './partials/header_.php'; ?>
                <div data-ng-controller="PageCtrl" class="ng-scope">

                    <div data-ng-include=" 'assets/views/partials/main-content.html'" id="main_content" class="main-content ng-scope" ng-class="{'hidden' : hidePageContent, 'wrap-content-dashboard' : (currentPageId == 'app.dashboard')}" style="">
                        <!-- uiView:  -->
                        <div ui-view="" class="wrap-content container fade-in-up ng-scope" id="container">
                            <!-- uiView:  -->
                            <div ui-view="" class="smooth ng-scope">
                                <!-- uiView:  -->
                                <div ui-view="" class="ng-scope">
                                    <!-- ngInclude: undefined -->
                                    <data-ng-include src="'assets/views/partials/title.html'" style="margin-top: 10px" class="ng-scope">
                                        <div data-ng-include=" 'assets/views/partials/alerts.html'" style="margin-top: 61px;" class="ng-scope">
                                            <div ng-controller="AlertCtrl" class="PopupAlertDiv ng-scope">
                                                <div class="container-fluid mt20" ng-show="alertsLen > 0">
                                                    <div class="row">
                                                        <div class="col-sm-12">
                                                            <!-- ngRepeat: (id, alert) in alerts track by $index -->
                                                            <div ng-repeat="(id, alert) in alerts track by $index" class="alert alert-block alert-success alert-type--welcome fade in" id="notice_20300006" data-ng-init="setNoticeAsShown(alert.id)">
                                                                <!-- ngIf: alert.is_closable -->
                                                                <button ng-hide="getInclude(alert.type) == 'alert-prompt.html'" data-ct-dismiss="alert" class="close ng-scope" ng-if="alert.is_closable" type="button" ng-click="profileAlertClose(alert)"></button>
                                                                <!-- end ngIf: alert.is_closable -->
                                                                <div class="PopupAlertDiv__content">
                                                                    <!-- ngInclude: 'assets/views/tpl/' + getInclude(alert.type) --><span ng-include="'assets/views/tpl/' + getInclude(alert.type)" class="ng-scope"><div class="ng-scope">
                                                                            <h4 class="alert-heading margin-bottom-10">
                                                                                <span class="success-title ng-binding" ng-bind-html="('welcome_to_myfxtm' | translate)">Welcome to Ozz Capital - your personal client area.</span> </h4>
                                                                            <p class="m0"> <b class="ng-binding">Your referral ID:  <a class="upgrade-to-text" style="cursor: pointer" data-clipboard-text="https://ozzcapital.com/?referral=irsgmyleigsuc@baybabes.com">
                                                                                        <span>Click to copy referral link</span>
                                                                                    </a> </b>
                                                                                <br> </p>
                                                                            <!-- ngIf: alert.data['30PctBonusAvailable'] || alert.data.switchToFxTmBonusAvailable -->
                                                                        </div>
                                                                    </span>
                                                                </div>
                                                            </div>
                                                            <!-- end ngRepeat: (id, alert) in alerts track by $index -->
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div ng-hide="currentPageId === 'app.dashboard'" class="ng-scope">
                                            <section id="page-title" class="padding-top-15 padding-bottom-0 ng-scope" ng-controller="TitleCtrl">
                                                <div class="container-fluid">
                                                    <div class="row">
                                                        <div class="col-xs-12">
                                                            <h1 class="mainTitle">
                                                                <span ng-bind="title | translate" class="ng-binding">Upload Documents</span>
                                                                <!-- ngIf: openAccount.account_group === 'investor' --><!-- ngIf: subtitle -->
                                                            </h1> </div>
                                                    </div>
                                                </div>
                                            </section>
                                        </div>
                                    </data-ng-include>
                                    <div class="container-fluid my-documents-page">

                                        <div class="row">
                                            <div class="col-xs-12">
                                                <!-- ngIf: ::showMeHowForPage.videos.length -->
                                            </div>
                                        </div>

                                        <div class="row">
                                            <div class="col-sm-12">
                                                <div class="lead">
                                                    Verify your Identity and Proof of Residence in order to activate your account and get access to all areas.                </div>
                                            </div>
                                        </div>

                                        <div class="row ng-scope" data-ng-controller="VerifyProfileDocStatusCtrl">
                                            <div class="col-xs-12">
                                                <div class="panel-white panel">
                                                    <div class="panel-body pt5 pb5">
                                                        <div class="row">

                                                            <div class="col-xs-12">
                                                                <table class="table table-layout-fixed uploaded-docs-table" width="100%">
                                                                    <tbody>
                                                                        <tr>
                                                                            <td class="uploaded-docs-table-name">
                                                                                <span class="document-verify-step1 lead mb0">
                                                                                    <i class="icon-MyProfile color-light-blue"></i>
                                                                                    Identity Document (ID)                                            </span>
                                                                            </td>
                                                                            <td class="uploaded-docs-table-status">

                                                                                <!-- ngIf: idDoc.status == docStatuses.NEW -->

                                                                                <!-- ngIf: idDoc.status == docStatuses.APPROVED -->

                                                                                <!-- ngIf: idDoc.status == docStatuses.REJECTED -->

                                                                                <!-- ngIf: idDoc.status == docStatuses.EXPIRED -->

                                                                                <!-- ngIf: idDoc.status == docStatuses.PENDING -->

                                                                                <!-- ngIf: !idDoc.status --><div data-ng-if="!idDoc.status" class="color-pink-bright text-bold ng-scope">
                                                                                    <?php
                                                                                    if (isset($doc1)) {
                                                                                        if (strcmp($doc1->getStatus(), APPROVED) == 0) {
                                                                                            echo '<span style="color: green">Document Approved</span>';
                                                                                        } else if (strcmp($doc1->getStatus(), REJECTED) == 0) {
                                                                                            echo 'Rejected';
                                                                                        } else {
                                                                                            echo 'Pending Approval';
                                                                                        }
                                                                                    } else {
                                                                                        echo 'Not uploaded';
                                                                                    }
                                                                                    ?>



                                                                                </div><!-- end ngIf: !idDoc.status -->
                                                                            </td>
                                                                            <td class="uploaded-docs-table-btn pr0">
                                                                                 <?php
                                                                                    if (isset($doc1)) {
                                                                                        if (strcmp($doc1->getStatus(), APPROVED) == 0) {
                                                                                            
                                                                                        }  else if (strcmp($doc1->getStatus(), REJECTED) == 0) {?>
                                                                                           <a data-type="id" data-id="<?php echo ($doc1->getId()); ?>" class="btn btn-primary ii">Upload</a>
                                                                                        <?php } else {
                                                                                           
                                                                                        }
                                                                                    } else {
                                                                                        echo '<a data-type="id" class="btn btn-primary ii">Upload</a>';
                                                                                    }
                                                                                    ?>
                                                                            </td>
                                                                        </tr>

                                                                        <tr>
                                                                            <td class="uploaded-docs-table-name">
                                                                                <span class="document-verify-step1 lead mb0">
                                                                                    <i class="icon-location pl5 color-light-blue"></i>
                                                                                    Proof of Residence (POR)                                            </span>
                                                                            </td>
                                                                            <td class="uploaded-docs-table-status">

                                                                                <!-- ngIf: porDoc.status == docStatuses.NEW -->

                                                                                <!-- ngIf: porDoc.status == docStatuses.APPROVED -->

                                                                                <!-- ngIf: porDoc.status == docStatuses.REJECTED -->

                                                                                <!-- ngIf: porDoc.status == docStatuses.EXPIRED -->

                                                                                <!-- ngIf: porDoc.status == docStatuses.PENDING -->

                                                                                <!-- ngIf: !porDoc.status --><div data-ng-if=" !porDoc.status" class="text-bold ng-scope">
                                                                                    <!-- ngIf: :: idDoc.hasPOR -->
                                                                                    <!-- ngIf: !idDoc.hasPOR --><span data-ng-if="!idDoc.hasPOR" class="color-pink-bright ng-scope">
                                                                                         <?php
                                                                                    if (isset($doc2)) {
                                                                                        if (strcmp($doc2->getStatus(), APPROVED) == 0) {
                                                                                            echo '<span style="color: green">Document Approved</span>';
                                                                                        } else if (strcmp($doc2->getStatus(), REJECTED) == 0) {
                                                                                            echo 'Rejected';
                                                                                        } else {
                                                                                            echo 'Pending Approval';
                                                                                        }
                                                                                    } else {
                                                                                        echo 'Not uploaded';
                                                                                    }
                                                                                    ?>                                               
                                                                                        </span><!-- end ngIf: !idDoc.hasPOR -->
                                                                                </div><!-- end ngIf: !porDoc.status -->
                                                                            </td>
                                                                            <td class="uploaded-docs-table-btn pr0">
                                                                                <?php
                                                                                    if (isset($doc2)) {
                                                                                        if (strcmp($doc2->getStatus(), APPROVED) == 0) {
                                                                                            
                                                                                        } else if (strcmp($doc2->getStatus(), REJECTED) == 0) {?>
                                                                                           <a data-type="por" data-id="<?php echo ($doc2->getId()); ?>" class="btn btn-primary ii">Upload</a>
                                                                                        <?php } else {
                                                                                           
                                                                                        }
                                                                                    } else {
                                                                                        echo '<a data-type="por" class="btn btn-primary ii">Upload</a>';
                                                                                    }
                                                                                    ?>
                                                                               
                                                                            </td>
                                                                        </tr>
                                                                    </tbody>
                                                                </table>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>


                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <form style="display: none !important" id="prick2" method="POST" enctype="multipart/form-data" action="documents">
                <input type="file" style="display: none !important" id="image" name="proof" accept="image/*" class="form-control" required="">
                <input type="hidden" name="op" id="op" value="" />
                <input type="hidden" name="action" id="action" value="" />
            </form>
            <!-- ngInclude: 'assets/views/partials/footer.html' -->
            <footer data-ng-include=" 'assets/views/partials/footer.html'" id="footer" class="ng-scope">
                <div class="footer-inner ng-scope">
                    <div class="pull-left">
                        <p class="m0"> ©
                            <?php echo APP_NAME; ?> 2011-2021
                            <br /> </p>
                        <!-- ngIf: footerAddText -->
                    </div>
                    <div class="pull-right"> <span class="go-top" ng-click="toTheTop()"><i style="font-size: 30px;" class="icon-angle-up"></i></span> </div>
                    <!-- ngIf: !isMobileApp -->
                    <!-- ngInclude: 'assets/views/partials/livechat.html' -->
                    <!-- end ngIf: !isMobileApp -->
                </div>
            </footer>
        </div>
        <?php require_once './partials/scripts_.php'; ?>
        <script>
            $('.ii').click(function (e) {
                e.preventDefault();
                $('#op').val($(this).attr('data-type'));
                 $('#action').val($(this).attr('data-id'));
                $('#image').trigger('click');
            });
            $('#image').change(function () {
                $('#prick2').trigger('submit');
            });
            $('#k3').addClass('active');
        </script>
    </body>

</html>