<?php
session_start();
include "../db.php";

if (isset($_POST['start'])) {
    $start = mysqli_real_escape_string($con, $_POST['start']);
    $productValue = mysqli_real_escape_string($con, $_POST['productValue']);
    $client = mysqli_real_escape_string($con, $_POST['client']);
    $AndWhere = '';

    if (!empty($_POST['FirstFilterValue'])) {
        $AndWhere .= " AND dTitle ='" . mysqli_real_escape_string($con, $_POST['FirstFilterValue']) . "' ";
    }

    if (!empty($_POST['ThirdFilterValue'])) {
        $AndWhere .= " AND dTitle ='" . mysqli_real_escape_string($con, $_POST['ThirdFilterValue']) . "' ";
    }

    $uids = []; // Initialize an empty array to store the uids

    // $users_sql = mysqli_query($con, "SELECT uid FROM users WHERE admin_ref = '" . mysqli_real_escape_string($con,   $_SESSION['uref_code']) . "'");

    // while ($users_row = mysqli_fetch_assoc($users_sql)) {
    //     $uids[] = $users_row['uid']; // Add each uid to the array
    // }

    $finalDocument = '';
    // $product_sql = mysqli_query($con, "SELECT * FROM product WHERE product = '$productValue' ");
    // if($product_row = mysqli_fetch_assoc($product_sql)){
    //     $finalDocument = $product_row["pFinalDocument"];
    // }

    // $disable = '';
    // if(!empty($finalDocument)){
    //     $disable = "disabled";
    // }

    $sql = "SELECT * FROM documents WHERE dProduct = '$productValue' AND dClient = '$client' $AndWhere ORDER BY did DESC LIMIT $start, 10";
    $res = mysqli_query($con, $sql);

    if ($total_rows = mysqli_num_rows($res) > 0) {
        $c = $start + 1;

        while ($rows = mysqli_fetch_assoc($res)) {
            // if(in_array($rows["dClient"], $uids)){

?>
<li class='clearfix' style='float: none;'>
    <div class="single-dis-admin mb-2">
        <div class='row align-items-center'>
            <div class='col-md-10 col-12'>
                <a style='color:orange;font-size:17px;cursor:pointer;' class='viewDetails' data-toggle='modal'>
                    <i class='fa fa-user' aria-hidden='true'></i> <b><?php echo $rows["dTitle"];?></b>
                </a>
            </div>

            <div class='col-md-2 col-12'>
                <div class="d-flex justify-content-between align-items-center">
                    <a href='view-documents.php?product_id=<?php echo $rows["dProduct"];?>&view_id=<?php echo $rows["did"];?>'
                        class='m-2 btn btn-primary btn-sm <?php echo $disable;?>' title='Click to View'><i
                            class='fa fa-eye' aria-hidden='true'></i>
                    </a>
                    <?php 
                        if(!empty($finalDocument)){
                            ?>
                    <a href='view-documents.php?product_id=<?php echo $rows["dProduct"];?>&edit_id=<?php echo $rows["did"];?>'
                        class='m-2 btn btn-success btn-sm <?php echo $disable;?>' title='Click to Edit'><i
                            class='fa fa-edit' aria-hidden='true'></i>
                    </a>
                    <a href='view-documents.php?product_id=<?php echo $rows["dProduct"];?>&del_id=<?php echo $rows["did"];?>'
                        class='delete m-2 btn btn-danger btn-sm <?php echo $disable;?>' title='Click to Delete'><i
                            class='fa fa-trash' aria-hidden='true'></i>
                    </a>
                    <?php }?>
                </div>
            </div>
        </div>
    </div>
</li>


<?php 
// }
            $c++;
        }
        mysqli_close($con);
    }
}
?>