using AR;
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 UniMarc
{
    public partial class Batch_processing : Form
    {
        Helper_DB db = new Helper_DB();
        Main main;
        MySql.Data.MySqlClient.MySqlDataAdapter da;
        DataTable dt = new DataTable();
        public Batch_processing(Main _main)
        {
            InitializeComponent();
            main = _main;
            this.Text = $"매크로 데이터 관리";
        }

        private void Batch_processing_Load(object sender, EventArgs e)
        {
            RefreshData();
        }
        void RefreshData()
        {
            var sql = "select * from SetMacro order by tag";
            var firstrun = da == null;
            if(firstrun)
            {
                var cn = db.CreateConnection();

                da = new MySql.Data.MySqlClient.MySqlDataAdapter(sql, cn);
                var cb = new MySql.Data.MySqlClient.MySqlCommandBuilder(da);

                // CommandBuilder가 Insert/Update/Delete 명령을 자동으로 생성하도록 설정
                da.InsertCommand = cb.GetInsertCommand();
                da.UpdateCommand = cb.GetUpdateCommand();
                da.DeleteCommand = cb.GetDeleteCommand();
            }

            this.dt.Clear();
            da.Fill(dt);
            dt.AcceptChanges();

            if(firstrun)
            {
                this.bs.DataSource = dt;
                this.bn.BindingSource = this.bs;
                this.dv1.DataSource = this.bs;
                
                // idx 열(첫번째 열) 편집 비활성화 및 배경색 회색으로 설정
                if (this.dv1.Columns.Count > 0)
                {
                    this.dv1.Columns[0].ReadOnly = true;
                    this.dv1.Columns[0].DefaultCellStyle.BackColor = Color.LightGray;
                    this.dv1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                }
                
                // 1, 3번 열 가운데 정렬
                if (this.dv1.Columns.Count > 1)
                {
                    this.dv1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                }
                if (this.dv1.Columns.Count > 3)
                {
                    this.dv1.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                }
            }
    
            this.dv1.AutoResizeColumns();
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bs.EndEdit();
            if (UTIL.MsgQ("자료를 저장할까요?") != DialogResult.Yes) return;
            var cnt = da.Update(this.dt);
            dt.AcceptChanges();
            UTIL.MsgI($"{cnt}건의 자료가 저장 되었습니다");
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            RefreshData();
        }
    }
}
