<?php
/**
 * Created by PhpStorm.
 * User: Kavand
 * Date: 2/22/2019
 * Time: 11:45 PM
 */
header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

require_once 'ResourcesHolder/core/CoreSystem.php';
require_once 'ResourcesHolder/core/DAL/DAL.php';
$loginState = false;
$DAL = new DAL('constant.php');
$index = new CoreSystem();
if (true or $index->checkState() AND $index->checkStateKey("client-page")) { ?>
    <?php if ($index->CheckAllDataInArray(array('page-mode', 'page-id', 'login-as'), $_REQUEST)) { ?>
        <?php
        switch ($_REQUEST['login-as']) {
            case 'acceptor':
                $select = $DAL->Select("SELECT `IsActive`, `pNumber` as `mobile`, CONCAT(`fName`, ' ', `lName`) As `full-name` FROM `app_acceptor_list` WHERE `ID`='{$_REQUEST['page-id']}'");
                if ($select) {
                    $select = $select[0];
                    if ($select['IsActive'] == true) {
                        require_once("ResourcesHolder/core/ClientPanel.inc");
                        $panel = new ClientPanel($DAL, $index);//$select['full-name'])
                        $panel->RunPanel('', $_REQUEST['page-id'], $select['full-name'], $select['mobile'], $_REQUEST['login-as']);
                    } else {
                        goto Error500;
                    }
                } else {
                    goto Error500;
                }
                break;
            case 'client':
                $select = $DAL->Select("SELECT `Mobile`as `mobile`, `Mode` as mode, CONCAT(`FirstName`, ' ', `LastName`) As `full-name` FROM `app_client_register` WHERE `ID`='{$_REQUEST['page-id']}'");
                if ($select) {
                    $select = $select[0];
                    if ($select['mode'] != '0' AND $_REQUEST['page-mode'] === $select['mode']) {
                        require_once("ResourcesHolder/core/ClientPanel.inc");
                        $panel = new ClientPanel($DAL, $index);//$select['full-name'])
                        $panel->RunPanel($select['mode'], $_REQUEST['page-id'], $select['full-name'], $select['mobile'], $_REQUEST['login-as']);
                    } else {
                        goto Error500;
                    }
                } else {
                    goto Error500;
                }
                break;
            default:
                goto Error500;
                break;
        }
        ?>
    <?php } else { ?>

        <script type="text/javascript">
            <?php
            echo "\n";
            echo "let FunctionCall = '';";
            if ($index->CheckAllDataInArray(array('mode'), $_REQUEST)) {
                require_once 'ResourcesHolder/core/ApplicationSystem.php';
                $app = new ApplicationSystem(@$_REQUEST, "NO-HEADER");
                switch ($_REQUEST['mode']) {
                    case "register":
                        $loginState = true;
                        echo "let mode = true;";
                        echo "FunctionCall='register';";
                        if ($index->CheckAllDataInArray(
                            array('fName', 'lName', 'nCode', 'bnCode', 'bank', 'mobile'), $_REQUEST)) {
                            $output = $app->ClientRegister(@$_REQUEST);
                            echo "let send_state = " . ($output['error'] === 0 ? "true" : "false") . ";";
                            echo "\n";
                            echo 'let message = "' . str_replace("\n", "", $output['msg']) . '";';
                            echo "\n";
                        }
                        break;
                    case "login-client":
                        $loginState = true;
                        echo "let mode = true;";
                        echo "FunctionCall='login';";
                        if ($index->CheckAllDataInArray(
                            array('nCode', 'mobile'), $_REQUEST)) {
                            $output = $app->ClientLogin(@$_REQUEST);
                            echo "let send_state = " . ($output['error'] === 0 ? "true" : "false") . ";";
                            echo "\n";
                            echo "let id = " . $output['id'] . ";";
                            echo "\n";
                            echo "let ClientMode = " . $output['mode'] . ";";
                            echo "\n";
                            echo "let state = 'client';";
                            echo "\n";
                            echo 'let message = "' . str_replace("\n", "", $output['msg']) . '";';
                            echo "\n";
                        }
                        break;
                    case "login-acceptor":
                        $loginState = true;
                        echo "let mode = true;";
                        echo "FunctionCall='login';";
                        if ($index->CheckAllDataInArray(
                            array('nCode', 'mobile'), $_REQUEST)) {
                            $output = $app->AcceptorLogin(@$_REQUEST);
                            echo "let send_state = " . ($output['error'] === 0 ? "true" : "false") . ";";
                            echo "\n";
                            echo "let id = " . $output['id'] . ";";
                            echo "\n";
                            echo "let ClientMode = " . $output['mode'] . ";";
                            echo "\n";
                            echo "let state = 'acceptor';";
                            echo "\n";
                            echo 'let message = "' . str_replace("\n", "", $output['msg']) . '";';
                            echo "\n";
                        }
                        break;
                    default:
                        $loginState = false;
                        echo "let mode = false;";
                        break;
                }
            } else {
                $loginState = false;
                echo "let mode = false;";
            }
            ?>
            if (typeof window.android !== "undefined") {
                if (FunctionCall !== '') {
                    switch (FunctionCall) {
                        case 'login':
                            if (mode) {
                                console.log(window.android.login(message, send_state, id, ClientMode, state));
                            }
                            break;
                        case 'register':
                            if (mode) {
                                console.log(window.android.register(message, send_state));
                            }
                            break;
                    }
                }
            } else {
                if (FunctionCall !== '') {
                    switch (FunctionCall) {
                        case 'login':
                            if (mode) {
                                alert(message);
                                window.location = "ClientPage.php?page-mode=" + ClientMode + "&page-id=" + id + "&login-as=" + state;
                            }
                            break;
                    }
                }

            }
        </script>

        <?php if (!$loginState) { ?>
            <!DOCTYPE html>
            <html>
            <head>
                <title></title>
                <!-- for-mobile-apps -->
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                <meta name="keywords" content=""/>
                <script type="application/x-javascript">
                    addEventListener("load", function () {
                        setTimeout(hideURLbar, 0);
                    }, false);

                    function hideURLbar() {
                        window.scrollTo(0, 1);
                    }
                </script>
                <!-- //for-mobile-apps -->
                <link href="Client/css/bootstrap.css" rel="stylesheet" type="text/css" media="all"/>
                <link href="Client/css/style.css" rel="stylesheet" type="text/css" media="all"/>
                <!-- js -->
                <script type="text/javascript" src="Client/js/jquery-2.1.4.min.js"></script>
                <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
                <!-- //js -->
                <link rel="stylesheet" href="Client/css/clndr.css" type="text/css"/>
                <link rel="stylesheet" href="Client/css/percircle.css">
                <link href="Client/css/fabochart.css" rel="stylesheet" type="text/css">
                <!-- player -->
                <link href="Client/css/acornmediaplayer.base.css" rel="stylesheet" type="text/css">
                <link href="Client/acornmediaplayer/acorn.access.css" rel="stylesheet" type="text/css">
                <!-- //player -->
                <!--skycons-icons-->
                <script src="Client/js/skycons.js"></script>
                <!--//skycons-icons-->
                <link href='//fonts.googleapis.com/css?family=Bitter:400,400italic,700' rel='stylesheet'
                      type='text/css'>
                <?php $index->publicLoadPlugin(array('Google Fonts Roboto', 'Google Fonts Material', 'Animation Css', 'font awesome', 'Farsi Font')); ?>
            </head>
            <body>
            <div class="smart-main fBTitrBold">
                <h6 class="title ">کاشان کارت</h6>
                <!-- navigation -->
                <div class="navigation">
                    <div class="logo">
                        <h1><a href="javascript:void(0);">پنل کاربری</a></h1>
                    </div>
                </div>
                <div class="climate-icons-right">
                    <div class="sap_tabs">

                        <div id="horizontalTab" style="display: block; width: 100%; margin: 0px;">

                            <ul>
                                <li class="resp-tab-item" aria-controls="tab_item-0" role="tab"><span>ثبت نام</span>
                                </li>
                                <li class="resp-tab-item" aria-controls="tab_item-1" role="tab"><span>ورود</span></li>
                            </ul>
                            <!---->
                            <div class="tab-1 resp-tab-content" aria-labelledby="tab_item-0">
                                <div class="facts">
                                    <div class="register">
                                        <form method="get">
                                            <input placeholder="نام" class="name" type="text" name="fName"
                                                   required="required" dir="rtl">
                                            <input placeholder="نام خانوادگی" class="name" type="text" name="lName"
                                                   required="required" dir="rtl"/>
                                            <input placeholder="کد ملی" class="name" type="text" name="nCode"
                                                   required="required" dir="rtl" minlength="10" maxlength="10"/>
                                            <input placeholder="کد 12 رقمی پشت کاشان کارت" class="name" type="text"
                                                   name="bnCode"
                                                   required="required" dir="rtl" minlength="12" maxlength="12"/>
                                            <input placeholder="شماره کارت بانکی" class="name" type="text" name="bank"
                                                   required="required" dir="rtl" minlength="16" maxlength="16"/>
                                            <input placeholder="شماره موبایل" class="name" type="text" name="mobile"
                                                   required="required" dir="rtl" minlength="11" maxlength="11"/>
                                            <input hidden type="hidden" value="register" name="mode">
                                            <div class="sign-up">
                                                <input type="submit" value="ثبت نام"/>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            <style>
                                input[type=radio] {
                                    position: absolute;
                                    visibility: hidden;
                                }

                                input[type=radio]:checked ~ .check {
                                    border: 5px solid #0DFF92;
                                }

                                input[type=radio]:checked ~ .check::before {
                                    background: #0DFF92;
                                }

                                input[type=radio]:checked ~ label {
                                    color: #0DFF92;
                                }

                            </style>
                            <div class="tab-2 resp-tab-content" aria-labelledby="tab_item-1">
                                <div class="facts">
                                    <div class="register">
                                        <form id="login-to-panel" action="ClientPage.php" method="get"
                                              class="login-to-panel">
                                            <input placeholder="شماره موبایل" class="name user-name" type="text"
                                                   name="mobile"
                                                   required="required" dir="rtl" minlength="11" maxlength="11"/>
                                            <input placeholder="کد ملی" class="password nCode" type="text"
                                                   name="nCode"
                                                   required="required" dir="rtl" minlength="10" maxlength="10"/>
                                            <div class="row">
                                                <div class="column">
                                                    <div class="sign-up">
                                                            <div class="">
                                                                <input type="radio" id="s-option" name="mode"
                                                                       value="login-acceptor">
                                                                <label class="btn btn-default btn-block" for="s-option">پذیرنده</label>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="column">
                                                        <div class="sign-up">
                                                            <div class="">
                                                                <input type="radio" id="t-option" name="mode"
                                                                       value="login-client">
                                                                <label class="btn btn-default btn-block" for="t-option">کاربر</label>
                                                            </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <button type="submit">
                                                ورود
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <script src="Client/js/easyResponsiveTabs.js" type="text/javascript"></script>
                        <script type="text/javascript">
                            $(document).ready(function () {
                                $('#horizontalTab').easyResponsiveTabs({
                                    type: 'default', //Types: default, vertical, accordion
                                    width: 'auto', //auto or any width like 600px
                                    fit: true   // 100% fit in a container
                                });
                            });
                        </script>
                    </div>

                    <!-- //newsletter -->
                    <div class="footer">
                        <p> © <?php echo date("Y"); ?> طراحی توسط کاشان کارت </p>
                    </div>
                </div>
            </div>
            <script src="Client/js/bootstrap.js"></script>
            <script type="text/javascript">

                let init_login = function () {
                    let buttonLogin = null;
                    $('.login').unbind().bind('tap, click', function (e) {
                        buttonLogin = this;
                    });

                    /*     $('form.login-to-panel').on('submit', function (e) {
                             let location = window.location;
                             location = location.substr(0, location.indexOf('.php'));
                             let userPassword = $('input.user-password').val() + "";
                             userPassword.replace('"', '');
                             userPassword.replace('%22', '');
                             location += ".php?nCode=" + userPassword + "&mobile=" + $('input.user-name').val() + "&mode=" + $(buttonLogin).val();
                             window.location = location.replace('"', '');
                         });*/
                };

                $(window).on("load", init_login);
            </script>
            <?php
            $index->publicLoadPlugin(array());
            ?>
            </body>
            </html>
        <?php } ?>
    <?php } ?>
<?php } else {
    Error500:
    ?>
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html>
    <head>
        <title>500 Internal Server Error</title>
    </head>
    <body>
    <h1>Internal Server Error</h1>
    <p>The server encountered an internal error or
        misconfiguration and was unable to complete
        your request.</p>
    <p>Please contact the server administrator at
        webmaster@android.kashancard.digihelp.net to inform them of the time this error occurred,
        and the actions you performed just before this error.</p>
    <p>More information about this error may be available
        in the server error log.</p>
    <p>Additionally, a 500 Internal Server Error
        error was encountered while trying to use an ErrorDocument to handle the request.</p>
    </body>
    </html>
<?php } ?>