using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Formats.Asn1;
using System.Linq;
using System.Text;
//using CsvReadWrite;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Drawing.Imaging;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Microsoft.VisualBasic;
using System.IO;
using PackingControlSystem.lib;
using lib;
using System.Drawing.Drawing2D;
//using System.Drawing.Imaging;
using System.Drawing.Text;
using TheArtOfDev.HtmlRenderer.Core;
using TheArtOfDev.HtmlRenderer.Core.Entities;
//using TheArtOfDev.HtmlRenderer.Core.Utils;
using TheArtOfDev.HtmlRenderer.WinForms.Adapters;
using TheArtOfDev.HtmlRenderer.WinForms.Utilities;
using TheArtOfDev.HtmlRenderer.WinForms;
using System.Reflection.Emit;
using System.Reflection;
using System.Security.Policy;
using TheArtOfDev.HtmlRenderer.Core.Utils;
//using SixLabors.ImageSharp;

namespace PackingControlSystem
{
    public partial class frmReprintKanban : Form
    {
        private List<List<string>> Settings = new List<List<string>>();
        DataTable dtSerialKanbanSelect = new DataTable();
        DataTable dtCustomerSelect = new DataTable();
        DataTable dtSerialKanbanLSelect = new DataTable();
        private string userName = string.Empty;
        private Image returnImage;
        private System.Drawing.Printing.PrintDocument printDocument;
        private string printBuffer;
        TextProcessing textProcessing;
        IniFile ini;
        string selected_preset;
        Graphics graphics;
        Rectangle rect = new Rectangle(280, 35, 20, 20);
        SolidBrush redBrush = new SolidBrush(Color.Red);
        Dictionary<String, Preset> presets = new Dictionary<string, Preset>();
        private string printerName;
        private string strCustomerName = string.Empty;
        private string strLocation = string.Empty;
        private string strADSPartNo = string.Empty;
        private string strFGP4 = string.Empty;
        private string strQTYP7 = string.Empty;
        private string strP9 = string.Empty;
        private string strQR = string.Empty;
        private string strSIDE = string.Empty;
        private string strDetail = string.Empty;
        private System.Windows.Forms.ToolStripStatusLabel printerStatus;
        DataTable dtSetting = new DataTable();
        private string strPART_NUM = string.Empty;
        private string strPART_NAME = string.Empty;
        private string strMODEL = string.Empty;
        private string strMODEL_CODE = string.Empty;
        private string strPART_ID = string.Empty;
        private string strCUST_ID = string.Empty;
        private string strCUST_PART_NUM = string.Empty;
        private string strCUST_PART_ID = string.Empty;
        private string strCUST_PART_NAME = string.Empty;
        private string strDESCRIPTION = string.Empty;
        private string strFRMLOCCODE = string.Empty;
        private string strCURLOCCODE = string.Empty;
        private string strNXTLOCCODE = string.Empty;
        private string strSHELF_NUM = string.Empty;
        private string strQPACK = string.Empty;
        private string strUNIT = string.Empty;
        private string strSERIAL = string.Empty;
        private string strQRKANBAN = string.Empty;
        private string strFRMSERIAL = string.Empty;
        private string strFRMQRKANBAN = string.Empty;
        private string strKANBANTYPE = string.Empty;
        private string strPRINTBY = string.Empty;
        private string strPRINTDATE = string.Empty;
        private string strPRINTTIME = string.Empty;
        private string strREMARK1 = string.Empty;
        private string strSerialNo = string.Empty;

        private Int32 ModelNameX = 0;
        private Int32 ModelNameY = 0;
        private Int32 ModelNameFS = 0;
        private Int32 CustomerNameX = 0;
        private Int32 CustomerNameY = 0;
        private Int32 CustomerNameFS = 0;
        private Int32 CustomerCodeX = 0;
        private Int32 CustomerCodeY = 0;
        private Int32 CustomerCodeFS = 0;
        private Int32 QtyX = 0;
        private Int32 QtyY = 0;
        private Int32 QtyFS = 0;
        private Int32 UserNameX = 0;
        private Int32 UserNameY = 0;
        private Int32 UserNameFS = 0;
        private Int32 SerialNoX = 0;
        private Int32 SerialNoY = 0;
        private Int32 SerialNoFS = 0;
        private Int32 PartNoX = 0;
        private Int32 PartNoY = 0;
        private Int32 PartNoFS = 0;
        private Int32 CustomerPartNoX = 0;
        private Int32 CustomerPartNoY = 0;
        private Int32 CustomerPartNoFS = 0;
        private Int32 LocationX = 0;
        private Int32 LocationY = 0;
        private Int32 LocationFS = 0;
        private Int32 SIDEX = 0;
        private Int32 SIDEY = 0;
        private Int32 SIDEFS = 0;
        private Int32 DateX = 0;
        private Int32 DateY = 0;
        private Int32 DateFS = 0;
        private Int32 TimeX = 0;
        private Int32 TimeY = 0;
        private Int32 TimeFS = 0;
        private Int32 QRX = 0;
        private Int32 QRY = 0;
        private Int32 QRFS = 0;
        private Int32 DetailX = 0;
        private Int32 DetailY = 0;
        private Int32 DetailFS = 0;

        private string ModelStyle = string.Empty;
        private string CustomerStyle = string.Empty;
        private string LocationStyle = string.Empty;
        private string SIDEStyle = string.Empty;
        private string QTYStyle = string.Empty;
        private string QRStyle = string.Empty;
        private string PartNameStyle = string.Empty;
        private string UserNameStyle = string.Empty;
        private string SerialStyle = string.Empty;
        private string ADSPartNoStyle = string.Empty;
        private string CustomerPartNoStyle = string.Empty;
        private string ScanDateStyle = string.Empty;
        private string ScanTimeStyle = string.Empty;
        private string DetailStyle = string.Empty;

        public frmReprintKanban(string systemUserName, string printerName, System.Windows.Forms.ToolStripStatusLabel tssLabelStatus)
        {
            InitializeComponent();
            userName = systemUserName;

            // 
            // ThePrintDocument
            // 
            this.printDocument = new System.Drawing.Printing.PrintDocument();
            this.printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.ThePrintDocument_PrintLabel);

            graphics = base.CreateGraphics();
            graphics.FillEllipse(redBrush, rect);

            // Prepare settings from INI
            ini = new lib.IniFile("config.ini");
            selected_preset = "192.168.1.1"; // ini.Read("default", null);
            loadAllPreset(selected_preset);

            //Initialize text processing
            string input_format = ini.Read("input", selected_preset);
            char delimiter = '|'; // ini.Read("delimiter", null).ToCharArray()[0];
            textProcessing = new TextProcessing(input_format, delimiter);

            this.printerName = printerName;
            printerStatus = tssLabelStatus;
        }

        private void loadAllPreset(string defaultPreset = null)
        {
            bool isFound = false;

            // Flush old settings
            presets.Clear();
            //for (int i = menuPreset.DropDownItems.Count; i > 2; i--)
            //{
            //    menuPreset.DropDownItems.RemoveAt(i - 1);
            //}

            foreach (string preset_id in ini.Read("preset", null).Split(','))
            {
                try
                {
                    // Update menu
                    //ToolStripMenuItem menu = new ToolStripMenuItem(preset_id
                    //    , null, presetSelect_Click);
                    if (preset_id.Equals(defaultPreset))
                    {
                        //menu.Checked = true;
                        isFound = true;

                        //log("Info: preset found, use " + preset_id + " as default preset.");
                        selected_preset = preset_id;

                        // Update printer
                        //searchPrinters(ini.Read("printer", preset_id));
                    }
                    //menuPreset.DropDownItems.Add(menu);

                    // Keep loaded preset settings
                    presets.Add(preset_id,
                        new Preset(
                        preset_id,
                        ini.Read("printer", preset_id),
                        ini.Read("template", preset_id),
                        ini.Read("width", preset_id),
                        ini.Read("height", preset_id),
                        ini.Read("input", preset_id)
                        ));
                }
                catch (Exception e)
                {

                }
            }

            if (defaultPreset != null && !isFound)
            {
                //log("Error: default preset not found!, please choose the new one.");
            }
        }

        protected void ThePrintDocument_PrintLabel(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
        {

            SolidBrush myBrush = new SolidBrush(Color.Black);

            Size size = new Size(700, 500);

            //HTML render
            //Image image = TheArtOfDev.HtmlRenderer.WinForms.HtmlRender.RenderToImage(printBuffer);

            Image image = RenderToImage(printBuffer, size, Color.White, null);


            Rectangle rect = new Rectangle(0, 0, Int32.Parse("700"), Int32.Parse("500"));
            ev.Graphics.DrawImage(image, rect);

            ev.HasMorePages = false;

            myBrush.Dispose();

        }

        public static Image RenderToImage(string html, Size size, Color backgroundColor = new Color(), CssData cssData = null,
            EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
        {
            if (backgroundColor == Color.Transparent)
                throw new ArgumentOutOfRangeException("backgroundColor", "Transparent background in not supported");

            // create the final image to render into
            var image = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb);

            if (!string.IsNullOrEmpty(html))
            {
                // create memory buffer from desktop handle that supports alpha channel
                IntPtr dib;
                var memoryHdc = Win32Utils.CreateMemoryHdc(IntPtr.Zero, image.Width, image.Height, out dib);
                try
                {
                    // create memory buffer graphics to use for HTML rendering
                    using (var memoryGraphics = Graphics.FromHdc(memoryHdc))
                    {
                        memoryGraphics.Clear(backgroundColor != Color.Empty ? backgroundColor : Color.White);

                        // render HTML into the memory buffer
                        RenderHtml(memoryGraphics, html, PointF.Empty, size, cssData, true, stylesheetLoad, imageLoad);
                    }

                    // copy from memory buffer to image
                    CopyBufferToImage(memoryHdc, image);
                }
                finally
                {
                    Win32Utils.ReleaseMemoryHdc(memoryHdc, dib);
                }
            }

            return image;
        }

        private static void CopyBufferToImage(IntPtr memoryHdc, Image image)
        {
            using (var imageGraphics = Graphics.FromImage(image))
            {
                var imgHdc = imageGraphics.GetHdc();
                Win32Utils.BitBlt(imgHdc, 0, 0, image.Width, image.Height, memoryHdc, 0, 0, Win32Utils.BitBltCopy);
                imageGraphics.ReleaseHdc(imgHdc);
            }
        }

        private static SizeF RenderHtml(Graphics g, string html, PointF location, SizeF maxSize, CssData cssData, bool useGdiPlusTextRendering, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad, EventHandler<HtmlImageLoadEventArgs> imageLoad)
        {
            SizeF actualSize = SizeF.Empty;

            if (!string.IsNullOrEmpty(html))
            {
                using (var container = new HtmlContainer())
                {
                    container.Location = location;
                    container.MaxSize = maxSize;
                    container.AvoidAsyncImagesLoading = true;
                    container.AvoidImagesLateLoading = true;
                    container.UseGdiPlusTextRendering = useGdiPlusTextRendering;

                    if (stylesheetLoad != null)
                        container.StylesheetLoad += stylesheetLoad;
                    if (imageLoad != null)
                        container.ImageLoad += imageLoad;

                    container.SetHtml(html, cssData);
                    container.PerformLayout(g);
                    container.PerformPaint(g);

                    actualSize = container.ActualSize;
                }
            }

            return actualSize;
        }

        public string RtnDbCon()
        {
            return "Data Source=" + this.Settings[0][0] + ";Initial Catalog=" + this.Settings[0][1] + ";User Id=sa;Password=" + this.Settings[0][2] + ";";

        }

        private void frmReprintKanban_Load(object sender, EventArgs e)
        {
            List<string> tmp = null;
            Settings = new List<List<string>>();
            tmp = null;
            using (var csv = new CsvReader(System.IO.Directory.GetCurrentDirectory() + "\\Settings.csv"))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    Settings.Add(tmp);

                }

            }//using end

            txtSerialNo.Text = string.Empty;
            txtSerialNo.Focus();

            SettxtStart();

            loadTableSetting();
        }

        private void SettxtStart()
        {
            strPART_NUM = string.Empty;
            strPART_NAME = string.Empty;
            strMODEL = string.Empty;
            strMODEL_CODE = string.Empty;
            strPART_ID = string.Empty;
            strCUST_ID = string.Empty;
            strCUST_PART_NUM = string.Empty;
            strCUST_PART_ID = string.Empty;
            strCUST_PART_NAME = string.Empty;
            strDESCRIPTION = string.Empty;
            strFRMLOCCODE = string.Empty;
            strCURLOCCODE = string.Empty;
            strNXTLOCCODE = string.Empty;
            strSHELF_NUM = string.Empty;
            strQPACK = string.Empty;
            strUNIT = string.Empty;
            strSERIAL = string.Empty;
            strQRKANBAN = string.Empty;
            strFRMSERIAL = string.Empty;
            strFRMQRKANBAN = string.Empty;
            strKANBANTYPE = string.Empty;
            strPRINTBY = string.Empty;
            strPRINTDATE = string.Empty;
            strPRINTTIME = string.Empty;
            strREMARK1 = string.Empty;

        }

        private void loadTableSetting()
        {
            dtSetting = new DataTable();
            clsUtil_MSSQL.GetDataFromTBsetting(RtnDbCon(), "TM_SETTING", ref dtSetting);

            //Customer Code -> ADS Part No

            if (dtSetting.Rows.Count > 0)
            {
                ModelNameX = Convert.ToInt32(dtSetting.Rows[0][1].ToString());
                ModelNameY = Convert.ToInt32(dtSetting.Rows[0][2].ToString());
                ModelNameFS = Convert.ToInt32(dtSetting.Rows[0][3].ToString());
                //position: absolute; display: block; margin - top:1px; margin - left:140px; font - size: 38px; font - weight: bold; font color: white;
                ModelStyle = "position: absolute; display: block; margin-top:" + ModelNameY + "px; margin-left:" + ModelNameX + "px;font-size: " + ModelNameFS + "px; font-weight: bold;  font color: white;";

                CustomerNameX = Convert.ToInt32(dtSetting.Rows[0][4].ToString());
                CustomerNameY = Convert.ToInt32(dtSetting.Rows[0][5].ToString());
                CustomerNameFS = Convert.ToInt32(dtSetting.Rows[0][6].ToString());
                //< span style = "position: absolute; display: block; margin-top:10px; margin-left:240px;font-size: 32px; font-weight: bold; " >${ Customer}</ span >
                CustomerStyle = "position: absolute; display: block; margin-top:" + CustomerNameY + "px; margin-left:" + CustomerNameX + "px;font-size: " + CustomerNameFS + "px; font-weight: bold; ";

                CustomerCodeX = Convert.ToInt32(dtSetting.Rows[0][7].ToString());
                CustomerCodeY = Convert.ToInt32(dtSetting.Rows[0][8].ToString());
                CustomerCodeFS = Convert.ToInt32(dtSetting.Rows[0][9].ToString());
                //<span style="position: absolute; display: block; margin-top:95px; margin-left:105px;font-size: 23px; font-weight: bold; ">(${ADSPartNo})</span>
                ADSPartNoStyle = "position: absolute; display: block; margin-top:" + CustomerCodeY + "px; margin-left:" + CustomerCodeX + "px;font-size: " + CustomerCodeFS + "px; font-weight: bold; ";

                QtyX = Convert.ToInt32(dtSetting.Rows[0][10].ToString());
                QtyY = Convert.ToInt32(dtSetting.Rows[0][11].ToString());
                QtyFS = Convert.ToInt32(dtSetting.Rows[0][12].ToString());
                //< span style = "position: absolute; display: block; margin-top:53px; margin-left:70px;font-size: 21px; font-weight: bold; " >${ QTY}</ span >
                QTYStyle = "position: absolute; display: block; margin-top:" + QtyY + "px; margin-left:" + QtyX + "px;font-size: " + QtyFS + "px; font-weight: bold; ";

                UserNameX = Convert.ToInt32(dtSetting.Rows[0][13].ToString());
                UserNameY = Convert.ToInt32(dtSetting.Rows[0][14].ToString());
                UserNameFS = Convert.ToInt32(dtSetting.Rows[0][15].ToString());
                //<span style="position: absolute; display: block; margin-top:95px; margin-left:30px;font-size: 11px; font-weight: bold; ">${UserName}</span>
                UserNameStyle = "position: absolute; display: block; margin-top:" + UserNameY + "px; margin-left:" + UserNameX + "px;font-size: " + UserNameFS + "px; font-weight: bold; ";

                SerialNoX = Convert.ToInt32(dtSetting.Rows[0][16].ToString());
                SerialNoY = Convert.ToInt32(dtSetting.Rows[0][17].ToString());
                SerialNoFS = Convert.ToInt32(dtSetting.Rows[0][18].ToString());
                //<span style="position: absolute; display: block; margin-top:115px; margin-left:30px;font-size: 9px; font-weight: bold; ">${SerialNo}</span>
                SerialStyle = "position: absolute; display: block; margin-top:" + SerialNoY + "px; margin-left:" + SerialNoX + "px;font-size: " + SerialNoFS + "px; font-weight: bold; ";

                PartNoX = Convert.ToInt32(dtSetting.Rows[0][19].ToString());
                PartNoY = Convert.ToInt32(dtSetting.Rows[0][20].ToString());
                PartNoFS = Convert.ToInt32(dtSetting.Rows[0][21].ToString());
                //<span style="position: absolute; display: block; margin-top:60px; margin-left:205px;font-size: 21px; font-weight: bold; ">${PartName}</span>
                PartNameStyle = "position: absolute; display: block; margin-top:" + PartNoY + "px; margin-left:" + PartNoX + "px;font-size: " + PartNoFS + "px; font-weight: bold; ";

                CustomerPartNoX = Convert.ToInt32(dtSetting.Rows[0][22].ToString());
                CustomerPartNoY = Convert.ToInt32(dtSetting.Rows[0][23].ToString());
                CustomerPartNoFS = Convert.ToInt32(dtSetting.Rows[0][24].ToString());
                //<span style="position: absolute; display: block; margin-top:113px; margin-left:113px;font-size: 23px; font-weight: bold; ">${CustomerPartNo}</span>
                CustomerPartNoStyle = "position: absolute; display: block; margin-top:" + CustomerPartNoY + "px; margin-left:" + CustomerPartNoX + "px;font-size: " + CustomerPartNoFS + "px; font-weight: bold; ";

                LocationX = Convert.ToInt32(dtSetting.Rows[0][25].ToString());
                LocationY = Convert.ToInt32(dtSetting.Rows[0][26].ToString());
                LocationFS = Convert.ToInt32(dtSetting.Rows[0][27].ToString());
                //< span style = "position: absolute; display: block; margin-top:20px; margin-left:10px;font-size: 21px; font-weight: bold; " >${ Location}</ span >
                LocationStyle = "position: absolute; display: block; margin-top:" + LocationY + "px; margin-left:" + LocationX + "px;font-size: " + LocationFS + "px; font-weight: bold; ";

                SIDEX = Convert.ToInt32(dtSetting.Rows[0][28].ToString());
                SIDEY = Convert.ToInt32(dtSetting.Rows[0][29].ToString());
                SIDEFS = Convert.ToInt32(dtSetting.Rows[0][30].ToString());
                //<span style="position: absolute; display: block; margin-top:53px; margin-left:20px;font-size: 21px; font-weight: bold; ">${SIDE}</span>
                SIDEStyle = "position: absolute; display: block; margin-top:" + SIDEY + "px; margin-left:" + SIDEX + "px;font-size: " + SIDEFS + "px; font-weight: bold; ";

                DateX = Convert.ToInt32(dtSetting.Rows[0][31].ToString());
                DateY = Convert.ToInt32(dtSetting.Rows[0][32].ToString());
                DateFS = Convert.ToInt32(dtSetting.Rows[0][33].ToString());
                //<span style="position: absolute; display: block; margin-top:110px; margin-left:245px;font-size: 12px; font-weight: bold; ">${Scan_date:datetime(dd/MM/yyyy)}</span>
                ScanDateStyle = "position: absolute; display: block; margin-top:" + DateY + "px; margin-left:" + DateX + "px;font-size: " + DateFS + "px; font-weight: bold; ";

                TimeX = Convert.ToInt32(dtSetting.Rows[0][34].ToString());
                TimeY = Convert.ToInt32(dtSetting.Rows[0][35].ToString());
                TimeFS = Convert.ToInt32(dtSetting.Rows[0][36].ToString());
                //<span style="position: absolute; display: block; margin-top:120px; margin-left:250px;font-size: 12px; font-weight: bold; ">${Scan_date:datetime(HH:mm:ss)}</span>
                ScanTimeStyle = "position: absolute; display: block; margin-top:" + TimeY + "px; margin-left:" + TimeX + "px;font-size: " + TimeFS + "px; font-weight: bold; ";

                QRX = Convert.ToInt32(dtSetting.Rows[0][37].ToString());
                QRY = Convert.ToInt32(dtSetting.Rows[0][38].ToString());
                QRFS = Convert.ToInt32(dtSetting.Rows[0][39].ToString());
                //< span style = "position: absolute; display: block; margin-top:70px; margin-left:255px;" >
                QRStyle = "position: absolute; display: block; margin-top:" + QRY + "px; margin-left:" + QRX + "px;font-size: " + QRFS + "px; font-weight: bold; ";

                DetailX = Convert.ToInt32(dtSetting.Rows[0][40].ToString());
                DetailY = Convert.ToInt32(dtSetting.Rows[0][41].ToString());
                DetailFS = Convert.ToInt32(dtSetting.Rows[0][42].ToString());
                //< span style = "position: absolute; display: block; margin-top:70px; margin-left:255px;" >
                DetailStyle = "position: absolute; display: block; margin-top:" + DetailY + "px; margin-left:" + DetailX + "px;font-size: " + DetailFS + "px; font-weight: bold; ";


            }
            else
            {
                ModelNameX = 115;
                ModelNameY = 1;
                ModelNameFS = 38;
                ModelStyle = "position: absolute; display: block; margin-top:" + ModelNameY + "px; margin-left:" + ModelNameX + "px;font-size: " + ModelNameFS + "px; font-weight: bold;  font color: white;";

                CustomerNameX = 215;
                CustomerNameY = 10;
                CustomerNameFS = 32;
                CustomerStyle = "position: absolute; display: block; margin-top:" + CustomerNameY + "px; margin-left:" + CustomerNameX + "px;font-size: " + CustomerNameFS + "px; font-weight: bold; ";

                CustomerCodeX = 139;
                CustomerCodeY = 100;
                CustomerCodeFS = 15;
                ADSPartNoStyle = "position: absolute; display: block; margin-top:" + CustomerCodeY + "px; margin-left:" + CustomerCodeX + "px;font-size: " + CustomerCodeFS + "px; font-weight: bold; ";

                QtyX = 57;
                QtyY = 55;
                QtyFS = 21;
                QTYStyle = "position: absolute; display: block; margin-top:" + QtyY + "px; margin-left:" + QtyX + "px;font-size: " + QtyFS + "px; font-weight: bold; ";

                UserNameX = 36;
                UserNameY = 95;
                UserNameFS = 11;
                UserNameStyle = "position: absolute; display: block; margin-top:" + UserNameY + "px; margin-left:" + UserNameX + "px;font-size: " + UserNameFS + "px; font-weight: bold; ";

                SerialNoX = 5;
                SerialNoY = 115;
                SerialNoFS = 15;
                SerialStyle = "position: absolute; display: block; margin-top:" + SerialNoY + "px; margin-left:" + SerialNoX + "px;font-size: " + SerialNoFS + "px; font-weight: bold; ";

                PartNoX = 175;
                PartNoY = 55;
                PartNoFS = 12;
                PartNameStyle = "position: absolute; display: block; margin-top:" + PartNoY + "px; margin-left:" + PartNoX + "px;font-size: " + PartNoFS + "px; font-weight: bold; ";

                CustomerPartNoX = 143;
                CustomerPartNoY = 118;
                CustomerPartNoFS = 15;
                CustomerPartNoStyle = "position: absolute; display: block; margin-top:" + CustomerPartNoY + "px; margin-left:" + CustomerPartNoX + "px;font-size: " + CustomerPartNoFS + "px; font-weight: bold; ";

                LocationX = 10;
                LocationY = 20;
                LocationFS = 21;
                LocationStyle = "position: absolute; display: block; margin-top:" + LocationY + "px; margin-left:" + LocationX + "px;font-size: " + LocationFS + "px; font-weight: bold; ";

                SIDEX = 10;
                SIDEY = 55;
                SIDEFS = 21;
                SIDEStyle = "position: absolute; display: block; margin-top:" + SIDEY + "px; margin-left:" + SIDEX + "px;font-size: " + SIDEFS + "px; font-weight: bold; ";

                DateX = 220;
                DateY = 110;
                DateFS = 12;
                ScanDateStyle = "position: absolute; display: block; margin-top:" + DateY + "px; margin-left:" + DateX + "px;font-size: " + DateFS + "px; font-weight: bold; ";

                TimeX = 220;
                TimeY = 120;
                TimeFS = 12;
                ScanTimeStyle = "position: absolute; display: block; margin-top:" + TimeY + "px; margin-left:" + TimeX + "px;font-size: " + TimeFS + "px; font-weight: bold; ";

                QRX = 225;
                QRY = 70;
                QRFS = 0;
                QRStyle = "position: absolute; display: block; margin-top:" + QRY + "px; margin-left:" + QRX + "px;font-size: " + QRFS + "px; font-weight: bold; ";

                DetailX = 175;
                DetailY = 70;
                DetailFS = 12;
                DetailStyle = "position: absolute; display: block; margin-top:" + DetailY + "px; margin-left:" + DetailX + "px;font-size: " + DetailFS + "px; font-weight: bold; ";

            }

        }

        private void RePrint(object sender, EventArgs e)
        {
            SettxtStart();

            strSerialNo = txtSerialNo.Text;

            dtSerialKanbanLSelect = new DataTable();
            clsUtil_MSSQL.GetDataFromTBKanbanLSelect(RtnDbCon(), "TT_KANBAN_L", ref dtSerialKanbanLSelect, txtSerialNo.Text);
            if (dtSerialKanbanLSelect is not null)
            {
                if (dtSerialKanbanLSelect.Rows.Count > 0)
                {
                    strSerialNo = dtSerialKanbanLSelect.Rows[0][0].ToString();
                }
                else { strSerialNo = txtSerialNo.Text; }
            }
            else { strSerialNo = txtSerialNo.Text; }

            dtSerialKanbanSelect = new DataTable();
            clsUtil_MSSQL.GetDataFromTBKanbanHSelect(RtnDbCon(), "TT_KANBAN_H", ref dtSerialKanbanSelect, strSerialNo);
            if (dtSerialKanbanSelect is not null)
            {
                if (dtSerialKanbanSelect.Rows.Count > 0)
                {

                    strPART_NUM = dtSerialKanbanSelect.Rows[0][0].ToString();
                    strPART_NAME = dtSerialKanbanSelect.Rows[0][1].ToString();
                    strMODEL = dtSerialKanbanSelect.Rows[0][2].ToString();
                    strMODEL_CODE = dtSerialKanbanSelect.Rows[0][3].ToString();
                    strPART_ID = dtSerialKanbanSelect.Rows[0][4].ToString();
                    strCUST_ID = dtSerialKanbanSelect.Rows[0][5].ToString();
                    strCUST_PART_NUM = dtSerialKanbanSelect.Rows[0][6].ToString();
                    strCUST_PART_ID = dtSerialKanbanSelect.Rows[0][7].ToString();
                    strCUST_PART_NAME = dtSerialKanbanSelect.Rows[0][8].ToString();
                    strDESCRIPTION = dtSerialKanbanSelect.Rows[0][9].ToString();
                    strFRMLOCCODE = dtSerialKanbanSelect.Rows[0][10].ToString();
                    strCURLOCCODE = dtSerialKanbanSelect.Rows[0][11].ToString();
                    strNXTLOCCODE = dtSerialKanbanSelect.Rows[0][12].ToString();
                    strSHELF_NUM = dtSerialKanbanSelect.Rows[0][13].ToString();
                    strQPACK = dtSerialKanbanSelect.Rows[0][14].ToString().Replace(".00", "");
                    strUNIT = dtSerialKanbanSelect.Rows[0][15].ToString();
                    strSERIAL = dtSerialKanbanSelect.Rows[0][16].ToString();
                    strQRKANBAN = dtSerialKanbanSelect.Rows[0][17].ToString();
                    strFRMSERIAL = dtSerialKanbanSelect.Rows[0][18].ToString();
                    strFRMQRKANBAN = dtSerialKanbanSelect.Rows[0][19].ToString();
                    strKANBANTYPE = dtSerialKanbanSelect.Rows[0][20].ToString();
                    strPRINTBY = dtSerialKanbanSelect.Rows[0][21].ToString();
                    strPRINTDATE = dtSerialKanbanSelect.Rows[0][22].ToString();
                    strPRINTTIME = dtSerialKanbanSelect.Rows[0][23].ToString();
                    strREMARK1 = dtSerialKanbanSelect.Rows[0][24].ToString();
                    strSIDE = dtSerialKanbanSelect.Rows[0][25].ToString();
                    strDetail = dtSerialKanbanSelect.Rows[0][26].ToString();

                    dtCustomerSelect = new DataTable();
                    clsUtil_MSSQL.GetDataFromTBCustomerSelect(RtnDbCon(), "TM_CUSTOMER", ref dtCustomerSelect, strCUST_ID);

                    strCustomerName = dtCustomerSelect.Rows[0][2].ToString();


                    printLabelProcess(sender, e);

                    txtSerialNo.Text = string.Empty;
                    txtSerialNo.Focus();

                }

                else
                {
                    MessageBox.Show(this, "ไม่มีหมายเลข Serial kanban ที่ท่านเลือก กรุณากรอกใหม่อีกครั้ง",
                                       "WKCS", MessageBoxButtons.OK,
                                       MessageBoxIcon.Error,
                                       MessageBoxDefaultButton.Button1, 0);

                    txtSerialNo.Text = string.Empty;
                    txtSerialNo.Focus();
                }
            }
            else
            {
                MessageBox.Show(this, "ไม่มีหมายเลข Serial kanban ที่ท่านเลือก กรุณากรอกใหม่อีกครั้ง",
                                       "WKCS", MessageBoxButtons.OK,
                                       MessageBoxIcon.Error,
                                       MessageBoxDefaultButton.Button1, 0);

                txtSerialNo.Text = string.Empty;
                txtSerialNo.Focus();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            RePrint(sender, e);
        }

        private void printLabelProcess(object sender, EventArgs e)
        {

            //strSIDE = "LH";

            if (printerStatus.BackColor == Color.LightGreen)
            {

                printPriviewLabel(sender, e, "&lt;==RePrint==>" + "|" + QRStyle + "|" + strQRKANBAN + "|" +
                    CustomerStyle + "|" + strCustomerName + "|" +
                    LocationStyle + "|" + strCURLOCCODE + "|" +
                    SIDEStyle + "|" + strSIDE + "|" +
                    QTYStyle + "|" + strQPACK + "| " +
                    PartNameStyle + "|" + strPART_NAME + "|" +
                    UserNameStyle + "|" + userName + "|" +
                    SerialStyle + "|" + strSERIAL + "|" +
                    ADSPartNoStyle + "|" + strPART_NUM + "|" +
                    CustomerPartNoStyle + "|" + strCUST_PART_NUM + "|" +
                    ScanDateStyle + "|" + ScanTimeStyle + "|" + DateTime.Now.ToString("yyyyMMddHHmmss") + "|" +
                    ModelStyle + "|" + strMODEL + "|" + DetailStyle + "|" + strDetail);



                //printLabel(sender, e, "==RePrint==>" + "|" + QRStyle + "|" + strQRKANBAN + "|" +
                //    CustomerStyle + "|" + strCustomerName + "|" +
                //    LocationStyle + "|" + strCURLOCCODE + "|" +
                //    SIDEStyle + "|" + strSIDE + "|" +
                //    QTYStyle + "|" + strQPACK + "| " +
                //    PartNameStyle + "|" + strPART_NAME + "|" +
                //    UserNameStyle + "|" + userName + "|" +
                //    SerialStyle + "|" + strSERIAL + "|" +
                //    ADSPartNoStyle + "|" + strPART_NUM + "|" +
                //    CustomerPartNoStyle + "|" + strCUST_PART_NUM + "|" +
                //    ScanDateStyle + "|" + ScanTimeStyle + "|" + DateTime.Now.ToString("yyyyMMddHHmmss") + "|" +
                //    ModelStyle + "|" + strMODEL);

            }
            else
            {
                MessageBox.Show(this, "ไม่สามารถเพิมพ์ได้ เนื่องจาก Printer Offline",
                                   "Reprint Label", MessageBoxButtons.OK,
                                   MessageBoxIcon.Error,
                                   MessageBoxDefaultButton.Button1, 0);

                printPriviewLabel(sender, e, "==RePrint==>" + "|" + QRStyle + "|" + strQRKANBAN + "|" +
                    CustomerStyle + "|" + strCustomerName + "|" +
                    LocationStyle + "|" + strCURLOCCODE + "|" +
                    SIDEStyle + "|" + strSIDE + "|" +
                    QTYStyle + "|" + strQPACK + "| " +
                    PartNameStyle + "|" + strPART_NAME + "|" +
                    UserNameStyle + "|" + userName + "|" +
                    SerialStyle + "|" + strSERIAL + "|" +
                    ADSPartNoStyle + "|" + strPART_NUM + "|" +
                    CustomerPartNoStyle + "|" + strCUST_PART_NUM + "|" +
                    ScanDateStyle + "|" + ScanTimeStyle + "|" + DateTime.Now.ToString("yyyyMMddHHmmss") + "|" +
                    ModelStyle + "|" + strMODEL);

            }
        }

        private void printPriviewLabel(object sender, EventArgs e, string strprintBuffer)
        {
            ini.Write("printer", printerName, selected_preset);
            ini.Write("template", System.IO.Directory.GetCurrentDirectory() + "\\Template-Packing.html", selected_preset);
            ini.Write("width", "500", selected_preset);
            ini.Write("height", "300", selected_preset);

            //Print Label
            if (setContentPrintBuffer(strprintBuffer))
            {
                printPreviewToolStripMenuItem_Click(sender, e);
            }
        }

        private void printLabel(object sender, EventArgs e, string strprintBuffer)
        {
            ini.Write("printer", printerName, selected_preset);
            ini.Write("template", System.IO.Directory.GetCurrentDirectory() + "\\Template-Packing.html", selected_preset);
            ini.Write("width", "500", selected_preset);
            ini.Write("height", "300", selected_preset);

            //Print Label
            if (setContentPrintBuffer(strprintBuffer))
            {
                printDocument.PrinterSettings.PrinterName = this.printerName;
                printDocument.Print();
            }
        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
                printDocument.PrinterSettings.PrinterName = this.printerName;
                printPreviewDialog1.Document = this.printDocument;
                printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
                printPreviewDialog1.ShowDialog();

                printPreviewDialog1.PrintPreviewControl.Zoom = 200 / 100f;
                (printPreviewDialog1 as Form).WindowState = FormWindowState.Maximized;
            }
            catch (Exception exp)
            {
                System.Console.WriteLine(exp.Message.ToString());
            }
        }

        private bool setContentPrintBuffer(string str)
        {
            bool ret = true;
            Dictionary<string, VarObject> vars = textProcessing.ParseInput(str, '|');

            foreach (string key in vars.Keys)
            {
                VarObject v;
                vars.TryGetValue(key, out v);
            }

            //Read template content
            try
            {
                string html = System.IO.File.ReadAllText(ini.Read("template", selected_preset));

                //Process html
                html = TextProcessing.ReplaceVariables(textProcessing, html, ini);

                printBuffer = html;
            }
            catch (Exception e)
            {
                //log("Error: " + e.Message);
                ret = false;

            }

            return ret;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSerialNo_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtSerialNo_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Return)
            {
                RePrint(sender, e);
            }
        }
    }
}
