<?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);

	$departments = array();
	$all_doctors = array();
		 //categories
	    $query_menu = "SELECT a.term_id,a.name FROM `wp_terms` a join wp_term_taxonomy b WHERE a.term_id = b.term_id and b.parent=4";
	    $res_menu = mysqli_query($con, $query_menu);
		while ($row_menu = mysqli_fetch_array($res_menu)) {
			// $depimg=$row_menu['app_icon'];
			// $dep_img_path = "https://online.lifelinehospitalkerala.com/".$depimg;
			$doctors = array();
			if($row_menu['term_id']!=16 && $row_menu['term_id']!=13 && $row_menu['term_id']!=35 && $row_menu['term_id']!=36 && $row_menu['term_id']!=4 ){

				$termeta = "SELECT `meta_value` FROM `wp_termmeta` WHERE `term_id`='$row_menu[term_id]' and `meta_key`='department_icon'";
				$rest = mysqli_query($con, $termeta);
				$arrterm = mysqli_fetch_array($rest);
				$metavalue = $arrterm['meta_value'];

				$depterms = "SELECT `guid` FROM `wp_posts` WHERE `ID` = '$metavalue'";
				$restdep = mysqli_query($con, $depterms);
				$arrdep = mysqli_fetch_array($restdep);
				$dep_icon = $arrdep['guid'];

				//$depid = $row_menu['term_id'];

				// $query_menu2 = "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 = '$row_menu[term_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";

				$query_menu2 = "SELECT ps.id as id, ps.post_title as post_title, tm.name as name, tm.term_id as term_id, (SELECT guid FROM wp_posts WHERE id = mt.meta_value) AS image
				FROM
				wp_posts ps, wp_postmeta mt, wp_term_relationships tr, wp_terms tm
				WHERE
				ps.id = tr.object_id
				AND tm.term_id = tr.term_taxonomy_id
				AND tm.term_id = '$row_menu[term_id]'
				AND ps.post_status = 'publish'
				AND ps.post_type = 'post'
				AND mt.post_id = ps.id
				AND mt.meta_key = '_thumbnail_id'
				ORDER BY ps.post_date DESC";
				$res_menu2 = mysqli_query($con, $query_menu2);
				if(mysqli_num_rows($res_menu2)<=0){
					$doctors[] = array(
						'doc_id' => '',
						'doc_name' => '',
						'desig' => '',
						'dep_id' => '',
						'dep_name' => '',
						'doc_image' => '',
						'booking_available' => '',
						'sel_days' => ''
						);
				}else{
					while ($row_menu2 = mysqli_fetch_array($res_menu2)){

						$desig = "SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key`='designation' and `post_id`='$row_menu2[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`='$row_menu2[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`='$row_menu2[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';
						}

						$doc_img_path=$row_menu2['image'];

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

				$departments[] = array(
					'dep_id' => $row_menu['term_id'],
					'dep_name' => $row_menu['name'],
					'dep_icon' => $dep_icon,
					'dep_doctors' => $doctors
					);

			}


		}


		$query_alldoctors = "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 = '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_all = mysqli_query($con, $query_alldoctors);
		while ($rowall = mysqli_fetch_array($res_menu_all)) {
			$doc_img_path=$rowall['image'];

			//designation
			$desig = "SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key`='designation' and `post_id`='$rowall[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`='$rowall[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`='$rowall[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';
			}

			//department details
			$depdetailsquery = "SELECT * FROM `wp_term_relationships` where object_id='$rowall[id]' and term_taxonomy_id!=4";
			$res_depdetail = mysqli_query($con, $depdetailsquery);
			$rowdepdetails = mysqli_fetch_array($res_depdetail);

				$depdetailsquery2 = "SELECT * FROM `wp_terms` WHERE `term_id`='$rowdepdetails[term_taxonomy_id]'";
				$res_depdetail2 = mysqli_query($con, $depdetailsquery2);
				$rowdepdetails2 = mysqli_fetch_array($res_depdetail2);
				$departmid = $rowdepdetails2['term_id'];
				$departmname = $rowdepdetails2['name'];


            $all_doctors[] = array(
            'doc_id' => $rowall['id'],
			'doc_name' => $rowall['post_title'],
			'desig' => $designation,
			'dep_id' => $departmid,
			'dep_name' => $departmname,
			'doc_image' => $doc_img_path,
			'booking_available' => $booking_avl,
			'sel_days' => $selected_days
            );
		}


		$data = array("departments" => $departments, "all_doctors" => $all_doctors );

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


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