using ryCommon;
using ryCommonDb;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 开发辅助工具.Manager;

namespace 开发辅助工具.Tools
{
    public partial class FrmToolsSearch : Form
    {
        public FrmToolsSearch()
        {
            InitializeComponent();
        }
        string orderSQL = "order by sort desc, addTime desc";
        string tableName = "Tools";
        private void 添加工具ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Manager.FrmAddTools frm = new Manager.FrmAddTools()
            {
                Text = "添加工具",
                Icon = Icon,
                isAdd = 1
            };
            frm.mr = new ModalForm(this, frm);
            frm.mr.OnDialogResult += new ModalForm.DialogResultHandler((object t, DialogResult dg) => {
                if (dg == DialogResult.OK)
                {
                    rySearch1.PerformClick();
                }
            });
            frm.mr.ShowModal();
        }
        private void GetRow(string id, int index)
        {
            #region 重新载入数据
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.Sys_SQLConn) == 1)
            {
                DataSet ds = db.ReadData("select * from " + tableName + " where id=" + id);
                if (mydb.HaveData(ds))
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    XPTable.Models.Row itemList = tableModel1.Rows[index];
                    //需要修改此处
                    itemList.Cells[ColToolName.Index].Text = row["Name"].ToString();
                    itemList.Cells[ColToolDes.Index].Text = row["Des"].ToString();
                    itemList.Cells[ColKeys.Index].Text = row["Keys"].ToString();
                }
                db.Free();
            }
            #endregion
        }
        private void 修改工具ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (table1.SelectedItems.Length == 0) { return; }
            string selectId = table1.SelectedItems[0].Tag.ToString();
            int index = table1.SelectedItems[0].Index;
            Manager.FrmAddTools frm = new Manager.FrmAddTools()
            {
                Text = "修改工具",
                Icon = Icon,
                isAdd = 0
            };
            frm.GetInfo(selectId);
            frm.mr = new ModalForm(this, frm);
            frm.mr.OnDialogResult += new ModalForm.DialogResultHandler((object t, DialogResult dg) => {
                if (dg == DialogResult.OK)
                {
                    GetRow(selectId, index);
                }
            });
            frm.mr.ShowModal();
        }

        private void CtlMyPage1_OnPageChange(object sender, EventArgs e)
        {
            #region 重新载入数据
            tableModel1.Rows.Clear();
            tableModel1.Selections.Clear();
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.Sys_SQLConn) == 1)
            {
                DataSet ds = db.ReadData(ctlMyPage1.GetSQLText, ctlMyPage1.T_Parameters);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow row = ds.Tables[0].Rows[i];
                    XPTable.Models.Row itemList = new XPTable.Models.Row()
                    {
                        Tag = row["id"].ToString()
                    };
                    //需要修改此处
                    itemList.Cells.Add(new XPTable.Models.Cell(row["Name"].ToString()));//示例
                    itemList.Cells.Add(new XPTable.Models.Cell(row["Keys"].ToString()));
                    itemList.Cells.Add(new XPTable.Models.Cell(row["Des"].ToString()));
                    tableModel1.Rows.Add(itemList);
                }
                db.Free();
            }
            #endregion
        }
        public void LoadDb(string whereSQL)
        {
            string _whereSQL = whereSQL;
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.Sys_SQLConn) == 1)
            {
                #region 重新载入数据
                db.AddParameter("SearchText", "%" + rySearch1.Text + "%");
                ctlMyPage1.RecordCount = db.GetCount(tableName, _whereSQL);
                ctlMyPage1.T_Parameters = db.GetParameter();
                ctlMyPage1.SQLText = db.GetPageSQL(tableName, _whereSQL, orderSQL);
                ctlMyPage1.SQLText2 = db.GetPageSQL2(tableName, _whereSQL, orderSQL);
                ctlMyPage1.GotoPageIndex(1);
                db.Free();
                #endregion
            }
        }
        private void RySearch1_OnSearch(object sender, EventArgs e)
        {
            LoadDb("(Name like @SearchText or Des like @SearchText or Keys like @SearchText or PY like @SearchText)");
        }

        private void 删除工具ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (table1.SelectedItems.Length == 0) { MessageBox.Show("请先选择要删除的项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; }
            string selectId = table1.SelectedItems[0].Tag.ToString();
            if (MessageBox.Show("确定要删除该项吗？一旦删除将不可恢复。", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
            {
                return;
            }
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.Sys_SQLConn) == 1)
            {
                db.DelById(tableName, selectId);
                tableModel1.Rows.RemoveAt(table1.SelectedItems[0].Index);
            }
        }

        private void FrmToolsSearch_Load(object sender, EventArgs e)
        {
            rySearch1.PerformClick();
        }
    }
}
