use strict;

sub PreCreateForm ()
{
   my $cRecord;
   my $cTable;
   my $iCnt;
   my $lAtt;
   my $oConfigFile;
   my $oConfigFileTmp;
   my $pDbHdl;
   my $pStmtHdl;
   my @lExtraFields = ();
   my @lFields;
   my @lResult      = ();

   # get the table-name, we generate the config-file for
   $cTable = $oSessionGL->getState("CreateAppRc,DbTable");
   &SetField("DbTable", $cTable);

   &MySetVal("Header", "Title",
             i18n(&MyVal("Header", "Title", "")) . " ($cTable)");

   # this button is for deletion of a button
   if(&GetAttr("LastBtn") =~ /^BtnUP_(.*?)_(.*?)$/)
   {
      &MyBtnMove($1, $2, -15);
   }
   elsif(&GetAttr("LastBtn") =~ /^BtnDN_(.*?)_(.*?)$/)
   {
      &MyBtnMove($1, $2, 15);
   }

   # this is the temporary config-file for the actual HTML-page
   $oConfigFileTmp = ConfigFile->new
       ($oSessionGL->getState("CreateAppRc,ConfigFile"),
        ReadOnly => 0);

   # this is the config-file, we work with
   $oConfigFile = ConfigFile->new
       (&GetAttr("BaseDir") . "/lib/WWWdb/tmp/" .
        $cTable . ".rc",
        ReadOnly => 0);

   # This are the fields in the title of the table
   my @lTblHdrAtts =
       (["LBL", i18n("Name of the field"),         "+2.+0.0" ],
        ["MOVE", i18n("Move this field "),         "+0.+1+1.0" ],
        ["VERT", i18n("Vertical<BR>Distance"),     "+0.+1+1.0" ],
        ["HOR", i18n("Horizontal<BR>Distance"),    "+0.+1+1.0" ],
        ["OFF", i18n("Column-<BR>Offset"),         "+0.+1+1.0" ],
        ["SPAN", i18n("Cell-<BR>Spanning"),        "+0.+1.0" ]
        );

   foreach $lAtt (@lTblHdrAtts)
   {
      $oConfigFileTmp->CreateSectionAndEntries
          (["Layout Label $lAtt->[0]",
            [["Text"   => $lAtt->[1]],
             ["Pos"    => $lAtt->[2]],
             ["Align"  => "Center"],
             ["Bold"   => "Yes"]]]);

   }

   $oConfigFileTmp->setValue("Layout Label Ruler1",
                             "Text", "<HR>");

   $iCnt = 0;

   # append field-names
   foreach ($oConfigFile->getSectionNames())
   {
      my $bVertRel;
      my $iVert;
      my $bHorRel;
      my $iHor;
      my $bOffRel;
      my $iOff;
      my $iSpan;
      my $cPos;
      my $cBackground;

      if ($_ =~ /Layout\s+(Label|Field)\s+(\S+)/)
      {
         my $cType = $1;
         my $cName = $2;

         $cPos = $oConfigFile->getValue("Layout ${cType} ${cName}",
                                        "Pos",
                                        "+0.+0.+0");

         $oConfigFileTmp->CreateSectionAndEntries
             (["Layout Label lbl_${cType}_${cName}",
               [["Pos"     => "+1.+0.0"],
                ["Text"    => "${cName} [" . i18n($cType) . "]"],
                ["Align"   => "Right"],
                ["Bold"    => "Yes"],
                ["BgColor" => $cBackground]]]);

         if($cPos =~ /(\+)?(\d+)\.(\+)?(\d+)((\+)?(\d+))?\.(\+)?(\d+)/)
         {

            $bVertRel = $1;
            $iVert    = $2;
            $bHorRel  = $3;
            $iHor     = $4;
            $bOffRel  = $8;
            $iOff     = $9;
            $iSpan    = $7? "$7": "0";
         }
         else
         {
            $bVertRel = "+";
            $iVert    = "1";
            $bHorRel  = "+";
            $iHor     = "0";
            $bOffRel  = "";
            $iOff     = "0";
            $iSpan    = "0";
         }

         # the attributes for each field
         foreach $lAtt (["UP", "Btn", "" ,
                         [["Label"      => i18n("Up.png")]]],
                        ["DN", "Btn", "" ,
                         [["Label"      => i18n("Down.png")]]],
                        ["VERT_R", "Checkbutton", $bVertRel,
                         [["Text"      => "+"]]],
                        ["VERT_V", "Select",      $iVert,
                         [["Values"    => join "\n", (0 .. 15)],
                          ["NoUndef"   => "Yes"]]],
                        ["HOR_R", "Checkbutton", $bHorRel,
                         [["Text"      => "+"]]],
                        ["HOR_V", "Select",      $iHor,
                         [["Values"    => join "\n", (0 .. 15)],
                          ["NoUndef"   => "Yes"]]],
                        ["OFF_R", "Checkbutton", $bOffRel,
                         [["Text"      => "+"]]],
                        ["OFF_V", "Select",      $iOff,
                         [["Values"    => join "\n", (0 .. 15)],
                          ["NoUndef"   => "Yes"]]],
                        ["SPAN_V", "Select",      $iSpan,
                         [["Values"    => join "\n", (0 .. 15)],
                          ["NoUndef"   => "Yes"]]])
         {
            my $lValue;

            # the single attributes
            foreach $lValue (["Text",    ""],
                             ["Pos",     "+0.+1.0"],
                             ["Type",    "$lAtt->[1]"],
                             ["Align",   "Center"],
                             ["BgColor", $cBackground])
            {

               $oConfigFileTmp->setValue("Layout Field $lAtt->[0]_${cType}_${cName}",
                                         $lValue->[0],
                                         $lValue->[1]);
            }

            # the extended attributes
            foreach $lValue (@{$lAtt->[3]})
            {

               $oConfigFileTmp->setValue("Layout Field $lAtt->[0]_${cType}_${cName}",
                                         $lValue->[0],
                                         $lValue->[1]);
            }

            &SetField("$lAtt->[0]_${cType}_${cName}", $lAtt->[2]);
         }
      }
      else
      {
         next;
      }

      $iCnt ++;
   }

   # $oConfigFileTmp->Dump();
   $oConfigFileTmp->WriteFile_Ini();

   $oConfPoolGL->addConfigFile ($oConfigFileTmp);

}



sub MyBtnFinish ()
{
   if(!&GetAttr("RecordOk"))
   {
      &GenPage();
   }
   else
   {

      &MyBtnMove();

      &Redirect(&GetAttr("SessionId"), "WWWdb:tmp:" . GetField("DbTable"));
   }
}


sub MyBtnNext ()
{

   if(!&GetAttr("RecordOk"))
   {
      &GenPage();
   }
   else
   {
      MyBtnMove();

      print &Redirect(&GetAttr("SessionId"), "WWWdb:Wiz:CreateAppRc_3");
   }
}


sub MyBtnPrev ()
{
   print &Redirect(&GetAttr("SessionId"), "WWWdb:Wiz:CreateAppRc_2");
}



# this is for the deletion of an extra-Field
sub MyBtnMove($$$)
{
   my $cTypePI  = shift;
   my $cFieldPI = shift;
   my $iDistPI  = shift;

   if(!&GetAttr("RecordOk"))
   {
      &GenPage();
   }
   else
   {
      my $oSection;
      my $oConfigFile;

      # Remove the corresponding sections
      $oConfigFile = ConfigFile->new
          (&GetAttr("BaseDir") . "/lib/WWWdb/tmp/" . GetField("DbTable") . ".rc",
           ReadOnly => 0);

      if($cTypePI && $cFieldPI)
      {
         $oSection = $oConfigFile->getSections()->Get("Layout $cTypePI $cFieldPI");

         if ($oSection)
         {
            $oSection->setSortNo($oSection->getSortNo() + $iDistPI);
         }

         $oConfigFile->Dump();
      }

      # append field-names
      foreach ($oConfigFile->getSectionNames())
      {

         if ($_ =~ /Layout\s+(Label|Field)\s+(\S+)/)
         {
            my $cType = $1;
            my $cName = $2;

            $oConfigFile->setValue
                ($_, "Pos",
                 sprintf("%s%d.%s%d%s.%s%d",
                         (&GetField("VERT_R_${cType}_${cName}")? "+": ""),
                         &GetField("VERT_V_${cType}_${cName}"),
                         (&GetField("HOR_R_${cType}_${cName}")? "+": ""),
                         &GetField("HOR_V_${cType}_${cName}"),
                         (&GetField("SPAN_V_${cType}_${cName}")?
                          "+" . &GetField("SPAN_V_${cType}_${cName}"):
                          ""),
                         (&GetField("OFF_R_${cType}_${cName}")? "+": ""),
                         &GetField("OFF_V_${cType}_${cName}")));
         }

      }

      $oConfigFile->WriteFile_Ini();

      system("cd " . &GetAttr("BaseDir") . "/lib/WWWdb/tmp; " .
             "mv " . &GetField("DbTable") . ".rc.new " .
             &GetField("DbTable") . ".rc");

   }

}


sub PostFirstColumn ()
{
   my $cResult = "";


   $cResult .= (br() x 2 .
                b(i18n("See also:")) .
                li(&ResolveRefField("wwwdb://WWWdb:Wiz:CreateAppRc_1",
                                    i18n("Select DB-table"))) .
                li(&ResolveRefField("wwwdb://WWWdb:Wiz:CreateAppRc_2",
                                    i18n("Select DB-fields"))) .
                li(&ResolveRefField("wwwdb://WWWdb:Wiz:CreateAppRc_2a",
                                    i18n("Field-order"))) .
                li(&ResolveRefField("wwwdb://WWWdb:Wiz:CreateAppRc_3",
                                    i18n("Mode-Information"))) .
                li(&ResolveRefField("wwwdb://WWWdb:Wiz:CreateAppRc_4",
                                    i18n("Field attributes")))
                );

   return small($cResult);

}



1;




