<?php
session_start();
include("conn.php");
if (isset($_POST['action']))
{
  $action=$_POST['action'];
  //Add Data Start
  if($action=="Add")
  {
       if($_POST['name'] && $_POST['mobile'] && $_POST['whatsapp'] && $_POST['email']  && $_POST['referred_by'] && $_POST['assign_to'] && $_POST['category'] && $_POST['state'] && $_POST['city'] && $_POST['website'] )
    {
        $v_code=$_SESSION['v_code'];
        $name=$_POST['name'];
        $mobile=$_POST['mobile'];
        $whatsapp = $_POST['whatsapp'];
        $email=$_POST['email'];
        $referred_by=$_POST['referred_by'];
        $assign_to=$_POST['assign_to'];
        $category=$_POST['category'];
        $state=$_POST['state'];
        $city=$_POST['city'];
        $website=$_POST['website'];

        $sql="insert into tbl_leads(v_code,name,mobile,whatsapp,email,referred_by,assign_to,category,state,city,website)values
        ('$v_code','$name','$mobile','$whatsapp','$email','$referred_by','$assign_to','$category','$state','$city','$website')";
        if(mysqli_query($conn,$sql))
        {
          echo "Customer Created successfully.";
        }
        else
        {
          echo "Record not added. Error:".mysqli_error($conn);
        }

    }
    else
    {
      echo "Please complete all required fields.";
    }
  }
  //Add Data End  
  //Update Data Start
  else if($action=="Edit")
  {
       if($_POST['name'] && $_POST['mobile'] && $_POST['whatsapp'] && $_POST['email']  && $_POST['referred_by'] && $_POST['assign_to'] && $_POST['category'] && $_POST['state'] && $_POST['city'] && $_POST['website'] )
    {
        $id=$_POST['id'];
        $name=$_POST['name'];
        $mobile=$_POST['mobile'];
        $whatsapp = $_POST['whatsapp'];
        $email=$_POST['email'];
        $referred_by=$_POST['referred_by'];
        $assign_to=$_POST['assign_to'];
        $category=$_POST['category'];
        $state=$_POST['state'];
        $city=$_POST['city'];
        $website=$_POST['website'];
        
        $sql="update tbl_leads set name='$name',mobile='$mobile',whatsapp='$whatsapp',email='$email',referred_by='$referred_by', assign_to='$assign_to',category='$category',state='$state',city='$city',website='$website',updated_at=CURRENT_TIMESTAMP() where id='$id'";
        if(mysqli_query($conn,$sql))
        {
          echo "Record updated successfully.";
        }
        else
        {
          echo "Record not updated. Error:".mysqli_error($conn);
        }

    }
        else
        {
          echo "Please complete all required fields.";
        }
  }
  //Update Data End
  //Delete Data Start
  else if($action=="Delete")
  {
    $id=$_POST['id'];

    $sql="update tbl_leads set deleted_at=CURRENT_TIMESTAMP() where id='$id'";
    if(mysqli_query($conn,$sql))
    {
      echo "Record deleted successfully.";
    }
    else
    {
      echo "Record not deleted. Error:".mysqli_error($conn);
    }
  }
  //Delete Data End
}
else
{
  echo "Invalid action specified.";
}
mysqli_close($conn);
?>