using LabelManager2;
using Seagull.BarTender.Print;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Media;
using System.Threading;
using System.Windows.Forms;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace UAS_BARCODEIO
{
    public partial class 极测_出货清单打印 : Form
    {

        DataHelper dh = new DataHelper();

        Engine engine;

        DataTable Dbfind;

        //自适应屏幕
        AutoSizeFormClass asc = new AutoSizeFormClass();

        LabelFormatDocument format;


        public 极测_出货清单打印()
        {
            InitializeComponent();
        }

        delegate void BindDataSource(DataGridView dgv, DataTable dt);//定义委托

        void bindingsource(DataGridView dgv, DataTable dt)
        {
            //dgv.AutoGenerateColumns = false;
            //dgv.DataSource = null;
            //dgv.DataSource = dt;
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new BindDataSource(bindingsource), new object[] { dgv, dt });
            }
            else
            {
                dgv.AutoGenerateColumns = false;
                dgv.DataSource = dt;
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            SystemInf.dh = dh;
            spl_code.TableName = "SnPartsList@ERP";
            spl_code.SelectField = "spl_code # 单据编号";
            spl_code.FormName = Name;
            spl_code.DBTitle = "解析规则查询";
            spl_code.SetValueField = new string[] { "spl_code" };
            spl_code.Condition = "";
            spl_code.DbChange += nr_rule_DBChange;
            SystemInf.dh = dh;
            try
            {
                engine = new Engine(true);
                format = engine.Documents.Open(System.Windows.Forms.Application.StartupPath + @"\BARCODE.btw");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }

            asc.controllInitializeSize(this);
            asc.controlAutoSize(this);

            PrintDocument print = new PrintDocument();
            string sDefault = print.PrinterSettings.PrinterName;//默认打印机名

            foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {
                PrinterList.Items.Add(sPrint);
                if (sPrint == sDefault)
                    PrinterList.SelectedIndex = PrinterList.Items.IndexOf(sPrint);
            }
        }

        private void nr_rule_DBChange(object sender, EventArgs e)
        {
            Dbfind = spl_code.ReturnData;
            BaseUtil.SetFormValue(this.Controls, Dbfind);
        }

        private void pi_inoutnokeydown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                LoadGridData();
            }
        }

        private void LoadGridData()
        {

        }


        bool AutoSized = false;
        private void 入库条码规则解析_AutoSizeChanged(object sender, EventArgs e)
        {
            if (!AutoSized)
            {
                asc.controlAutoSize(this);
                AutoSized = true;
            }
        }

        private void PrintBarCode_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < BarcodeIO.Rows.Count; i++)
            {
                if (BarcodeIO.Rows[i].Cells["CheckBox"].FormattedValue != null && BarcodeIO.Rows[i].Cells["CheckBox"].FormattedValue.ToString() == "True")
                {
                    string barcode = "";
                    string[] param = new string[] { "", "", barcode };
                    dh.CallProcedure("SP_GETBARCODE@ERP", ref param);
                    barcode = param[2].Replace("BARCODE:", "");
                    format.SubStrings["BARCODE"].Value = barcode;
                    format.SubStrings["PRSPEC"].Value = BarcodeIO.Rows[i].Cells["spld_prodspec"].Value.ToString();
                    format.SubStrings["PRCODE"].Value = BarcodeIO.Rows[i].Cells["spld_prodcode"].Value.ToString();
                    format.SubStrings["PRDETAIL"].Value = BarcodeIO.Rows[i].Cells["spld_proddetail"].Value.ToString();
                    format.SubStrings["QTY"].Value = BarcodeIO.Rows[i].Cells["spld_thisqty"].Value.ToString();
                    format.SubStrings["DC"].Value = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                    dh.ExecuteSql("insert into prodiobarcode@ERP(pib_id,pib_prodcode,pib_inoutno,pib_barcode,pib_indate,pib_qty)" +
                        "values(prodiobarcode_seq.nextval@ERP,'" + BarcodeIO.Rows[i].Cells["spld_prodcode"].Value.ToString() + "','" + spl_code.Text + "','" + barcode + "',sysdate,'" + BarcodeIO.Rows[i].Cells["spld_thisqty"].Value.ToString() + "')", "insert");

                    format.PrintSetup.PrinterName = PrinterList.Text;
                    format.PrintSetup.IdenticalCopiesOfLabel = 1;
                    format.Print();
                }
            }
        }



        private void ChooseAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < BarcodeIO.Rows.Count; i++)
            {
                BarcodeIO.Rows[i].Cells["CheckBox"].Value = true;
            }
        }


        private void PrintStockBarCode_Click(object sender, EventArgs e)
        {
            库存条码打印 form = new 库存条码打印();
            form.ShowDialog();
        }

        Thread thread;
        string FileName = "";
        public void Play(string Type)
        {
            if (Type == "OK")
            {
                FileName = System.Windows.Forms.Application.StartupPath + @"\Resources\Sound\8378.wav";
                thread = new Thread(PlaySound);
                thread.Start();

            }
            else
            {
                FileName = System.Windows.Forms.Application.StartupPath + @"\Resources\Sound\5185.wav";
                thread = new Thread(PlaySound);
                thread.Start();
            }
        }

        private void PlaySound()
        {
            //要加载COM组件:Microsoft speech object Library    
            if (!System.IO.File.Exists(FileName))
            {
                return;
            }
            try
            {
                SoundPlayer player = new SoundPlayer();
                player.SoundLocation = FileName;
                player.Load();
                player.Play();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void spl_code_UserControlTextChanged(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dh.ExecuteSql("select spld_detno,spld_module,spld_prodcode,spld_proddetail" +
                ",spld_prodspec,spld_produnit,spld_baseqty,spld_qty,spld_sncode,spld_sndetno,spld_thisqty,spld_remark " +
                " from SnPartsList@ERP left join SNPARTSLISTDETAIL@ERP on spl_id=spld_splid where spl_code='" + spl_code.Text + "'", "select");
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("单据编号不存在");
                return;
            }
            BarcodeIO.DataSource = dt;
        }

        private void Refresh_Click(object sender, EventArgs e)
        {
            LoadGridData();
        }
    }
}