<?php
include('dbconnect.php'); // your database connection file
include('function.php');
if(isset($_POST['update_profile'])) {
    $lead_id = $_POST['lead_id'];
    $lead_profile_id = $_POST['lead_profile_id'];
    //echo"Ram";echo $user_id;
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $altphone = $_POST['alt-phone'];
    $project = $_POST['project'];

    // Update tbl_lead_user_profile
    $update_profile = "UPDATE tbl_lead_user_profile 
                       SET tbl_lead_user_profile_email = '$email', 
                           tbl_lead_user_profile_mobile = '$phone', 
                           tbl_lead_user_profile_alt_mobile = '$altphone'
                       WHERE tbl_lead_user_profile_id = '$lead_profile_id'";

    // Update tbl_lead_user
    $update_user = "UPDATE tbl_lead_user 
                    SET  tbl_lead_user_mobile = '$phone' ,
                    tbl_lead_user_alt_mobile = '$altphone' 
                    WHERE 	tbl_lead_user_id = '$lead_id'";
    

    if(mysqli_query($conn, $update_profile) && mysqli_query($conn, $update_user)) {
        // Previous project id of lead
        $project_name=getproject_name($project);
        $sql_prj_id=mysqli_num_rows(mysqli_query($conn,"select tbl_lead_user_prj_id from tbl_lead_user_projects where 1 and	tbl_lead_user_id='".$lead_id."' and tbl_lead_user_prj_id='".$project."'"));
        if($sql_prj_id <= 0){
            // Update tbl_lead_user_projects
            $update_project = "UPDATE tbl_lead_user_projects 
                            SET  tbl_lead_user_prj_id = '$project',
                            tbl_lead_user_projects_name = '$project_name'
                            WHERE 	tbl_lead_user_id = '$lead_id'";
            mysqli_query($conn, $update_project);
            
            // Insert into tbl_lead_user_access_projects_dup
            $cdate=date('Y-md h:i:s A');
            echo "INSERT INTO tbl_lead_user_projects_dup(tbl_lead_user_id,tbl_lead_user_prj_id,tbl_lead_user_projects_name,cdate) 
                        VALUES ('$lead_id', '$project','$project_name','$cdate')";
            $sql_prj_id=mysqli_query($conn,"INSERT INTO tbl_lead_user_projects_dup(tbl_lead_user_id,tbl_lead_user_prj_id,tbl_lead_user_projects_name,cdate) 
                        VALUES ('$lead_id', '$project','$project_name','$cdate')");
            
            
        }
        
        
        echo "<script>alert('Profile updated successfully'); window.history.back();</script>";
       // echo " window.location.reload();</script>";
    } else {
        echo "<script>alert('Error updating profile'); window.history.back();</script>";
    }
}
?>
