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

namespace WindowsFormsApp1.Delivery
{
    public partial class Order_input_Search : Form
    {
        Order_input oin;
        Purchase_Book pb;
        Remit_reg2 rem2;
        Helper_DB db = new Helper_DB();

        int grididx = 0;
        public int[] oin_grid_idx = { 0, 0 };
        public string Where_Open;
        public string searchText;
        string compidx;

        public Order_input_Search(Order_input o_in)
        {
            InitializeComponent();
            oin = o_in;
            compidx = oin.compidx;
        }
        public Order_input_Search(Remit_reg2 _rem2)
        {
            InitializeComponent();
            rem2 = _rem2;
            compidx = rem2.compidx;
            searchText = rem2.tb_purchase.Text;
        }
        public Order_input_Search(Purchase_Book _pb)
        {
            InitializeComponent();
            pb = _pb;
            compidx = pb.compidx;
            searchText = pb.tb_purchase.Text;
        }
        public Order_input_Search()
        {
            InitializeComponent();
        }
        private void Order_input_Search_Load(object sender, EventArgs e)
        {
            db.DBcon();
            if(Where_Open == "book_list") {
                if(searchText == "") {
                    string cmd = db.DB_Contains("Obj_List", compidx, 
                        "", "", "`list_name`, `charge`, `state`");
                    made_grid(cmd);
                }
                else {
                    string cmd = db.DB_Contains("Obj_List", compidx, 
                        "list_name", searchText, "`list_name`, `charge`, `state`");
                    made_grid(cmd);
                }
            }
            else if(Where_Open.Contains("Order")) {
                list_name.HeaderText = "주문처";
                charge.HeaderText = "대표자";
                state.HeaderText = "종목";

                string cmd = db.DB_Contains("Purchase", compidx,
                    "sangho", searchText, "`sangho`, `boss`, `jongmok`");
                made_grid(cmd);
            }
        }
        private void made_grid(string strValue)
        {
            string[] data = strValue.Split('|');
            dataGridView1.Rows.Clear();
            string[] res = { "", "", "" };
            for (int a = 0; a < data.Length; a++)
            {
                if (a % 3 == 0) { res[0] = data[a]; }
                if (a % 3 == 1) { res[1] = data[a]; }
                if (a % 3 == 2) { res[2] = data[a]; 
                    if(Contain_Text(res[0]))
                        dataGridView1.Rows.Add(res); 
                }
            }
        }
        private bool Contain_Text(string ori)
        {
            if (ori.Contains(searchText))
                return true;

            return false;
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            grididx = e.RowIndex;
        }
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up) { grididx--; }
            if (e.KeyCode == Keys.Down) { grididx++; }

            if (e.KeyCode == Keys.Enter) { dataGridView1_CellDoubleClick(null, null); }
            if (e.KeyCode == Keys.Escape) { Close(); }
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Where_Open == "") { grididx = 0; }
            if (e != null) { grididx = e.RowIndex; }
            if (oin != null) {
                oin_result();
            }
            else if (pb != null) {
                pb_result();
            }
            else if (rem2 != null) {
                rem2.tb_purchase.Text = dataGridView1.Rows[grididx].Cells["list_name"].Value.ToString();
                rem2.mk_base(rem2.tb_purchase.Text);
            }
            Close();
        }
        private void oin_result()
        {
            if (Where_Open == "book_list")
            {
                string[] tmp_col = { "compidx", "list_name" };
                string[] tmp_data = { compidx, dataGridView1.Rows[grididx].Cells["list_name"].Value.ToString() };
                string takedata = "`order`, `order_stat`, `isbn`, `book_name`, `author`, " +
                                  "`book_comp`, `order_count`, `count`, `pay`, `total`, " +
                                  "`etc`, `list_name`, `order_date`, `send_date`, `header`, " +
                                  "`num`";
                string cmd = db.More_DB_Search("Obj_List_Book", tmp_col, tmp_data, takedata);
                oin.made_grid(cmd, false);
                oin.tb_search_book_list.Text = tmp_data[1];
            }
            else if (Where_Open.Contains("Order"))
            {
                if(Where_Open == "Order_Grid") {
                    oin.dataGridView1.Rows[oin_grid_idx[0]].Cells[oin_grid_idx[1]].Value =
                        dataGridView1.Rows[grididx].Cells["list_name"].Value.ToString();
                }
                else {
                    oin.tb_orderText.Text = dataGridView1.Rows[grididx].Cells["list_name"].Value.ToString();
                }
            }
        }
        private void pb_result()
        {
            pb.tb_purchase.Text = dataGridView1.Rows[grididx].Cells["list_name"].Value.ToString();
            pb.btn_Lookup_Click(null, null);
        }
    }
}
