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;

namespace 开发辅助工具.Manager
{
    public partial class FrmAddTools : Form
    {
        public FrmAddTools()
        {
            InitializeComponent();
        }
        public ModalForm mr = null;
        public string tableName = "Tools";
        public int isAdd = 1;
        public string selectId = "-1";

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            ModalForm.SetDialogResult(this, mr, DialogResult.Cancel);
        }

        #region 需要修改
        public void GetInfo(string id)
        {
            selectId = id;
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.Sys_SQLConn) == 1)
            {
                DataSet ds = db.ReadData(tableName, id);
                if (mydb.HaveData(ds))
                {
                    DataRow reader = ds.Tables[0].Rows[0];
                    #region 读取信息               
                    TxtName.Text = reader["Name"].ToString();
                    TxtKeys.Text = reader["Keys"].ToString();
                    TxtToolAddr.Text = reader["Addr"].ToString();
                    TxtToolDes.Text = reader["Des"].ToString();
                    numericUpDownEx1.Value = reader["Sort"].ToDecimal();
                    #endregion
                }
            }
            db.Free();
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if (TxtName.Text == "")
            {
                MessageBox.Show("名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            RyQuickSQL mySQL = new RyQuickSQL(tableName);
            mySQL.AddField("Name", TxtName.Text);
            mySQL.AddField("Keys", TxtKeys.Text);
            mySQL.AddField("Addr", TxtToolAddr.Text);
            mySQL.AddField("Des", TxtToolDes.Text);
            mySQL.AddField("Sort", numericUpDownEx1.Value.ToInt());
            mySQL.AddField("PY",  Chinese2Spell.Convert(TxtName.Text + "\n" + TxtKeys.Text)+"\n"+ Chinese2Spell.ConvertFirstPY(TxtName.Text + "\n" + TxtKeys.Text));
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.Sys_SQLConn) == 1)
            {
                if (isAdd >= 1)
                {
                    db.ExecuteNonQuery(mySQL.GetInsertSQL(), db.GetParameter(mySQL));
                }
                else
                {
                    db.ExecuteNonQuery(mySQL.GetUpdateSQL() + " where id=" + selectId, db.GetParameter(mySQL));
                }
            }
            ModalForm.SetDialogResult(this, mr, DialogResult.OK);
        }
        #endregion
    }
}
