using MESClassLibrary.BLL.BasicInfo;
using MESClassLibrary.BLL.Inspection;
using Stone.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InjectionSearch
{
    public partial class FrmPackageDetail : Form
    {
        private BarCodeBLL _barCodeBLL = new BarCodeBLL();
        private InjectionBoxBLL _injectionBoxBLL = new InjectionBoxBLL();
        private string _packageCode = string.Empty;

        public FrmPackageDetail(string pPackageCode)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
            this.Size = new Size(1200, 820);
            MyGridViewStyle.SetDataGridRowNumber(dataGridView1);
            this._packageCode = pPackageCode;
            txtPackageCode.Text = pPackageCode;
            dataGridView1.AutoGenerateColumns = false;
            if(!string.IsNullOrEmpty(pPackageCode))
                btnSearch_Click(null, null);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string boxCode = txtPackageCode.Text?.Trim();
            
            string date1 = this.dateTimePicker1.Value.ToString("yyyy-MM-dd 00:00:00");
            if (string.IsNullOrWhiteSpace(this.dateTimePicker1.Text))
            {
                date1 = "1900-01-01 00:00:00";
            }
            string date2 = this.dateTimePicker2.Value.ToString("yyyy-MM-dd 23:59:59");
            if (string.IsNullOrWhiteSpace(this.dateTimePicker2.Text))
            {
                date2 = "9999-01-01 00:00:00";
            }

            string whereStr = $" CreateTime > '{date1}' and CreateTime<'{date2}' ";
            if (!string.IsNullOrEmpty(txtPackageCode.Text.Trim()))
            {
                whereStr = whereStr + $" and BoxNo='{txtPackageCode.Text.Trim()}'";
            }

            DataTable dt = _barCodeBLL.SearchBarCodeByWhere(whereStr, "v_Code", "200");
            if (!dt.Columns.Contains("Replace"))
            {
                dt.Columns.Add("Replace");
            }
            foreach(DataRow dr in dt.Rows)
            {
                dr["Replace"] = "不良替换";
            }
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.RowIndex != -1 && !dataGridView1.Rows[e.RowIndex].IsNewRow)
            {

                if (e.ColumnIndex == 4)
                {
                    DataRow dataRow = (this.dataGridView1[e.ColumnIndex, e.RowIndex].OwningRow.DataBoundItem as DataRowView).Row;
                    FrmBarCodeReplace frm = new FrmBarCodeReplace(dataRow);
                    frm.FormClosed += (a, b) => {
                        btnSearch_Click(null, null);
                    };
                    frm.ShowDialog();
                }
            }
        }
    }
}