<?php
if (!isset($PathPrefix)) {
	$PathPrefix='../';
}

include($PathPrefix . 'includes/session.php');
$Title = _('Create Appointment');

if(isset($_POST['set_appointment']))
{
    if($_POST['aa'] == 'pm' && $_POST['hh'] != '12')
    {
        $_POST['hh'] += 12;
    }

    $lastappointment = $_POST['date'] . ' ' . $_POST['hh'] . ':' . $_POST['mm'];

    $sql = "UPDATE lms_leads SET lastupdated=CURRENT_TIMESTAMP, lastappointment='"."$lastappointment"."'
            WHERE id='".$_POST['lead']."'";
    $result = DB_query($sql);

$ical = "BEGIN:VCALENDAR
VERSION:2.0
PRODID:
BEGIN:VEVENT
UID:".sha1($lastappointment . $_POST['lead'])."
DTSTAMP:".date('Ymd', strtotime($lastappointment))."T".date('His', strtotime($lastappointment))."
DTSTART:".date('Ymd', strtotime($lastappointment))."T".date('His', strtotime($lastappointment))."
DTEND:".date('Ymd',(strtotime ( '1 hour' , strtotime ( $lastappointment ) ) ))."T".date('His',(strtotime ( '1 hour' , strtotime ( $lastappointment ) ) ))."
SUMMARY:".$_POST['name']."
LOCATION:
DESCRIPTION:
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:".$_POST['name']."
TRIGGER:-PT1H
END:VALARM
END:VEVENT
END:VCALENDAR";
    
    header( 'Content-Type: text/calendar' );
    header( 'Content-Disposition: attachment; filename="appointment.ics";' );

    // clean output buffer
    ob_end_clean();

    $handle = fopen( 'php://output', 'w' );
    
    fwrite($handle, $ical);

    fclose($handle);

    // flush buffer
    ob_flush();

    exit();

}

include('includes/header.php');

if(isset($_POST['lead']) && is_numeric($_POST['lead']))
{
    $sql = "SELECT id, name, contactno, email, company, location, remarks, status
            FROM lms_leads WHERE id='".$_POST['lead']."'";
    $result = DB_query($sql);
    while($myrow = DB_fetch_array($result))
    {
        $lead = $myrow['id'];

        $name = $myrow['name'];
        $contactno = $myrow['contactno'];
        $email = $myrow['email'];
        $company = $myrow['company'];
        $location = stripcslashes($myrow['location']);
        $remarks = stripcslashes($myrow['remarks']);
        $status = $myrow['status'];
    }
}

// check for valid lead id
if(isset($lead))
{
?>

<div class="row">
    <div class="col-sm-12">
        <button type="button" class="btn btn-danger" onclick="window.close();">
            Close <span class="glyphicon glyphicon-remove"></span>
        </button>
    </div>
</div>

<?php
//     if(isset($_POST['set_appointment']))
//     {
//         $lastappointment = $_POST['date'] . ' ' . $_POST['hh'] . ':' . $_POST['mm'];

//         $sql = "UPDATE lms_leads SET lastupdated=CURRENT_TIMESTAMP, lastappointment='"."$lastappointment"."'
//                 WHERE id='".$lead."'";
//         $result = DB_query($sql);

        

// $ical = "BEGIN:VCALENDAR
// VERSION:2.0
// PRODID:http://www.icalmaker.com
// BEGIN:VEVENT
// UID:".sha1($lead)."
// DTSTAMP:".date('Y-m-d', strtotime($_POST['date']))."T".$_POST['hh'].$_POST['mm']."00
// DTSTART:".date('Y-m-d', strtotime($_POST['date']))."T".$_POST['hh'].$_POST['mm']."00
// DTEND:".date('Y-m-d',(strtotime ( '1 hour' , strtotime ( $lastappointment ) ) ))."T".date('H',(strtotime ( '1 hour' , strtotime ( $lastappointment ) ) )).$_POST['mm']."00
// SUMMARY:Appointment with ".$name."
// LOCATION:
// DESCRIPTION:
// END:VEVENT
// END:VCALENDAR";
        
//         header( 'Content-Type: text/calendar' );
//         header( 'Content-Disposition: attachment; filename="appointment.ics";' );

//         // clean output buffer
//         ob_end_clean();

//         $handle = fopen( 'php://output', 'w' );
        
//         fwrite($handle, $ical);

//         fclose($handle);

//         // flush buffer
//         ob_flush();

//         exit();

//     }
//     else
//     {
?>
<div class="row">
    <div class="col-sm-4"></div>
    <div class="col-sm-4">
        <h2>Appointment: <?php echo $name; ?></h2>
        <form method="post">
        <?php
        echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
        echo '<input type="hidden" name="lead" value="' . $lead . '" />';
        echo '<input type="hidden" name="name" value="' . $name . '" />';
        echo '<input type="hidden" name="key" value="'.uniqid().'" />';
        ?>
            <div class="form-group row">
                <div class="col-xs-12">
                    <label for="name">Date:</label>
                    <input type="date" class="form-control" name="date" value="" required >
                </div>
                
            </div>
            <div class="form-group row">
                <div class="col-xs-4">
                    <label for="time">Time:</label>
                    <select class="form-control" name="hh" required>
                        <option value="" disabled selected>hh</option>
                        <?php
                        for($hh=1; $hh<=12; $hh++)
                        {
                            echo '<option value="'.$hh.'">'.str_pad($hh,2,'0',STR_PAD_LEFT).'</option>';
                        }
                        ?>
                    </select>
                </div>
                <div class="col-xs-4">
                    <label for="time">&nbsp;</label>
                    <select class="form-control" name="mm" required>
                        <option value="" disabled selected>mm</option>
                        <?php
                        for($mm=0; $mm<60; $mm=$mm+15)
                        {
                            echo '<option value="'.$mm.'">'.str_pad($mm,2,'0',STR_PAD_LEFT).'</option>';
                        }
                        ?>
                    </select>
                </div>
                <div class="col-xs-4">
                    <label for="time">&nbsp;</label>
                    <select class="form-control" name="aa" required>
                       <option value="am">a.m.</option>
                       <option value="pm">p.m.</option>
                    </select>
                </div>
            </div>
            

            <button type="submit" name="set_appointment" class="btn btn-block btn-info"><span class="glyphicon glyphicon-calendar"></span> Set Appointment</button>
        </form>
    </div>
    <div class="col-sm-4"></div>
</div>

<?php
    // }
}
else
{
    // no lead id detected
    prnMsg(_('Lead record not found! Please select a record for edit.'),'error');
}
include('includes/footer.php');
?>
