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

namespace Project.Dialog
{
    public partial class PMPUpdate : Form
    {
        public PMPUpdate()
        {
            InitializeComponent();
        }

        private void PMPUpdate_Load(object sender, EventArgs e)
        {
            // TODO: 이 코드는 데이터를 'dsPMPDatabase.SPMaster' 테이블에 로드합니다. 필요 시 이 코드를 이동하거나 제거할 수 있습니다.
         

        }

        //public DataSet convertDataReaderToDataSet(SqlClient.SqlDataReader reader)
        //{
        //    DataSet dataSet = new DataSet();
        //    DataRow dataRow;
        //    string columnName;
        //    DataColumn column;
        //    DataTable schemaTable;
        //    DataTable dataTable;

        //    do
        //    {
        //        // Create new data table
        //        schemaTable = reader.GetSchemaTable;
        //        dataTable = new DataTable();
        //        if (!IsDBNull(schemaTable))
        //        {
        //            // A query returning records was executed
        //            int i;
        //            for (i = 0; i <= schemaTable.Rows.Count - 1; i++)
        //            {
        //                dataRow = schemaTable.Rows(i);
        //                // Create a column name that is unique in the data table
        //                columnName = dataRow("ColumnName");
        //                // Add the column definition to the data table
        //                column = new DataColumn(columnName, (Type)dataRow("DataType"));
        //                dataTable.Columns.Add(column);
        //            }

        //            dataSet.Tables.Add(dataTable);

        //            // Fill the data table we just created
        //            while (reader.Read())
        //            {
        //                dataRow = dataTable.NewRow();
        //                for (i = 0; i <= reader.FieldCount - 1; i++)
        //                    dataRow(i) = reader(i);
        //                dataTable.Rows.Add(dataRow);
        //            }
        //        }
        //        else
        //        {
        //            // No records were returned
        //            column = new DataColumn("RowsAffected");
        //            dataTable.Columns.Add(column);
        //            dataSet.Tables.Add(dataTable);
        //            dataRow = dataTable.NewRow();
        //            dataRow(0) = reader.RecordsAffected;
        //            dataTable.Rows.Add(dataRow);
        //        }
        //    }
        //    while (reader.NextResult());
        //    return dataSet;
        //}

        private void button1_Click(object sender, EventArgs e)
        {
            var cs = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ textBox1.Text.Trim();
            Properties.Settings.Default["PMPCS"] = cs;
            var cs2 = Properties.Settings.Default.PMPCS;
            try
            {
                this.ta.Fill(this.dsPMPDatabase.SPMaster);
            }catch (Exception ex)
            {
                Util.MsgE(ex.Message);
            }
            

        }

        private void sPMasterBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bs.EndEdit();
            this.tam.UpdateAll(this.dsPMPDatabase);

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            var dlg = Util.MsgQ("기존 자료가 모두 삭제되고 다시 업로드 됩니다");
            if (dlg != DialogResult.Yes) return;

            var taMssql = new dsMSSQLTableAdapters.SPMasterTableAdapter();
            taMssql.DeleteAll();

            this.prb1.Minimum = 0;
            this.prb1.Maximum = this.dsPMPDatabase.SPMaster.Rows.Count;
            this.prb1.Value = 0;

            foreach(DsPMPDatabase.SPMasterRow src in this.dsPMPDatabase.SPMaster.Rows)
            {
                this.prb1.Value += 1;

                var datestr = "";
                if (src.IsEnrDateNull() == false) datestr = src.EnrDate.ToShortDateString();

                //이 값을 MSSQL에 넣는다
                taMssql.Insert(src.ID, src.CtrlNo, src.SIDNo, src.Storage, src.Location,
                    src.UseEqmt, src.PartName, src.PartNo, src.Process, src.SupplierNo, (int)src.CurrentQty, (int)src.MHA,
                    src.Division, (int)src.BadQty, (int)src.Price, src.PriceUnit, src.CostCenter, src.Memo, datestr, src.Enrollee);
            }

            Util.MsgI("완료");
                
        }
    }
}
