<?php
class UserRole{
    // database connection and table name
    private $conn;
    private $table_name = "userrole";
 
    // field list start
	public $id;
	public $rolename;
    public $create_role;
	public $create_account;
	public $change_password;

	// public $city;
	public $ont;
	public $package;
	public $price_change;
	public $promotion;
	public $member;
	public $customer;
	public $extra_days;
	public $suspend;

	public $bank_in_out_list;
	public $order_list;
	public $sales;
	public $installation;
	public $activate_list;
	public $item_sales;
	public $complaint;
	public $sms;

	public $item;
	public $stock_in;
	public $stock_out;
	public $stock_transfer;

	public $expense;
	public $income;
	public $transfer;

	public $item_sales_rpt;
	public $stock_in_rpt;
	public $stock_out_rpt;
	public $stock_transfer_rpt;
	public $stock_balance_rpt;

	public $expense_rpt;
	public $income_rpt;
	public $transfer_rpt;
	public $cash_book_rpt;
	public $monthly_rpt;

	public $control_rpt;
	public $sales_rpt;
	public $payment_rpt;
	public $sales_by_mbps_rpt;
	public $installation_rpt;
	public $assign_rpt;
	public $expire_rpt;

    public function __construct($db){
        $this->conn = $db;
    }
	
	function getAllRows(){
		$query = "SELECT * FROM " . $this->table_name . "  ORDER BY id";
		$stmt = $this->conn->prepare($query);
		$stmt->execute();
		return $stmt;
	}

	function getOneRow(){
		$query = "SELECT * FROM " . $this->table_name . " WHERE id = ?";
		$stmt = $this->conn->prepare($query);    
		$stmt->bindParam(1, $this->id);    
		$stmt->execute();
		$row = $stmt->fetch(PDO::FETCH_ASSOC);
	
		// Dynamically assign properties from the row to the object
		if ($row) {
			foreach ($row as $key => $value) {
				if (property_exists($this, $key)) {
					$this->$key = $value;
				}
			}
		}
	}

	function getPermission(){
		$query = "SELECT * FROM " . $this->table_name . " WHERE rolename = ? LIMIT 0,1";
		$stmt = $this->conn->prepare($query);
		$stmt->bindParam(1, $this->rolename);
		$stmt->execute();
		$row = $stmt->fetch(PDO::FETCH_ASSOC);
	
		// If row exists, assign values dynamically
		if ($row) {
			foreach ($row as $key => $value) {
				if (property_exists($this, $key)) {
					$this->$key = $value;
				}
			}
		} else {
			// If no row is found, set all properties to null
			foreach ($this as $key => $value) {
				$this->$key = null;
			}
		}
	}
	
	function delete(){
		$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
		$stmt = $this->conn->prepare($query);
		
		$stmt->bindParam(1, $this->id);
		if($stmt->execute()){
			return true;
		}
		return false;
	}
	
	public function create() {
		// Prepare the columns and placeholders dynamically based on the front-end data
		$columns = '';
		$placeholders = '';
		$params = [];
	
		// Loop through the object properties (assuming this is the input data)
		foreach ($this as $key => $value) {
			// Skip any properties that are not part of the database columns (e.g., table_name)
			if ($key == 'table_name' || $key == 'conn') continue;
	
			$columns .= $key . ', ';
			$placeholders .= ':' . $key . ', ';
	
			// Only sanitize the 'rolename' field (since it's a string), the rest are integers (0 or 1)
			if ($key === 'rolename') {
				// Sanitize rolename as a string
				$sanitized_value = htmlspecialchars(strip_tags($value));
				$params[':' . $key] = $sanitized_value;
			} else {
				// For other fields, directly assign the value as is (since they are 0 or 1)
				$params[':' . $key] = $value;
			}
		}
	
		// Remove trailing commas and spaces
		$columns = rtrim($columns, ', ');
		$placeholders = rtrim($placeholders, ', ');
	
		// Prepare the query
		$query = "INSERT INTO " . $this->table_name . " ($columns) VALUES ($placeholders)";
		
		// Ensure the PDO object is used for preparing the statement correctly
		$stmt = $this->conn->prepare($query);  // Make sure $this->conn is being used here
		
		// Bind values dynamically
		foreach ($params as $param => $value) {
			// Bind the parameter with its appropriate type
			$stmt->bindValue($param, $value, is_string($value) ? PDO::PARAM_STR : PDO::PARAM_INT);
		}
	
		// Execute the query and return success/failure
		return $stmt->execute();  // Make sure you execute on the prepared statement
	}
	
	function exist(){
		$query = "SELECT id FROM " . $this->table_name . " WHERE rolename = ?  LIMIT 0,1";
		$stmt = $this->conn->prepare( $query );
		$this->rolename=htmlspecialchars(strip_tags($this->rolename));
	 
		$stmt->bindParam(1, $this->rolename);
		
		$stmt->execute();
		if($stmt->rowCount()>0){
			return true;
		}
		return false;
	}

	public function update() {
		$setFields = '';
		$params = [];
	
		// Loop through object properties, dynamically preparing the query
		foreach ($this as $key => $value) {
			if ($key !== 'table_name' && $key !== 'conn' && $key !== 'id') { // Ensure 'id' is not updated
				$setFields .= "$key = :$key, ";
				$params[":$key"] = $value;
			}
		}
	
		// Remove trailing comma and space
		$setFields = rtrim($setFields, ', ');
	
		// Prepare the SQL query
		$query = "UPDATE " . $this->table_name . " SET $setFields WHERE rolename = :rolename";
		$stmt = $this->conn->prepare($query);
	
		// Bind all parameters dynamically
		foreach ($params as $param => $value) {
			$stmt->bindValue($param, $value, is_string($value) ? PDO::PARAM_STR : PDO::PARAM_INT);
		}
	
		// Bind 'rolename' separately to ensure it is always set
		$stmt->bindValue(":rolename", $this->rolename, PDO::PARAM_STR);
	
		// Execute and return result
		return $stmt->execute();
	}
}
?>