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

 if($doc_id)
{


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

		 //categories
	    $query_menu = "SELECT wp_posts.id, wp_posts.post_title, wp_posts.`post_content`, 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_posts.id = '$doc_id' and term_taxonomy_id!=4 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 limit 6";
        $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'];

                //designation
                $desig = "SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key`='designation' and `post_id`='$doc_id'";
                $res_desig = mysqli_query($con, $desig);
                $rowdesig = mysqli_fetch_array($res_desig);
                $designation = $rowdesig['meta_value'];

                //booking_available
                $booking = "SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key`='booking' and `post_id`='$doc_id'";
                $res_booking = mysqli_query($con, $booking);
                $rowbooking = mysqli_fetch_array($res_booking);
                $booking_avl = $rowbooking['meta_value'];

                //sel days
                $sel_days = "SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key`='sel_days' and `post_id`='$doc_id'";
                $res_sel_days = mysqli_query($con, $sel_days);
                $rowsel_days = mysqli_fetch_array($res_sel_days);
                $select_days = unserialize($rowsel_days['meta_value']);
                $text='';
                if(!empty($select_days)){
                    for ($i=0; $i <count($select_days) ; $i++) {
                        $text.= $select_days[$i].',';
                    }
                    $selected_days = trim($text, ",");
                }else{
                    $selected_days = '1,2,3,4,5,6';
                }

                $post_content = str_replace("\n"," ",$row_menu['post_content']);
                $post_contents1 =  stripslashes($post_content);
                $post_contents2 =  strip_tags($post_contents1);
                $post_contents3 = str_replace("\t","",$post_contents2);
                $post_contents = str_replace("“",'"',$post_contents3);

                $doctors[] = array(
                'doc_id' => $row_menu['id'],
                'doc_name' => $row_menu['post_title'],
                'desig' => $designation,
                'dep_id' => $row_menu['term_id'],
                'dep_name' => htmlspecialchars_decode($row_menu['name']),
                'doc_image' => $doc_img_path,
                'booking_available' => $booking_avl,
                'sel_days' => $selected_days,
                'doc_content' => $post_contents
                );
            }
        }else{
                $doctors[] = array(
                'doc_id' => '',
                'doc_name' => '',
                'desig' => '',
                'dep_id' => '',
                'dep_name' => '',
                'doc_image' => '',
                'booking_available' => '',
                'sel_days' => '',
                'doc_content' => ''
                );
        }



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

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

}

else{
	    $doctors[] = array(
            'doc_id' => '',
            'doc_name' => '',
            'desig' => '',
            'dep_id' => '',
            'dep_name' => '',
            'doc_image' => '',
            'booking_available' => '',
            'sel_days' => '',
            'doc_content' => ''
            //'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);
}
?>