<?php
session_start();
include("conn.php");
if (isset($_POST['action']))
{
  $action=$_POST['action'];
  //Add Data Start
  if($action=="Add")
  {
    if($_POST['role'] && $_POST['name'] && $_POST['mobile'] && $_POST['password'] && $_POST['state_id']  && $_POST['district_id'])
    {
        $pid=$_SESSION['v_code'];
         $state_id=$_POST['state_id'];
        $district_id=$_POST['district_id'];
        $address=$_POST['address'];
        $gpid=$_SESSION['pid'];
        $mobile=$_POST['mobile'];
        $code ='ESC'.$mobile;
        $name=$_POST['name'];
        $email=$_POST['email'];
        $role = $_POST['role'];
        $password=$_POST['password'];

        $sql="insert into tbl_vendor(v_code,pid,gpid,name,email,mobile,role,password,state_id,district_id,address)values('$code','$pid','$gpid','$name','$email','$mobile','$role','$password','$state_id','$district_id','$address')";
        if(mysqli_query($conn,$sql))
        {
          echo "Your account created successfully.\nUser ID- ".$code."\nPassword- ".$password;
        }
        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['email'] && $_POST['mobile'] && $_POST['password'])
    {
        $id=$_POST['id'];
       
        $v_code=$_POST['v_code'];
        $name=$_POST['name'];
        $email=$_POST['email'];
        $mobile=$_POST['mobile'];
        $role=$_POST['role'];
        $password=$_POST['password'];
        
        $sql="update tbl_vendor set name='$name',email='$email',mobile='$mobile',role='$role', password='$password',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_vendor 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);
?>