<?php
$name = $_REQUEST['name'];
$contact = $_REQUEST['contact'];
$email = $_REQUEST['email'];
$opnumber = $_REQUEST['opnumber'];
$amount = $_REQUEST['amount'];
$ordernumber = $_REQUEST['ordernumber'];
$transnumber = $_REQUEST['transno'];
$transdate = $_REQUEST['transdate'];
$appdate = $_REQUEST['appdate'];
$paymentmesssage = $_REQUEST['paymsg'];
$docid = $_REQUEST['docid'];
$depid = $_REQUEST['depid'];

/***************************getDoctorDet api ***************************/
$getDoctorDet_url = 'http://117.240.62.146/iiislifelineobapi/srvBkAPI.asmx/getDoctorDet?doc_id='.$docid;

$getDoctorDet_filteredUrl = filter_var($getDoctorDet_url, FILTER_SANITIZE_URL);

$getDoctorDet_filteredUrl = preg_replace('/[^A-Za-z0-9\-._~:\/?#\[\]@!$&\'()*+,;=]/', '', $getDoctorDet_filteredUrl);
$xmlResponse_getDoctor = file_get_contents($getDoctorDet_filteredUrl);
if ($xmlResponse_getDoctor === false) {
    echo "Failed to fetch data from the URL.";
} else {
    $xml_getDoctor = simplexml_load_string($xmlResponse_getDoctor);
    if ($xml_getDoctor === false) {
        echo "Failed to parse XML response.";
    } else {
        $jsonString_getDoctor = (string)$xml_getDoctor;
        $data_getDoctor = json_decode($jsonString_getDoctor, true);
        if ($data_getDoctor === null) {
        echo "Failed to decode JSON data.";
        } else {
        //   print_r($data_getDoctor);
            if($data_getDoctor['Id']==$docid && $data_getDoctor['Dept_Id']==$depid){
                $DOCNAME = $data_getDoctor['Name'];
                $DEPNAME = $data_getDoctor['DeptName'];
            }
        }
    }
}

$mailbody ="<table cellSpacing='0' cellPadding='0' width='100%' border='0'>
    <TR>
        <TD align='Center' height='25'
            style='FONT-WEIGHT: bold; FONT-SIZE: 9pt; COLOR: #ffffff; FONT-FAMILY: Verdana; BACKGROUND-COLOR:#00b09b'>
            <sup>The </sup> Lifeline : Appointment Form</TD>
    </TR>
    <TR>
        <TD align='left'>
            <table width='100%' border='0' cellPadding='1' cellSpacing='0'>
                <TR bgcolor='#f1f1f1'>
                    <TD align='right' height='25'
                        style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Name &nbsp;</TD>
                    <TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD>
                    <TD width='49%' height='25' align='left'
                        style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$name."</TD>
                </TR>
                <TR bgcolor='#e6e6e6'>
                    <TD align='right' height='25'
                        style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Contact &nbsp;
                    </TD>
                    <TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD>
                    <TD width='49%' height='25' align='left'
                        style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$contact."</TD>
                </TR>
                <TR bgcolor='#f1f1f1'>
                    <TD align='right' height='25'
                        style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Email&nbsp;
                    </TD>
                    <TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD>
                    <TD width='49%' height='25' align='left'
                        style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$email."</TD>
                </TR>
                <TR bgcolor='#e6e6e6'>
                    <TD align='right' height='25'
                        style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Op Number &nbsp;
                    </TD>
                    <TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD>
                    <TD width='49%' height='25' align='left'
                        style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$opnumber."</TD>
                </TR>
                <TR bgcolor='#f1f1f1'>
                    <TD align='right' height='25'
                        style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Doctor &nbsp;
                    </TD>
                    <TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD>
                    <TD width='49%' height='25' align='left'
                        style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$DOCNAME."</TD>
                </TR>
                <TR bgcolor='#e6e6e6'>
                    <TD align='right' height='25'
                        style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Department &nbsp;
                    </TD>
                    <TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD>
                    <TD width='49%' height='25' align='left'
                        style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$DEPNAME."</TD>
                </TR>
                <TR bgcolor='#f1f1f1'>
                    <TD align='right' height='25'
                        style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Appointment
                        Date&nbsp;</TD>
                    <TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD>
                    <TD width='49%' height='25' align='left'
                        style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$appdate."
                    </TD>
                </TR>
                <TR bgcolor='#e6e6e6'>
                    <TD align='right' height='25'
                        style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>
                        Amount&nbsp;</TD>
                    <TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD>
                    <TD width='49%' height='25' align='left'
                        style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$amount."
                    </TD>
                </TR>
                <TR bgcolor='#f1f1f1'>
                    <TD align='right' height='25'
                        style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Order Id&nbsp;
                    </TD>
                    <TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD>
                    <TD width='49%' height='25' align='left'
                        style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$ordernumber."
                    </TD>
                </TR>
                <TR bgcolor='#e6e6e6'>
                    <TD align='right' height='25'
                        style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Transaction Ref No&nbsp;
                    </TD>
                    <TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD>
                    <TD width='49%' height='25' align='left'
                        style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>
                        ".$transnumber."</TD>
                </TR>
                <TR bgcolor='#f1f1f1'>
                    <TD align='right' height='25'
                        style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Transaction Date&nbsp;
                    </TD>
                    <TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD>
                    <TD width='49%' height='25' align='left'
                        style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>
                        ".$transdate."</TD>
                </TR>
                <TR bgcolor='#e6e6e6'>
                    <TD align='right' height='25'
                        style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Message&nbsp;
                    </TD>
                    <TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD>
                    <TD width='49%' height='25' align='left'
                        style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$paymentmesssage."</TD>
                </TR>
            </table>
        </TD>
    </TR>
</table>";

$mailheaders = "From: " . strip_tags('info@lifelinehospitalkerala.com') . "\r\n";
$mailheaders .= "Reply-To: " . strip_tags('info@lifelinehospitalkerala.com') . "\r\n";
$mailheaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$mailheaders .= "Content-Type: text/html; charset=UTF-8\r\n";

$mailsubject = "Lifeline Payment Confirmation";
$adminmail = "abijith@meridian.net.in";

$mailrecipients = !empty($email) ? $adminmail.",".$email : $adminmail;
$mailSent = mail($mailrecipients, $mailsubject, $mailbody, $mailheaders);
if ($mailSent) {
    $mailstatus = "true";
    $mailresp = "Email sent successfully.";
} else {
    $mailstatus = "false";
    $mailresp = "Email not sent. Something went wrong.";
}

$result = array(
    'status' => $mailstatus,
    'mailresp' => $mailresp
);

header('Content-Type: application/json'); // Set the content type to JSON
echo json_encode($result);
?>