// Export Telefonliste html
// nachbearbeiteter Code
// s.a. 2024-08-16
using craftsReport.Interfaces;

namespace craftsReport.modules
{
	public class rkdhtm_xbs : IReportModule
	{
		public IReportHost HostObject { set; get; }

		public rkdhtm_xbs()
		{
		}


		//  Telefonliste HTML
		// ------------------------------------------------------------
		public bool Execute()
		{

			HostObject.DefineVariable2("Seite", "N,4,0");
			HostObject.DefineVariable2("Zeile", "N,2,0");
			HostObject.DefineVariable2("Charset", "C,10,0");
			HostObject.DefineVariable2("Heute", "D,10,0");
			HostObject.DefineRecord("Dest", "ADRESSSTAMM", 0, 0, HostObject.EvalString(@""));
			HostObject.Set("Charset", @"ANSI", true);
			HostObject.Set("Heute", @"H", true);
			HostObject.Set("Seite", @"1", true);
			HostObject.Set("Zeile", @"1", true);

			if (!HostObject.IsVar("mKond"))
				HostObject.DefineVariable2("mKond", "l,4,0");
			if (!HostObject.IsVar("mSel1"))
				HostObject.DefineVariable2("mSel1", "u,15,0");
			if (!HostObject.IsVar("mSel2"))
				HostObject.DefineVariable2("mSel2", "u,15,0");

			HostObject.WriteLine(HostObject.EvalString(@"<HTML><TITLE>Kundentelefonliste</TITLE><BODY>"));
			HostObject.WriteLine(HostObject.EvalString(@"<TABLE WIDTH=100% BORDER>"));
			HostObject.WriteLine(HostObject.EvalString(@"<TH bgcolor=#ffff00 ALIGN=left>Nr</TH>"));
			HostObject.WriteLine(HostObject.EvalString(@"<TH bgcolor=#ffff00 ALIGN=left>Name</TH>"));
			HostObject.WriteLine(HostObject.EvalString(@"<TH bgcolor=#ffff00 ALIGN=left>Telefon</TH>"));

			HostObject.FindRecord("Dest", HostObject.EvalString("$[KeyVon]"));
			while(!HostObject.TestEof("Dest"))
			{
				SUB_DestLoop();
				HostObject.SkipRecord("Dest");
			}

			HostObject.WriteLine(HostObject.EvalString(@"</TABLE>"));
			HostObject.WriteLine(HostObject.EvalString(@"</BODY></HTML>"));
			return true;
		}


		// ------------------------------------------------------------
		//  Schleife ueber die Datensaetze der Variable Dest (Adressen)
		bool SUB_DestLoop()
		{
			HostObject.VariablesPush();

			if (HostObject.EvalBool("$[mKond] = 0"))
				goto NoKond;
			if (HostObject.EvalBool("$[mKond] <> $[Dest.KONDITION]"))
				return HostObject.VariablesPop();
			NoKond:

			//  * ==================================================
			//  * Abfrage der Bedingung Bis-Key
			if (HostObject.IsEmpty("KeyBis"))
				goto DoIt;

			if (HostObject.CompareString("Dest.DESTDEBNR", "<", HostObject.EvalString(@"$[KeyBis]")))
				goto DoIt;

			if (HostObject.CompareString("Dest.DESTDEBNR", "=", HostObject.EvalString(@"$[KeyBis]")))
				goto DoIt;

			HostObject.BreakRecord("Dest");
			return HostObject.VariablesPop();

			//  * ==================================================
			DoIt:

			if (HostObject.IsEmpty("mSel1"))
				goto DoIt2;
			if (!HostObject.CompareString("Dest.SELECTION1", "=", HostObject.EvalString(@"$[mSel1]")))
				return HostObject.VariablesPop();

			DoIt2:
			if (HostObject.IsEmpty("mSel2"))
				goto DoIt3;
			if (!HostObject.CompareString("Dest.SELECTION2", "=", HostObject.EvalString(@"$[mSel2]")))
				return HostObject.VariablesPop();

			DoIt3:

			HostObject.DefineVariable2("TmpName", "C,80,0");
			HostObject.DefineVariable2("TmpAddr", "C,80,0");

			HostObject.Set("TmpName", @"$[Dest.Name1] $[Dest.Name2]", true);
			HostObject.Set("TmpAddr", @"$[Dest.Strasse] $[Dest.Plz<8]", true);
			HostObject.Set("TmpAddr", @"$[TmpAddr] $[Dest.Ort]", true);

			HostObject.WriteLine(HostObject.EvalString(@"<TR>"));
			HostObject.WriteLine(HostObject.EvalString(@"<TD ROWSPAN=2 VALIGN=top>$[Dest.DESTDEBNR]</TD>"));
			HostObject.WriteLine(HostObject.EvalString(@"<TD>$[TmpName]</TD>"));
			HostObject.WriteLine(HostObject.EvalString(@"<TD>$[Dest.Telefon1]</TD>"));
			HostObject.WriteLine(HostObject.EvalString(@"</TR>"));
			HostObject.WriteLine(HostObject.EvalString(@"<TR>"));
			HostObject.WriteLine(HostObject.EvalString(@"<TD>$[TmpAddr]</TD>"));
			HostObject.WriteLine(HostObject.EvalString(@"<TD>$[Dest.FaxNummer]</TD>"));
			HostObject.WriteLine(HostObject.EvalString(@"</TR>"));

			HostObject.Set("Zeile", @"$[Zeile] + 1", true);

			return HostObject.VariablesPop();
		}



	}
}
