<?php
if (isset($_POST["action"])) {
    
	$output = '';
	require('../config/dbcon.php');
	if ($_POST["action"] == "Add") {
	    
		$query = "Select srno from control_mst where description='SUPP'";
		$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
		while ($row = mysqli_fetch_assoc($rs)) {
			$srno = $row['srno'];
			$srnoupd = $srno+1;
    		$supplier_name = mysqli_escape_string($connect, $_POST["supplier_name"]);
    		$supp_address_one = mysqli_escape_string($connect, $_POST["supp_address_one"]);
    		$supp_address_two = mysqli_escape_string($connect, $_POST["supp_address_two"]);
    		$supp_place = mysqli_escape_string($connect, $_POST["supp_place"]);
    		$supp_contact_person = mysqli_escape_string($connect, $_POST["supp_contact_person"]);
    		$supp_mobile_number = mysqli_escape_string($connect, $_POST["supp_mobile_number"]);
    		$supp_pan_number = mysqli_escape_string($connect, $_POST["supp_pan_number"]);
    		$supp_bank_name = mysqli_escape_string($connect, $_POST["supp_bank_name"]);
    		$supp_bank_acc_number = mysqli_escape_string($connect, $_POST["supp_bank_acc_number"]);
    		$supp_bank_branch = mysqli_escape_string($connect, $_POST["supp_bank_branch"]);
    		$supp_ifsc_code = mysqli_escape_string($connect, $_POST["supp_ifsc_code"]);
    		$supp_supp_aadhar_no = mysqli_escape_string($connect, $_POST["supp_supp_aadhar_no"]);
    		$supp_remarks = mysqli_escape_string($connect, $_POST["supp_remarks"]);
    		
    		$query = "insert into supp_mst(id, supplier_name,supp_address_one,supp_address_two,supp_place,supp_contact_person,supp_mobile_number,supp_pan_number,supp_bank_name,supp_bank_acc_number,supp_bank_branch,supp_ifsc_code,supp_supp_aadhar_no,supp_remarks) Values ('" . $srno . "','" . $supplier_name . "','" . $supp_address_one . "','" . $supp_address_two . "','" . $supp_place . "','" . $supp_contact_person . "','" . $supp_mobile_number . "','" . $supp_pan_number . "','" . $supp_bank_name . "','" . $supp_bank_acc_number . "','" . $supp_bank_branch . "','" . $supp_ifsc_code . "','" . $supp_supp_aadhar_no . "','" . $supp_remarks . "')";
    		if (mysqli_query($connect, $query)) {
    			$update = "update control_mst set srno = '" . $srnoupd . "' where description='SUPP'";
    			mysqli_query($connect, $update) or die(mysqli_error());
    		}
		}
	}
	if ($_POST["action"] == "Update") {
	    
		$id = mysqli_escape_string($connect, $_POST["id"]);
		$supplier_name = mysqli_escape_string($connect, $_POST["supplier_name"]);
		$supp_address_one = mysqli_escape_string($connect, $_POST["supp_address_one"]);
		$supp_address_two = mysqli_escape_string($connect, $_POST["supp_address_two"]);
		$supp_place = mysqli_escape_string($connect, $_POST["supp_place"]);
		$supp_contact_person = mysqli_escape_string($connect, $_POST["supp_contact_person"]);
		$supp_mobile_number = mysqli_escape_string($connect, $_POST["supp_mobile_number"]);
		$supp_pan_number = mysqli_escape_string($connect, $_POST["supp_pan_number"]);
		$supp_bank_name = mysqli_escape_string($connect, $_POST["supp_bank_name"]);
		$supp_bank_acc_number = mysqli_escape_string($connect, $_POST["supp_bank_acc_number"]);
		$supp_bank_branch = mysqli_escape_string($connect, $_POST["supp_bank_branch"]);
		$supp_ifsc_code = mysqli_escape_string($connect, $_POST["supp_ifsc_code"]);
		$supp_supp_aadhar_no = mysqli_escape_string($connect, $_POST["supp_supp_aadhar_no"]);
		$supp_remarks = mysqli_escape_string($connect, $_POST["supp_remarks"]);
		
		$update = "update supp_mst set supplier_name = '" . $supplier_name . "',supp_address_one = '" . $supp_address_one . "',supp_address_two = '" . $supp_address_two . "',supp_place = '" . $supp_place . "',supp_contact_person = '" . $supp_contact_person . "',supp_mobile_number = '" . $supp_mobile_number . "',supp_pan_number = '" . $supp_pan_number . "',supp_bank_name = '" . $supp_bank_name . "',supp_bank_acc_number = '" . $supp_bank_acc_number . "',supp_bank_branch = '" . $supp_bank_branch . "',supp_ifsc_code = '" . $supp_ifsc_code . "',supp_supp_aadhar_no = '" . $supp_supp_aadhar_no . "',supp_remarks = '" . $supp_remarks . "' where id = '" . $id . "'";
		if (mysqli_query($connect, $update)) {
		    
		}
		
	}
	if ($_POST["action"] == "Delete") {
		$delete = "Delete from supp_mst where id = '" . $_POST["id"] . "'";
		if (mysqli_query($connect, $delete)) {
		    
		}
	}
	
}