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 WindowsFormsApp1.Home
{
    public partial class Order_manage : Form
    {
        Helper_DB db = new Helper_DB();
        Main main;
        int grididx = -3333;
        public Order_manage(Main _main)
        {
            InitializeComponent();
            main = _main;
        }
        private void Order_manage_Load(object sender, EventArgs e)
        {
            db.DBcon();
            string[] filter = { "상호", "대표자명", "전화번호", "팩스번호", "사업자번호" };
            cb_search_filter.Items.AddRange(filter);
            cb_search_filter.SelectedIndex = 0;
            string[] gubun = { "출판", "도매", "기타", "경비" };
            cb_gubun.Items.AddRange(gubun);
            cb_gubun.SelectedIndex = 0;
        }
        private void btn_Lookup_Click(object sender, EventArgs e)   // 조회
        {
            string Search_Table = "`idx`, `send_chk`, `sangho`, `boss`, `bubin`, " +
                                  "`uptae`, `jongmok`, `zip`, `addr`, `tel`, " +
                                  "`fax`, `bank_no`, `bank_comp`, `bank_name`, `gu`, " +
                                  "`barea`, `etc_1`, `etc_2`, `email`, `id`, " +
                                  "`pw`, `site`";
            if (tb_search.Text == "")
            {
                string[] Where_Table = { "comparyidx" };
                string[] Search_Data = { main.com_idx };
                string tmpdata = db.More_DB_Search("Purchase", Where_Table, Search_Data, Search_Table);
                Made_Grid(tmpdata);
            }
            else if (tb_search.Text != "") 
            {
                string[] Search_col = { "", "" };
                if (cb_search_filter.SelectedIndex == 0) { Search_col[0] = "sangho"; Search_col[1] = "comparyidx"; }
                if (cb_search_filter.SelectedIndex == 1) { Search_col[0] = "boss";   Search_col[1] = "comparyidx"; }
                if (cb_search_filter.SelectedIndex == 2) { Search_col[0] = "tel";    Search_col[1] = "comparyidx"; }
                if (cb_search_filter.SelectedIndex == 4) { Search_col[0] = "fax";    Search_col[1] = "comparyidx"; }
                if (cb_search_filter.SelectedIndex == 3) { Search_col[0] = "bubin";  Search_col[1] = "comparyidx"; }
                string tmpdata = db.DB_Contains("Purchase", main.com_idx, Search_col[0], tb_search.Text, Search_Table, Search_col[1], main.com_idx);
                Made_Grid(tmpdata);
            }
        }
        private void Made_Grid(string strValue)
        {
            dataGridView1.Rows.Clear();
            string[] dbdata = strValue.Split('|');
            string[] grid = { "", "", "", "", "", 
                              "", "", "", "", "", 
                              "", "", "", "", "", 
                              "", "", "", "", "", 
                              "", ""};
            int count = dbdata.Length;
            int ea = 0;
            for(int a = 0; a < count; a++)
            {
                if (a % 22 == ea) {
                    if (ea == 1) {
                        if (dbdata[a] == "0") { grid[ea] = ""; }
                        if (dbdata[a] == "1") { grid[ea] = "V"; }
                        ea++;
                    }
                    else if(ea == 14) {
                        if (dbdata[a] == "0") { grid[14] = "출판"; }
                        if (dbdata[a] == "1") { grid[14] = "도매"; }
                        if (dbdata[a] == "2") { grid[14] = "기타"; }
                        if (dbdata[a] == "3") { grid[14] = "경비"; }
                        ea++;
                    }
                    else if (ea == 21) { 
                        grid[ea] = dbdata[a];
                        dataGridView1.Rows.Add(grid);
                        ea = 0; 
                    }
                    else {
                        grid[ea] = dbdata[a];
                        ea++;
                    }
                }
            }
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            grididx = e.RowIndex;
            Fill_textBox();
        }
        void Fill_textBox()
        {
            btn_Add_Click(null, null);
            tb_sangho.Text = dataGridView1.Rows[grididx].Cells["sangho"].Value.ToString();
            tb_boss.Text = dataGridView1.Rows[grididx].Cells["boss"].Value.ToString();
            tb_bubin.Text = dataGridView1.Rows[grididx].Cells["bubin"].Value.ToString();
            tb_uptae.Text = dataGridView1.Rows[grididx].Cells["uptae"].Value.ToString();
            tb_jongmok.Text = dataGridView1.Rows[grididx].Cells["jongmok"].Value.ToString();
            tb_zip.Text = dataGridView1.Rows[grididx].Cells["zip"].Value.ToString();
            tb_addr.Text = dataGridView1.Rows[grididx].Cells["addr"].Value.ToString();
            tb_tel.Text = dataGridView1.Rows[grididx].Cells["tel"].Value.ToString();
            tb_fax.Text = dataGridView1.Rows[grididx].Cells["fax"].Value.ToString();
            tb_bank_no.Text = dataGridView1.Rows[grididx].Cells["bank_no"].Value.ToString();
            tb_bank_comp.Text = dataGridView1.Rows[grididx].Cells["bank_comp"].Value.ToString();
            tb_bank_name.Text = dataGridView1.Rows[grididx].Cells["bank_name"].Value.ToString();
            tb_barea.Text = dataGridView1.Rows[grididx].Cells["barea"].Value.ToString();

            string[] Search_data = { tb_sangho.Text, tb_boss.Text, tb_bubin.Text, tb_jongmok.Text, tb_zip.Text };
            string[] Where_col = { "sangho", "boss", "bubin", "jongmok", "zip" };
            string tmpdata = db.More_DB_Search("Purchase", Where_col, Search_data, "emchk");
            tmpdata = tmpdata.Replace("|", "");
            if (tmpdata == "0")
            {
                rtb_etc.Text = dataGridView1.Rows[grididx].Cells["etc_1"].Value.ToString();
                rtb_etc.Text += "\n" + dataGridView1.Rows[grididx].Cells["etc_2"].Value.ToString();
            }
            else
            {
                rtb_etc.Text = dataGridView1.Rows[grididx].Cells["etc_1"].Value.ToString();
            }
            tb_email.Text = dataGridView1.Rows[grididx].Cells["email"].Value.ToString();
            tb_id.Text = dataGridView1.Rows[grididx].Cells["id"].Value.ToString();
            tb_pw.Text = dataGridView1.Rows[grididx].Cells["pw"].Value.ToString();
            tb_site.Text = dataGridView1.Rows[grididx].Cells["site"].Value.ToString();

            if (dataGridView1.Rows[grididx].Cells["send_chk"].Value.ToString() == "") { chk_send.Checked = false; }
            if (dataGridView1.Rows[grididx].Cells["send_chk"].Value.ToString() == "V") { chk_send.Checked = true; }

            if(dataGridView1.Rows[grididx].Cells["gu"].Value.ToString() == "출판") { cb_gubun.SelectedIndex = 0; }
            if(dataGridView1.Rows[grididx].Cells["gu"].Value.ToString() == "도매") { cb_gubun.SelectedIndex = 1; }
            if(dataGridView1.Rows[grididx].Cells["gu"].Value.ToString() == "기타") { cb_gubun.SelectedIndex = 2; }
            if(dataGridView1.Rows[grididx].Cells["gu"].Value.ToString() == "경비") { cb_gubun.SelectedIndex = 3; }
        }
        private void btn_Add_Click(object sender, EventArgs e)      // 추가
        {
            tb_sangho.Text = "";
            tb_boss.Text = "";
            tb_bubin.Text = "";
            tb_uptae.Text = "";
            tb_jongmok.Text = "";
            tb_zip.Text = "";
            tb_addr.Text = "";
            tb_tel.Text = "";
            tb_fax.Text = "";
            tb_bank_no.Text = "";
            tb_bank_comp.Text = "";
            tb_bank_name.Text = "";
            tb_barea.Text = "";
            rtb_etc.Text = "";
            tb_email.Text = "";
            tb_id.Text = "";
            tb_pw.Text = "";
            tb_site.Text = "";
            cb_gubun.SelectedIndex = 0;
        }
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                grididx--;
                Fill_textBox();
            }
            if (e.KeyCode == Keys.Down)
            {
                grididx++;
                Fill_textBox();
            }
        }
        private void btn_Save_Click(object sender, EventArgs e)     // 저장
        {
            string send_chk = "0";
            string gu = "";
            if (chk_send.Checked == true) { send_chk = "1"; }
            if(cb_gubun.SelectedIndex == 0) { gu = "출판"; }
            if(cb_gubun.SelectedIndex == 1) { gu = "도매"; }
            if(cb_gubun.SelectedIndex == 2) { gu = "기타"; }
            if(cb_gubun.SelectedIndex == 3) { gu = "경비"; }
            string[] Insert_Table = {"send_chk", "sangho", "boss", "bubin", "uptae",  
                                     "jongmok", "zip", "addr", "tel", "fax", 
                                     "bank_no", "bank_comp", "bank_name", "gu", "barea", 
                                     "etc_1", "email", "id", "pw", "site", "emchk"};
            string[] Insert_Data = { send_chk, tb_sangho.Text, tb_boss.Text, tb_bubin.Text, tb_uptae.Text,
                                     tb_jongmok.Text, tb_zip.Text, tb_addr.Text, tb_tel.Text, tb_fax.Text,
                                     tb_bank_no.Text, tb_bank_comp.Text, tb_bank_name.Text, gu, tb_barea.Text,
                                     rtb_etc.Text, tb_email.Text, tb_id.Text, tb_pw.Text, tb_site.Text, "1"};
            db.DB_INSERT("Purchase", Insert_Table, Insert_Data);
            MessageBox.Show(tb_sangho.Text + " 저장 완료");
        }
        private void btn_Delete_Click(object sender, EventArgs e)   // 삭제
        {
            string idx = dataGridView1.Rows[grididx].Cells["idx"].Value.ToString();
            string sangho = dataGridView1.Rows[grididx].Cells["sangho"].Value.ToString();
            db.DB_Delete("Purchase", "idx", idx, "sangho", sangho);
            MessageBox.Show(sangho + " 삭제 완료");
        }
        private void tb_memo_Click(object sender, EventArgs e)
        {
            memo memo = new memo();
            memo.Show();
        }
        private void btn_SitePage_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(tb_site.Text);
        }
        private void btn_close_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void tb_search_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                btn_Lookup_Click(null, null);
            }
        }
        private void chk_stopUse_CheckedChanged(object sender, EventArgs e)
        {
            if(chk_stopUse.Checked == true)
            {
                tb_sangho.Text += "-사용중지";
            }
            else if (chk_stopUse.Checked == false)
            {
                tb_sangho.Text = tb_sangho.Text.Replace("-사용중지", "");
            }
        }
    }
}
