use strict;

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

   $cTable = $oSessionGL->getState("CreateAppRc,DbTable");

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

   $oConfigFileTmp = ConfigFile->new
       ($oSessionGL->getState("CreateAppRc,ConfigFile"),
        ReadOnly => 0);

   $oConfigFile = ConfigFile->new
       (&GetAttr("BaseDir") . "/lib/WWWdb/tmp/" .
        $cTable . ".rc",
        ReadOnly => 0);

   # Should this field appear in query-mode
   $oConfigFileTmp->setValue("Layout Label Ruler1",
                             "Text", "<HR>");

#     $oDbHandle = &GetAttr("DbHandle");

#     # this is a trick to find all field-names of a table
#     $oStmtHdl = $oDbHandle->prepare("SELECT * FROM $cTable WHERE 0 = 1")
#         or die "Can't prepare SELECT * FROM $cTable\n";
#     $oStmtHdl->execute()
#         or die("Can't get the fields of table $cTable\n");

#     $iCnt = 0;

#     # append field-names
#     foreach (@{$oStmtHdl->{NAME}})
#     {

#        push @lFields, $_;

#        $iCnt ++;
#     }

#     &SetField("Fields", join ",", @lFields);


   $oConfigFileTmp->CreateSectionAndEntries
       (["Layout Label Hdr_Qry",
         [["Text"   => i18n("Qry-Section")],
          ["Pos"    => "+2.0+5.0"],
          ["BgColor" => "#dddddd"],
          ["Bold"    => "True"],
          ["Big"    => "True"],
          ["Font"    => "Color=blue"]]],

        ["Layout Label Qry_BgColor2",
         [["Text"   => i18n("Second Background-Color for query-mode")],
          ["Pos"    => "+1.0.0"]]],
        ["Layout Field Qry_BgColor2",
         [["Pos"    => "+0.+1+4.0"],
          ["Type"   => "Text"],
          ["Size"   => "10"]]],

        ["Layout Label Qry_NrOfColLine",
         [["Text"   => i18n("After how many records should appear a " .
                            "line with the second Background-Color")],
          ["Pos"    => "+1.0.0"]]],
        ["Layout Field Qry_NrOfColLine",
         [["Pos"    => "+0.+1+4.0"],
          ["Type"   => "Select"],
          ["Size"   => "1"],
          ["Values" => (join "\n", (1 .. 20,
                                    25, 30, 35, 40, 50,
                                    100, 200, 300, 400,
                                    9999))]]])
           if ($cTable ne "UserFormat");

   $oConfigFileTmp->CreateSectionAndEntries
       (["Layout Label Hdr_State_All",
         [["Text"   => i18n("State All-Section")],
           ["Pos"    => "+1.0+5.0"],
           ["BgColor" => "#dddddd"],
           ["Bold"    => "True"],
           ["Big"    => "True"],
           ["Font"    => "Color=blue"]]],

         ["Layout Label State_All_BtnCancel",
          [["Text"   => i18n("Label for <B>Cancel</B>-Button:<BR>" .
                             "(A '-' will disable that button)")],
           ["Pos"    => "+1.0.0"]]],
         ["Layout Field State_All_BtnCancel",
          [["Pos"    => "+0.+1+4.0"],
           ["Size"   => "40"]]],

         ["Layout Label Hdr_State_Init",
          [["Text"   => i18n("State Init-Section")],
           ["Pos"    => "+1.0+5.0"],
           ["BgColor" => "#dddddd"],
           ["Bold"    => "True"],
           ["Big"    => "True"],
           ["Font"    => "Color=blue"]]],

         ["Layout Label State_Init_Header",
          [["Text"   => i18n("Header for this mode")],
           ["Pos"    => "+1.0.0"]]],
         ["Layout Field State_Init_Header",
          [["Pos"    => "+0.+1+4.0"],
           ["Size"   => "60"]]],

         ["Layout Label State_Init_BtnNew",
          [["Text"   => i18n("Label for <B>New</B>-Button:<BR>" .
                             "(A '-' will disable that button)")],
           ["Pos"    => "+1.0.0"]]],
         ["Layout Field State_Init_BtnNew",
          [["Pos"    => "+0.+1+4.0"],
           ["Size"   => "40"]]],

         ["Layout Label State_Init_BtnQry",
          [["Text"   => i18n("Label for <B>Qry</B>-Button:<BR>" .
                             "(A '-' will disable that button)")],
           ["Pos"    => "+1.0.0"]]],
         ["Layout Field State_Init_BtnQry",
          [["Pos"    => "+0.+1+4.0"],
           ["Size"   => "40"]]]);

   $oConfigFileTmp->CreateSectionAndEntries
       (["Layout Label Hdr_State_Work",
          [["Text"   => i18n("State Work-Section")],
           ["Pos"    => "+1.0+5.0"],
           ["BgColor" => "#dddddd"],
           ["Bold"    => "True"],
           ["Big"    => "True"],
           ["Font"    => "Color=blue"]]],

         ["Layout Label State_Work_Header",
          [["Text"   => i18n("Header for this mode")],
           ["Pos"    => "+1.0.0"]]],
         ["Layout Field State_Work_Header",
          [["Pos"    => "+0.+1+4.0"],
           ["Size"   => "60"]]],

         ["Layout Label State_Work_BtnUpd",
          [["Text"   => i18n("Label for <B>Upd</B>-Button:<BR>" .
                             "(A '-' will disable that button)")],
           ["Pos"    => "+1.0.0"]]],
         ["Layout Field State_Work_BtnUpd",
          [["Pos"    => "+0.+1+4.0"],
           ["Size"   => "40"]]],

         ["Layout Label State_Work_BtnDel",
          [["Text"   => i18n("Label for <B>Del</B>-Button:<BR>" .
                             "(A '-' will disable that button)")],
           ["Pos"    => "+1.0.0"]]],
         ["Layout Field State_Work_BtnDel",
          [["Pos"    => "+0.+1+4.0"],
           ["Size"   => "40"]]],

         ["Layout Label Hdr_State_Qry",
          [["Text"   => i18n("State Qry-Section")],
           ["Pos"    => "+1.0+5.0"],
           ["BgColor" => "#dddddd"],
           ["Bold"    => "True"],
           ["Big"    => "True"],
           ["Font"    => "Color=blue"]]],

         ["Layout Label State_Qry_Header",
          [["Text"   => i18n("Header for this mode")],
           ["Pos"    => "+1.0.0"]]],
         ["Layout Field State_Qry_Header",
          [["Pos"    => "+0.+1+4.0"],
           ["Size"   => "60"]]])
           if ($cTable ne "UserFormat");

   &SetField("Qry_BgColor2",
             $oConfigFile->getValue("Qry",
                                    "BgColor2",
                                    "#eeeeee"));
   &SetField("Qry_NrOfColLine",
             $oConfigFile->getValue("Qry",
                                    "NrOfColLine",
                                    "2"));
   &SetField("State_All_BtnCancel",
             $oConfigFile->getValue("State All",
                                    "BtnCancel",
                                    i18n("Back")));
   &SetField("State_Init_Header",
             $oConfigFile->getValue("State Init",
                                    "Header",
                                    i18n("Search / Create")));
   &SetField("State_Init_BtnNew",
             $oConfigFile->getValue("State Init",
                                    "BtnNew",
                                    i18n("Create")));
   &SetField("State_Init_BtnQry",
             $oConfigFile->getValue("State Init",
                                    "BtnQry",
                                    i18n("Search")));
   &SetField("State_Work_Header",
             $oConfigFile->getValue("State Work",
                                    "Header",
                                    i18n("Work with ...")));
   &SetField("State_Work_BtnUpd",
             $oConfigFile->getValue("State Work",
                                    "BtnUpd",
                                    i18n("Save")));
   &SetField("State_Work_BtnDel",
             $oConfigFile->getValue("State Work",
                                    "BtnDel",
                                    i18n("Delete")));
   &SetField("State_Qry_Header",
             $oConfigFile->getValue("State Qry",
                                    "Header",
                                    i18n("Select Data")));


#     foreach $lAtt (@lExtraFields)
#     {
#        my $lValues;

#        $oConfigFileTmp->CreateSectionAndEntries
#            (["Layout Label $lAtt->[0]",
#               [["Text"   => $lAtt->[1]],
#                ["Pos"    => "+31.0.0"],
#                ["Align"  => "Right"],
#                ["Bold"   => "Yes"]]],

#              ["Layout Field $lAtt->[0]",
#               [["Pos"    => "+0.+1+4.0"]]]);


#        foreach $lValues (@lAtt->[2])
#        {

#           # Should this field appear in query-mode
#           $oConfigFileTmp->setValue("Layout Field $lAtt->[0]",
#                                  $lAtt->[2]->[0],
#                                  $lAtt->[2]->[1]);

#        }
#     }


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

   $oConfPoolGL->addConfigFile ($oConfigFileTmp);

}

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

      GenConfigFile();

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


sub MyBtnNext ()
{

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

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


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


sub GenConfigFile ()
{
   my $oConfigFile;

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

   $oConfigFile->CreateSectionAndEntries
       (["Qry",
         [["BgColor2"    => &GetField("Qry_BgColor2")]]],
        ["Qry",
         [["NrOfColLine" => &GetField("Qry_NrOfColLine")]]],
        ["State All",
         [["Header"      => &GetField("State_All_Header")],
          ["BtnCancel"   => &GetField("State_All_BtnCancel")]]],
        ["State Qry",
         [["Header"      => &GetField("State_Qry_Header")]]],
        ["State Init",
         [["Header"      => &GetField("State_Init_Header")],
          ["BtnNew"      => &GetField("State_Init_BtnNew")],
          ["BtnQry"      => &GetField("State_Init_BtnQry")]]],
        ["State Work",
         [["Header"      => &GetField("State_Work_Header")],
          ["BtnUpd"      => &GetField("State_Work_BtnUpd")],
          ["BtnDel"      => &GetField("State_Work_BtnDel")]]]);

   $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;

