using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI.Relational;

namespace WindowsFormsApp1.Home
{
    public partial class Home_User_manage : Form
    {
        public string User_Name { get; internal set; }
        bool overlap = false;
        string[] tempData;
        Helper_DB _DB = new Helper_DB();
        Main main;
        string Table_User = "User_Data";
        int gridIndex;

        public Home_User_manage(Main _main)
        {
            InitializeComponent();
            main = _main;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            _DB.DBcon();
            tb_Affil.Enabled = false;
            _DB.DB_Select_Search("`affil`", Table_User, "");
            User_Manage();
            init_check();
        }
        /// <summary>
        /// DB에 저장된 사용자 데이터를 dataGridView1로 입력하는 함수.
        /// </summary>
        private void User_Manage()
        {
            string tmpstr = _DB.DB_Search("User_Data", "affil", "글로리아북");       // 글로리아북 -> data[3]
            string[] data = tmpstr.Split('|');
            int cout = 0;
            for (int a = 0; a < data.Length / 9; a++) { dataGridView1.Rows.Add(); }
            for (int a = 0; a < data.Length; a++)
            {
                if (a % 9 == 1) { dataGridView1.Rows[cout].Cells[0].Value = data[a]; }
                if (a % 9 == 3) { dataGridView1.Rows[cout].Cells[1].Value = data[a]; }
                if (a % 9 == 6) { dataGridView1.Rows[cout].Cells[2].Value = data[a]; }
                if (a % 9 == 7) { dataGridView1.Rows[cout].Cells[3].Value = data[a]; cout++; }
            }
        }
        /// <summary>
        /// 권한 설정 0일경우 체크없음, 1일경우 활성화만, 2일경우 활성화와 수정체크
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e == null) { gridIndex = 0; }
            else { gridIndex = e.RowIndex; }
            string value = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            string tmpstr = _DB.DB_Search(Table_User, "id", value);
            tempData = tmpstr.Split('|');
            tb_ID.Text = tempData[1];
            tb_PW.Text = tempData[2];
            tb_Name.Text = tempData[3];
            tb_Affil.Text = tempData[5];
            tb_position.Text = tempData[6];
            tb_Phone.Text = tempData[7];
            string[] real = tempData[4].Split(',');
            {
                if (real[0] == "0") { check1_1.Checked = false; check1_2.Checked = false; }
                else if (real[0] == "1") { check1_1.Checked = true; check1_2.Checked = false; }
                else if (real[0] == "2") { check1_1.Checked = true; check1_2.Checked = true; }

                if (real[1] == "0") { check2_1.Checked = false; check2_2.Checked = false; }
                else if (real[1] == "1") { check2_1.Checked = true; check2_2.Checked = false; }
                else if (real[1] == "2") { check2_1.Checked = true; check2_2.Checked = true; }

                if (real[2] == "0") { check3_1.Checked = false; check3_2.Checked = false; }
                else if (real[2] == "1") { check3_1.Checked = true; check3_2.Checked = false; }
                else if (real[2] == "2") { check3_1.Checked = true; check3_2.Checked = true; }

                if (real[3] == "0") { check4_1.Checked = false; check4_2.Checked = false; }
                else if (real[3] == "1") { check4_1.Checked = true; check4_2.Checked = false; }
                else if (real[3] == "2") { check4_1.Checked = true; check4_2.Checked = true; }

                if (real[4] == "0") { check5_1.Checked = false; check5_2.Checked = false; }
                else if (real[4] == "1") { check5_1.Checked = true; check5_2.Checked = false; }
                else if (real[4] == "2") { check5_1.Checked = true; check5_2.Checked = true; }

                if (real[5] == "0") { check6_1.Checked = false; check6_2.Checked = false; }
                else if (real[5] == "1") { check6_1.Checked = true; check6_2.Checked = false; }
                else if (real[5] == "2") { check6_1.Checked = true; check6_2.Checked = true; }

                if (real[6] == "0") { check7_1.Checked = false; check7_2.Checked = false; }
                else if (real[6] == "1") { check7_1.Checked = true; check7_2.Checked = false; }
                else if (real[6] == "2") { check7_1.Checked = true; check7_2.Checked = true; }

                if (real[7] == "0") { check8_1.Checked = false; check8_2.Checked = false; }
                else if (real[7] == "1") { check8_1.Checked = true; check8_2.Checked = false; }
                else if (real[7] == "2") { check8_1.Checked = true; check8_2.Checked = true; }

                if (real[8] == "0") { check9_1.Checked = false; check9_2.Checked = false; }
                else if (real[8] == "1") { check9_1.Checked = true; check9_2.Checked = false; }
                else if (real[8] == "2") { check9_1.Checked = true; check9_2.Checked = true; }

                if (real[9] == "0") { check10_1.Checked = false; check10_2.Checked = false; }
                else if (real[9] == "1") { check10_1.Checked = true; check10_2.Checked = false; }
                else if (real[9] == "2") { check10_1.Checked = true; check10_2.Checked = true; }

                if (real[10] == "0") { check11_1.Checked = false; check11_2.Checked = false; }
                else if (real[10] == "1") { check11_1.Checked = true; check11_2.Checked = false; }
                else if (real[10] == "2") { check11_1.Checked = true; check11_2.Checked = true; }

                if (real[11] == "0") { check12_1.Checked = false; check12_2.Checked = false; }
                else if (real[11] == "1") { check12_1.Checked = true; check12_2.Checked = false; }
                else if (real[11] == "2") { check12_1.Checked = true; check12_2.Checked = true; }

                if (real[12] == "0") { check13_1.Checked = false; check13_2.Checked = false; }
                else if (real[12] == "1") { check13_1.Checked = true; check13_2.Checked = false; }
                else if (real[12] == "2") { check13_1.Checked = true; check13_2.Checked = true; }

                if (real[13] == "0") { check14_1.Checked = false; check14_2.Checked = false; }
                else if (real[13] == "1") { check14_1.Checked = true; check14_2.Checked = false; }
                else if (real[13] == "2") { check14_1.Checked = true; check14_2.Checked = true; }

                if (real[14] == "0") { check15_1.Checked = false; check15_2.Checked = false; }
                else if (real[14] == "1") { check15_1.Checked = true; check15_2.Checked = false; }
                else if (real[14] == "2") { check15_1.Checked = true; check15_2.Checked = true; }

                if (real[15] == "0") { check16_1.Checked = false; check16_2.Checked = false; }
                else if (real[15] == "1") { check16_1.Checked = true; check16_2.Checked = false; }
                else if (real[15] == "2") { check16_1.Checked = true; check16_2.Checked = true; }

                if (real[16] == "0") { check17_1.Checked = false; check17_2.Checked = false; }
                else if (real[16] == "1") { check17_1.Checked = true; check17_2.Checked = false; }
                else if (real[16] == "2") { check17_1.Checked = true; check17_2.Checked = true; }

                if (real[17] == "0") { check18_1.Checked = false; check18_2.Checked = false; }
                else if (real[17] == "1") { check18_1.Checked = true; check18_2.Checked = false; }
                else if (real[17] == "2") { check18_1.Checked = true; check18_2.Checked = true; }

                if (real[18] == "0") { check19_1.Checked = false; check19_2.Checked = false; }
                else if (real[18] == "1") { check19_1.Checked = true; check19_2.Checked = false; }
                else if (real[18] == "2") { check19_1.Checked = true; check19_2.Checked = true; }

                if (real[19] == "0") { check20_1.Checked = false; check20_2.Checked = false; }
                else if (real[19] == "1") { check20_1.Checked = true; check20_2.Checked = false; }
                else if (real[19] == "2") { check20_1.Checked = true; check20_2.Checked = true; }
            }   // 열면 후회함 (권한 나눈거 체크체크)
            init_check();
        }
        private void check1_1_Click(object sender, EventArgs e)
        {
            init_check();
        }
        private void btn_IDOverlap_Click(object sender, EventArgs e)
        {
            string check = _DB.DB_Search(Table_User, "id", tb_ID.Text);
            if (check == "") { MessageBox.Show("사용가능한 아이디입니다. [" + tb_ID.Text + "]"); overlap = true; }
            else { MessageBox.Show("중복된 아이디입니다. [" + tb_ID.Text + "]"); overlap = false; }
        }
        private void All_Check(object sender, EventArgs e)
        {
            bool yesorno = false;
            if (((CheckBox)sender).Checked == true) { yesorno = true; }
            else if (((CheckBox)sender).Checked == false) { yesorno = false; }
            ALL_Check(sender, yesorno);
        }
        private void btn_Save_Click(object sender, EventArgs e)
        {
            string right = Return_right();
            User_Manage();
            _DB.DB_Update(Table_User, "right", right, "ID", tempData[1]);   //(Table_User, "right", right, tempData[1]);
        }
        private void btn_Add_Click(object sender, EventArgs e)
        {
            if (tb_ID.Text == "" || tb_PW.Text == "" || tb_Name.Text == "")
            {
                MessageBox.Show("성명 / 아이디 / 비밀번호 를 채워주세요.");
            }
            else if (overlap == true)
            {
                string right = Return_right();
                string create_DB = "INSERT INTO User_Data" +
                    "(`ID`, `PW`, `name`, `right`, `affil`, `position`, `Phone`, `grade`)" +
                    "values('" + tb_ID.Text + "', '" + tb_PW.Text + "', '" + tb_Name.Text + "', '" + right + "',";
                if (_DB.chk_comp(tb_Affil.Text) == true) {
                    MessageBox.Show("DB에 등록되지 않은 업체입니다.\n관리자에게 연락바랍니다.", "Error");
                    return;
                }
                if (tb_Affil.Text == "") { create_DB += "null,"; }
                else { create_DB += "'" + tb_Affil.Text + "',"; }
                if (tb_position.Text == "") { create_DB += "null,"; }
                else { create_DB += "'" + tb_position.Text + "',"; }
                if (tb_Phone.Text == "") { create_DB += "null,"; }
                else { create_DB += "'" + tb_Phone.Text + "',"; }
                if (check_Away.Checked == true) { create_DB += "'외부업체');"; }
                else { create_DB += "null);"; }
                _DB.self_Made_Cmd(create_DB);
                overlap = false;
                User_Manage();
            }
            else { MessageBox.Show("아이디 중복확인을 해주세요"); }
        }
        private void btn_Del_Click(object sender, EventArgs e)
        {
            _DB.DB_Delete(Table_User, "id", tempData[0], "PW", tempData[1]);
            dataGridView1.Rows.Remove(dataGridView1.Rows[gridIndex]);
        }
        /// <summary>
        /// 체크박스로 권한(right)값 구하기
        /// </summary>
        /// <returns></returns>
        private string Return_right()
        {
            string right = "";
            {
                if (check1_1.Checked == false) { right += "0,"; }
                else if (check1_1.Checked == true && check1_2.Checked == false) { right += "1,"; }
                else if (check1_1.Checked == true && check1_2.Checked == true) { right += "2,"; }

                if (check2_1.Checked == false) { right += "0,"; }
                else if (check2_1.Checked == true && check2_2.Checked == false) { right += "1,"; }
                else if (check2_1.Checked == true && check2_2.Checked == true) { right += "2,"; }

                if (check3_1.Checked == false) { right += "0,"; }
                else if (check3_1.Checked == true && check3_2.Checked == false) { right += "1,"; }
                else if (check3_1.Checked == true && check3_2.Checked == true) { right += "2,"; }

                if (check4_1.Checked == false) { right += "0,"; }
                else if (check4_1.Checked == true && check4_2.Checked == false) { right += "1,"; }
                else if (check4_1.Checked == true && check4_2.Checked == true) { right += "2,"; }

                if (check5_1.Checked == false) { right += "0,"; }
                else if (check5_1.Checked == true && check5_2.Checked == false) { right += "1,"; }
                else if (check5_1.Checked == true && check5_2.Checked == true) { right += "2,"; }

                if (check6_1.Checked == false) { right += "0,"; }
                else if (check6_1.Checked == true && check6_2.Checked == false) { right += "1,"; }
                else if (check6_1.Checked == true && check6_2.Checked == true) { right += "2,"; }

                if (check7_1.Checked == false) { right += "0,"; }
                else if (check7_1.Checked == true && check7_2.Checked == false) { right += "1,"; }
                else if (check7_1.Checked == true && check7_2.Checked == true) { right += "2,"; }

                if (check8_1.Checked == false) { right += "0,"; }
                else if (check8_1.Checked == true && check8_2.Checked == false) { right += "1,"; }
                else if (check8_1.Checked == true && check8_2.Checked == true) { right += "2,"; }

                if (check9_1.Checked == false) { right += "0,"; }
                else if (check9_1.Checked == true && check9_2.Checked == false) { right += "1,"; }
                else if (check9_1.Checked == true && check9_2.Checked == true) { right += "2,"; }

                if (check10_1.Checked == false) { right += "0,"; }
                else if (check10_1.Checked == true && check10_2.Checked == false) { right += "1,"; }
                else if (check10_1.Checked == true && check10_2.Checked == true) { right += "2,"; }

                if (check11_1.Checked == false) { right += "0,"; }
                else if (check11_1.Checked == true && check11_2.Checked == false) { right += "1,"; }
                else if (check11_1.Checked == true && check11_2.Checked == true) { right += "2,"; }

                if (check12_1.Checked == false) { right += "0,"; }
                else if (check12_1.Checked == true && check12_2.Checked == false) { right += "1,"; }
                else if (check12_1.Checked == true && check12_2.Checked == true) { right += "2,"; }

                if (check13_1.Checked == false) { right += "0,"; }
                else if (check13_1.Checked == true && check13_2.Checked == false) { right += "1,"; }
                else if (check13_1.Checked == true && check13_2.Checked == true) { right += "2,"; }

                if (check14_1.Checked == false) { right += "0,"; }
                else if (check14_1.Checked == true && check14_2.Checked == false) { right += "1,"; }
                else if (check14_1.Checked == true && check14_2.Checked == true) { right += "2,"; }

                if (check15_1.Checked == false) { right += "0,"; }
                else if (check15_1.Checked == true && check15_2.Checked == false) { right += "1,"; }
                else if (check15_1.Checked == true && check15_2.Checked == true) { right += "2,"; }

                if (check16_1.Checked == false) { right += "0,"; }
                else if (check16_1.Checked == true && check16_2.Checked == false) { right += "1,"; }
                else if (check16_1.Checked == true && check16_2.Checked == true) { right += "2,"; }

                if (check17_1.Checked == false) { right += "0,"; }
                else if (check17_1.Checked == true && check17_2.Checked == false) { right += "1,"; }
                else if (check17_1.Checked == true && check17_2.Checked == true) { right += "2,"; }

                if (check18_1.Checked == false) { right += "0,"; }
                else if (check18_1.Checked == true && check18_2.Checked == false) { right += "1,"; }
                else if (check18_1.Checked == true && check18_2.Checked == true) { right += "2,"; }

                if (check19_1.Checked == false) { right += "0,"; }
                else if (check19_1.Checked == true && check19_2.Checked == false) { right += "1,"; }
                else if (check19_1.Checked == true && check19_2.Checked == true) { right += "2,"; }

                if (check20_1.Checked == false) { right += "0"; }
                else if (check20_1.Checked == true && check20_2.Checked == false) { right += "1"; }
                else if (check20_1.Checked == true && check20_2.Checked == true) { right += "2"; }
            }       // 체크박스로 권한값구하기
            return right;
        }
        /// <summary>
        /// 체크박스 초기화
        /// </summary>
        private void init_check()
        {
            if (check1_1.Checked == false) { check1_2.Enabled = false; }
            else if (check1_1.Checked == true) { check1_2.Enabled = true; }
            if (check2_1.Checked == false) { check2_2.Enabled = false; }
            else if (check2_1.Checked == true) { check2_2.Enabled = true; }
            if (check3_1.Checked == false) { check3_2.Enabled = false; }
            else if (check3_1.Checked == true) { check3_2.Enabled = true; }
            if (check4_1.Checked == false) { check4_2.Enabled = false; }
            else if (check4_1.Checked == true) { check4_2.Enabled = true; }
            if (check5_1.Checked == false) { check5_2.Enabled = false; }
            else if (check5_1.Checked == true) { check5_2.Enabled = true; }
            if (check6_1.Checked == false) { check6_2.Enabled = false; }
            else if (check6_1.Checked == true) { check6_2.Enabled = true; }
            if (check7_1.Checked == false) { check7_2.Enabled = false; }
            else if (check7_1.Checked == true) { check7_2.Enabled = true; }
            if (check8_1.Checked == false) { check8_2.Enabled = false; }
            else if (check8_1.Checked == true) { check8_2.Enabled = true; }
            if (check9_1.Checked == false) { check9_2.Enabled = false; }
            else if (check9_1.Checked == true) { check9_2.Enabled = true; }
            if (check10_1.Checked == false) { check10_2.Enabled = false; }
            else if (check10_1.Checked == true) { check10_2.Enabled = true; }
            if (check11_1.Checked == false) { check11_2.Enabled = false; }
            else if (check11_1.Checked == true) { check11_2.Enabled = true; }
            if (check12_1.Checked == false) { check12_2.Enabled = false; }
            else if (check12_1.Checked == true) { check12_2.Enabled = true; }
            if (check13_1.Checked == false) { check13_2.Enabled = false; }
            else if (check13_1.Checked == true) { check13_2.Enabled = true; }
            if (check14_1.Checked == false) { check14_2.Enabled = false; }
            else if (check14_1.Checked == true) { check14_2.Enabled = true; }
            if (check15_1.Checked == false) { check15_2.Enabled = false; }
            else if (check15_1.Checked == true) { check15_2.Enabled = true; }
            if (check16_1.Checked == false) { check16_2.Enabled = false; }
            else if (check16_1.Checked == true) { check16_2.Enabled = true; }
            if (check17_1.Checked == false) { check17_2.Enabled = false; }
            else if (check17_1.Checked == true) { check17_2.Enabled = true; }
            if (check18_1.Checked == false) { check18_2.Enabled = false; }
            else if (check18_1.Checked == true) { check18_2.Enabled = true; }
            if (check19_1.Checked == false) { check19_2.Enabled = false; }
            else if (check19_1.Checked == true) { check19_2.Enabled = true; }
            if (check20_1.Checked == false) { check20_2.Enabled = false; }
            else if (check20_1.Checked == true) { check20_2.Enabled = true; }
        }
        /// <summary>
        /// 일괄체크
        /// </summary>
        /// <param name="sender">이벤트 sender 그대로 가져와야함</param>
        /// <param name="chk">true or false</param>
        private void ALL_Check(object sender, bool chk)
        {
            string target = ((CheckBox)sender).Name;
            { 
                if (target == "ALL_check1" && chk == true)
                {
                    check1_1.Checked = true;
                    check2_1.Checked = true;
                    check3_1.Checked = true;
                    check4_1.Checked = true;
                    check5_1.Checked = true;
                    check6_1.Checked = true;
                    check7_1.Checked = true;
                    check8_1.Checked = true;
                    check9_1.Checked = true;
                    check10_1.Checked = true;
                    check11_1.Checked = true;
                    check12_1.Checked = true;
                    check13_1.Checked = true;
                    check14_1.Checked = true;
                    check15_1.Checked = true;
                    check16_1.Checked = true;
                    check17_1.Checked = true;
                    check18_1.Checked = true;
                    check19_1.Checked = true;
                    check20_1.Checked = true;
                }
                else if (target == "ALL_check1" && chk == false)
                {
                    check1_1.Checked = false;
                    check2_1.Checked = false;
                    check3_1.Checked = false;
                    check4_1.Checked = false;
                    check5_1.Checked = false;
                    check6_1.Checked = false;
                    check7_1.Checked = false;
                    check8_1.Checked = false;
                    check9_1.Checked = false;
                    check10_1.Checked = false;
                    check11_1.Checked = false;
                    check12_1.Checked = false;
                    check13_1.Checked = false;
                    check14_1.Checked = false;
                    check15_1.Checked = false;
                    check16_1.Checked = false;
                    check17_1.Checked = false;
                    check18_1.Checked = false;
                    check19_1.Checked = false;
                    check20_1.Checked = false;
                }
                else if (target == "ALL_check2" && chk == true)
                {
                    check1_2.Checked = true;
                    check2_2.Checked = true;
                    check3_2.Checked = true;
                    check4_2.Checked = true;
                    check5_2.Checked = true;
                    check6_2.Checked = true;
                    check7_2.Checked = true;
                    check8_2.Checked = true;
                    check9_2.Checked = true;
                    check10_2.Checked = true;
                    check11_2.Checked = true;
                    check12_2.Checked = true;
                    check13_2.Checked = true;
                    check14_2.Checked = true;
                    check15_2.Checked = true;
                    check16_2.Checked = true;
                    check17_2.Checked = true;
                    check18_2.Checked = true;
                    check19_2.Checked = true;
                    check20_2.Checked = true;
                }
                else if (target == "ALL_check2" && chk == false)
            {
                check1_2.Checked = false;
                check2_2.Checked = false;
                check3_2.Checked = false;
                check4_2.Checked = false;
                check5_2.Checked = false;
                check6_2.Checked = false;
                check7_2.Checked = false;
                check8_2.Checked = false;
                check9_2.Checked = false;
                check10_2.Checked = false;
                check11_2.Checked = false;
                check12_2.Checked = false;
                check13_2.Checked = false;
                check14_2.Checked = false;
                check15_2.Checked = false;
                check16_2.Checked = false;
                check17_2.Checked = false;
                check18_2.Checked = false;
                check19_2.Checked = false;
                check20_2.Checked = false;
            }
            }
            init_check();
        }
    }
}