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.Delivery;

namespace WindowsFormsApp1.Home
{
    public partial class Transaction_manage : Form
    {
        Helper_DB db = new Helper_DB();
        public string compidx;
        int grididx;
        Main main;
        public Transaction_manage(Main _main)
        {
            InitializeComponent();
            main = _main;
        }
        private void Transaction_manage_Load(object sender, EventArgs e)
        {
            string[] gubun1 = { "전체", "납품처", "거래처" };
            cb_gubun1.Items.AddRange(gubun1);
            cb_gubun1.SelectedIndex = 0;
            compidx = main.com_idx;
            marcLabel.Visible = false;
            gearLabel.Visible = false;
            marcGrid.Visible = false;
            gearGrid.Visible = false;
            tb_marcEmpty.Visible = false;
            tb_marcSave.Visible = false;
            tb_gearEmpty.Visible = false;
            db.DBcon();
        }
        private void Made_Grid()
        {
            string seartempdb = "`idx`, `c_sangho`, `c_gu`, `c_boss`, `c_bubin`, " +
                                "`c_uptae`, `c_jongmok`, `c_tel`, `c_fax`, `c_email`, " +
                                "`c_man`, `c_mantel`, `c_user`, `c_zip`, `c_addr`, " +
                                "`c_dlsID`, `c_dlsPW`, `c_division`, `c_label`, `c_program`, " +
                                "`c_etc`";
            string tmpdb = db.DB_Select_Search(seartempdb, "Client", "campanyidx", compidx);
            dataGridView1.Rows.Clear();
            string[] data = tmpdb.Split('|');
            string[] result = { "", "", "", "", "", 
                                "", "", "", "", "", 
                                "", "", "", "", "",
                                "", "", "", "", "",
                                "" };
            int ea = 0;
            for(int a = 0; a < data.Length; a++)
            {
                if (ea == 11)
                {
                    result[10] += " | " + data[a];
                }
                else if(a%21 == ea)
                {
                    if (ea > 11)
                    {
                        result[ea - 1] = data[a];
                        if (ea == 20)
                        {
                            dataGridView1.Rows.Add(result);
                            ea = 0;
                        }
                    }
                    else
                    {
                        result[ea] = data[a];
                    }
                }
                ea++;
            }
        }
        public void btn_add_Click(object sender, EventArgs e)
        {
            tb_sangho.Text = "";
            tb_boss.Text = "";
            tb_bubin.Text = "";
            tb_uptae.Text = "";
            tb_jongmok.Text = "";
            tb_tel.Text = "";
            tb_fax.Text = "";
            tb_mail.Text = "";
            tb_man.Text = "";
            tb_man1.Text = "";
            tb_user.Text = main.User;
            tb_zip.Text = "";
            tb_addr.Text = "";
            tb_id.Text = "";
            tb_pw.Text = "";
            tb_division.Text = "";
            tb_label.Text = "";
            tb_program.Text = "";
            rtb_etc.Text = "";
            cb_gubun1.SelectedIndex = 0;
        }
        private void btn_save_Click(object sender, EventArgs e)
        {
            if (tb_sangho.Text == "")
            {
                MessageBox.Show("업체명이 비어있습니다.");
                return;
            }
            if (cb_gubun1.SelectedIndex == 0)
            {
                MessageBox.Show("구분을 선택해주세요.");
                return;
            }
            if (cb_gubun2.SelectedIndex == 0)
            {
                MessageBox.Show("구분을 선택해주세요.");
                return;
            }
            string[] tmpdb = { "c_sangho`, `c_gu`, `c_boss`, `c_bubin`, `c_uptae`, " +
                               "`c_jongmok`, `c_tel`, `c_fax`, `c_email`, `c_man`, " +
                               "`c_mantel`, `c_user`, `c_zip`, `c_addr`, `c_dlsID`, " +
                               "`c_dlsPW`, `c_division`, `c_label`, `c_program`, `c_etc`, " +
                               "`campanyidx" };
            string[] tmpdb1 = { tb_sangho.Text, cb_gubun2.Text, tb_boss.Text, tb_bubin.Text, tb_uptae.Text,
                                tb_jongmok.Text, tb_tel.Text, tb_fax.Text, tb_mail.Text, tb_man.Text,
                                tb_man1.Text, tb_user.Text, tb_zip.Text, tb_addr.Text, tb_id.Text,
                                tb_pw.Text, tb_division.Text, tb_label.Text, tb_program.Text, rtb_etc.Text, 
                                compidx };
            db.DB_INSERT("Client", tmpdb, tmpdb1);
            MessageBox.Show(tb_sangho.Text + "가 성공적으로 저장되었습니다.");
            Made_Grid();
        }
        private void btn_edit_Click(object sender, EventArgs e)
        {
            string[] editcol = { "c_sangho`, `c_gu`, `c_boss`, `c_bubin`, `c_uptae`, " +
                               "`c_jongmok`, `c_tel`, `c_fax`, `c_email`, `c_man`, " +
                               "`c_mantel`, `c_user`, `c_zip`, `c_addr`, `c_dlsID`, " +
                               "`c_dlsPW`, `c_division`, `c_label`, `c_program`, `c_etc" };
            string[] editname = { tb_sangho.Text, cb_gubun2.Text, tb_boss.Text, tb_bubin.Text, tb_uptae.Text,
                                tb_jongmok.Text, tb_tel.Text, tb_fax.Text, tb_mail.Text, tb_man.Text,
                                tb_man1.Text, tb_user.Text, tb_zip.Text, tb_addr.Text, tb_id.Text,
                                tb_pw.Text, tb_division.Text, tb_label.Text, tb_program.Text, rtb_etc.Text };
            string[] searchcol = { "idx", "campanyidx" };
            string[] searchname = { dataGridView1.Rows[grididx].Cells["idx"].Value.ToString(), compidx };
            db.More_Update("Client", editcol, editname, searchcol, searchname);
            Made_Grid();
        }
        private void btn_delete_Click(object sender, EventArgs e)
        {
            db.DB_Delete("Client", "idx", dataGridView1.Rows[grididx].Cells["idx"].Value.ToString(), "c_sangho", tb_sangho.Text);
            Made_Grid();
        }
        string updown = "";
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                updown = "UP";
                dataGridView1_CellClick(sender, null);
            }
            if (e.KeyCode == Keys.Down)
            {
                updown = "DOWN";
                dataGridView1_CellClick(sender, null);
            }
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e == null) { 
                if(updown == "UP")
                {
                    if (grididx < 1) return;
                    grididx--;
                }
                else if(updown == "DOWN")
                {
                    if (grididx > dataGridView1.Rows.Count) return;
                    grididx++;
                }
            }
            else if(e != null) { grididx = e.RowIndex; }
            string[] tmp_man = dataGridView1.Rows[grididx].Cells["man"].Value.ToString().Split('|');
            tb_boss.Text = dataGridView1.Rows[grididx].Cells["boss"].Value.ToString();
            tb_bubin.Text = dataGridView1.Rows[grididx].Cells["bubin"].Value.ToString();
            tb_division.Text = dataGridView1.Rows[grididx].Cells["division"].Value.ToString();
            tb_jongmok.Text = dataGridView1.Rows[grididx].Cells["jongmok"].Value.ToString();
            tb_label.Text = dataGridView1.Rows[grididx].Cells["label"].Value.ToString();
            tb_mail.Text = dataGridView1.Rows[grididx].Cells["mail"].Value.ToString();
            tb_program.Text = dataGridView1.Rows[grididx].Cells["program"].Value.ToString();
            tb_id.Text = dataGridView1.Rows[grididx].Cells["DLSid"].Value.ToString();
            tb_pw.Text = dataGridView1.Rows[grididx].Cells["DLSpw"].Value.ToString();
            tb_uptae.Text = dataGridView1.Rows[grididx].Cells["uptae"].Value.ToString();
            tb_zip.Text = dataGridView1.Rows[grididx].Cells["post_num"].Value.ToString();
            tb_user.Text = dataGridView1.Rows[grididx].Cells["assumer"].Value.ToString();
            tb_sangho.Text = dataGridView1.Rows[grididx].Cells["sangho"].Value.ToString();
            tb_tel.Text = dataGridView1.Rows[grididx].Cells["tel"].Value.ToString();
            tb_fax.Text = dataGridView1.Rows[grididx].Cells["fax"].Value.ToString();

            if (tmp_man.Length > 1)
            {
                if(tmp_man[0] != "") { tb_man.Text = tmp_man[0]; }
                if(tmp_man[1] != "") { tb_man1.Text = tmp_man[1]; }
            }

            tb_addr.Text = dataGridView1.Rows[grididx].Cells["addr"].Value.ToString();
            rtb_etc.Text = dataGridView1.Rows[grididx].Cells["bigo"].Value.ToString();

            if(dataGridView1.Rows[grididx].Cells["gubun"].Value.ToString() == "학교")
            {
                cb_gubun1.SelectedIndex = 1;
                cb_gubun2.Items.Clear();
                string[] gubun2 = { "전체", "학교", "도서관" };
                cb_gubun2.Items.AddRange(gubun2);
                cb_gubun2.SelectedIndex = 1;
            }
            if(dataGridView1.Rows[grididx].Cells["gubun"].Value.ToString() == "도서관")
            {
                cb_gubun1.SelectedIndex = 1;
                cb_gubun2.Items.Clear();
                string[] gubun2 = { "전체", "학교", "도서관" };
                cb_gubun2.Items.AddRange(gubun2);
                cb_gubun2.SelectedIndex = 2;
            }
            if(dataGridView1.Rows[grididx].Cells["gubun"].Value.ToString() == "서점")
            {
                cb_gubun1.SelectedIndex = 2;
                cb_gubun2.Items.Clear();
                string[] gubun2 = { "전체", "서점", "기타" };
                cb_gubun2.Items.AddRange(gubun2);
                cb_gubun2.SelectedIndex = 1;
            }
            if(dataGridView1.Rows[grididx].Cells["gubun"].Value.ToString() == "기타")
            {
                cb_gubun1.SelectedIndex = 2;
                cb_gubun2.Items.Clear();
                string[] gubun2 = { "전체", "서점", "기타" };
                cb_gubun2.Items.AddRange(gubun2);
                cb_gubun2.SelectedIndex = 2;
            }
        }
        private void tb_sangho_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                btn_search_Click(null, null);
            }
        }
        private void btn_search_Click(object sender, EventArgs e)
        {
            /// _Sub_Search_Form sub_search = new _Sub_Search_Form(this);
            /// sub_search.Show();
            /// 
            dataGridView1.Rows.Clear();

            string search = "`idx`, `c_sangho`, `c_gu`, `c_boss`, `c_bubin`, " +
                            "`c_uptae`, `c_jongmok`, `c_tel`, `c_fax`, `c_email`, " +
                            "`c_man`, `c_mantel`, `c_user`, `c_zip`, `c_addr`, " +
                            "`c_dlsID`, `c_dlsPW`, `c_division`, `c_label`, `c_program`, " +
                            "`c_etc`";
            string dbtext = db.DB_Contains("Client", compidx, "c_sangho", tb_sangho.Text, search);
            string[] dbtext1 = dbtext.Split('|');
            string[] result = { "", "", "", "", "",
                                "", "", "", "", "",
                                "", "", "", "", "",
                                "", "", "", "", "",
                                "", "" };
            for (int a = 0; a < dbtext1.Length; a++)
            {
                if (a % 21 == 0) { result[0] = dbtext1[a]; }             // idx
                if (a % 21 == 1) { result[1] = dbtext1[a]; }             // 거래처명
                if (a % 21 == 2) { result[2] = dbtext1[a]; }             // 구분
                if (a % 21 == 3) { result[3] = dbtext1[a]; }             // 대표자명
                if (a % 21 == 4) { result[4] = dbtext1[a]; }             // 등록번호
                if (a % 21 == 5) { result[5] = dbtext1[a]; }             // 업태
                if (a % 21 == 6) { result[6] = dbtext1[a]; }             // 종목
                if (a % 21 == 7) { result[7] = dbtext1[a]; }             // 전화번호
                if (a % 21 == 8) { result[8] = dbtext1[a]; }             // 팩스
                if (a % 21 == 9) { result[9] = dbtext1[a]; }             // 메일
                if (a % 21 == 10) { result[10] = dbtext1[a]; }             // 담당자정보1
                if (a % 21 == 11) { result[11] = dbtext1[a]; }             // 담당자정보2
                if (a % 21 == 12) { result[12] = dbtext1[a]; }             // 담당직원
                if (a % 21 == 13) { result[13] = dbtext1[a]; }             // 우편번호
                if (a % 21 == 14) { result[14] = dbtext1[a]; }             // 주소
                if (a % 21 == 15) { result[15] = dbtext1[a]; }             // 아이디
                if (a % 21 == 16) { result[16] = dbtext1[a]; }             // 비밀번호
                if (a % 21 == 17) { result[17] = dbtext1[a]; }             // 용지칸수
                if (a % 21 == 18) { result[18] = dbtext1[a]; }             // 띠라벨
                if (a % 21 == 19) { result[19] = dbtext1[a]; }             // 사용프로그램
                if (a % 21 == 20) { result[20] = dbtext1[a]; dataGridView1.Rows.Add(result); } // 비고
            }
            dataGridView1.Focus();
        }
        private void btn_close_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void cb_gubun1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_gubun1.SelectedIndex == 1)
            {
                cb_gubun2.Items.Clear();
                string[] gubun2 = { "전체", "학교", "도서관" };
                cb_gubun2.Items.AddRange(gubun2);
                cb_gubun2.SelectedIndex = 0;
                Made_gubun_Grid("학교", "도서관");
            }
            if (cb_gubun1.SelectedIndex == 2)
            {
                cb_gubun2.Items.Clear();
                string[] gubun2 = { "전체", "서점", "기타" };
                cb_gubun2.Items.AddRange(gubun2);
                cb_gubun2.SelectedIndex = 0;
                Made_gubun_Grid("서점", "기타");
            }
        }
        private void Made_gubun_Grid(string strValue1, string strValue2)
        {
            string seartempdb = "`idx`, `c_sangho`, `c_gu`, `c_boss`, `c_bubin`, " +
                                "`c_uptae`, `c_jongmok`, `c_tel`, `c_fax`, `c_email`, " +
                                "`c_man`, `c_mantel`, `c_user`, `c_zip`, `c_addr`, " +
                                "`c_dlsID`, `c_dlsPW`, `c_division`, `c_label`, `c_program`, " +
                                "`c_etc`";
            
            string cmd = "SELECT " + seartempdb + " FROM Client " +
                         "WHERE `c_gu` = '" + strValue1 + "' OR `c_gu` = '" + strValue2 + "';";
            string tmpdb = db.self_Made_Cmd(cmd);
            dataGridView1.Rows.Clear();
            string[] data = tmpdb.Split('|');
            string[] result = { "", "", "", "", "",
                                "", "", "", "", "",
                                "", "", "", "", "",
                                "", "", "", "", "",
                                "" };
            for (int a = 0; a < data.Length; a++)
            {
                if (a % 21 == 0)  { result[0]  = data[a]; }             // idx
                if (a % 21 == 1)  { result[1]  = data[a]; }             // 거래처명
                if (a % 21 == 2)  { result[2]  = data[a]; }             // 구분
                if (a % 21 == 3)  { result[3]  = data[a]; }             // 대표자명
                if (a % 21 == 4)  { result[4]  = data[a]; }             // 등록번호
                if (a % 21 == 5)  { result[5]  = data[a]; }             // 업태
                if (a % 21 == 6)  { result[6]  = data[a]; }             // 종목
                if (a % 21 == 7)  { result[7]  = data[a]; }             // 전화번호
                if (a % 21 == 8)  { result[8]  = data[a]; }             // 팩스
                if (a % 21 == 9)  { result[9]  = data[a]; }             // 메일
                if (a % 21 == 10) { result[10] = data[a]; }             // 담당자정보1
                if (a % 21 == 11) { result[10] += " | " + data[a]; }    // 담당자정보2
                if (a % 21 == 12) { result[11] = data[a]; }             // 담당직원
                if (a % 21 == 13) { result[12] = data[a]; }             // 우편번호
                if (a % 21 == 14) { result[13] = data[a]; }             // 주소
                if (a % 21 == 15) { result[14] = data[a]; }             // 아이디
                if (a % 21 == 16) { result[15] = data[a]; }             // 비밀번호
                if (a % 21 == 17) { result[16] = data[a]; }             // 용지칸수
                if (a % 21 == 18) { result[17] = data[a]; }             // 띠라벨
                if (a % 21 == 19) { result[18] = data[a]; }             // 사용프로그램
                if (a % 21 == 20) { result[19] = data[a]; dataGridView1.Rows.Add(result); } // 비고
            }
        }
        bool marc_lookup = false;
        private void btn_marc_expand_Click(object sender, EventArgs e)
        {
            if(marc_lookup == false)
            {
                btn_marc_expand.Text = "마크 확장 >>";
                marc_lookup = true;
            }
            else
            {
                btn_marc_expand.Text = "마크 확장 <<";
                marc_lookup = false;
            }
            marcLabel.Visible = marc_lookup;
            gearLabel.Visible = marc_lookup;
            marcGrid.Visible =  marc_lookup;
            gearGrid.Visible =  marc_lookup; 
            tb_marcEmpty.Visible = marc_lookup;
            tb_marcSave.Visible = marc_lookup;
            tb_gearEmpty.Visible = marc_lookup;
        }
    }
}
