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 DotNettyFrom.config;
using DotNettyFrom.db;
using DotNettyFrom.excel;
using DotNettyFrom.model;

namespace DotNettyFrom.form
{
    public partial class ItemManagerForm : Form
    {
        private int _SelectIndex = -1;

        public ItemManagerForm()
        {
            InitializeComponent();
        }

        private void ItemManagerForm_Load(object sender, EventArgs e)
        {
            CbFindType.SelectedIndex = 0;
            DataTable dataSource = DbHelper.GetInstance().SelectCmds();
            dataGridViewItem.DataSource = dataSource;
        }


        #region 事件

        private void dataGridViewItem_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int row = dataGridViewItem.RowCount;
            int x = e.RowIndex;
            if (x >= 0 && x < row)
            {
                object value = dataGridViewItem[0, x].Value;
                ItemEditorForm ief = new ItemEditorForm(value.ToString());
                ief.ItemHandler += Ief_ItemHandler;
                ief.ShowDialog();
            }
        }


        private void BtnAddCmd_Click(object sender, EventArgs e)
        {
            ItemEditorForm ief = new ItemEditorForm();
            ief.ItemHandler += Ief_ItemHandler;
            ief.ShowDialog();
        }

        private void Ief_ItemHandler(int action, string message)
        {
            RtbLogInfo.Text = message;
            ReLoadDatatable();
        }

        private void BtnDelSelectItem_Click(object sender, EventArgs e)
        {
            if (_SelectIndex >= 0)
            {
                int id = int.Parse(dataGridViewItem[0, _SelectIndex].Value + "");
                string hex = dataGridViewItem[2, _SelectIndex].Value + "";
                DialogResult dialogResult = MessageBox.Show("确定删除 Primary Key=" + id + " Hex=" + hex + "?", "提示",
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Warning);
                if (dialogResult == DialogResult.OK)
                {
                    int byId = DbHelper.GetInstance().DeleteCmdById(id);
                    if (byId > 0)
                    {
                        ReLoadDatatable();
                        RtbLogInfo.Text = "删除成功";
                    }
                }
            }
            else
            {
                RtbLogInfo.Text = "请重新选中";
            }
        }

        private void ReLoadDatatable()
        {
            DataTable dt = dataGridViewItem.DataSource as DataTable;
            dt.Clear();
            dt = DbHelper.GetInstance().SelectCmds();
            dataGridViewItem.DataSource = dt;
        }

        private void BtnRefresh_Click(object sender, EventArgs e)
        {
            DataTable dt = dataGridViewItem.DataSource as DataTable;
            dt.Clear();
            dt = DbHelper.GetInstance().SelectCmds();
            dataGridViewItem.DataSource = dt;
        }

        private void BtnReset_Click(object sender, EventArgs e)
        {
            int res = DbHelper.GetInstance().ResetUserData();

            RtbLogInfo.Text = res > 0 ? "重置数据成功" : "重置失败";
            ReLoadDatatable();
        }

        private void BtnImportExcel_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 ExcelHelper(openFileDialog.FileName, RtbLogInfo).LoadExcelFile()
                    .GetExcelList();
                InsertExcelList2DB(wvBeans);
            }

            this.DialogResult = DialogResult.None;
        }

        private void InsertExcelList2DB(List<WvBean> list)
        {
            for (var i = 0; i < list.Count; i++)
            {
                int res = DbHelper.GetInstance().InsertCmd(list[i]);
                if (res <= 0)
                {
                    RtbLogInfo.Text += "插入失败" + list[i].ToString() + "\n";
                }
            }
            ReLoadDatatable();
        }

        private void BtnClearAll_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult =
                MessageBox.Show("确定要清空所有数据?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dialogResult == DialogResult.OK)
            {
                //
                DbHelper.GetInstance().DeleteAllData();
                ReLoadDatatable();
            }
        }

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

        private void BtnFind_Click(object sender, EventArgs e)
        {
            string query = TBInputFind.Text.Trim();
            if (query.Equals(""))
            {
                ReLoadDatatable();
            }
            else
            {
//                ID - 10进制
//                ID - 16进制
//                名字 - IDInfo
//                描述 - Desp
                int mType = CbFindType.SelectedIndex;
                string table = DBConfig.GetTableName();
                string sql = "";
                switch (mType)
                {
                    case 0:
                        sql = @"select* from " + table + " where my_index = " + query;
                        break;
                    case 1:
                        sql = @"select* from " + table + " where MY_ID = '" + query + "'";
                        break;
                    case 2:
                        sql = @"select* from " + table + " where idInfo like '%" + query + "%'";
                        break;
                    case 3:
                        sql = @"select* from " + table + " where desp like '%" + query + "%'";
                        break;
                    default:
                        ReLoadDatatable();
                        return;
                }
                DataTable dt = dataGridViewItem.DataSource as DataTable;
                dt.Clear();
                dt = DbHelper.GetInstance().SelectCmds(sql);
                dataGridViewItem.DataSource = dt;
            }
        }

        #endregion

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

                if (x >= 0 && x < dataGridViewItem.RowCount)
                {
                    _SelectIndex = x;
                }
            }
        }
    }
}