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

include($PathPrefix . 'includes/session.php');
$Title = _('Edit Lead');
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))
{
    $status_list = array();
    $sql = "SELECT id, label, probability FROM lms_status";
    $result = DB_query($sql);
    while($myrow = DB_fetch_array($result))
    {
        $status_list[$myrow['id']] = array('label' => $myrow['label'], 'probability' => $myrow['probability']);
    }

    if(isset($_POST['update']))
    {
        if(isset($_SESSION['last_id']) && $_SESSION['last_id'] == $_POST['key'])
        {
            // do nothing if same id as previous indicating a form resubmit
            prnMsg('Lead record for ' . $_POST['name'] . ' has been processed and will not be processed again','warn');
        }
        else
        {
            $_SESSION['last_id'] = $_POST['key'];
            
            $update_fields = "";
            if($name != $_POST['name'])
            {
                $name = $_POST['name'];
                $update_fields .= "name='".$name."', ";
            }
            if(strcmp($contactno, $_POST['contactno']) != 0)
            {
                $contactno = $_POST['contactno'];
                $contactno = str_ireplace(array(' ', '-'), '', $contactno); // remove all - and space
                $update_fields .= "contactno='".$contactno."', ";
            }
            if($email != $_POST['email'])
            {
                $email = $_POST['email'];
                $update_fields .= "email='".$email."', ";
            }
            if($company != $_POST['company'])
            {
                $company = $_POST['company'];
                $update_fields .= "company='".$company."', ";
            }
            if($location != stripcslashes($_POST['location']))
            {
                $location = $_POST['location'];
                $update_fields .= "location='".$location."', ";
            }
            if($remarks != stripcslashes($_POST['remarks']))
            {
                $remarks = $_POST['remarks'];
                $update_fields .= "remarks='".$remarks."', ";
            }
            if($status != $_POST['status'])
            {
                $status = $_POST['status'];
                $update_fields .= "status='".$status."', ";
            }

            if(!empty($update_fields))
            {
                $update_fields .= "lastupdated=CURRENT_TIMESTAMP";

                $sql = "UPDATE lms_leads SET ".$update_fields."                                        
                        WHERE id='".$lead."'";
                $result = DB_query($sql);
                prnMsg(_('Lead record for') . ' ' . $name . ' ' . _('has been updated'),'success');
            }
            else
            {
                prnMsg(_('No change(s) detected.'),'warn');
            }

            $location = stripcslashes($location);
            $remarks = stripcslashes($remarks);
            
            // unset($name);
            // unset($contactno);
            // unset($email);
            // unset($company);
            // unset($location);
            // unset($remarks);
            // unset($message);
            // unset($status);
            // unset($url);
        }
    }
?>

<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>

<div class="row">
    <div class="col-sm-4"></div>
    <div class="col-sm-4">
        <h2>Edit Contact Info <span class="glyphicon glyphicon-user"></span></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="key" value="'.uniqid().'" />';
        ?>
            <div class="form-group">
                <label for="name">Name:</label>
                <input type="text" class="form-control" name="name" placeholder="Contact Name" value="<?php echo (isset($name) ? $name : '' ); ?>" required >
            </div>
            <div class="form-group">
                <label for="contactno">Phone No:</label>
                <input type="tel" class="form-control" name="contactno" placeholder="Contact Phone Number" value="<?php echo (isset($contactno) ? $contactno : '' ); ?>">
                <span class="help-block">(Eg: +60123456789) </span>
            </div>
            <div class="form-group">
                <label for="email">Email address:</label>
                <input type="email" class="form-control" name="email" placeholder="Contact Email Address" value="<?php echo (isset($email) ? $email : '' ); ?>">
            </div>
            <div class="form-group">
                <label for="company">Company:</label>
                <input type="text" class="form-control" name="company" placeholder="Company of Contact" value="<?php echo (isset($company) ? $company : '' ); ?>">
            </div>
            <div class="form-group">
                <label for="location">Location:</label>
                <textarea class="form-control" rows="5" name="location" placeholder="Location / Address of Contact"><?php echo (isset($location) ? $location : '' ); ?></textarea>
            </div>
            <div class="form-group">
                <label for="remarks">Remarks:</label>
                <textarea class="form-control" rows="2" name="remarks" placeholder="Your Remarks / Comments / Notes"><?php echo (isset($remarks) ? $remarks : '' ); ?></textarea>
            </div>
            <div class="form-group">
                <label for="status">Status:</label>
                <select class="form-control" name="status">
                <?php
                foreach($status_list as $id => $desc)
                {
                    echo '<option value="'.$id.'" ' . ($status == $id ? 'selected' : '') . '>' . $desc['label'] . ' - ' . ($desc['probability'] * 100) . '%</option>';
                }
                ?>
                </select>
            </div>

            <button type="submit" name="update" class="btn btn-block btn-info">Save Changes <span class="glyphicon glyphicon-save"></span></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');
?>
