<?php
if (isset($_POST["action"])) {
    
	$output = '';
	require('../config/dbcon.php');
	if ($_POST["action"] == "Add") {
	    
		$query = "Select srno from control_mst where description='TRUCK'";
		$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
		while ($row = mysqli_fetch_assoc($rs)) {
			$srno = $row['srno'];
			$srnoupd = $srno+1;
    		$truck_number = mysqli_escape_string($connect, $_POST["truck_number"]);
    		$truck_owner_name = mysqli_escape_string($connect, $_POST["truck_owner_name"]);
    		$truck_address_one = mysqli_escape_string($connect, $_POST["truck_address_one"]);
    		$truck_address_two = mysqli_escape_string($connect, $_POST["truck_address_two"]);
    		$truck_place = mysqli_escape_string($connect, $_POST["truck_place"]);
    		$truck_mobile_number = mysqli_escape_string($connect, $_POST["truck_mobile_number"]);
    		$truck_pan_number = mysqli_escape_string($connect, $_POST["truck_pan_number"]);
    		$truck_bank_name = mysqli_escape_string($connect, $_POST["truck_bank_name"]);
    		$truck_bank_acc_number = mysqli_escape_string($connect, $_POST["truck_bank_acc_number"]);
    		$truck_bank_branch = mysqli_escape_string($connect, $_POST["truck_bank_branch"]);
    		$truck_ifsc_code = mysqli_escape_string($connect, $_POST["truck_ifsc_code"]);
    		$truck_aadhar_no = mysqli_escape_string($connect, $_POST["truck_aadhar_no"]);
    		$truck_remarks = mysqli_escape_string($connect, $_POST["truck_remarks"]);
    		
    		$query = "insert into truck_mst(id, truck_number,truck_owner_name,truck_address_one,truck_address_two,truck_place,truck_mobile_number,truck_pan_number,truck_bank_name,truck_bank_acc_number,truck_bank_branch,truck_ifsc_code,truck_aadhar_no,truck_remarks) Values ('" . $srno . "','" . $truck_number . "','" . $truck_owner_name . "','" . $truck_address_one . "','" . $truck_address_two . "','" . $truck_place . "','" . $truck_mobile_number . "','" . $truck_pan_number . "','" . $truck_bank_name . "','" . $truck_bank_acc_number . "','" . $truck_bank_branch . "','" . $truck_ifsc_code . "','" . $truck_aadhar_no . "','" . $truck_remarks . "')";
    		if (mysqli_query($connect, $query)) {
    			$update = "update control_mst set srno = '" . $srnoupd . "' where description='TRUCK'";
    			mysqli_query($connect, $update) or die(mysqli_error());
    		}
		}
	}
	if ($_POST["action"] == "Update") {
	    
		$id = mysqli_escape_string($connect, $_POST["id"]);
		$truck_number = mysqli_escape_string($connect, $_POST["truck_number"]);
		$truck_owner_name = mysqli_escape_string($connect, $_POST["truck_owner_name"]);
		$truck_address_one = mysqli_escape_string($connect, $_POST["truck_address_one"]);
		$truck_address_two = mysqli_escape_string($connect, $_POST["truck_address_two"]);
		$truck_place = mysqli_escape_string($connect, $_POST["truck_place"]);
		$truck_mobile_number = mysqli_escape_string($connect, $_POST["truck_mobile_number"]);
		$truck_pan_number = mysqli_escape_string($connect, $_POST["truck_pan_number"]);
		$truck_bank_name = mysqli_escape_string($connect, $_POST["truck_bank_name"]);
		$truck_bank_acc_number = mysqli_escape_string($connect, $_POST["truck_bank_acc_number"]);
		$truck_bank_branch = mysqli_escape_string($connect, $_POST["truck_bank_branch"]);
		$truck_ifsc_code = mysqli_escape_string($connect, $_POST["truck_ifsc_code"]);
		$truck_aadhar_no = mysqli_escape_string($connect, $_POST["truck_aadhar_no"]);
		$truck_remarks = mysqli_escape_string($connect, $_POST["truck_remarks"]);
		
		$update = "update truck_mst set truck_number = '" . $truck_number . "',truck_owner_name = '" . $truck_owner_name . "',truck_address_one = '" . $truck_address_one . "',truck_address_two = '" . $truck_address_two . "',truck_place = '" . $truck_place . "',truck_mobile_number = '" . $truck_mobile_number . "',truck_pan_number = '" . $truck_pan_number . "',truck_bank_name = '" . $truck_bank_name . "',truck_bank_acc_number = '" . $truck_bank_acc_number . "',truck_bank_branch = '" . $truck_bank_branch . "',truck_ifsc_code = '" . $truck_ifsc_code . "',truck_aadhar_no = '" . $truck_aadhar_no . "',truck_remarks = '" . $truck_remarks . "' where id = '" . $id . "'";
		if (mysqli_query($connect, $update)) {
		    
		}
		
	}
	if ($_POST["action"] == "Delete") {
		$delete = "Delete from truck_mst where id = '" . $_POST["id"] . "'";
		if (mysqli_query($connect, $delete)) {
		    
		}
	}
	
}