<?php
session_start();
include("conn.php");
if (isset($_POST['action']))
{
  $action=$_POST['action'];
  //Add Data Start
  if($action=="Add")
  {
       if($_POST['services'] && $_POST['name'] && $_POST['email'] && $_POST['mobile'] && $_POST['whatsapp'] && $_POST['web_url']  && $_POST['state_id']  && $_POST['district_id'] && $_POST['address'] && $_POST['req'])
    {
        $v_code=$_SESSION['v_code'];
        $s_code ='ESHC'.rand(100000,999999);
        $services=$_POST['services'];
        $name=$_POST['name'];
        $email=$_POST['email'];
        $mobile=$_POST['mobile'];
        $whatsapp = $_POST['whatsapp'];
        $web_url=$_POST['web_url'];
        $state_id=$_POST['state_id'];
        $district_id=$_POST['district_id'];
        $file_url = $_SESSION['file_url'];
        $address=$_POST['address'];
        $req=$_POST['req'];
                
         
        $sql="insert into tbl_customer(v_code,s_code,services,name,email,mobile,whatsapp,web_url,state_id,district_id,file_url,address,req)values
        ('$v_code','$s_code','$services','$name','$email','$mobile','$whatsapp','$web_url','$state_id','$district_id','$file_url','$address','$req')";
        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['services'] && $_POST['name'] && $_POST['email'] && $_POST['mobile'] && $_POST['whatsapp'] && $_POST['web_url']  && $_POST['state_id']  && $_POST['district_id'] && $_POST['address'] && $_POST['req'])
    {
        $id=$_POST['id'];
          $services=$_POST['services'];
        $name=$_POST['name'];
        $email=$_POST['email'];
        $mobile=$_POST['mobile'];
        $whatsapp=$_POST['whatsapp'];
        $web_url=$_POST['web_url'];
        $state_id=$_POST['state_id'];
        $district_id=$_POST['district_id'];
        $file_url=$_SESSION['file_url'];
        $address=$_POST['address'];
        $req=$_POST['req'];
        
        $sql="update tbl_customer set services='$services',name='$name',email='$email',mobile='$mobile',whatsapp='$whatsapp', web_url='$web_url',state_id='$state_id',district_id='$district_id',file_url='$file_url',address='$address',req='$req',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_customer 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);
?>