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.마크;

namespace WindowsFormsApp1.Mac
{
    public partial class Search_Infor : Form
    {
        Main main;
        Helper_DB db = new Helper_DB();
        public string compidx;
        public Search_Infor(Main _main)
        {
            InitializeComponent();
            main = _main;
            compidx = main.com_idx;
        }
        private void Search_Infor_Load(object sender, EventArgs e)
        {
            db.DBcon();

            string[] filter = { "도서명", "저자", "출판사", "ISBN" };
            cb_filter.Items.AddRange(filter);
            cb_filter.SelectedIndex = 0;

            for(int a= 0; a < dataGridView1.Columns.Count; a++)
            {
                if (a != 7 && a != 8)
                    dataGridView1.Columns[a].ReadOnly = true;
            }
        }
        private void btn_search_Click(object sender, EventArgs e)
        {
            string target = string.Empty;
            string Area = "`grade`, `ISBN`, `서명`, `저자`, `출판사`, `출판년월`, `가격`, `비고1`, `비고2`, `marc`";
            if (cb_filter.SelectedIndex == -1) {
                MessageBox.Show("검색필터를 선택해주세요!");
                cb_filter.Focus();
                return;
            }
            if (cb_filter.SelectedItem.ToString() == "도서명") { target = "서명"; }
            else { target = cb_filter.SelectedItem.ToString(); }

            string tmp_data = db.DB_Contains("Marc", compidx, target, tb_search.Text, Area);
            string[] tmp_arr = tmp_data.Split('|');
            input_grid(tmp_arr);
        }
        void input_grid(string[] arr)
        {
            string[] grid = { "", "", "", "", "",
                              "", "", "", "", "" };
            for(int a = 0; a < arr.Length; a++)
            {
                if (a % 10 == 0) grid[0] = Change_Grade(arr[a]);
                if (a % 10 == 1) grid[1] = arr[a];
                if (a % 10 == 2) grid[2] = arr[a];
                if (a % 10 == 3) grid[3] = arr[a];
                if (a % 10 == 4) grid[4] = arr[a];
                if (a % 10 == 5) grid[5] = arr[a];
                if (a % 10 == 6) grid[6] = arr[a];
                if (a % 10 == 7) grid[7] = arr[a];
                if (a % 10 == 8) grid[8] = arr[a];
                if (a % 10 == 9) {
                    grid[9] = arr[a];
                    dataGridView1.Rows.Add(grid);
                }
            }
        }
        string Change_Grade(string idx)
        {
            string result = string.Empty;
            switch (idx)
            {
                case "0":
                    result = "D";
                    break;
                case "1":
                    result = "C";
                    break;
                case "2":
                    result = "B";
                    break;
                case "3":
                    result = "A";
                    break;
                default:
                    result = "null";
                    break;
            }
            return result;
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            int col = e.ColumnIndex;

            Search_Infor_Sub sis = new Search_Infor_Sub();
        }
        private void tb_search_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) { btn_search_Click(null, null); }
        }
        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
