<?php 
if (!defined('IN_SITE')) {
    die('The Request Not Found');
}
$title = 'Quản Lý Thành Viên | ' . $DVR->site('title');
$body = [
    'title' => 'Danh sách Đơn Tố Cáo'
];
$body['header'] = '
<style>
    .report-page .page-header-breadcrumb{
        background: linear-gradient(135deg, #0f172a 0%, #1e293b 45%, #334155 100%);
        border-radius: 20px;
        padding: 20px;
        box-shadow: 0 12px 35px rgba(15, 23, 42, 0.12);
        position: relative;
        overflow: hidden;
    }
    .report-page .page-header-breadcrumb::before{
        content: "";
        position: absolute;
        top: -40px;
        right: -40px;
        width: 140px;
        height: 140px;
        border-radius: 50%;
        background: rgba(255,255,255,.06);
    }
    .report-page .page-title,
    .report-page .breadcrumb,
    .report-page .breadcrumb-item,
    .report-page .breadcrumb-item.active{
        position: relative;
        z-index: 2;
    }
    .report-page .page-title{
        color: #fff;
    }
    .report-page .breadcrumb-item.active,
    .report-page .breadcrumb-item a,
    .report-page .breadcrumb{
        color: rgba(255,255,255,.8) !important;
    }

    .report-page .custom-card{
        border: 0;
        border-radius: 20px;
        overflow: hidden;
        box-shadow: 0 12px 30px rgba(15, 23, 42, 0.08);
    }
    .report-page .custom-card .card-header{
        border-bottom: 1px solid #eef2f7;
        background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
        padding: 18px 22px;
    }
    .report-page .custom-card .card-title{
        font-weight: 700;
        font-size: 18px;
        color: #0f172a;
    }
    .report-page .custom-card .card-body{
        padding: 22px;
    }

    .report-filter-box{
        background: #f8fafc;
        border: 1px solid #e2e8f0;
        border-radius: 18px;
        padding: 18px;
        margin-bottom: 20px;
    }
    .report-filter-box .form-control,
    .report-filter-box .form-select{
        border-radius: 12px;
        min-height: 42px;
        border: 1px solid #dbe2ea;
        box-shadow: none !important;
    }
    .report-filter-box .form-control:focus,
    .report-filter-box .form-select:focus{
        border-color: #86b7fe;
    }

    .filter-top-title{
        font-size: 13px;
        font-weight: 700;
        text-transform: uppercase;
        color: #64748b;
        letter-spacing: .5px;
        margin-bottom: 14px;
    }

    .report-btn{
        border-radius: 12px;
        min-height: 42px;
        font-weight: 600;
        padding: 10px 16px;
    }

    .top-filter{
        display: flex;
        justify-content: space-between;
        gap: 14px;
        flex-wrap: wrap;
        margin-top: 10px;
        padding-top: 14px;
        border-top: 1px dashed #dbe2ea;
    }
    .filter-show,
    .filter-short{
        display: flex;
        align-items: center;
        gap: 10px;
        flex-wrap: wrap;
    }
    .filter-label{
        margin: 0;
        font-size: 13px;
        font-weight: 700;
        color: #475569;
    }
    .filter-select{
        min-width: 120px;
        border-radius: 12px;
    }

    .table-wrapper{
        border: 1px solid #e9edf3;
        border-radius: 18px;
        overflow: hidden;
        background: #fff;
    }
    .report-page .table{
        margin-bottom: 0;
        vertical-align: middle;
    }
    .report-page .table thead th{
        background: #0f172a;
        color: #fff;
        border-color: #1e293b !important;
        font-size: 13px;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: .4px;
        padding: 14px 12px;
    }
    .report-page .table tbody td{
        padding: 14px 12px;
        border-color: #eef2f7 !important;
        white-space: normal !important;
    }
    .report-page .table tbody tr:hover{
        background: #f8fbff;
    }

    .report-link{
        font-weight: 700;
        text-decoration: none;
        word-break: break-word;
    }

    .report-reason{
        max-width: 280px;
        word-break: break-word;
        color: #334155;
        line-height: 1.5;
    }

    .report-proof a{
        margin: 2px;
        display: inline-block;
    }

    .report-url{
        display: inline-block;
        max-width: 220px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        vertical-align: middle;
    }

    .status-btn{
        min-width: 42px;
        min-height: 42px;
        border-radius: 12px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        font-size: 15px;
        box-shadow: 0 8px 18px rgba(0,0,0,.08);
    }

    .action-delete{
        border-radius: 12px;
        font-weight: 600;
        min-height: 40px;
        padding: 8px 14px;
    }

    .pagination-style-1 .pagination{
        gap: 6px;
        flex-wrap: wrap;
    }
    .pagination-style-1 .page-item .page-link{
        border-radius: 12px;
        border: 1px solid #dbe2ea;
        color: #0f172a;
        min-width: 42px;
        text-align: center;
        font-weight: 600;
        box-shadow: none !important;
    }
    .pagination-style-1 .page-item.active .page-link{
        background: #0d6efd;
        border-color: #0d6efd;
        color: #fff;
    }

    .stats-mini{
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 14px;
        margin-bottom: 20px;
    }
    .stats-mini-card{
        background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
        border: 1px solid #e9edf3;
        border-radius: 18px;
        padding: 16px;
        box-shadow: 0 10px 25px rgba(15,23,42,.04);
        height: 100%;
    }
    .stats-mini-label{
        font-size: 13px;
        color: #64748b;
        font-weight: 600;
        margin-bottom: 8px;
    }
    .stats-mini-value{
        font-size: 24px;
        font-weight: 800;
        color: #0f172a;
        line-height: 1.2;
    }

    @media (max-width: 991.98px){
        .stats-mini{
            grid-template-columns: repeat(2, minmax(0, 1fr));
        }
    }

    @media (max-width: 767.98px){
        .report-page .page-header-breadcrumb{
            padding: 16px;
            border-radius: 18px;
        }
        .report-page .custom-card .card-body{
            padding: 16px;
        }
        .report-filter-box{
            padding: 14px;
        }
        .top-filter{
            flex-direction: column;
            align-items: stretch;
        }
        .filter-show,
        .filter-short{
            width: 100%;
            justify-content: space-between;
        }
        .filter-select{
            min-width: unset;
            width: 100%;
        }
    }

    @media (max-width: 575.98px){
        .stats-mini{
            grid-template-columns: 1fr;
        }
        .report-page .table thead th,
        .report-page .table tbody td{
            font-size: 13px;
        }
        .report-btn{
            width: 100%;
        }
    }
</style>
';
$body['footer'] = '';
require_once(__DIR__.'/Header.php');
require_once(__DIR__.'/Sidebar.php');
require_once(__DIR__ . '/../../../core/is_user.php');
CheckLogin();
CheckAdmin();

if(!empty($_GET['page']))
{
    $page = xss(intval($_GET['page']));
}
else
{
    $page = 1;
}
if(!empty($_GET['limit']))
{
    $limit = xss(intval($_GET['limit']));
}
else
{
    $limit = 10;
}

$sourceCodes = $DVR->get_list("SELECT * FROM `tocaoscam` ORDER BY id DESC");
$totalItems = count($sourceCodes);
$totalPages = ceil($totalItems / $limit);

$from = ($page - 1) * $limit;
$order_by = ' ORDER BY id DESC ';
$where = '1=1';
$id_don = '';
$stk = '';
$sdt = '';
$ngay = '';

if(!empty($_GET['id_don']))
{
    $id_don = xss($_GET['id_don']);
    $where .= " AND `id` = '$id_don'";
}

if(!empty($_GET['stk']))
{
    $stk = xss($_GET['stk']);
    $where .= " AND `stk` = '$stk'";
}

if(!empty($_GET['sdt']))
{
    $sdt = xss($_GET['sdt']);
    $where .= " AND `sdt` = '$sdt'";
}

if(!empty($_GET['ngay']))
{
    $ngay = xss($_GET['ngay']);
    $where .= " AND `ngay` = '$ngay'";
}
?>
<?php
if (isset($_GET['xuly']) && $getUser['level'] == 1) {
    if ($DVR->site('status_demo') != 0) {
        die('<script type="text/javascript">if(!alert("Không được dùng chức năng này vì đây là trang web demo")){window.history.back().location.reload();}</script>');
    }
    $isUpdate = $DVR->update("tocaoscam", array(
        'status' => 'xuly',
    ), " `id` = '".xss($_GET['xuly'])."' ");
    if ($isUpdate) {
        echo '<script type="text/javascript"> showMessage("Thay đổi thành công", "success"); setTimeout(function(){ window.location.href = "/Cpanel/duyet-don"; }, 3000);</script>';
    } else {
        die('<script type="text/javascript">if(!alert("Lưu thất bại!")){window.history.back().location.reload();}</script>');
    }
}

if (isset($_GET['scam']) && $getUser['level'] == 1) {
    if ($DVR->site('status_demo') != 0) {
        die('<script type="text/javascript">if(!alert("Không được dùng chức năng này vì đây là trang web demo")){window.history.back().location.reload();}</script>');
    }
    $isUpdate = $DVR->update("tocaoscam", array(
        'status' => 'scam',
    ), " `id` = '".xss($_GET['scam'])."' ");
    if ($isUpdate) {
        echo '<script type="text/javascript"> showMessage("Thay đổi thành công", "success"); setTimeout(function(){ window.location.href = "/Cpanel/duyet-don"; }, 3000);</script>';
    } else {
        die('<script type="text/javascript">if(!alert("Lưu thất bại!")){window.history.back().location.reload();}</script>');
    }
}
?>

<div class="main-content app-content report-page">
    <div class="container-fluid">
        <div class="d-md-flex d-block align-items-center justify-content-between my-4 page-header-breadcrumb">
            <div>
                <h1 class="page-title fw-semibold fs-18 mb-1">Duyệt Đơn Tố Cáo</h1>
                <div style="color: rgba(255,255,255,.75); font-size: 13px; position: relative; z-index: 2;">
                    Quản lý, rà soát và xử lý các đơn tố cáo trong hệ thống
                </div>
            </div>
            <div class="ms-md-1 ms-0 mt-3 mt-md-0">
                <nav>
                    <ol class="breadcrumb mb-0">
                        <li class="breadcrumb-item active" aria-current="page">Duyệt Đơn Tố Cáo</li>
                    </ol>
                </nav>
            </div>
        </div>

        <div class="stats-mini">
            <div class="stats-mini-card">
                <div class="stats-mini-label">Tổng đơn tố cáo</div>
                <div class="stats-mini-value"><?= format_cash($DVR->num_rows("SELECT * FROM `tocaoscam`")); ?></div>
            </div>
            <div class="stats-mini-card">
                <div class="stats-mini-label">Đơn scam</div>
                <div class="stats-mini-value"><?= format_cash($DVR->num_rows("SELECT * FROM `tocaoscam` WHERE `status` = 'scam'")); ?></div>
            </div>
            <div class="stats-mini-card">
                <div class="stats-mini-label">Đơn đang xử lý</div>
                <div class="stats-mini-value"><?= format_cash($DVR->num_rows("SELECT * FROM `tocaoscam` WHERE `status` = 'xuly'")); ?></div>
            </div>
            <div class="stats-mini-card">
                <div class="stats-mini-label">Hiển thị mỗi trang</div>
                <div class="stats-mini-value"><?= format_cash($limit); ?></div>
            </div>
        </div>

        <div class="row">
            <div class="col-xl-12">
                <div class="card custom-card">
                    <div class="card-header justify-content-between">
                        <div class="card-title">
                            Danh sách đơn tố cáo
                        </div>
                    </div>
                    <div class="card-body">
                        <form action="" class="align-items-center mb-3" name="formSearch" method="GET">
                            <input type="hidden" value="<?= $getUser['token'] ?>" id="token">

                            <div class="report-filter-box">
                                <div class="filter-top-title">Bộ lọc tìm kiếm</div>

                                <div class="row g-3">
                                    <div class="col-xl-3 col-lg-3 col-md-6 col-12">
                                        <input class="form-control form-control-sm"
                                               value="<?= htmlspecialchars($id_don); ?>"
                                               name="id_don"
                                               placeholder="ID Đơn">
                                    </div>

                                    <div class="col-xl-3 col-lg-3 col-md-6 col-12">
                                        <input class="form-control form-control-sm"
                                               value="<?= htmlspecialchars($stk); ?>"
                                               name="stk"
                                               placeholder="STK">
                                    </div>

                                    <div class="col-xl-3 col-lg-3 col-md-6 col-12">
                                        <input class="form-control form-control-sm"
                                               value="<?= htmlspecialchars($sdt); ?>"
                                               name="sdt"
                                               placeholder="SDT">
                                    </div>

                                    <div class="col-xl-3 col-lg-3 col-md-6 col-12">
                                        <input type="text"
                                               name="ngay"
                                               class="form-control form-control-sm"
                                               id="daterange"
                                               value="<?= htmlspecialchars($ngay); ?>"
                                               placeholder="Chọn thời gian">
                                    </div>

                                    <div class="col-12 d-flex flex-wrap gap-2">
                                        <button class="btn btn-primary report-btn" type="submit">
                                            <i class="fa fa-search me-1"></i> Search
                                        </button>
                                        <a class="btn btn-danger report-btn" href="/Cpanel/duyet-don">
                                            <i class="fa fa-trash me-1"></i> Clear filter
                                        </a>
                                    </div>
                                </div>

                                <div class="top-filter">
                                    <div class="filter-show">
                                        <label class="filter-label">Show :</label>
                                        <select name="limit" onchange="this.form.submit()" class="form-select filter-select">
                                            <option <?= ($limit == '5') ? 'selected' : ''; ?> value="5">5</option>
                                            <option <?= ($limit == '10') ? 'selected' : ''; ?> value="10">10</option>
                                            <option <?= ($limit == '20') ? 'selected' : ''; ?> value="20">20</option>
                                            <option <?= ($limit == '50') ? 'selected' : ''; ?> value="50">50</option>
                                            <option <?= ($limit == '100') ? 'selected' : ''; ?> value="100">100</option>
                                            <option <?= ($limit == '500') ? 'selected' : ''; ?> value="500">500</option>
                                            <option <?= ($limit == '1000') ? 'selected' : ''; ?> value="1000">1.000</option>
                                            <option <?= ($limit == '5000') ? 'selected' : ''; ?> value="5000">5.000</option>
                                            <option <?= ($limit == '10000') ? 'selected' : ''; ?> value="10000">10.000</option>
                                        </select>
                                    </div>

                                    <div class="filter-short">
                                        <label class="filter-label">Short by Date:</label>
                                        <select name="shortByDate" onchange="this.form.submit()" class="form-select filter-select">
                                            <option value="">Tất cả</option>
                                            <option value="1">Hôm nay</option>
                                            <option value="2">Tuần này</option>
                                            <option value="3">Tháng này</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </form>

                        <div class="table-responsive table-wrapper mb-3">
                            <table class="table text-nowrap table-striped table-hover table-bordered align-middle">
                                <thead>
                                    <tr>
                                        <th>Họ Tên</th>
                                        <th>SĐT</th>
                                        <th>STK</th>
                                        <th>Lý Do</th>
                                        <th>Ảnh</th>
                                        <th>Link Phốt</th>
                                        <th>Thời Gian</th>
                                        <th>Status</th>
                                        <th>Thao Tác</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($DVR->get_list("SELECT * FROM `tocaoscam` WHERE $where $order_by LIMIT $from,$limit") as $row) { ?>
                                        <tr>
                                            <td>
                                                <a class="text-primary report-link" href="/scams/<?= $row['code']; ?>.html">
                                                    <?= $row['username']; ?> [ID <?= $row['id']; ?>]
                                                </a>
                                            </td>

                                            <td>
                                                <span class="badge bg-success-gradient"><?= $row['sdt']; ?></span>
                                            </td>

                                            <td>
                                                <span class="badge bg-danger-gradient"><?= $row['stk']; ?></span>
                                            </td>

                                            <td>
                                                <div class="report-reason"><?= $row['ly_do']; ?></div>
                                            </td>

                                            <td class="report-proof">
                                                <?php foreach ($DVR->get_list("SELECT * FROM `bangchung` WHERE `code` = '".$row['code']."'") as $img) { ?>
                                                    <a class="badge bg-success-gradient" href="<?= $img['image']; ?>" target="_blank">
                                                        Xem
                                                    </a>
                                                <?php } ?>
                                            </td>

                                            <td>
                                                <span class="badge bg-light text-dark report-url"><?= $row['link_report']; ?></span>
                                            </td>

                                            <td><?= $row['ngay']; ?></td>

                                            <td>
                                                <?php if($row['status'] == "scam") { ?>
                                                    <a href="?xuly=<?= $row['id']; ?>" class="btn btn-danger status-btn" title="Chuyển sang xử lý">
                                                        <i class="fas fa-times" aria-hidden="true"></i>
                                                    </a>
                                                <?php } else { ?>
                                                    <a href="?scam=<?= $row['id']; ?>" class="btn btn-success status-btn" title="Xác nhận scam">
                                                        <i class="fa fa-check" aria-hidden="true"></i>
                                                    </a>
                                                <?php } ?>
                                            </td>

                                            <td>
                                                <button style="color:white;"
                                                        onclick="RemoveRow(<?= $row['id'] ?>)"
                                                        class="btn btn-danger btn-sm action-delete"
                                                        type="button">
                                                    <span>Delete</span>
                                                </button>
                                            </td>
                                        </tr>
                                    <?php } ?>

                                    <?php if(count($DVR->get_list("SELECT * FROM `tocaoscam` WHERE $where $order_by LIMIT $from,$limit")) <= 0) { ?>
                                        <tr>
                                            <td colspan="9" class="text-center text-muted py-4">
                                                Không có dữ liệu phù hợp
                                            </td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>

                        <div class="row">
                            <div class="col-sm-12 col-md-12 mb-3">
                                <div class="pagination-style-1">
                                    <div class="d-flex justify-content-center">
                                        <div class="paging_simple_numbers">
                                            <ul class="pagination">
                                                <?php for ($i = 1; $i <= $totalPages; $i++) { ?>
                                                    <?php if ($i === $page) { ?>
                                                        <li class="page-item active">
                                                            <a class="page-link link" href="#"><?= $i; ?></a>
                                                        </li>
                                                    <?php } else if ($i <= 4 || $i > $totalPages - 4 || abs($i - $page) <= 1) { ?>
                                                        <li class="page-item <?= ($i == $page) ? 'active' : ''; ?>">
                                                            <a class="page-link link" href="/Cpanel/duyet-don?id_don=<?= $id_don; ?>&stk=<?= $stk; ?>&sdt=<?= $sdt; ?>&ngay=<?= $ngay; ?>&limit=<?= $limit; ?>&shortByDate=&page=<?= $i; ?>"><?= $i; ?></a>
                                                        </li>
                                                    <?php } else if (abs($i - $page) === 2) { ?>
                                                        <li class="page-item">
                                                            <a class="page-link link" href="javascript:void(0)">...</a>
                                                        </li>
                                                    <?php } ?>
                                                <?php } ?>

                                                <li class="paginate_button page-item previous">
                                                    <a class="page-link" href="/Cpanel/duyet-don?id_don=<?= $id_don; ?>&stk=<?= $stk; ?>&sdt=<?= $sdt; ?>&ngay=<?= $ngay; ?>&limit=<?= $limit; ?>&shortByDate=&page=<?= $page + 1; ?>">Next</a>
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div><!-- card-body -->
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function RemoveRow(id) {
    cuteAlert({
        type: "question",
        title: "Xác Nhận Xóa Thành Viên",
        message: "Bạn có chắc chắn muốn xóa thành viên ID " + id + " không ?",
        confirmText: "Đồng Ý",
        cancelText: "Hủy"
    }).then((e) => {
        if (e) {
            $.ajax({
                url: "<?=BASE_URL('')?>ajaxs/admin/remove_don_scam.php",
                method: "POST",
                dataType: "JSON",
                data: {
                    id: id,
                    token: $("#token").val()
                },
                success: function(respone) {
                    if (respone.status == 'success') {
                        showMessage(respone.msg, "success");
                        setTimeout(function() {
                            location.reload();
                        }, 2000);
                    } else {
                        showMessage(respone.msg, respone.status);
                    }
                },
                error: function() {
                    location.reload();
                }
            });
        }
    })
}
</script>

<?php
require_once(__DIR__.'/Footer.php');
?>