<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

include("asl.php");



?>



<!DOCTYPE HTML>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html ; charset=utf-8" />
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="persian" />

    <title>طراحی ربات ahp</title>


    <link rel="Stylesheet" type="text/css" href="style.css" />


    <style>
        @font-face {
            font-family: 'amir';
            src: url('font/BKoodakBold.ttf')format('truetype'),
                url('font/BKoodakBold.eot')format('eot'),
                url('font/BKoodakBold.woff')format('woff');

        }
    </style>


</head>

<body dir="rtl" align="center">
    <div align="center">
        <?php

        //   import_request_variables("p" , "a_");


        //old import
        function checkarray($summerarray, $mainkey, $mainarray)
        {
            if (in_array($mainkey, $summerarray)) {
                unset($mainarray["$mainkey"]);
                return $mainarray;
            } else {
                return "ok";
            }
        }

        $summerarray = array();
        $artest = $_POST;
        foreach ($artest as $postinfo) {

            $ar_mainkey = array_search($postinfo, $artest);
            if (checkarray($summerarray, $ar_mainkey, $artest) != "ok") {
                $artest = checkarray($summerarray, $ar_mainkey, $artest);
                $ar_mainkey = array_search($postinfo, $artest);
            }
            ${'a_' . $ar_mainkey} = $postinfo;
            $summerarray[] = $ar_mainkey;
        }
        //old import


        try {
            $conn = new PDO("mysql:host=$servername;dbname=$mydb", $username, $password,  array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
            // set the PDO error mode to exception

            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
            echo "Connection failed: " . $e->getMessage();
        }




        $sql = "select * from admin";
        $stmt =   $conn->prepare($sql);
        $stmt->execute();

        $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $show = $stmt->fetch();

        $user = $show["user"];
        $pass = $show["pass"];


        $getpass = hash_hmac('md5', $a_pass, 'salt');


        if ($user == $a_user && $pass == $getpass) {
        } else {





            echo "<p style='color:green' >یوزرنیم و پسورد اشتباه
</p>";




            function redirect($url)
            {
                if (!headers_sent()) {
                    header('Location: ' . $url);
                    exit;
                } else {
                    echo '<script type="text/javascript">';
                    echo 'window.location.href="' . $url . '";';
                    echo '</script>';
                    echo '<noscript>';
                    echo '<meta http-equiv="refresh" content="0;url=' . $url . '" />';
                    echo '</noscript>';
                    exit;
                }
            }

            redirect("enter.php");
            exit();
        }








        $content = file_get_contents("php://input");


        include('../class/bot.php');
        include('../class/db.php');
        
        $bot = new bot("7305082680:AAFueBDE7l1iTJPHsmZ0ERsf_2FBZfwQwzc", $content);








        ?>

        <br /><br />
        <h4 class="white">پنل مدیریتی ربات</h4>
        <div id="content" align="right">
            <div id="content-content">
    

            <table>
                    <tr>
                        <td><img class="radius" src="images/user.png" /></td>
                        <td class="gray" valign="bottom">خوش آمدید <?php print $userse; ?></td>

                        <!-- <td>
                            <form target="_blank" method='post' action='categories.php'>


                                <input type='submit' style='width:100%' class='font' value='دسته بندی ها' />
                                <input type='hidden' name='user' value="<?php echo $a_user  ?>" />
                                <input type='hidden' name='pass' value="<?php echo $a_pass  ?>" class='font' />
                            </form>
                        </td> -->

                        <td>
                            <form target="_blank" method='post' action='users.php'>


                                <input type='submit' style='width:100%' class='font' value='نمایش کاربران' />
                                <input type='hidden' name='user' value="<?php echo $a_user  ?>" />
                                <input type='hidden' name='pass' value="<?php echo $a_pass  ?>" class='font' />
                            </form>
                        </td>




                    </tr>

                </table>

    <hr />
    <hr />
    <hr />



                    <table>
                        <tr>

                            <th class='th'>اسم</th>
                            <th class='th'>یوزرنیم</th>
                            <th class='th'>متن سفارش</th>
                            <th class='th'>متن ارسالی</th>
                            <th class='th'>ارسال</th>

                        </tr>
                        <tr>





                            <?php

                            /* if (isset($_POST['new_balance'])) {
                                $sql = "update users set balance='" . $_POST['new_balance'] . "'  where chatid='" . $_POST['chatid'] . "' ";
                                $conn->exec($sql);
                                print("انجام شد");
                            } */

                            if (isset($_POST['delete'])) {
                                $sql = "delete from products where id='$_POST[id]'";
                                $conn->exec($sql);
                                print("انجام شد");
                             }

                             if (isset($_POST['send'])) {

                                $answer = $_POST['answer'];
                                $message = $_POST['message'];
                                $chatid = $_POST['chatid'];
                                $telname = $_POST['telname'];
                                $username = $_POST['username'];
                                $status = "در انتظار پرداخت";
                                $id = $_POST['id'];
                                $sql = "update orders set  answer='$answer', `status`='sent'  where id='$id'";
                                $conn->exec($sql);


                                $sql = "insert into basket (message, chatid, username, telname, answer, `status`) values ('$message', '$chatid', '$username', '$telname', '$answer', '$status')";
                                $conn->exec($sql);



                                

                                $last_id = $conn->lastInsertId();
                                $key = json_encode(array("inline_keyboard" => array(
                                    array(array("callback_data" => "id_$last_id", "text" => "ارسال عکس فیش پرداخت")),
                                )));
                                // print('$idOfInsertedRow : ' . $last_id);

                                // var_dump($_POST);
                                print("انجام شد");


                                $bot->sendmessage(urlencode($answer), $_POST['chatid'], $key);

                             }
                            $stmt = $conn->prepare("SELECT * FROM orders where `status` = 'no' ");
                            $stmt->execute();

                            // set the resulting array to associative
                            $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

                            $k = $stmt->fetchAll();
                            $num = $stmt->rowCount();

                            print " <h5 class='gray'>◄ تعداد فروشگاه ها: $num</h5>";

                            foreach ($k as $v) {
                                print "
                                    <tr>
                                    <form method='post' action='orders.php'>
                                        <td align='center' class='td'>". $v["telname"] ."</td>
                                        <td align='center' class='td'>". $v["username"] ."</td>
                                        <td align='center' class='td'><p name='message' >". $v["message"] ."</p></td>
                                        <td align='center' class='td'><textarea name='answer' ></textarea></td>
                                        <td align='center' class='td'>
                                            <input type='submit' name='send' style='width:100%' class='font' value='ارسال' />
                                            <input type='hidden' name='user' value='".$a_user."' />
                                            <input type='hidden' name='pass' value='".$a_pass."' class='font' />
                                            <input type='hidden' name='id' value='".$v["id"]."' />
                                            <input type='hidden' name='chatid' value='".$v["chatid"]."' />
                                            <input type='hidden' name='telname' value='".$v["telname"]."' />
                                            <input type='hidden' name='username' value='".$v["username"]."' />
                                            <input type='hidden' name='message' value='".$v["message"]."' />
                                        </form>
                                        </td>
                                       
                                    </tr>
                                ";
                            }
                            ?>
                            











                        </tr>



                    </table>

                    















                    <hr />
                    <hr />
                    <hr />

                    <br />
                    <br />
                    <br />
                    <br />






                </div>



















                <br />
                <br />
            </div>
        </div>




        <a style="text-decoration:none ;" href="http://ahpbot.ir" target="_blank" class="gray">طراحی شده توسط ahpbot</a>
    </div>

    <?php
    $conn = null;
    ?>
</body>

</html>