#!/usr/bin/perl

use strict;

use Gtk2 -init;
use Gtk2::GladeXML;
use Gtk2::Ex::Simple::List;

use DBI;

use lib("../lib");
use Gtk2::Ex::Datasheet::DBI;

# Some globals
my ( $testwindow, $dbh, $staff_datasheet );

Gtk2::Ex::Simple::List->add_column_type(
					'combo',
					type		=> 'Glib::String',      
					renderer	=> 'Gtk2::CellRendererCombo',
					attr		=> "text"
					#attr     => sub
					#	{
					#		my ($tree_column, $combo, $model, $iter, $column) = @_;
					#		my $data = $model->get ($iter, $column);
					#		my $c_model = $combo->get('model');
					#		my $c_iter = $c_model->get_iter_first;
					#		
					#		while ($c_iter) {                    
					#			if ($data eq $c_model->get($c_iter, 0)) {
					#				$combo->set_active_iter($c_iter);
					#				last;
					#			}
					#			$c_iter = $c_model->iter_next($c_iter);
					#		}
					#
					#	}
);

sub LoadForm {
	
	$dbh = DBI->connect ("dbi:mysql:dbname=sales;host=screamer;port=3306", "sales", "salespass", {
                                                                                            PrintError => 0,
                                                                                            RaiseError => 0,
                                                                                            AutoCommit => 1,     });
	
	$testwindow = Gtk2::GladeXML->new("test_datasheet.glade", 'TestWindow');
	$testwindow->signal_autoconnect_from_package;
	
	my $groupno_model = create_combomodel(
                                            $dbh,
                                            "select GroupID, Description
                                                from NUS.Groups",
                                            {
                                               id          => "GroupID",
                                               id_def      => "Glib::Int",
                                               display     => "Description",
                                               display_def => "Glib::String"
                                            }
                                          );
	
	my $datasheet_def = {
				dbh		=> $dbh,
				table		=> "AllStaff",
				primary_key	=> "ID",
				sql_select	=> "select FirstName, LastName, Active, GroupNo",
				sql_order_by	=> "order by LastName",
				treeview	=> $testwindow->get_widget("AllStaff_SimpleList"),
				status_label	=> $testwindow->get_widget("lbl_DatasheetStatus"),
				fields		=> [
						    {
							display		=> "First Name",
							percent		=> 35
						    },
						    {
							display		=> "Last Name",
							percent		=> 35
						    },
						    {
							display		=> "Active",
							percent		=> 10,
							#renderer	=> "bool"
						    },
						    {
							display		=> "Group",
							percent		=> 20,
							renderer	=> "combo",
							model		=> $groupno_model
						    }
						   ]
			    };
	
	$staff_datasheet = Gtk2::Ex::Datasheet::DBI->new($datasheet_def)
		|| die ("Error setting up Gtk2::Ex::Datasheet::DBI\n");
	
	
	
}

sub create_combomodel {
	
	# Returns a model for use in a GtkComboBoxEntry
	
	my ( $dbh, $sql, $fields ) = @_;
	
	my $liststore = Gtk2::ListStore->new(
						$fields->{id_def},
						$fields->{display_def}
					    );
	
	my $sth = $dbh->prepare($sql);
	$sth->execute;
	
	my $iter;
	
	while (my $row = $sth->fetchrow_hashref) {
		$iter = $liststore->append;
		$liststore->set($iter, 0, $row->{$fields->{id}}, 1, $row->{$fields->{display}});
	}
	
	return $liststore;
	
}

{
    
    LoadForm;
    Gtk2->main;
    
}