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 VSoft.Prams;

namespace VSoft
{
    public partial class FrmAddType : Form
    {
        public string SQLConnStr { get; set; } = Itrycn_Db.SQLConnStr;
        public FrmAddType()
        {
            InitializeComponent();
        }
        public string tableName = "Columns";
        public int IsAdd { get; set; } = 1;
        public int SelectId { get; set; } = -1;
        public int ParentId { get; set; } = -1;
        public void GetInfo(int id)
        {
            SelectId = id;
            Text = "修改分类";
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(SQLConnStr) == 1)
            {
                DataSet ds = db.ReadData(tableName, id.ToString());
                if (mydb.HaveData(ds))
                {
                    DataRow reader = ds.Tables[0].Rows[0];
                    #region 读取信息     
                    TxtName.Text = reader["Name"].ToString();
                    TxtDes.Text = reader["Des"].ToString();
                    #endregion
                }
                ds.Dispose();
            }
            db.Free();
        }
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if (TxtName.Text.Length == 0)
            {
                MessageBox.Show("名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            RyQuickSQL mySQL = new RyQuickSQL(tableName);
            mySQL.AddField("Name", TxtName.Text);
            mySQL.AddField("Des", TxtDes.Text);
            mySQL.AddField("editTime", DateTime.Now);
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(SQLConnStr) == 1)
            {
                if (IsAdd >= 1)
                {
                    mySQL.AddField("sortindex", Itrycn_Db.GetColumnCount(db, ParentId) + 1);
                    mySQL.AddField("parentId", ParentId);
                    mySQL.AddField("addTime", DateTime.Now);
                    db.ExecuteNonQuery(mySQL.GetInsertSQL(), mySQL);
                }
                else
                {
                    db.ExecuteNonQuery(mySQL.GetUpdateSQL() + " where id=" + SelectId, mySQL);
                }
            }
            db.Free();
            DialogResult = DialogResult.OK;
        }
    }
}
