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 ExtendUI.FTPManager
{
    internal partial class FrmAddFTP : Form
    {
        /// <summary>
        /// 
        /// </summary>
        public ModalForm mr = null;
        /// <summary>
        /// 
        /// </summary>
        public string tableName = "FTP";
        /// <summary>
        /// 
        /// </summary>
        public int isAdd = 1;
        /// <summary>
        /// 
        /// </summary>
        public string selectId = "-1";
        /// <summary>
        /// 使用的数据库接口
        /// </summary>
        public IDbInterface UseDb { get; set; } =null;
        /// <summary>
        /// 使用的数据库连接字符串
        /// </summary>
        public string UseSQLConn { get; set; } = "";
        /// <summary>
        /// 
        /// </summary>
        public FrmAddFTP()
        {
            InitializeComponent();
            CbbEncryption.SelectedIndex = 0;
        }
        #region 需要修改
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void GetInfo(string id)
        {
            selectId = id;
            IDbInterface db = UseDb;
            DataSet ds = db.ReadData(tableName, id);
            if (ds.HaveData())
            {
                DataRow reader = ds.Tables[0].Rows[0];
                #region 读取信息 
                TxtName.Text = reader["name"].ToString();
                TxtHost.Text = reader["ip"].ToString();
                TxtPort.Text = reader["port"].ToString();
                TxtRemoteDir.Text = reader["remoteDir"].ToString();
                TxtUserName.Text = reader["username"].ToString();
                TxtPwd.Text = reader["pwd"].ToString();
                CbbEncryption.SelectedIndex = reader["encrypt"].ToInt(0, 2, 0);
                TxtDes.Text = reader["des"].ToString();
                #endregion
            }
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if (TxtName.Text == "")
            {
                MessageBox.Show("名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            RyQuickSQL mySQL = new RyQuickSQL(tableName);
            mySQL.AddField("name", TxtName.Text);
            mySQL.AddField("ip", TxtHost.Text);
            mySQL.AddField("port", TxtPort.Text.ToInt());
            mySQL.AddField("remoteDir", TxtRemoteDir.Text);
            mySQL.AddField("username", TxtUserName.Text);
            mySQL.AddField("pwd", TxtPwd.Text);
            mySQL.AddField("encrypt", CbbEncryption.SelectedIndex);
            mySQL.AddField("des", TxtDes.Text);
            IDbInterface db = UseDb;
            if (isAdd >= 1)
            {
                mySQL.AddField("addTime", DateTime.Now);
                mySQL.AddField("editTime", DateTime.Now);
                db.ExecuteNonQuery(mySQL.GetInsertSQL(), db.GetParameter(mySQL));
            }
            else
            {
                mySQL.AddField("editTime", DateTime.Now);
                db.ExecuteNonQuery(mySQL.GetUpdateSQL() + " where id=" + selectId, db.GetParameter(mySQL));
            }
            ModalForm.SetDialogResult(this, mr, DialogResult.OK);
        } 
        #endregion

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