<?php
$pageName  = "Edit User";
include($_SERVER['DOCUMENT_ROOT'] . "/admin/layout/header.php");




// 

// Other scripts Available


$id = $_GET['id'];
$sql = "SELECT * FROM users WHERE id =:id";
$data = $conn->prepare($sql);
$data->execute(['id' => $id]);

$row = $data->fetch(PDO::FETCH_ASSOC);



if (isset($_POST['upload_picture'])) {
    if (isset($_FILES['image'])) {
        $file = $_FILES['image'];
        $name = $file['name'];

        $path = pathinfo($name, PATHINFO_EXTENSION);

        $allowed = array('jpg', 'png', 'jpeg');


        $folder = "../assets/user/profile/";
        $n = $row['acct_no'] . $name;

        $destination = $folder . $n;
    }
    if (move_uploaded_file($file['tmp_name'], $destination)) {
        $sql = "UPDATE users SET acct_image=:image WHERE id =:user_id";
        $stmt = $conn->prepare($sql);

        $stmt->execute([
            'image' => $n,
            'user_id' => $id

        ]);

        if (true) {
            toast_alert("success", "Your Image Uploaded Successfully", "Thanks!");
        } else {
            echo "invalid";
        }
    }
}

if (isset($_POST['profile_save'])) {
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $acct_type = $_POST['acct_type'];
    $acct_ssn = $_POST['acct_ssn'];
    $acct_email = $_POST['acct_email'];
    $acct_gender = $_POST['acct_gender'];
    $billing_code = $_POST['billing_code'];
    $transfer = $_POST['transfer'];
    $acct_currency = $_POST['acct_currency'];
    $acct_cot = $_POST['acct_cot'];
    $acct_tax = $_POST['acct_tax'];
    $acct_imf = $_POST['acct_imf'];
    $acct_phone = $_POST['acct_phone'];
    $acct_address = $_POST['acct_address'];
    $acct_dob = $_POST['acct_dob'];
    $acct_balance = $_POST['acct_balance'];
    $loan_balance = $_POST['loan_balance'];
    $limit_remain = $_POST['limit_remain'];
    $state = $_POST['state'];
    $zipcode = $_POST['zipcode'];
    $id_no = $_POST['id_no'];



    $sql = "UPDATE users SET firstname=:firstname,lastname=:lastname,acct_type=:acct_type,acct_ssn=:acct_ssn,acct_email=:acct_email,acct_gender=:acct_gender,billing_code=:billing_code,transfer=:transfer,acct_currency=:acct_currency,acct_cot=:acct_cot,acct_tax=:acct_tax,acct_imf=:acct_imf,acct_phone=:acct_phone,acct_address=:acct_address,acct_dob=:acct_dob,acct_balance=:acct_balance,loan_balance=:loan_balance,limit_remain=:limit_remain,state=:state,zipcode=:zipcode,id_no=:id_no  WHERE id=:id";
    $stmt = $conn->prepare($sql);
    $stmt->execute([
        'firstname' => $firstname,
        'lastname' => $lastname,
        'acct_type' => $acct_type,
        'acct_ssn' => $acct_ssn,
        'acct_email' => $acct_email,
        'acct_gender' => $acct_gender,
        'billing_code' => $billing_code,
        'transfer' => $transfer,
        'acct_currency'=> $acct_currency,
        'acct_cot' => $acct_cot,
        'acct_tax' => $acct_tax,
        'acct_imf' => $acct_imf,
        'acct_phone' => $acct_phone,
        'acct_address' => $acct_address,
        'acct_dob' => $acct_dob,
        'acct_balance' => $acct_balance,
        'loan_balance' => $loan_balance,
        'limit_remain' => $limit_remain,
        'state' => $state,
        'zipcode' => $zipcode,
        'id_no' => $id_no,
        'id' => $id
    ]);

    if (true) {
        $msg1 = "
        <div class='alert alert-warning'>
        
        <script type='text/javascript'>
             
                function Redirect() {
                window.location='./users.php';
                }
                document.write ('');
                setTimeout('Redirect()', 3000);
             
                </script>
                
        <center><img src='../assets/images/loading.gif' width='180px'  /></center>
        
        
        <center>	<strong style='color:black;'>Account updated successfully, Please Wait while we redirect you...
               </strong></center>
          </div>
        ";
    } else {
        toast_alert('error', 'Sorry something went wrong');
    }
}





if (isset($_POST['change_pin'])) {
    $current_pin = inputValidation($_POST['current_pin']);
    $new_pin = inputValidation($_POST['new_pin']);
    $confirm_pin = inputValidation($_POST['confirm_pin']);
    $verify_pin = $row['acct_pin'];

    if ($current_pin !== $verify_pin) {
        toast_alert('error', 'Invalid Old Pin');
    } else if ($new_pin !== $confirm_pin) {
        toast_alert('error', 'Confirm Pin not Matched');
    } else if ($new_pin === $verify_pin) {
        toast_alert('error', 'New Pin Matched with Old Pin');
    } else {
        $sql2 = "UPDATE users SET acct_pin=:acct_pin WHERE id =:user_id";
        $passwordUpdate = $conn->prepare($sql2);
        $passwordUpdate->execute([
            'acct_pin' => $new_pin,
            'user_id' => $id
        ]);
        if (true) {
            $msg1 = "
        <div class='alert alert-warning'>
        
        <script type='text/javascript'>
             
                function Redirect() {
                window.location='./users.php';
                }
                document.write ('');
                setTimeout('Redirect()', 3000);
             
                </script>
                
        <center><img src='../assets/images/loading.gif' width='180px'  /></center>
        
        
        <center>	<strong style='color:black;'>Pin Changed successfully, Please Wait while we redirect you...
               </strong></center>
          </div>
        ";
        } else {
            toast_alert('error', 'Sorry Something Went Wrong');
        }
    }
}

if (isset($_POST['change_password'])) {
    $old_password = inputValidation($_POST['old_password']);
    $new_password = inputValidation($_POST['new_password']);
    $confirm_password = inputValidation($_POST['confirm_password']);

    $new_password2 = password_hash((string)$new_password, PASSWORD_BCRYPT);
    $verification = password_verify($old_password, $row['acct_password']);

    if ($verification === false) {
        toast_alert("error", "Incorrect Old Password");
    } else if ($new_password !== $confirm_password) {
        toast_alert("error", "Confirm Password not matched");
    } else if ($new_password === $old_password) {
        toast_alert('error', 'New Password Matched with Old Password');
    } else {
        $sql2 = "UPDATE users SET acct_password=:acct_password WHERE id =:user_id";
        $passwordUpdate = $conn->prepare($sql2);
        $passwordUpdate->execute([
            'acct_password' => $new_password2,
            'user_id' => $id
        ]);
        if (true) {
            $msg1 = "
            <div class='alert alert-warning'>
            
            <script type='text/javascript'>
                 
                    function Redirect() {
                    window.location='./users.php';
                    }
                    document.write ('');
                    setTimeout('Redirect()', 3000);
                 
                    </script>
                    
            <center><img src='../assets/images/loading.gif' width='180px'  /></center>
            
            
            <center>	<strong style='color:black;'>Your Password Change Successfully, Please Wait while we redirect you...
                   </strong></center>
              </div>
            ";
        } else {
            toast_alert('error', 'Sorry Something Went Wrong');
        }
    }
}

if (isset($_POST['status_submit'])) {
    $acct_status = $_POST['acct_status'];
    $kyc_status = "1";

    $sql = "UPDATE users SET acct_status=:acct_status,kyc_status=:kyc_status WHERE id =:user_id";
    $stmt = $conn->prepare($sql);
    $stmt->execute([
        'acct_status' => $acct_status,
        'kyc_status' => $kyc_status,
        'user_id' => $id
    ]);


    $full_name = $row['firstname'] . " " . $row['lastname'];
    $APP_NAME = WEB_TITLE;
    $APP_URL = WEB_URL;
    $SITE_ADDRESS = $page['url_address'];
    $user_email = $row['acct_email'];
    $acct_no = $row['acct_no'];
    $acct_status = $row['acct_status'];
    $message = $sendMail->AdminRegisterMsg($full_name, $acct_no, $acct_status, $APP_NAME, $APP_URL, $SITE_ADDRESS);
    // User Email
    $subject = "Account Status" . "-" . $APP_NAME;
    $email_message->send_mail($user_email, $message, $subject);

    if (true) {
        $msg1 = "
        <div class='alert alert-warning'>
        
        <script type='text/javascript'>
             
                function Redirect() {
                window.location='./users.php';
                }
                document.write ('');
                setTimeout('Redirect()', 3000);
             
                </script>
                
        <center><img src='../assets/images/loading.gif' width='180px'  /></center>
        
        
        <center>	<strong style='color:black;'>Account Status Change to ($acct_status), Please Wait while we redirect you...
               </strong></center>
          </div>
        ";
    } else {
        toast_alert('error', 'Sorry Something Went Wrong');
    }
}


function BillingCode($row)
{
    if ($row['billing_code'] == '1') {
        return 'ACTIVE';
    }

    if ($row['billing_code'] == '0') {
        return 'Disactivated';
    }
}

$BillingCode = BillingCode($row);

function TransferCode($row)
{
    if ($row['transfer'] == '1') {
        return 'ACTIVE';
    }

    if ($row['transfer'] == '0') {
        return 'Disactivated';
    }
}

$TransferCode = TransferCode($row);

function KycStatus($row)
{
    if ($row['kyc_status'] == '1') {
        return 'Activated';
    }

    if ($row['kyc_status'] == '0') {
        return 'Disactivated';
    }
}

$KycStatus = KycStatus($row);

?>

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            Edit User Profile
        </h1>
        <ol class="breadcrumb">
            <li><a href="./dashboard.php"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">

        <!-- SELECT2 EXAMPLE -->
        <form method="POST" enctype="multipart/form-data" action="">
            <div class="box box-default">
                <div class="box-header with-border">
                    <div class="box-tools pull-right">
                        <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                    </div>
                </div>
                <!-- /.box-header -->
                <div class="box-body">
                    <div class="row">

                        <?php if (isset($msg1)) echo $msg1; ?>

                        <div class="col-md-6">

                            <div class="form-group">
                                <label for="exampleInputEmail1">First Name</label>
                                <input type="text" class="form-control" placeholder="<?= $row['firstname'] ?>" name="firstname" value="<?= $row['firstname'] ?>">

                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">Last Name</label>
                                <input type="text" class="form-control" value="<?= $row['lastname'] ?>" placeholder="<?= $row['lastname'] ?>" name="lastname">
                            </div>
                            <!-- /.form-group -->
                            <div class="form-group">
                                <label for="exampleInputEmail1">Account Type</label>
                                <select class="form-control select2" name="acct_type" style="width: 100%;">
                                    <option value="<?= $row['acct_type'] ?>">Select Account Type</option>
                                    <option value="Savings">Savings Account</option>
                                    <option value="Current">Current Account</option>
                                    <option value="Checking">Checking Account</option>
                                    <option value="Fixed Deposit">Fixed Deposit</option>
                                    <option value="Non Resident">Non Resident Account</option>
                                    <option value="Online Banking">Online Banking</option>
                                    <option value="Domicilary Account">Domicilary Account</option>
                                    <option value="Joint Account">Joint Account</option>
                                </select>
                            </div>

                            <div class="form-group">
                                <label for="exampleInputEmail1">Email address</label>
                                <input type="email" class="form-control" value="<?= $row['acct_email'] ?>" placeholder="<?= $row['acct_email'] ?>" name="acct_email">
                            </div>

                            <div class="form-group">
                                <label class="form-label">Gender</label>
                                <select class="form-control select2" name="acct_gender">
                                    <option value="<?= $row['acct_gender'] ?>">Select Gender</option>
                                    <option value="Male">Male</option>
                                    <option value="Female">Female</option>
                                    <option value="Other">Other</option>

                                </select>
                            </div>

                            <div class="form-group">
                                <label>Billing Code Option <span style="color: red;"><?= $BillingCode ?></label>
                                <select class="form-control select2" name="billing_code" style="width: 100%;">
                                    <option value="<?= $row['billing_code'] ?>">Select Option</option>
                                    <option value="1">On</option>
                                    <option value="0">Off</option>
                                </select>
                            </div>
                            
                            
                            <div class="form-group">
                                <label>Transfer Option <span style="color: red;"><?= $TransferCode ?></label>
                                <select class="form-control select2" name="transfer" style="width: 100%;">
                                    <option value="<?= $row['transfer'] ?>">Select Option</option>
                                    <option value="1">On</option>
                                    <option value="0">Off</option>
                                </select>
                            </div>

                            <div class="form-group">
                                <label for="exampleInputEmail1">Cot Code</label>
                                <input type="text" inputmode="numeric" required pattern="[0-9]+" maxlength="4" autocomplete="off" value="<?= $row['acct_cot'] ?>" class="form-control" name="acct_cot" placeholder="<?= $row['acct_cot'] ?>">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">Tax Code</label>
                                <input type="text" inputmode="numeric" required pattern="[0-9]+" maxlength="4" autocomplete="off" value="<?= $row['acct_tax'] ?>" class="form-control" name="acct_tax" placeholder="<?= $row['acct_tax'] ?>">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">Imf Code</label>
                                <input type="text" inputmode="numeric" required pattern="[0-9]+" maxlength="4" autocomplete="off" value="<?= $row['acct_imf'] ?>" class="form-control" name="acct_imf" placeholder="<?= $row['acct_imf'] ?>">
                            </div>


                            <table class="table table-bordered mb-4">
                                <tbody>

                                      <tr>
                                        <th>Profile Picture</th>
                                        <th><a href="<?= $web_url ?>/assets/user/profile/<?= $row['acct_image'] ?>" target="_blank"><img src="<?= $web_url ?>/assets/user/profile/<?= $row['acct_image'] ?>" width="20%" alt=""></a></th>
                                    </tr>
                                    <tr>
                                        <th>Address Proof</th>
                                        <th>

                                            <a href="<?= $web_url ?>/assets/user/profile/<?= $row['proofaddress'] ?>" target="_blank"><img src="<?= $web_url ?>/assets/user/profile/<?= $row['proofaddress'] ?>" width="20%" alt=""></a>
                                        </th>

                                    </tr>
                                    
                                </tbody>
                            </table>


                            <!-- /.form-group -->
                        </div>
                        <!-- /.col -->
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="exampleInputEmail1">Account Phone</label>
                                <input type="text"  required  minlength="8"  autocomplete="off" placeholder="<?= $row['acct_phone'] ?>" value="<?= $row['acct_phone'] ?>" class="form-control wizard-required" name="acct_phone">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">Account Address</label>
                                <input type="text" value="<?= $row['acct_address'] ?>" class="form-control" name="acct_address" placeholder="<?= $row['acct_address'] ?>">
                            </div>

                            <div class="form-group">
                                <label for="exampleInputEmail1">Date of Birth</label>
                                <input type="date" class="form-control" value="<?= $row['acct_dob'] ?>" name="acct_dob" placeholder="<?= $row['acct_dob'] ?>">
                            </div>
                            
                            <div class="form-group">
                                <label class="form-label">SSN</label>
                                <input type="text" inputmode="numeric" required pattern="[0-9]+" maxlength="20" autocomplete="off" class="form-control" value="<?= $row['acct_ssn'] ?>" placeholder="<?= $row['acct_ssn'] ?>" name="acct_ssn">
                            </div>
                            
                            <div class="form-group">
                                <label for="exampleInputEmail1">Balance</label>
                                <input type="number" class="form-control" value="<?= $row['acct_balance'] ?>" name="acct_balance" placeholder="<?= $row['acct_balance'] ?>">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">Currency</label>
                                <input type="text" class="form-control" value="<?= $row['acct_currency'] ?>" name="acct_currency" placeholder="$">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">Loan</label>
                                <input type="number" value="<?= $row['loan_balance'] ?>" class="form-control" name="loan_balance" placeholder="<?= $row['loan_balance'] ?>">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">Limit Remain</label>
                                <input type="number" value="<?= $row['limit_remain'] ?>" class="form-control" name="limit_remain" placeholder="<?= $row['limit_remain'] ?>">
                            </div>

                            <div class="form-group">
                                <label>State</label>
                                <input type="text" value="<?= $row['state'] ?>" class="form-control" name="state" placeholder="<?= $row['state'] ?>">
                            </div>

                            <div class="form-group">
                                <label class="form-label">Zipcode</label>
                                <input type="text" inputmode="numeric" required pattern="[0-9]+" maxlength="5" autocomplete="off" class="form-control" value="<?= $row['zipcode'] ?>" placeholder="<?= $row['zipcode'] ?>" name="zipcode">
                            </div>

                            <div class="form-group">
                                <label class="form-label">ID Card Number</label>
                                <input type="text" inputmode="numeric" pattern="[0-9]+"  autocomplete="off" class="form-control" value="<?= $row['id_no'] ?>" placeholder="<?= $row['id_no'] ?>" name="id_no">
                            </div>
                            
                             

                            <table class="table table-bordered mb-4">
                                <tbody>

                                      <tr>
                                        <th>Front ID Card</th>
                                        <th><a href="<?= $web_url ?>/assets/user/profile/<?= $row['idfront'] ?>" target="_blank"><img src="<?= $web_url ?>/assets/user/profile/<?= $row['idfront'] ?>" width="20%" alt=""></a></th>
                                    </tr>
                                    <tr>
                                        <th>Back ID Card</th>
                                        <th>

                                            <a href="<?= $web_url ?>/assets/user/profile/<?= $row['idBack'] ?>" target="_blank"><img src="<?= $web_url ?>/assets/user/profile/<?= $row['idBack'] ?>" width="20%" alt=""></a>
                                        </th>

                                    </tr>
                                    
                                </tbody>
                            </table>

                        </div>

                        <!-- /.col -->
                    </div>
                    <!-- /.row -->
                </div>
                <!-- /.box-body -->
                <div class="box-footer">
                    <button type="submit" name="profile_save" class="btn btn-primary">Update Profile</button>
                </div>
            </div>
        </form>
        <!-- /.box -->

        <div class="row">
            <!-- left column -->
            <div class="col-md-6">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">Change Password</h3>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form method="post">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="exampleInputEmail1">Current Password</label>
                                <input type="password" class="form-control" name="old_password" placeholder="Old Password">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">New Password</label>
                                <input type="password" class="form-control" name="new_password" placeholder="Old Password">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">Confirm New Password</label>
                                <input type="password" class="form-control" name="confirm_password" placeholder="Old Password">
                            </div>
                        </div>
                        <!-- /.box-body -->

                        <div class="box-footer">
                            <button type="submit" name="change_password" class="btn btn-primary">Change
                                Password</button>
                        </div>
                    </form>
                </div>
                <div>
                    Profile Image
                </div>
                <form method="POST" id="general-info" enctype="multipart/form-data">
                    <div class="form-group">
                        <input type="file" id="input-file-max-fs" class="form-control" name="image" data-max-file-size="2M" />
                        <input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
                     </div>
                    <div class="box-footer">
                        <button type="submit" name="upload_picture" class="btn btn-primary">Change Image</button>
                    </div>
                </form>
                <br><br>
                <!-- /.box -->
            </div>
            <!--/.col (left) -->
            <!-- right column -->
            <div class="col-md-6">
                <!-- Horizontal Form -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">Change Current Pin : <?= $row['acct_pin'] ?></h3>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form method="post" autocomplete="off" autofocus="off">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="exampleInputEmail1">Old Pin</label>
                                <input type="text" inputmode="numeric" required pattern="[0-9]+" maxlength="4" autocomplete="off" class="form-control" name="current_pin" placeholder="Current Pin">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">New Pin</label>
                                <input type="text" inputmode="numeric" required pattern="[0-9]+" maxlength="4" autocomplete="off" class="form-control" name="new_pin" placeholder="New Pin">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">Confirm New Pin</label>
                                <input type="text" inputmode="numeric" required pattern="[0-9]+" maxlength="4" autocomplete="off" class="form-control" name="confirm_pin" placeholder="Confirm Pin">
                            </div>
                        </div>
                        <!-- /.box-body -->

                        <div class="box-footer">
                            <button type="submit" name="change_pin" class="btn btn-primary">Change Pin</button>
                        </div>
                    </form>
                </div>

                <div>
                    CURRENT STATUS: <b><?= ucwords($row['acct_status']) ?></b><br>
                     KYC STATUS: <b><?= $KycStatus ?></b>
                </div>
                <form method="POST">
                    <div class="form-group">
                        <select class="form-control select2" name="acct_status" style="width: 100%;">
                            <option>Select Account Status</option>
                            <option value="active">Activate/Approve</option>
                            <option value="hold">Hold/Reject</option>
                        </select>
                    </div>
                    <div class="box-footer">
                        <button type="submit" name="status_submit" class="btn btn-primary">Update Account/Kyc</button>
                    </div>
                </form>
                <br><br>
                
                
                <!-- /.box -->
            </div>
            <!--/.col (right) -->
        </div>
        <!-- /.row -->


    </section>
    <!-- /.content -->
</div>
<!-- /.content-wrapper -->


<?php
include($_SERVER['DOCUMENT_ROOT'] . "/admin/layout/footer.php");

?>