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;
using WindowsFormsApp1.Mac;

namespace WindowsFormsApp1.마크
{
    public partial class Mac_List_Merge : Form
    {
        Mac_List ml;
        Helper_DB db = new Helper_DB();
        Skill_Grid skill = new Skill_Grid();
        public Mac_List_Merge(Mac_List _ml)
        {
            InitializeComponent();
            ml = _ml;
        }
        private void Mac_List_Merge_Load(object sender, EventArgs e)
        {
            data_Input();
            string[] list_kdc = { "KDC4", "KDC5", "KDC6" };
            string[] list_grade = { "A", "B", "C", "D" };

            cb_KDC.Items.AddRange(list_kdc);
            cb_grade.Items.AddRange(list_grade);

            cb_KDC.SelectedIndex = 0;
            cb_grade.SelectedItem = "C";
        }
        void data_Input()
        {
            string[] input_data = { "", "", "", "", "" };
            int count = ml.dataGridView1.Rows.Count;
            for(int a = 0; a < count; a++)
            {
                if (ml.dataGridView1.Rows[a].Cells["chk_V"].Value.ToString() == "V") {
                    input_data[0] = dataGridView1.Rows[a].Cells["idx"].Value.ToString();
                    input_data[1] = dataGridView1.Rows[a].Cells["list_marc"].Value.ToString();
                    input_data[2] = dataGridView1.Rows[a].Cells["total"].Value.ToString();
                    input_data[3] = dataGridView1.Rows[a].Cells["flatter"].Value.ToString();
                    input_data[4] = dataGridView1.Rows[a].Cells["unflatter"].Value.ToString();
                    lbl_charge.Text = dataGridView1.Rows[a].Cells["charge"].Value.ToString();
                    lbl_state.Text = dataGridView1.Rows[a].Cells["state"].Value.ToString();
                    dataGridView1.Rows.Add(input_data);
                }
            }
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            skill.Print_Grid_Num(sender, e);
        }
        private void tb_list_name_Click(object sender, EventArgs e)
        {
            if (tb_list_name.Text == "변경할 작업처의 이름") {
                tb_list_name.Text = "";
            }
        }
        private void btn_Merge_Click(object sender, EventArgs e)
        {
            if (rb_Delete.Checked == false && rb_Keep.Checked == false) {
                MessageBox.Show("기존목록의 유지여부를 선택해주세요.");
                return;
            }
            if (tb_list_name.Text == "변경할 작업처의 이름") {
                MessageBox.Show("작업처의 이름을 입력해주세요.");
                return;
            }
            if (chk_Overlap() == false) {
                MessageBox.Show("작업처의 이름이 중복됩니다. 다시 설정해주세요.");
                return;
            }
            
            // TODO: 생각나는 필터링있으면 if문 추가할 것.
            // 이 밑으론 DB적용사항. 기존목록유지시 INSERT만 / 기존목록삭제시 INSERT이후 기존목록DELETE
            data_Insert();
            if (rb_Delete.Checked == true) {
                // 삭제 함수
                data_delete();
            }
        }
        void data_Insert()
        {
            string table = "Obj_List_Marc";
            string start = DateTime.Now.ToString("yyyy-MM-dd");
            string end = end_date.Value.ToString().Substring(0, 10);

            string[] in_col = { "compidx", "date", "date_res", "list_name", "KDC", 
                                "m_charge", "total", "flatter", "unflatter", "state", 
                                "grade", "etc" };
            string[] in_data = { ml.compidx, start, end, tb_list_name.Text, cb_KDC.Text,
                                 lbl_charge.Text, cal_Total("total").ToString(), cal_Total("flatter").ToString(), cal_Total("unflatter").ToString(), lbl_state.Text,
                                 cb_grade.Text, rtb_etc.Text };

            db.DB_INSERT(table, in_col, in_data);
        }
        void data_delete()
        {
            string table = "Obj_List_Marc";
            string[] target_area = { "idx" };
            string[] target = { "" };
            for(int a = 0; a < dataGridView1.Rows.Count - 1; a++)
            {
                target[0] = dataGridView1.Rows[a].Cells["idx"].Value.ToString();
                db.DB_Delete_More_term(table, "compidx", ml.compidx, target_area, target);
            }

        }
        bool chk_Overlap()
        {
            string tmp_data = db.DB_Select_Search("list_name", "Obj_List_Marc", "compidx", ml.compidx);
            string[] db_data = tmp_data.Split('|');
            for(int a = 0; a < db_data.Length; a++)
            {
                if (tb_list_name.Text == db_data[a]) {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 전체 / 편목 / 미편목의 합계를 구하는 함수.
        /// </summary>
        /// <param name="column">total / flatter / unflatter 만 사용 가능</param>
        /// <returns></returns>
        int cal_Total(string column)
        {
            int result = 0;
            List<int> total = new List<int>();
            
            for(int a = 0; a < dataGridView1.Rows.Count - 1; a++)
            {
                total.Add(Convert.ToInt32(dataGridView1.Rows[a].Cells[column].Value.ToString()));
            }
            for(int a = 0; a < total.Count; a++)
            {
                result += total[a];
            }
            return result;
        }
        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}