<?php
include "config.php";
header('Content-Type: application/json');
$valid_passwords = array("LifeLCT" => "LifLinCnslt");
$valid_users = array_keys($valid_passwords);

$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];

$validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);

if (!$validated) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    $responsefull = array('error_message' => 'Unathorized: please provide a valid password!', 'data' => []);
} else {


    $datas = json_decode(file_get_contents('php://input'), true);

    $lifeline_clinics = array();
    $doctor_timing = array();

		 //categories
	    // $query_menu = "SELECT ID,post_title,name as category,cat_terms.term_id as cat_id,`post_content` FROM wp_term_taxonomy AS cat_term_taxonomy
        // INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id
        // INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
        // INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID
        // INNER JOIN wp_postmeta AS meta ON cat_posts.ID = meta.post_id
        // WHERE
        // cat_posts.post_status = 'publish' AND cat_term_taxonomy.term_id = 28
        // GROUP BY name, post_title ORDER BY name, post_title";
        $query_menu = "SELECT ID,post_title,name as category,cat_terms.term_id as cat_id,`post_content` FROM wp_term_taxonomy AS cat_term_taxonomy
        INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id
        INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
        INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID
        INNER JOIN wp_postmeta AS meta ON cat_posts.ID = meta.post_id
        WHERE
        cat_posts.post_status = 'publish' AND cat_term_taxonomy.term_id = 28
        GROUP BY name, post_title ORDER BY post_date asc";
	    $res_menu = mysqli_query($con, $query_menu);
		while ($row_menu = mysqli_fetch_array($res_menu)) {
            $post_content = str_replace("\n"," ",$row_menu['post_content']);
            // $post_content2 = str_replace("<!-- wp:html -->"," ",$post_content);
            // $post_content3 = str_replace(" <!-- /wp:html -->"," ",$post_content2);

            $lifeline_clinics[] = array(
            'id' => $row_menu['ID'],
            'name' => $row_menu['post_title'],
            //'content' => $post_content,
            'category' => $row_menu['category'],
            'cat_id' => $row_menu['cat_id']
            );
		}



		$data = array("lifeline_clinics" => $lifeline_clinics );

$response = array("status" => "true","message" => "Success", "data" => $data);


}
if(isset($response))
{
	echo json_encode($response, JSON_UNESCAPED_SLASHES);
}
?>