<?php
    require('db.php');
    
    if (isset($_POST["last_id"])) {
        $location = strtolower($_POST["location"]);
        $name = strtolower($_POST["name"]);
        $dept = strtolower($_POST["dept"]);
        //sleep(5);
        if ($_POST["last_id"] != '') {
            $query = "SELECT * FROM users WHERE utype != 'master_admin' AND uid > '".$_POST["last_id"]."' GROUP BY uname ORDER BY uid ASC LIMIT 5";

            if ( $location != "" && $name == "" && $dept == "" ) {
                $query = "SELECT * FROM users WHERE utype != 'master_admin' AND uid > '".$_POST["last_id"]."' AND uaddress1 = '$location' GROUP BY uname ORDER BY uid ASC LIMIT 7";
                // $query = "SELECT * FROM users AS u
                // LEFT JOIN doccat AS d
                // ON u.uid = d.dcatby
                // WHERE utype != 'master_admin' AND uid > '".$_POST["last_id"]."' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                // GROUP BY u.uname
                // LIMIT 7
                // ";
            }
            if ( $location == "" && $name != "" && $dept == "" ) {
                $query = "SELECT * FROM users WHERE utype != 'master_admin' AND uid > '".$_POST["last_id"]."' AND uname = '$name' ORDER BY uid ASC LIMIT 7";
                // $query = "SELECT * FROM users AS u
                // LEFT JOIN doccat AS d
                // ON u.uid = d.dcatby
                // WHERE utype != 'master_admin' AND uid > '".$_POST["last_id"]."' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                // GROUP BY u.uname
                // LIMIT 7
                // ";
            }
            if ( $location != "" && $name != "" && $dept == "" ) {
                $query = "SELECT * FROM users WHERE utype != 'master_admin' AND uid > '".$_POST["last_id"]."' AND uaddress1 = '$location' AND uname = '$name' ORDER BY uid ASC LIMIT 7";
                // $query = "SELECT * FROM users AS u
                // LEFT JOIN doccat AS d
                // ON u.uid = d.dcatby
                // WHERE utype != 'master_admin' AND uid > '".$_POST["last_id"]."' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                // GROUP BY u.uname
                // LIMIT 7
                // ";
            }
            if ( $location == "" && $name == "" && $dept != "" ) {
                $query = "SELECT * FROM users AS u LEFT JOIN doccat AS d ON u.uid = d.dcatby WHERE u.utype != 'master_admin' AND u.uid > '".$_POST["last_id"]."' AND d.dcname = '$dept' ORDER BY u.uid ASC LIMIT 7";
                // $query = "SELECT * FROM users AS u
                // LEFT JOIN doccat AS d
                // ON u.uid = d.dcatby
                // WHERE utype != 'master_admin' AND uid > '".$_POST["last_id"]."' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                // GROUP BY u.uname
                // LIMIT 7
                // ";
            }
            if ( $location != "" && $name == "" && $dept != "" ) {
                $query = "SELECT * FROM users AS u LEFT JOIN doccat AS d ON u.uid = d.dcatby WHERE u.utype != 'master_admin' AND u.uid > '".$_POST["last_id"]."' AND d.dcname = '$dept' AND u.uaddress1 = '$location' ORDER BY u.uid ASC LIMIT 7";
                // $query = "SELECT * FROM users AS u
                // LEFT JOIN doccat AS d
                // ON u.uid = d.dcatby
                // WHERE utype != 'master_admin' AND uid > '".$_POST["last_id"]."' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                // GROUP BY u.uname
                // LIMIT 7
                // ";
            }
            if ( $location == "" && $name != "" && $dept != "" ) {
                $query = "SELECT * FROM users AS u LEFT JOIN doccat AS d ON u.uid = d.dcatby WHERE u.utype != 'master_admin' AND u.uid > '".$_POST["last_id"]."' AND d.dcname = '$dept' AND u.uname = '$name' ORDER BY u.uid ASC LIMIT 7";
                // $query = "SELECT * FROM users AS u
                // LEFT JOIN doccat AS d
                // ON u.uid = d.dcatby
                // WHERE utype != 'master_admin' AND uid > '".$_POST["last_id"]."' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                // GROUP BY u.uname
                // LIMIT 7
                // ";
            }
            if ( $location != "" && $name != "" && $dept != "" ) {
                $query = "SELECT * FROM users AS u LEFT JOIN doccat AS d ON u.uid = d.dcatby WHERE u.utype != 'master_admin' AND u.uid > '".$_POST["last_id"]."' AND d.dcname = '$dept' AND u.uname = '$name'AND u.uaddress1 = '$location' ORDER BY u.uid ASC LIMIT 7";
                // $query = "SELECT * FROM users AS u
                // LEFT JOIN doccat AS d
                // ON u.uid = d.dcatby
                // WHERE utype != 'master_admin' AND uid > '".$_POST["last_id"]."' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                // GROUP BY u.uname
                // LIMIT 7
                // ";
            }
        }


        else {
            $query = "SELECT * FROM users WHERE utype != 'master_admin' GROUP BY uname ORDER BY uid ASC LIMIT 5";

            if ( $location != "" && $name == "" && $dept == "" ) {
                $query = "SELECT * FROM users AS u
                LEFT JOIN doccat AS d
                ON u.uid = d.dcatby
                WHERE utype != 'master_admin' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                GROUP BY u.uname
                LIMIT 7
                ";
                $query = "SELECT * FROM users WHERE utype != 'master_admin' AND uaddress1 = '$location' GROUP BY uname ORDER BY uid ASC LIMIT 7";
            }
            if ( $location == "" && $name != "" && $dept == "" ) {
                $query = "SELECT * FROM users WHERE utype != 'master_admin' AND uname = '$name' ORDER BY uid ASC LIMIT 7";
                // $query = "SELECT * FROM users AS u
                // LEFT JOIN doccat AS d
                // ON u.uid = d.dcatby
                // WHERE utype != 'master_admin' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                // GROUP BY u.uname
                // LIMIT 7
                // ";
            }
            if ( $location != "" && $name != "" && $dept == "" ) {
                $query = "SELECT * FROM users WHERE utype != 'master_admin' AND uaddress1 = '$location' AND uname = '$name' ORDER BY uid ASC LIMIT 7";
                // $query = "SELECT * FROM users AS u
                // LEFT JOIN doccat AS d
                // ON u.uid = d.dcatby
                // WHERE utype != 'master_admin' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                // GROUP BY u.uname
                // LIMIT 7
                // ";
            }
            if ( $location == "" && $name == "" && $dept != "" ) {
                $query = "SELECT * FROM users AS u LEFT JOIN doccat AS d ON u.uid = d.dcatby WHERE u.utype != 'master_admin' AND d.dcname = '$dept' ORDER BY u.uid ASC LIMIT 7";
                // $query = "SELECT * FROM users AS u
                // LEFT JOIN doccat AS d
                // ON u.uid = d.dcatby
                // WHERE utype != 'master_admin' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                // GROUP BY u.uname
                // LIMIT 7
                // ";
            }
            if ( $location != "" && $name == "" && $dept != "" ) {
                $query = "SELECT * FROM users AS u LEFT JOIN doccat AS d ON u.uid = d.dcatby WHERE u.utype != 'master_admin' AND d.dcname = '$dept' AND u.uaddress1 = '$location' ORDER BY u.uid ASC LIMIT 7";
                // $query = "SELECT * FROM users AS u
                // LEFT JOIN doccat AS d
                // ON u.uid = d.dcatby
                // WHERE utype != 'master_admin' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                // GROUP BY u.uname
                // LIMIT 7
                // ";
            }
            if ( $location == "" && $name != "" && $dept != "" ) {
                $query = "SELECT * FROM users AS u LEFT JOIN doccat AS d ON u.uid = d.dcatby WHERE u.utype != 'master_admin' AND d.dcname = '$dept' AND u.uname = '$name' ORDER BY u.uid ASC LIMIT 7";
                // $query = "SELECT * FROM users AS u
                // LEFT JOIN doccat AS d
                // ON u.uid = d.dcatby
                // WHERE utype != 'master_admin' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                // GROUP BY u.uname
                // LIMIT 7
                // ";
            }
            if ( $location != "" && $name != "" && $dept != "" ) {
                $query = "SELECT * FROM users AS u
                LEFT JOIN doccat AS d
                ON u.uid = d.dcatby
                WHERE utype != 'master_admin' AND u.uaddress1 = '$location' OR u.uname = '$name' OR d.dcname = '$dept'
                GROUP BY u.uname
                LIMIT 7
                ";
                $query = "SELECT * FROM users AS u LEFT JOIN doccat AS d ON u.uid = d.dcatby WHERE u.utype != 'master_admin' AND d.dcname = '$dept' AND u.uname = '$name' AND u.uaddress1 = '$location' ORDER BY u.uid ASC LIMIT 7";
                
            }
        }
        $statement = $connect -> prepare($query);
        $statement -> execute();
        $total_row = $statement -> rowCount();
        $output = '';
        $not_found = '';
        $last_id = 0;
        if ($total_row > 0) {
            $result = $statement -> fetchAll();
            foreach($result as $row) {

                $depts = "";

                $uid = $row["uid"];
                $new_query = "SELECT * FROM users AS u INNER JOIN doccat AS d ON u.uid = d.dcatby WHERE u.uid = '$uid'";
                $new_statement = $connect -> prepare($new_query);
                $new_statement -> execute();
                $new_row = $new_statement -> rowCount();
                if ($new_row > 0) {
                    $result = $statement -> fetchAll();
                    foreach($result as $rows) {
                        $depts = $rows["dcname"];
                    }
                } else {
                    $depts = "";
                }

                $output .= '
                <li>
                    <div class="card mb-3 shadow-sm">
                        <div class="car d-body">
                            <div class="row m x-0">
							 <div class="col-3">
								<a href="#"><img src="../login/logo/'.$row["ulogo"].'" alt="ome" width="100%">
                                  </a>  
                                </div>
                                <div class="col-6" >
								<a href="hospitaldetils.php?id='.$row["uid"].'/'.$row["uname"].'"  style="text-decoration: none;">
                                    <p class="text-muted mb-2">'.$row["uname"].'<br>'.$row["uaddress2"].' <br>
'.$row["uname"].'									</p>
                                   
                                    <p class="small text-muted mb-0">'.$depts.'</p>
									</a>
                                </div>
                                <div class="col-2 text-right">
                                    <a href="tel:'.$row["uphone"].'" class="btn btn-light border active btn-sm" style="padding: 2px 4px;font-size: 12px;width: 50px;"> <i class="fas fa-phone fa-rotate-90"></i> </a>
                                    <br>
                                    <a href="#" class="btn btn-light border active btn-sm" style="padding: 1px 4px;width: 50px;"> <i class="fab fa-whatsapp"></i> </a>
                                    <br>
                                    <a href="#book" data-toggle="modal" class="text-right btn btn-light border active btn-sm" style="padding: 1px 4px;width: 50px;">Book</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>';

                $last_id = $row["uid"];

            }
        } else {
            $not_found = '<div align="center" class="alert alert-info">..</div>';
        }

        $output .= '<li class="clearfix" id="clearfix_id" data-last_id="'.$last_id.'" style="float: none;"></li>';

        $test_output = array(
            'query' => $query,
            'last_id' => $last_id,
            'timeline_output' => $output,
            'no_data_output' => $not_found
        );

        echo json_encode($test_output);
    }
?>