
<?php include('head.php');?>
<?php include('nav.php');?>

<?php
if (isset($_GET['delete'])) 
{
    $delete = $_GET['delete'];

    $create = mysqli_query($ketnoi,"DELETE FROM `napdt` WHERE `id` = '".$delete."' ");

    if ($create)
    {
      echo '<script type="text/javascript">swal("Thành Công","XÓA THÀNH CÔNG","success");setTimeout(function(){ location.href = "nap-dt.php" },500);</script>'; 
    }
    else
    {
      echo '<script type="text/javascript">swal("Lỗi","LỖI MÁY CHỦ, VUI LÒNG LIÊN HỆ KỸ THUẬT VIÊN","error");setTimeout(function(){ location.href = "nap-dt.php" },1000);</script>'; 
    }
}
?>


        <div class="row mb-2">
          <div class="col-sm-6">

          </div><!-- /.col -->
        </div><!-- /.row -->

        <div class="row">
          <div class="col-md-12">
            <div class="card">
              <div class="card-header">
                <h3 class="card-title">DANH SÁCH ĐƠN NẠP ĐIỆN THOẠI</h3>
              </div>
              <!-- /.card-header -->
              <div class="card-body">
                <div class="table-responsive">
                <table id="datatable1" class="table table-bordered table-striped">
                  <thead>                  
                    <tr role="row">
                                <th class="text-center">
                                    <b>MÃ GD</b>
                                </th>
                                <th class="text-center">
                                    <b>USERNAME</b>
                                </th>
                                <th class="text-center">
                                    <b>NHÀ MẠNG</b>
                                </th>
                                <th class="text-center">
                                    <b>MỆNH GIÁ</b>
                                </th>
                                <th class="text-center">
                                    <b>THANH TOÁN</b>
                                </th>
                                <th class="text-center">
                                    <b>PASS MY</b>
                                </th>
                                <th class="text-center">
                                    <b>SDT NẠP</b>
                                </th>
                                <th class="text-center">
                                    <b>CREATEDATE</b>
                                </th>
                                <th class="text-center">
                                    <b>UPDATE</b>
                                </th>
                                <th class="text-center">
                                    <b>STATUS</b>
                                </th>
                                <th class="text-center">
                                    <b>THAO TÁC</b>
                                </th>
                                
                            </tr>
                  </thead>
                  <tbody>
<?php
$result = mysqli_query($ketnoi,"SELECT * FROM `napdt` ORDER BY id desc limit 0, 100000");
while($row = mysqli_fetch_assoc($result))
{
?>
                    <tr>
                      <td class="text-center"><?=$row['code'];?></td>
                      <td class="text-center"><a href="edit-thanh-vien.php?username=<?=$row['username'];?>" ><?=$row['username'];?></td>
                      <td class="text-center"><b style="color:green"><?=$row['loaithe'];?></b></td>
                      <td class="text-center"><b style="color:red"><?=format_cash($row['menhgia']);?></b></td>
                      <td class="text-center"><b style="color:blue"><?=format_cash($row['money']);?></b></td>
                      <td class="text-center"><i><?=$row['passmy'];?></i></td>
                      <td class="text-center"><b><?=$row['sdt'];?></b></td>
                      <td class="text-center"><?=$row['createdate'];?></td>
                      <td class="text-center"><?=$row['updatedate'];?></td>
                      <td class="text-center">
                      <?php if($row['status'] == 'xuly'){ ?>
                                        <label class="btn btn-warning">Chờ xử lý</label>
                            <?php }?> 
                            <?php if($row['status'] == 'hoantat'){ ?>
                                        <label class="btn btn-success">Hoàn tất</label>
                              <?php }?> 
                            <?php if($row['status'] == 'thatbai'){ ?>
                                        <label class="btn btn-danger">Thất bại</label>
                                <?php }?> 
                                </td>
                      <td class="text-center">
                        <a type="button" class="btn btn-default" data-toggle="modal" data-target="#EDIT_<?=$row['code'];?>" class="btn btn-info"><i class="fa fa-search" aria-hidden="true"></i> EDIT</a>
                        <!--<a href="nap-dt.php?delete=<?=$row['id'];?>" class="btn btn-default">
                          <i class="fas fa-trash"></i>
                        </a> -->
                      </td>  
                    </tr>


     <div class="modal fade" id="EDIT_<?=$row['code'];?>">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">ĐƠN NẠP ĐT #<?=$row['code'];?></h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              <form role="form" action="" method="post">
                  <div class="form-group">
                    <label for="exampleInputEmail1">GHI CHÚ</label>
                    <textarea name="note" class="form-control"><?=$row['note'];?></textarea>
                  </div>
                  <div class="form-group">
                  <label>TRẠNG THÁI</label>
                    <select class="form-control select2bs4" name="status" style="width: 100%;">
                      <option value="<?=$row['status'];?>" >
                        <?php if($row['status'] == 'xuly'){?>
                          Đang chờ xử lý
                        <?php }?>
                        <?php if($row['status'] == 'hoantat'){?>  
                          Hoàn tất
                        <?php }?>
                        <?php if($row['status'] == 'thatbai'){?>  
                          Thất bại
                        <?php }?>
                        </option>
                      <option value="xuly">Đang chờ xử lý</option>
                      <option value="hoantat">Hoàn tất</option>
                      <option value="thatbai">Thất bại</option>
                    </select>
                  </div>        
            </div>
            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">ĐÓNG</button>
              <button type="submit" name="submit_<?=$row['code'];?>" class="btn btn-primary">LƯU</button>
            </div>
            </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>




<?php
    if (isset($_POST['submit_'.$row['code']]))
    {
        if($_POST['status'] == 'thatbai')
        {
          $get_money = mysqli_fetch_assoc(mysqli_query($ketnoi, "SELECT `VND` FROM `account` WHERE `username` = '".$row['username']."'  ")) ['VND'];

          $create = mysqli_query($ketnoi,"UPDATE `account` SET 
          `VND` = `VND` + ".$row['money']." 
          WHERE `username` = '".$row['username']."'");

          if($create)
          {
            mysqli_query($ketnoi,"UPDATE `napdt` SET 
            `status` = '".$_POST['status']."',
            `updatedate` = now(),
            `note` = '".$_POST['note']."'
            WHERE `code` = '".$row['code']."' ");

            
            mysqli_query($ketnoi,"INSERT INTO `logs` SET 
                `content` = '".format_cash($get_money)." + ".format_cash($row['money'])." = ".format_cash(phepcong($get_money, $row['money']))." lý do: Hủy Đơn Nạp Điện Thoại ID ".$row['code'].". ',
                `username` = '".$row['username']."',
                `createdate` = now() ");
        
            echo '<script type="text/javascript">swal("Thành Công","Hoàn tiền User hoàn tất!","success");
            setTimeout(function(){ location.href = "" },500);</script>';
            exit();
          }
          else
          {
            echo '<script type="text/javascript">swal("Lỗi","Lỗi kỹ thuật","error");
            setTimeout(function(){ location.href = "" },500);</script>';
            exit();
          }
        }
        else
        {
          $create = mysqli_query($ketnoi,"UPDATE `napdt` SET 
            `status` = '".$_POST['status']."',
            `updatedate` = now(),
            `note` = '".$_POST['note']."'
            WHERE `code` = '".$row['code']."' ");

          if($create)
          {
            echo '<script type="text/javascript">swal("Thành Công","Lưu thành công!","success");
            setTimeout(function(){ location.href = "" },500);</script>';
            exit();
          }
          else
          {
            echo '<script type="text/javascript">swal("Lỗi","Lỗi kỹ thuật","error");
            setTimeout(function(){ location.href = "" },500);</script>';
            exit();
          }
        }
    }
?>

<?php }?>
                  </tbody>
                </table>
              </div>
              </div>
              <!-- /.card-body -->
            </div>
            <!-- /.card -->
          </div>
          <!-- /.col -->
        </div>
        <!-- /.row (main row) -->
      
<?php include('foot.php');?>