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 FrmAddColumn : Skins.SKinForm
    {
        public string SQLConnStr { get; set; } = Itrycn_Db.SQLConnStr;
        public FrmAddColumn()
        {
            InitializeComponent();
        }
        public string tableName = "Columns";
        public int IsAdd { get; set; } = 1;
        public int SelectId { 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
                }
            }
            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)
            {
                Msg.ShowMsg("名称不能为空。", "提示", 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);
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(SQLConnStr) == 1)
            {
                if (IsAdd >= 1)
                {
                    mySQL.AddField("sortindex", Itrycn_Db.GetColumnCount(db, 0) + 1);
                    mySQL.AddField("addTime", DateTime.Now);
                    var ds= db.ReadData(mySQL.GetInsertSQL()+ ";select last_insert_rowid();", mySQL);
                    if(mydb.HaveData(ds))
                    {
                        var ParentId = mydb.GetValue(ds);
                        RyQuickSQL mySQL2 = new RyQuickSQL(tableName);
                        mySQL2.AddField("Name","默认分类");
                        mySQL2.AddField("Des", "");
                        mySQL2.AddField("editTime", DateTime.Now);
                        mySQL2.AddField("sortindex", Itrycn_Db.GetColumnCount(db, ParentId) + 1);
                        mySQL2.AddField("parentId", ParentId);
                        mySQL2.AddField("addTime", DateTime.Now);
                        db.ExecuteNonQuery(mySQL2.GetInsertSQL(), mySQL2);
                    }
                }
                else
                {
                    db.ExecuteNonQuery(mySQL.GetUpdateSQL() + " where id=" + SelectId, mySQL);
                }
            }
            db.Free();
            DialogResult = DialogResult.OK;
        }
    }
}
