<?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);
$dep_id = $datas['dep_id'];

 if($dep_id)
{


	$doctors = array();
    $doctor_timing = array();

		 //categories
	    $query_menu = "SELECT wp_posts.id, wp_posts.post_title, wp_terms.name, wp_terms.term_id, (SELECT guid FROM wp_posts WHERE id = wp_postmeta.meta_value) AS image FROM wp_posts, wp_postmeta, wp_term_relationships, wp_terms WHERE wp_posts.id = wp_term_relationships.object_id AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id AND wp_terms.term_id = '$dep_id' AND wp_posts.post_status = 'publish' AND wp_posts.post_type = 'post' AND wp_postmeta.post_id = wp_posts.id AND wp_postmeta.meta_key = '_thumbnail_id' ORDER BY wp_posts.post_date DESC";
        $res_menu = mysqli_query($con, $query_menu);
        if(mysqli_num_rows($res_menu)>0){
            while ($row_menu = mysqli_fetch_array($res_menu)) {
                $doc_img_path=$row_menu['image'];

                $doctors[] = array(
                'doc_id' => $row_menu['id'],
                'doc_name' => $row_menu['post_title'],
                'dep_id' => $row_menu['term_id'],
                'doc_image' => $doc_img_path
                );
            }
        }else{
                $doctors[] = array(
                'doc_id' => '',
                'doc_name' => '',
                'dep_id' => '',
                'doc_image' => ''
                );
        }



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

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

}

else{
	    $doctors[] = array(
            'doc_id' => '',
            'doc_name' => '',
            'dep_id' => '',
            'doc_image' => ''
            //'doc_timing' => $doctor_timing
        );

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

	    $response =array("status" => "false", "message" => "parameter missing", "data" => $data);
}

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