<!DOCTYPE html>
<html lang="vi">

<head>
    <?php require_once('head.php');?>
    <title>AdminLTE 3 | Dashboard</title>
    <?php require_once('nav.php');?>
</head>
<?php
if(isset($_GET['id'])){
    $id = $_GET['id'];
    $toz_code =  $ketnoi->query("SELECT * FROM `users` WHERE `id` = '$id' ")->fetch_array();
}
?>
<?php
$now = time();
if (isset($_POST["submit"]))
{
  $create = mysqli_query($ketnoi,"UPDATE `users` SET 
    `username` = '".$_POST['username']."',
    `email` = '".$_POST['email']."',
    `money` = '".$_POST['money']."',
    `bannd` = '".$_POST['bannd']."',
    `level` = '".$_POST['level']."',
    `tong_nap` = `tong_nap`+'".$_POST['money']."' WHERE `id` = '".$id."' ");
    if($_POST['money']!=$toz_code['money']){
        if($toz_code['money']>$_POST['money']){
            $reason = "Admin trừ tiền";
        }elseif($toz_code['money']<$_POST['money']){
            $reason = "Admin cộng tiền";
               }$magd = random('ABCDEFGHIJKLMNOPQRSTUVWXYZ',3).rand(1000000000, 9999999999);
        $ketnoi->query("INSERT INTO `history_nap_bank` SET 
                  `trans_id` = '$magd',
                  `username` =  '".$_POST['username']."',
                  `type` = 'Hệ thống',
                  `ctk` = '$reason',
                  `stk` = 'Null',
                  `thucnhan` = '".$_POST['money']."',
                  `status` = 'hoantat',
                  `time` = '$now' ");
    }

  if($create)
  {
    echo '<script type="text/javascript">if(!alert("Cập nhật thành công !")){window.history.back().location.reload();}</script>';
  }
  else
  {
    echo '<script type="text/javascript">if(!alert("Có lỗi xảy ra !")){window.history.back().location.reload();}</script>'; 
  }
}

?>

<body class="hold-transition sidebar-mini layout-fixed" style="font-family: 'Plus Jakarta Sans', 'Inter', sans-serif;">
    <div class="wrapper">
        <div class="content-wrapper">
            <div class="content-header">
                <div class="container-fluid">
                    <div class="row mb-3 align-items-center">
                        <div class="col-sm-6">
                            <h1 class="m-0 fw-boldest text-gray-800"><i class="bi bi-person-gear text-primary me-2"></i>Quản Lý Thành Viên</h1>
                        </div><div class="col-sm-6">
                            <ol class="breadcrumb float-sm-right bg-transparent p-0 m-0">
                                <li class="breadcrumb-item"><a href="#">Hệ thống</a></li>
                                <li class="breadcrumb-item active">Chỉnh sửa User</li>
                            </ol>
                        </div></div></div></div>
            <section class="content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="card card-default shadow-sm rounded-3">
                                <div class="card-header border-bottom border-gray-100 py-4">
                                    <h3 class="card-title fw-bold text-uppercase tracking-wider text-secondary fs-7 m-0">
                                        <i class="bi bi-pencil-square me-1"></i>Thay đổi thông tin tài khoản
                                    </h3>
                                </div>
                                <div class="card-body p-6">
                                    <form role="form" action="" method="post">
                                        <div class="form-group mb-4">
                                            <label class="fw-bold text-muted text-uppercase tracking-wide fs-8 mb-2">Tên người dùng (Username)</label>
                                            <input type="text" class="form-control form-control-solid fw-bold font-monospace bg-light" name="username" value="<?=$toz_code['username'];?>" readonly style="font-family: 'JetBrains Mono', monospace;">
                                        </div>

                                        <div class="form-group mb-4">
                                            <label class="fw-bold text-muted text-uppercase tracking-wide fs-8 mb-2">Địa chỉ Email</label>
                                            <input type="text" class="form-control rounded-2 py-2" name="email" value="<?=$toz_code['email'];?>" placeholder="Nhập địa chỉ email...">
                                        </div>

                                        <div class="form-group mb-4">
                                            <label class="fw-bold text-muted text-uppercase tracking-wide fs-8 mb-2">Số dư tài khoản (đ)</label>
                                            <div class="input-group">
                                                <input type="number" class="form-control rounded-start-2 py-2 font-monospace fw-bold text-success" name="money" value="<?=$toz_code['money'];?>" placeholder="0" style="font-family: 'JetBrains Mono', monospace;">
                                                <span class="input-group-text bg-light fw-bold text-gray-600">VND</span>
                                            </div>
                                        </div>

                                        <div class="form-group mb-4">
                                            <label class="fw-bold text-muted text-uppercase tracking-wide fs-8 mb-2">Cấp bậc & Danh hiệu Hệ thống</label>
                                            <select class="form-control rounded-2 py-2 fw-semibold text-gray-800" name="level">
                                                <option value="<?=$toz_code['level'];?>" selected hidden>
                                                    Hiện tại: <?php 
                                                        if($toz_code['level'] == 0) echo 'Thạch Anh (Mặc định)';
                                                        elseif($toz_code['level'] == 1) echo 'Đồng (Bronze)';
                                                        elseif($toz_code['level'] == 2) echo 'Bạc (Silver)';
                                                        elseif($toz_code['level'] == 3) echo 'Vàng (Gold)';
                                                        elseif($toz_code['level'] == 4) echo 'Bạch Kim (Platinum)';
                                                        elseif($toz_code['level'] == 5) echo 'Lục Bảo (Emerald)';
                                                        elseif($toz_code['level'] == 6) echo 'Kim Cương (Diamond)';
                                                        elseif($toz_code['level'] == 9) echo 'Quản trị viên (Admin)';
                                                        else echo 'Thạch Anh';
                                                    ?>
                                                </option>
                                                <option value="0">Thạch Anh (Mặc định)</option>
                                                <option value="1">Đồng (Bronze)</option>
                                                <option value="2">Bạc (Silver)</option>
                                                <option value="3">Vàng (Gold)</option>
                                                <option value="4">Bạch Kim (Platinum)</option>
                                                <option value="5">Lục Bảo (Emerald) [Thuê dịch vụ]</option>
                                                <option value="6">Kim Cương (Diamond) [Premium]</option>
                                                <option value="9" class="text-danger fw-bold">Quản trị viên (Admin)</option>
                                            </select>
                                        </div>

                                        <div class="form-group mb-5">
                                            <label class="fw-bold text-muted text-uppercase tracking-wide fs-8 mb-2">Trạng thái tài khoản (Band)</label>
                                            <select class="form-control rounded-2 py-2" name="bannd">
                                                <option value="<?=$toz_code['bannd'];?>" selected hidden>
                                                    <?php if($toz_code['bannd'] == 0){ echo 'Hoạt động thường (Un Band)'; } else { echo 'Đang bị khóa (Band)'; };?>
                                                </option>
                                                <option value="0">Hoạt động thường (Un Band)</option>
                                                <option value="1" class="text-danger">Khóa tài khoản (Band)</option>
                                            </select>
                                        </div>

                                        <button type="submit" name="submit" class="btn btn-primary px-5 py-2 fw-bold text-uppercase tracking-wide rounded-2 shadow-sm">
                                            <i class="bi bi-save2-fill me-1"></i> Lưu dữ liệu thay đổi
                                        </button>
                                    </form>
                                </div>
                                <div class="card-footer py-4 px-6 border-top border-gray-100 bg-light bg-opacity-40 rounded-bottom-3">
                                    <a href="users.php" class="btn btn-sm btn-outline-secondary fw-bold px-4 py-2">
                                        <i class="bi bi-arrow-left-short fs-5 align-middle"></i> Trở về danh sách thành viên
                                    </a>
                                </div>
                            </div>
                            </div>
                    </div>
                    </div></section>
            </div>
        <aside class="control-sidebar control-sidebar-dark">
            </aside>
        </div>
    <!-- ./wrapper -->

    <?php require_once('foot.php');?>
</body>

</html>