using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GWSocketClient.db;
using GWSocketClient.excel;
using GWSocketClient.model;

namespace GWSocketClient
{
    public partial class InfoEditor : Form
    {
        private int selectIndex = -1;

        public InfoEditor()
        {
            InitializeComponent();
        }

        private void InfoEditor_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = AccsessDbLoader.getInstance().getDataTable();
            cbItemType.SelectedIndex = 0;
        }


        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int col = dataGridView1.ColumnCount;
            int row = dataGridView1.RowCount;
            int x = e.RowIndex;
            int y = e.ColumnIndex;
            if (x >= 0 && y >= 0 && x < row && y < col)
            {
                WvBean wb = new WvBean();
                wb.id = int.Parse(dataGridView1[0, x].Value + "");
                wb.index = int.Parse(dataGridView1[1, x].Value + "");
                wb.myId = dataGridView1[2, x].Value + "";
                wb.idInfo = dataGridView1[3, x].Value + "";
                wb.len = dataGridView1[4, x].Value + "";
                wb.mode = dataGridView1[5, x].Value + "";
                wb.desp = dataGridView1[6, x].Value + "";
                wb.defalutValue = dataGridView1[7, x].Value + "";
                wb.userValue = dataGridView1[8, x].Value + "";
                wb.remark = dataGridView1[9, x].Value + "";

                IdItemEditor iie = new IdItemEditor(wb);
                iie.HandlerResult += Iie_HandlerResult;
                iie.ShowDialog();
            }
        }

        private void Iie_HandlerResult(string topmost)
        {
            InfoEditorTip.Text = topmost;
            dataGridView1.DataSource = AccsessDbLoader.getInstance().reloadAccessDb().getDataTable();
        }


        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int x = e.RowIndex;

                if (x >= 0 && x < dataGridView1.RowCount)
                {
                    selectIndex = x;
                }
            }
        }

        //选中数据
        private void buttonDeleteSelect_Click(object sender, EventArgs e)
        {
            if (selectIndex >= 0)
            {
                AccsessDbLoader.getInstance().delete4Db(int.Parse(dataGridView1[0, selectIndex].Value + ""));
                dataGridView1.DataSource = AccsessDbLoader.getInstance().reloadAccessDb().getDataTable();
            }
            else
            {
                InfoEditorTip.Text = "请重新选择一行数据";
            }
        }

        //刷新数据
        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = AccsessDbLoader.getInstance().reloadAccessDb().getDataTable();
        }

        //添加命令
        private void buttonAddCmd_Click(object sender, EventArgs e)
        {
            IdItemEditor iie = new IdItemEditor();
            iie.HandlerResult += Iie_HandlerResult;
            iie.ShowDialog();
        }

        private void btnResetUserdata_Click(object sender, EventArgs e)
        {
            int res = AccsessDbLoader.getInstance().resetUserData();
            InfoEditorTip.Text = res > 0 ? "重置数据成功" : "重置失败";
            dataGridView1.DataSource = AccsessDbLoader.getInstance().reloadAccessDb().getDataTable();

            this.DialogResult = DialogResult.None;
        }

        private void btnClosedThis_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLoadFromExcel_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Excel文件|*.xls;*.xlsx|所有文件|*.*";
            openFileDialog.InitialDirectory = Application.StartupPath;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                List<WvBean> wvBeans = new ExcelLoader(openFileDialog.FileName, InfoEditorTip).loadExcelFile()
                    .getExcelList();
                insertList2DB(wvBeans);
            }

            this.DialogResult = DialogResult.None;
        }

        private void insertList2DB(List<WvBean> list)
        {
            Console.WriteLine(list.Count + "");

            for (var i = 0; i < list.Count; i++)
            {
                int res = AccsessDbLoader.getInstance().insert2Db(list[i]);
                if (res <= 0)
                {
                    InfoEditorTip.Text += "插入失败" + list[i].ToString() + "\n";
                }
            }
            dataGridView1.DataSource = AccsessDbLoader.getInstance().reloadAccessDb().getDataTable();
        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("你确定要清空所有数据?", "确定清空", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Warning))
            {
                AccsessDbLoader.getInstance().deleteAllData();
                dataGridView1.DataSource = AccsessDbLoader.getInstance().reloadAccessDb().getDataTable();
            }
            this.DialogResult = DialogResult.None;
        }

        private void btnQueryItem_Click(object sender, EventArgs e)
        {
            int mType = cbItemType.SelectedIndex;
            dataGridView1.DataSource = AccsessDbLoader.getInstance().findDataByType(mType, tbInputData.Text.Trim())
                .getDataTable();

            this.DialogResult = DialogResult.None;
        }
    }
}