using combit.Reporting;

using System.Data;

namespace CreateAndSplitPreview
{
    public partial class Form1 : Form
    {
        string workingdir = Application.StartupPath;
        string reportfile = "report.lst";
        string previewfilename = "preview.ll";
        
        public Form1()
        {
            InitializeComponent();
        }


        //Create Dummy DataSet
        private DataSet CreateDataSet()
        {
            DataSet ds = new();
            DataTable MasterData = new("Master");
            DataTable ChildData = new("Child");
            MasterData.Columns.Add("Id");
            MasterData.Columns.Add("Value");

            ChildData.Columns.Add("Id");
            ChildData.Columns.Add("MasterId");
            ChildData.Columns.Add("Value");

            MasterData.Rows.Add("1", "Master 1");
            MasterData.Rows.Add("2", "Master 2");
            MasterData.Rows.Add("3", "Master 3");

            ChildData.Rows.Add("1", "1", "Child 1");
            ChildData.Rows.Add("2", "1", "Child 2");
            ChildData.Rows.Add("3", "1", "Child 3");
            ChildData.Rows.Add("4", "2", "Child 1");
            ChildData.Rows.Add("5", "2", "Child 2");
            ChildData.Rows.Add("6", "2", "Child 3");
            ChildData.Rows.Add("7", "2", "Child 4");
            ChildData.Rows.Add("8", "2", "Child 5");
            ChildData.Rows.Add("9", "3", "Child 1");
            ChildData.Rows.Add("10", "3", "Child 2");
            ChildData.Rows.Add("11", "3", "Child 3");
            ChildData.Rows.Add("12", "3", "Child 4");
            ChildData.Rows.Add("13", "3", "Child 5");

            ds.Tables.Add(MasterData);
            ds.Tables.Add(ChildData);

            //Relationen zwischen den Tabellen anlegen
            ds.Relations.Add("MasterChild", ds.Tables["Master"]!.Columns["Id"]!, ds.Tables["Child"]!.Columns["MasterId"]!);

            return ds;

        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                using ListLabel LL = new();
                LL.AutoMasterMode = LlAutoMasterMode.AsVariables;
                LL.DataSource = CreateDataSet();
                LL.DataMember = "Master";
                LL.AutoProjectFile = Path.Combine(workingdir, reportfile);                
                LL.AutoShowSelectFile = false;
                LL.Design();
            }
            catch(ListLabelException exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                using ListLabel LL = new();
                LL.AutoMasterMode = LlAutoMasterMode.AsVariables;
                LL.DataSource = CreateDataSet();
                LL.DataMember = "Master";
                LL.AutoDestination = LlPrintMode.Export;
                LL.IncrementalPreview = false;

                LL.AutoProjectFile = Path.Combine(workingdir, reportfile);
                LL.AutoShowPrintOptions = false;
                LL.AutoShowSelectFile = false;

                //Silent Export without User Interaction
                LL.ExportOptions.Add(LlExportOption.ExportQuiet, "1");
                LL.ExportOptions.Add(LlExportOption.ExportFile, previewfilename);
                LL.ExportOptions.Add(LlExportOption.ExportPath, workingdir);
                LL.ExportOptions.Add(LlExportOption.ExportTarget, "PRV");

                LL.Print();
            }
            catch (ListLabelException exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                //Vorschau-Datei öffnen, WICHTIG: oneJobTranslation = false!
                using (PreviewFile pf = new PreviewFile(Path.Combine(workingdir, previewfilename), false, workingdir, LlLanguage.Default, oneJobTranslation: false))
                {

                    MessageBox.Show("Anzahl Jobs in LL-Datei: " + pf.JobCount.ToString());
                    int jobCount = pf.JobCount;   //Anzahl der Jobs in der Vorschau-Datei ermitteln


                    for (int jobIndex = pf.JobIndex; jobIndex <= jobCount; jobIndex++)
                    {
                        // Dateiname je JobIndex
                        string outputPdf = workingdir + $@"master_{jobIndex}.pdf";

                        // WICHTIG: JobIndexRange auf genau diesen Job setzen
                        // Syntax: "PDF;Export.JobIndexRange=<von>-<bis>"
                        // Hier: nur ein Job => jobIndex-jobIndex
                        pf.ConvertTo(outputPdf, $"PDF;Export.JobIndexRange={jobIndex}-{jobIndex}");
                    }

                }
            }
            catch (ListLabelException exc)
            {
                MessageBox.Show(exc.Message);
            }
        }
    }
}
