<?php
include('dist/db.php');
$error = '';
$is_success = 0;
if(isset($_POST['sub_form']) && !empty($_POST['req_email']) && !empty($_POST['req_company'])){
    $email      = trim($_POST['req_email']);
    $compnay    = $_POST['req_company']; 
    $get_company_name = mysqli_query($conn, "select * from db_users where username='$compnay' ");
    if($get_company_name->num_rows > 0){
        $user_info = get_user_by_mail($email);
        if($user_info['type'] == 'admin'):
            $error = '<p class="error_red" style="color:red;">Admin password doest not change.</p>';
        else:
        
            if($user_info['type'] == 'employee' || $user_info['type'] == 'manager'){
                $emp_list = mysqli_query($conn, "select * from db_users where TRIM(email)='$email' and company_name=".mysqli_fetch_array($get_company_name)['id']);
                
            }else {
                $emp_list = mysqli_query($conn, "select * from db_users where TRIM(email)='$email' and id=".mysqli_fetch_array($get_company_name)['id']);
            }
    
        if(mysqli_num_rows($emp_list) > 0){
            $single_emp = mysqli_fetch_assoc($emp_list);
            $error = '<p class="success_green" style="color:green;">We have sent you mail for reset password.</p>';
            $headers = "Content-Type: text/html; charset=UTF-8\r\n";
            $is_success = 1;
            $token = bin2hex(openssl_random_pseudo_bytes(8));
            //mail($email,"New Password Request", "Hello ".$single_emp['full_name'].",<br> You can create your new password from here: <a href='".$site_url."new_password.php?req_email=".$email."'><u>".$site_url."new_password.php?req_email=".$email."</u></a>",$headers);
            send_zoho_mail($email,"New Password Request", "Hello ".$single_emp['full_name'].",<br> You can create your new password from here: <a href='".$site_url."new_password.php?req_email=".md5($single_emp['id']).$single_emp['id']."&reset_token=".$token."'><u>".$site_url."new_password.php?req_email=".md5($single_emp['id']).$single_emp['id']."&reset_token=".$token."</u></a><br><br>Regards,<br> Timesheethub.");
            mysqli_query($conn, "UPDATE db_users SET reset_token='".$token."', reset_token_status=0 WHERE id=".$single_emp['id']);
        } else {
            $error = '<p class="error_red" style="color:red;">Entered email is mismatch with company.</p>';
        }
    endif;
    } else {
        $error = '<p class="error_red" style="color:red;">Company name not found.</p>';
    }
}
?>
<!doctype html>
<html class="no-js" lang="en" dir="ltr">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Timesheethub | Reset Password</title>
    <link rel="icon" href="<?= $site_url; ?>favicon.ico" type="image/x-icon"> <!-- Favicon-->

    <!-- project css file  -->
    <link rel="stylesheet" href="<?= $site_url; ?>/assets/css/timetracker.style.min.css">
</head>
<body>
    <div id="timetracker-layout" class="theme-mist">

        <!-- main body area -->
        <div class="main p-2 py-3 p-xl-5">

            <!-- Body: Header -->
            <div class="container-xxl">
                <!-- header rightbar icon -->
                <div class="row align-items-center">
                    <div class="col">
                        <a href="index.php" class="d-flex align-item-center">
                            <i class="icofont-stopwatch fs-3"></i>
                            <h5 class="mb-0 mt-1 mx-2">Timesheethub</h5>
                        </a>
                    </div>
                    <div class="col-auto">
                        <div class="d-flex align-item-center">
                            <!--<div class="dropdown">
                                <a class="nav-link dropdown-toggle pulse" href="#" role="button" data-bs-toggle="dropdown">
                                    <img src="../assets/images/flag/GB.png" alt="">
                                </a>
                                <div class="dropdown-menu rounded-lg shadow border-0  dropdown-menu-md-end p-0 m-0">
                                    <div class="card border-0">
                                        <ul class="list-unstyled py-2 px-3">
                                            <li>
                                                <a href="#" class=""><img src="../assets/images/flag/GB.png" alt=""> English</a>
                                            </li>
                                            <li>
                                                <a href="#" class=""><img src="../assets/images/flag/DE.png" alt=""> German</a>
                                            </li>
                                            <li>
                                                <a href="#" class=""><img src="../assets/images/flag/FR.png" alt=""> French</a>
                                            </li>
                                            <li>
                                                <a href="#" class=""><img src="../assets/images/flag/IT.png" alt=""> Italian</a>
                                            </li>
                                            <li>
                                                <a href="#" class=""><img src="../assets/images/flag/RU.png" alt=""> Russian</a>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>-->
                            <!-- <div class="setting ms-2 mt-1">
                                <a href="#" data-bs-toggle="modal" data-bs-target="#Settingmodal">
                                    <svg xmlns="http://www.w3.org/2000/svg"  x="0px" y="0px" width="30px" height="30px" viewBox="0 0 38 38">
                                        <path   d="M19,28c-4.964,0-9-4.04-9-9c0-4.964,4.036-9,9-9c4.96,0,9,4.036,9,9  C28,23.96,23.96,28,19,28z M19,16c-1.654,0-3,1.346-3,3s1.346,3,3,3s3-1.346,3-3S20.654,16,19,16z" style="fill:var(--primary-color);" data-st="fill:var(--chart-color4);"></path>
                                        <path class="st0" d="M19,24c-2.757,0-5-2.243-5-5s2.243-5,5-5s5,2.243,5,5S21.757,24,19,24z M19,16c-1.654,0-3,1.346-3,3  s1.346,3,3,3s3-1.346,3-3S20.654,16,19,16z M32,19c0-1.408-0.232-2.763-0.648-4.034l3.737-1.548l-0.766-1.848l-3.743,1.551  c-1.25-2.452-3.251-4.452-5.702-5.701l1.551-3.744l-1.848-0.765l-1.548,3.737C21.762,6.232,20.409,6,19,6  c-1.409,0-2.756,0.248-4.026,0.668l-1.556-3.756L11.57,3.677l2.316,5.592C15.416,8.462,17.154,8,19,8c6.065,0,11,4.935,11,11  s-4.935,11-11,11S8,25.065,8,19c0-3.031,1.232-5.779,3.222-7.771L9.808,9.816c-0.962,0.963-1.764,2.082-2.388,3.306l-3.744-1.551  l-0.765,1.847l3.738,1.548C6.232,16.238,6,17.592,6,19c0,1.409,0.232,2.763,0.648,4.034l-3.737,1.548l0.766,1.848l3.744-1.551  c1.25,2.451,3.25,4.451,5.701,5.7l-1.551,3.744l1.848,0.766l1.548-3.737C16.237,31.768,17.591,32,19,32s2.762-0.232,4.033-0.648  l1.549,3.737l1.848-0.766l-1.552-3.743c2.451-1.25,4.451-3.25,5.701-5.701l3.744,1.551l0.765-1.848l-3.736-1.548  C31.768,21.763,32,20.409,32,19z"></path>
                                    </svg>
                                </a>
                            </div> -->
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Body: Body -->
            <div class="body d-flex p-0 p-md-2 p-xl-5">
                <div class="container-xxl">
                    <div class="row g-0 border border-secondary rounded-3 mt-2 mt-md-0">
                        <div class="col-lg-6 d-flex justify-content-center align-items-center bg-secondary py-2 py-md-0" style="padding: 6% !important;">
                            <div class="d-flex flex-column p-2 reset-password">
                                <h1>Reset Your Password</h1>
                                
                                <div class="tab-content mt-4 mb-3">
                                    <div class="tab-pane fade show active" id="Email">
                                        <div class="card">
                                            <div class="card-body p-4">
                                                <?= $error; ?>
                                                <form method="post" class="reset-form <?= ($is_success== 1) ? "disabled" : ""; ?>" <?= ($is_success== 1) ? "disabled" : ""; ?>>
                                                <div class="mb-3"> 
                                                        <label class="form-label fs-6">Company</label>
                                                        <input type="text" class="form-control" name="req_company" value="<?= $_POST['req_company']; ?>" required="">
                                                    </div>
                                                    <div class="mb-3">
                                                        <label class="form-label fs-6">Email address</label>
                                                        <input type="email" class="form-control" name="req_email" value="<?= $_POST['req_email']; ?>" required="">
                                                    </div>
                                                    <input type="submit" name="sub_form" value="Next" class="btn btn-primary text-uppercase py-2 fs-5 w-100 mt-">
                                                    <!-- <a href="auth-two-step.html" class="btn btn-primary text-uppercase py-2 fs-5 w-100 mt-2">Next</a> -->
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                    
                                </div>
                            </div>
                        </div>

                        <div class="col-lg-6 d-none d-lg-flex justify-content-center align-items-center auth-h100">
                            <div class="qr-block text-center">
                               <div class="col">
                                    <a href="/" class="timetracker-logo align-item-center">
                                        <i class="icofont-stopwatch fs-3" style="font-size: 70px !important;line-height: 70px !important;"></i>
                                        <h5 class="logo-text">Timesheethub</h5>
                                    </a>
                                </div>
                                
                            </div>
                        </div>
                    </div> <!-- End Row -->
                    
                </div>
            </div>

            <!-- Modal Custom Settings-->
            <div class="modal fade right" id="Settingmodal" tabindex="-1"  aria-hidden="true">
                <div class="modal-dialog  modal-sm">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title">Custom Settings</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body custom_setting">
                            <!-- Settings: Color -->
                            <div class="setting-theme pb-3">
                                <h6 class="card-title mb-2 fs-6 d-flex align-items-center"><i class="icofont-color-bucket fs-4 me-2 text-primary"></i>Template Color Settings</h6>
                                <ul class="list-unstyled row row-cols-3 g-2 choose-skin mb-2 mt-2">
                                    <li data-theme="indigo"><div class="indigo"></div></li>
                                    <li data-theme="tradewind"><div class="tradewind"></div></li>
                                    <li data-theme="monalisa"><div class="monalisa"></div></li>
                                    <li data-theme="mist" class="active"><div class="mist"></div></li>
                                    <li data-theme="cyan"><div class="cyan"></div></li>
                                    <li data-theme="green"><div class="green"></div></li>
                                    <li data-theme="orange"><div class="orange"></div></li>
                                    <li data-theme="blush"><div class="blush"></div></li>
                                    <li data-theme="red"><div class="red"></div></li>
                                </ul>
                            </div>
                            <!-- Settings: Template dynamics -->
                            <div class="dynamic-block py-3">
                                <ul class="list-unstyled choose-skin mb-2 mt-1">
                                    <li data-theme="dynamic"><div class="dynamic"><i class="icofont-paint me-2"></i> Click to Dyanmic Setting</div></li>
                                </ul>
                                <div class="dt-setting">
                                    <ul class="list-group list-unstyled mt-1">
                                        <li class="list-group-item d-flex justify-content-between align-items-center py-1 px-2">
                                            <label>Primary Color</label>
                                            <button id="primaryColorPicker" class="btn bg-primary avatar xs border-0 rounded-0"></button>
                                        </li>
                                        <li class="list-group-item d-flex justify-content-between align-items-center py-1 px-2">
                                            <label>Secondary Color</label>
                                            <button id="secondaryColorPicker" class="btn bg-secondary avatar xs border-0 rounded-0"></button>
                                        </li>
                                        <li class="list-group-item d-flex justify-content-between align-items-center py-1 px-2">
                                            <label class="text-muted">Chart Color 1</label>
                                            <button id="chartColorPicker1" class="btn chart-color1 avatar xs border-0 rounded-0"></button>
                                        </li>
                                        <li class="list-group-item d-flex justify-content-between align-items-center py-1 px-2">
                                            <label class="text-muted">Chart Color 2</label>
                                            <button id="chartColorPicker2" class="btn chart-color2 avatar xs border-0 rounded-0"></button>
                                        </li>
                                        <li class="list-group-item d-flex justify-content-between align-items-center py-1 px-2">
                                            <label class="text-muted">Chart Color 3</label>
                                            <button id="chartColorPicker3" class="btn chart-color3 avatar xs border-0 rounded-0"></button>
                                        </li>
                                        <li class="list-group-item d-flex justify-content-between align-items-center py-1 px-2">
                                            <label class="text-muted">Chart Color 4</label>
                                            <button id="chartColorPicker4" class="btn chart-color4 avatar xs border-0 rounded-0"></button>
                                        </li>
                                        <li class="list-group-item d-flex justify-content-between align-items-center py-1 px-2">
                                            <label class="text-muted">Chart Color 5</label>
                                            <button id="chartColorPicker5" class="btn chart-color5 avatar xs border-0 rounded-0"></button>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            <!-- Settings: Font -->
                            <div class="setting-font py-3">
                                <h6 class="card-title mb-2 fs-6 d-flex align-items-center"><i class="icofont-font fs-4 me-2 text-primary"></i> Font Settings</h6>
                                <ul class="list-group font_setting mt-1">
                                    <li class="list-group-item py-1 px-2">
                                        <div class="form-check mb-0">
                                            <input class="form-check-input" type="radio" name="font" id="font-poppins" value="font-poppins">
                                            <label class="form-check-label" for="font-poppins">
                                                Poppins Google Font
                                            </label>
                                        </div>
                                    </li>
                                    <li class="list-group-item py-1 px-2">
                                        <div class="form-check mb-0">
                                            <input class="form-check-input" type="radio" name="font" id="font-opensans" value="font-opensans">
                                            <label class="form-check-label" for="font-opensans">
                                                Open Sans Google Font
                                            </label>
                                        </div>
                                    </li>
                                    <li class="list-group-item py-1 px-2">
                                        <div class="form-check mb-0">
                                            <input class="form-check-input" type="radio" name="font" id="font-montserrat" value="font-montserrat">
                                            <label class="form-check-label" for="font-montserrat">
                                                Montserrat Google Font
                                            </label>
                                        </div>
                                    </li>
                                    <li class="list-group-item py-1 px-2">
                                        <div class="form-check mb-0">
                                            <input class="form-check-input" type="radio" name="font" id="font-Plex" value="font-Plex" checked="">
                                            <label class="form-check-label" for="font-Plex">
                                                Plex Google Font
                                            </label>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                            <!-- Settings: Light/dark -->
                            <div class="setting-mode py-3">
                                <h6 class="card-title mb-2 fs-6 d-flex align-items-center"><i class="icofont-layout fs-4 me-2 text-primary"></i>Contrast Layout</h6>
                                <ul class="list-group list-unstyled mb-0 mt-1">
                                    <li class="list-group-item d-flex align-items-center py-1 px-2">
                                        <div class="form-check form-switch theme-switch mb-0">
                                            <input class="form-check-input" type="checkbox" id="theme-switch">
                                            <label class="form-check-label" for="theme-switch">Enable Dark Mode!</label>
                                        </div>
                                    </li>
                                    <li class="list-group-item d-flex align-items-center py-1 px-2">
                                        <div class="form-check form-switch theme-high-contrast mb-0">
                                            <input class="form-check-input" type="checkbox" id="theme-high-contrast">
                                            <label class="form-check-label" for="theme-high-contrast">Enable High Contrast</label>
                                        </div>
                                    </li>
                                    <li class="list-group-item d-flex align-items-center py-1 px-2">
                                        <div class="form-check form-switch theme-rtl mb-0">
                                            <input class="form-check-input" type="checkbox" id="theme-rtl">
                                            <label class="form-check-label" for="theme-rtl">Enable RTL Mode!</label>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                        </div>
                        <div class="modal-footer justify-content-start">
                            <button type="button" class="btn btn-white border lift" data-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary lift">Save Changes</button>
                        </div>
                    </div>
                </div>
            </div>

            
            
        </div>

    </div>

      <!-- Jquery Core Js -->
    <script src="<?= $site_url; ?>/assets/bundles/libscripts.bundle.js"></script>

    <!-- Jquery Page Js -->
    <script src="<?= $site_url; ?>/js/template.js"></script>
    <style type="text/css">
        .reset-password h1 {
            color: #fff;
        }
        form.reset-form.disabled {
            pointer-events: none;
            opacity: 0.5;
        }
    </style>
</body>

</html>