<?php
//Check login các thứ
if(!isLoggedIn()) redirect(1,'login');
// common class
$common = new common();

// Retrieve Account Information
$accountInfo = $common->accountInformation($_SESSION['userid']);
if(!is_array($accountInfo)) throw new Exception(lang('error_12',true));
# account online status
$onlineStatus = ($common->accountOnline($_SESSION['username']) ? '<span class="label label-success">'.lang('myaccount_txt_9',true).'</span>' : '<span class="label label-danger">'.lang('myaccount_txt_10',true).'</span>');
# account status
$accountStatus = ($accountInfo[_CLMN_BLOCCODE_] == 1 ? '<span class="label label-danger">'.lang('myaccount_txt_8',true).'</span>' : '<span class="label label-default">'.lang('myaccount_txt_7',true).'</span>');
echo '<span style="color:#FFF;font-size: 13px;">'.$_SESSION['username'].':'.$onlineStatus.'/'.$accountStatus.'</span>';


if (isset($_POST['submit'])) {
    if (!isset($_POST['telco']) || !isset($_POST['amount']) || !isset($_POST['serial']) || !isset($_POST['code'])) {
        $err = 'Bạn cần nhập đầy đủ thông tin';
    } else {

        $request_id = $_SESSION['username'].'-'.rand(100000000, 999999999);  //Mã đơn hàng của bạn
        $command = 'charging';  // Nap the
        $url = 'https://api.naptudong.com/chargingws/v2';
        $partner_id = '4804813061';
        $partner_key = 'a0dc02449994c55e9d0d9a356b7c4e51';

        $dataPost = array();
        $dataPost['request_id'] = $request_id;
        $dataPost['code'] = $_POST['code'];
        $dataPost['partner_id'] = $partner_id;
        $dataPost['serial'] = $_POST['serial'];
        $dataPost['telco'] = $_POST['telco'];
        $dataPost['command'] = $command;
        ksort($dataPost);
        $sign = $partner_key;
        foreach ($dataPost as $item) {
            $sign .= $item;
        }
        
        $mysign = md5($sign);

        $dataPost['amount'] = $_POST['amount'];
        $dataPost['sign'] = $mysign;

        $data = http_build_query($dataPost);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        curl_setopt($ch, CURLOPT_REFERER, $actual_link);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);

        $obj = json_decode($result);
    
        if ($obj->status == 99) {
            //Gửi thẻ thành công, đợi duyệt.
            echo '<pre>';
            print_r($obj->message);
            echo '</pre>';
        } elseif ($obj->status == 1 || $obj->status == 2) {
            //Thành công
            echo '<pre>';
            print_r($obj->message);
            echo '</pre>';
        } elseif ($obj->status == 3) {
            //Thẻ lỗi
            echo '<pre>';
            print_r($obj->message);
            echo '</pre>';
        } elseif ($obj->status == 4) {
            //Bảo trì
            echo '<pre>';
            print_r($obj->message);
            echo '</pre>';
        } else {
            //Lỗi
            echo '<pre>';
            print_r($obj->message);
            echo '</pre>';
        }


    }
}
?>


    <div class="row" style="margin-top: 50px;">
        <div class="col-md-8" style="float:none;margin:0 auto;color: #fff;font-size: 12px;">
            <form method="POST" action="">
                <div class="form-group">
                    <label>Loại thẻ:</label>
                    <select class="form-control" name="telco">
                        <option value="">Chọn loại thẻ</option>
                        <option value="VIETTEL">Viettel</option>
                        <option value="VIETTELAUTO">Viettel Auto</option>
                        <option value="MOBIFONE">Mobifone</option>
                        <option value="MOBIFONEAUTO">Mobifone Auto</option>
                        <option value="VINAPHONE">Vinaphone</option>
                        <option value="VINAPHONEAUTO">Vinaphone Auto</option>
                        <option value="GATE">Gate</option>
                        <option value="ZING">Zing</option>
                        <option value="MEGACARD">Megacard</option>
                        <option value="BIT">BIT</option>
                        <option value="GARENA">Garena</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Mệnh giá:</label>
                    <select class="form-control" name="amount">
                        <option value="">Chọn mệnh giá</option>
                        <option value="10000">10.000</option>
                        <option value="20000">20.000</option>
                        <option value="30000">30.000</option>
                        <option value="50000">50.000</option>
                        <option value="100000">100.000</option>
                        <option value="200000">200.000</option>
                        <option value="300000">300.000</option>
                        <option value="500000">500.000</option>
                        <option value="1000000">1.000.000</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Số seri:</label>
                    <input type="text" class="form-control" name="serial"/>
                </div>
                <div class="form-group">
                    <label>Mã thẻ:</label>
                    <input type="text" class="form-control" name="code"/>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-success btn-block" name="submit">NẠP NGAY</button>
                </div>
            </form>
        </div>
    </div>
