using DevExpress.Printing.Core.PdfExport.Metafile;
using HslCommunication;
using LabelManager2;
using Seagull.BarTender.Print;
using System;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using UAS_MES_NEW.DataOperate;
using UAS_MES_NEW.Entity;
using UAS_MES_NEW.PublicForm;
using UAS_MES_NEW.PublicMethod;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace UAS_MES_NEW.Query
{
    public partial class Query_BarcodeIn1 : Form
    {

        DataHelper dh = SystemInf.dh;

        DataTable Dbfind;

        Thread InitPrint;

        Engine engine;

        public Query_BarcodeIn1()
        {
            InitializeComponent();
        }

        private void InPrint()
        {
            try
            {
                engine = new Engine(true);
            }
            catch (Exception)
            {
                MessageBox.Show("未正确安装BarTender软件");
            }
        }

        private void Query_LoadMake_Load(object sender, EventArgs e)
        {
            pi_inoutno.TableName = "prodinout";
            pi_inoutno.SelectField = "pi_inoutno#单号";
            pi_inoutno.FormName = Name;
            pi_inoutno.SetValueField = new string[] { "pi_inoutno" };
            pi_inoutno.Condition = "1=1";
            pi_inoutno.DbChange += Ma_code_DbChange;

            InitPrint = new Thread(InPrint);
            SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
            BaseUtil.SetFormCenter(stw);
            stw.ShowDialog();

            LoadGridData();

            DataTable _dt = (DataTable)dh.ExecuteSql("select la_id,la_url,la_isdefault,replace(LA_SOFTTYPE,';',',')LA_SOFTTYPE from label where la_prodcode='生产退料标签' and la_templatetype='自定义' and la_statuscode='AUDITED' order by la_isdefault", "select");
            if (_dt.Rows.Count == 0)
            {
                _dt = (DataTable)dh.ExecuteSql("select la_id,la_url,la_isdefault,replace(LA_SOFTTYPE,';',',')LA_SOFTTYPE from label where la_prodcode is null and la_templatetype='自定义' and la_statuscode='AUDITED' order by la_isdefault", "select");
            }
            if (_dt.Rows.Count > 0)
            {
                string la_id = _dt.Rows[0]["la_id"].ToString();
                _dt = (DataTable)dh.ExecuteSql("select fp_name la_url,'" + la_id + "' la_id from FILEPATH where fp_id in (select *  from table(select parsestring(LA_SOFTTYPE,';') from label where la_templatetype='自定义' and la_id='" + la_id + "') where COLUMN_VALUE is not null)", "select");
                PrintLabel.DataSource = _dt;
                PrintLabel.DisplayMember = "la_url";
                PrintLabel.ValueMember = "la_id";
            }
        }

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

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

        private void LoadGridData()
        {
            DataTable dt = (DataTable)dh.ExecuteSql("select * from barcodeio left join product on pr_code=bi_prodcode where bi_inoutno='" + pi_inoutno.Text + "'", "select");
            BaseUtil.FillDgvWithDataTable(DGV, dt);
        }

        private bool ifcheckrow()
        {
            for (int i = 0; i < DGV.Rows.Count; i++)
            {
                if (DGV.Rows[i].Cells["Choose"].Value != null && DGV.Rows[i].Cells["Choose"].FormattedValue.ToString() == "True")
                {
                    return true;
                }
            }
            return false;
        }

        private void Print_Click(object sender, EventArgs e)
        {
            string ErrMsg = "";
            LogicHandler.GenProdiodetail(pi_inoutno.Text, out ErrMsg);
            MessageBox.Show("生成明细成功");
        }


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

        private void MakeCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (pi_inoutno.Text == "")
                {
                    MessageBox.Show("出入库单号不能为空");
                    return;
                }
                DataTable dt = (DataTable)dh.ExecuteSql("select bi_inqty,bi_barcode from barcodeio left join product on pr_code=bi_prodcode" +
                    " where bi_inoutno='" + pi_inoutno.Text + "' and bi_barcode='" + barcode.Text + "'", "select");
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("出入库单已采集条码" + barcode.Text);
                    return;
                }
                dt = (DataTable)dh.ExecuteSql("select bar_vendbarcode,bar_remain,to_char(nvl(bar_madedate,sysdate),'yyyy-mm-dd')bar_madedate,bar_code,bar_remain,bar_prodcode from barcode left " +
                    "join product on pr_code=bar_prodcode where bar_code='" + barcode.Text + "'", "select");
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("条码" + barcode.Text + "不存在");
                    return;
                }
                string pi_id = dh.getFieldDataByCondition("prodinout", "pi_id", "pi_inoutno='" + pi_inoutno.Text + "'").ToString();
                string bar_code = dt.Rows[0]["bar_code"].ToString();
                string bar_prodcode = dt.Rows[0]["bar_prodcode"].ToString();
                string bar_madedate = dt.Rows[0]["bar_madedate"].ToString();
                string bar_remain = dt.Rows[0]["bar_remain"].ToString();
                string bar_vendbarcode = dt.Rows[0]["bar_vendbarcode"].ToString();
                dh.ExecuteSql("insert into barcodeio(bi_id,bi_piid,bi_barcode,bi_inoutno,bi_prodcode,bi_indate,bi_inqty,bi_vendbarcode,bi_madedate,bi_sourcecode,bi_printstatus)" +
                    "values(barcodeio_seq.nextval,'" + pi_id + "','" + barcode.Text + "','" + pi_inoutno.Text + "','" + bar_prodcode + "',sysdate,'" + bar_remain + "','" + bar_vendbarcode + "',to_date('" + bar_madedate + "','yyyy-mm-dd'),'" + bar_code + "',1)", "insert");
                LoadGridData();
            }
        }

        private void Query_BarcodeIn_Click(object sender, EventArgs e)
        {

        }

        private void DeleteBarcode_Click(object sender, EventArgs e)
        {
            if (!ifcheckrow())
            {
                MessageBox.Show("请勾选需要操作的行");
                return;
            }
            for (int i = 0; i < DGV.Rows.Count; i++)
            {
                if (DGV.Rows[i].Cells["Choose"].Value != null && DGV.Rows[i].Cells["Choose"].FormattedValue.ToString() == "True")
                {
                    string barcode = DGV.Rows[i].Cells["bi_barcode"].Value.ToString();
                    string prodcode = DGV.Rows[i].Cells["bi_prodcode"].Value.ToString();
                    dh.ExecuteSql("delete from barcodeio where bi_id=" + DGV.Rows[i].Cells["bi_id"].Value.ToString(), "delete");
                }
            }
            LoadGridData();
        }

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

        private void Print_Click_1(object sender, EventArgs e)
        {
            if (!ifcheckrow())
            {
                MessageBox.Show("请勾选需要操作的行");
                return;
            }
            for (int i = 0; i < DGV.Rows.Count; i++)
            {
                if (DGV.Rows[i].Cells["Choose"].Value != null && DGV.Rows[i].Cells["Choose"].FormattedValue.ToString() == "True")
                {
                    string barcode = DGV.Rows[i].Cells["bi_barcode"].Value.ToString();
                    string prodcode = DGV.Rows[i].Cells["bi_prodcode"].Value.ToString();
                    string ErrorMessage = "";
                    if (Print.BarTender(Tag.ToString(), ref engine, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), PrinterList.Text, barcode, int.Parse("1"), pi_inoutno.Text, prodcode, "自定义", "0", out ErrorMessage))
                    {

                    }
                }
            }
        }
    }
}