var Connection = require('./connection');
var Model = require('./model');

module.exports = new Class({
	Extends: Vn.Form
	,Tag: 'db-lot'
	,Properties: {
		/**
		 * The connection used to execute the statement.
		 */
		conn: {
			type: Connection
			,set: function(x) {
				this.model.conn = x;
			}
			,get: function() {
				return this.model.conn;
			}
		},
		/**
		 * The model query.
		 */
		query: {
			type: String
			,set: function(x) {
				this.model.query = x;
			}
			,get: function() {
				return this.model.query;
			}
		},
		/**
		 * The model select statement.
		 */
		stmt: {
			type: Sql.Stmt
			,set: function(x) {
				this.model.stmt = x;
			}
			,get: function() {
				return this.model.stmt;
			}
		},
		/**
		 * The lot used to execute the statement.
		 */
		lot: {
			type: Vn.Lot
			,set: function(x) {
				this.model.lot = x;
			}
			,get: function() {
				return this.model.lot;
			}
		}
	}

	,initialize: function(props) {
		this.model = new Model();
		Vn.Form.prototype.initialize.call(this, props);
	}

	,appendChild: function(child) {
		if (child.nodeType === Node.TEXT_NODE)
			this.query = child.textContent;
	}
	
	,refresh: function() {
		if (this._model)
			this._model.refresh();
	}
	
	,performOperations: function() {
		if (this._model)
			this._model.performOperations();
	}
	
	/**
	 * Get the index of the column from its name.
	 *
	 * @param {string} columnName The column name
	 * @return {integer} The column index or -1 if column not exists
	 */
	,getColumnIndex: function(columnName) {
		return this._model ?
			this._model.getColumnIndex(columnName) : -1;
	}

	/**
	 * Gets a value from the form using the column index.
	 *
	 * @param {string} columnName The column index
	 * @return {Object} The value
	 */
	,getByIndex: function(column) {
		return this._model.getByIndex(this._row, column);
	}

	/**
	 * Sets a value on the form using the column index.
	 *
	 * @param {string} columnName The column index
	 * @param {Object} value The new value
	 */
	,setByIndex: function(column, value) {
		return this._model.setByIndex(this._row, column, value);
	}
});