using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrintBarCode
{
    public partial class PrintBarCodeForm : Form
    {
        public static string ConnectString_MES = ConfigurationManager.AppSettings["ConnectionString_MES"];
        Hashtable hsSnIsPrinted = new Hashtable ();
        bool IsSnIsPrintedOK=true;
        public PrintBarCodeForm()
        {
            InitializeComponent();
            dateTimePicker1.Value= DateTime.Now;
            
        }

        private void button_Print_Click(object sender, EventArgs e)
        {
            string partNo;
            int partCount;
            int beginNumber;
            DateTime proDate;

            partNo = comboBox_PartNNo.Text;
            partCount =(int) numericUpDown_Count.Value;
            beginNumber = (int)numericUpDown_BeginNumber.Value;
            proDate = dateTimePicker1.Value;
            if(listBox1.Items.Count<1)
            {
                MessageBox.Show("请先点击变速器序列号预览按钮");
                return;
            }
            if(!IsSnIsPrintedOK)
            {
                MessageBox.Show("变速器序列号有重复打印的，请选择手动方式打印");
                return;
            }
            PrintLogInset(partNo, partCount, beginNumber, proDate);

            //InitLiatBox1();
            PrintSnList();


        }


        private void PrintSnList()
        {


            foreach(var sn in listBox1.Items)
            {
                PrintSnToPrinter(sn.ToString());
            }

        }



        private void PrintSnToPrinter(string sn)
        {

            string partNo;
            int partCount;
            int beginNumber;
            DateTime proDate;

            partNo = comboBox_PartNNo.Text;
            partCount = (int)numericUpDown_Count.Value;
            beginNumber = (int)numericUpDown_BeginNumber.Value;
            proDate = dateTimePicker1.Value;

            if (IsSnIsCanPrinted(sn.ToString()))
            {
                PrintBarCode(sn.ToString());
                PrintSnInset(sn.ToString(), partNo, partCount, beginNumber, proDate);

            }
            else
            {
                if (MessageBox.Show("变速器序列号已经打印过", "变速器序列号已经打印过，是否重复打印", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    PrintBarCode(sn.ToString());
                    PrintSnUpdate(sn.ToString(), partNo, partCount, beginNumber, proDate);
                }

            }

        }


        private void PrintBarCode(string barCode) { }
        private void InitLiatBox1()
        {
            string partNo;
            int partCount;
            int beginNumber;
            DateTime proDate;

            partNo = comboBox_PartNNo.Text;
            partCount = (int)numericUpDown_Count.Value;
            beginNumber = (int)numericUpDown_BeginNumber.Value;
            proDate = dateTimePicker1.Value;

            List<string> list = CreatePartList(partNo, partCount, beginNumber, proDate);
            hsSnIsPrinted.Clear();
            listBox1.Items.Clear();
            foreach (var item in list)
            {
                listBox1.Items.Add(item);
                if(!IsSnIsCanPrinted(item))
                {
                    IsSnIsPrintedOK = false;
                }
                int count;
                count = PrintSnIsPrinted(item);
                hsSnIsPrinted.Add(item, count);
            }
        }

        private bool IsSnIsCanPrinted(string sn)
        {
            int count;
            count = PrintSnIsPrinted(sn);
            if (count > 0)
            {
                IsSnIsPrintedOK = false;
                return false;
            }
            return true;
        }
        private List<string> CreatePartList(string partNo, int partCount, int beginNumber, DateTime proDate)
        {
            List<string> list = new List<string>(); 
            string proDateString;
            string sn;
            proDateString = proDate.ToString("ddMMyyyy");
            for(int i = beginNumber;i< beginNumber+ partCount;i++)
            {
                sn = i.ToString("00000000");

                list.Add(partNo + "*" + proDateString + "*" + sn);
            }

            return list;    
             
        }

       
        private void button_PreView_Click(object sender, EventArgs e)
        {
            InitLiatBox1();

        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();          //先调用基类实现

            if (e.Index < 0)            //form load 的时候return

                return;
            int count = Convert.ToInt32( hsSnIsPrinted[listBox1.Items[e.Index].ToString()]);
            if(count > 0 ) {

                e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),

                e.Font, Brushes.Red, e.Bounds);
            }
            else
            {
                e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),

                e.Font, Brushes.Black, e.Bounds);
            }


        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //form load 的时候return
            string item;
            item = ((ListBox) sender).SelectedItem.ToString();
            textBox_SN.Text = item; 
        }

        private void button_M_SnOne_Click(object sender, EventArgs e)
        {
            string sn;
            sn = textBox_SN.Text;
            if(sn.Length > 10)
            {
                PrintSnToPrinter(sn);
            }
            else
            {
                MessageBox.Show("请选择打印条码变速器序列号");
            }
           
        }
    }
}
