using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MESClassLibrary.BLL.Log;
using MESClassLibrary.BLL.Painting;
using grdesLib;
using grproLib;

namespace PaintingView
{
    public partial class FrmDetail : Form
    {
        public FrmDetail()
        {
            InitializeComponent();
        }

        private void FrmDetail_Load(object sender, EventArgs e)
        {
            int a = Program.a;
            Control.CheckForIllegalCrossThreadCalls = false;
            Thread t = new Thread(new ThreadStart(TimeGo));
            t.Start();
            initList(a);
            
        }

        public void initList(int a)
        {
            //listView1.Items.Clear();

            StockInBLL bll=new StockInBLL();
            DataTable dt = null;
            try
            {
                this.listView1.Columns.Clear();
                this.listView1.Columns.Add("配置", 600, HorizontalAlignment.Center);
                this.listView1.Columns.Add("颜色", 300, HorizontalAlignment.Center);
                this.listView1.Columns.Add("数量", 350, HorizontalAlignment.Center);
                this.listView1.Items.Clear();
                if (a==1)
                {
                    dt = bll.SearchDetailA();
                }
                else
                {
                    dt = bll.SearchDetailB();
                }

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListViewItem lvi = new ListViewItem(dt.Rows[i]["PType"].ToString());
                    lvi.SubItems.Add(dt.Rows[i]["color"].ToString());
                    lvi.SubItems.Add(dt.Rows[i]["totle"].ToString());
                    this.listView1.Items.Add(lvi);
                }
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(),MethodBase.GetCurrentMethod());
            }
        }

        private void TimeGo()
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 10;
            timer.Enabled = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Tick);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            label2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Thread.Sleep(500);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (Program.a == 1)
                {
                    GridppReport report = new GridppReport();

                    report.Register("");
                    report.LoadFromFile(@"D:\StockA.grf");
                    report.DesignPaperOrientation = GRPaperOrientation.grpoLandscape;
                    //report.Printer.PrinterName = "";
                    report.Print(false);
                }
                if (Program.a == 2)
                {
                    GridppReport report = new GridppReport();

                    report.Register("");
                    if (DateTime.Now > DateTime.Parse(DateTime.Now.ToShortDateString() + " 19:30:00") && DateTime.Now < DateTime.Parse(DateTime.Now.ToShortDateString() + " 23:59:59"))
                    {
                        report.LoadFromFile(@"D:\StockB1.grf");
                    }

                    if (DateTime.Now > DateTime.Parse(DateTime.Now.ToShortDateString() + " 00:00:00") && DateTime.Now < DateTime.Parse(DateTime.Now.ToShortDateString() + " 07:30:00"))
                    {
                        report.LoadFromFile(@"D:\StockB2.grf");
                    }

                    report.DesignPaperOrientation = GRPaperOrientation.grpoLandscape;
                    report.Print(false);
                }
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {
            //Environment.Exit(0);
            this.Dispose();
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (!IsHandleCreated)
            {
                this.Close();
            }
        }

        private const Int32 WM_SYSCOMMAND = 274;

        private const UInt32 SC_CLOSE = 61536;

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

        private static extern int RegisterWindowMessage(string lpString);
    }
}