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 FrmFTPView : Form
    {
        readonly string orderSQL = "order by addTime desc";
        readonly string tableName = "FTP";
        readonly string titleName = "FTP管理";
        /// <summary>
        /// 使用的数据库接口
        /// </summary>
        public IDbInterface UseDb { get; set; } =null;
        /// <summary>
        /// 
        /// </summary>
        public FrmFTPView()
        {
            InitializeComponent();
            OlvName.AspectGetter = delegate (object x) { return ((FTPInfo)x).Name; };
            OlvIP.AspectGetter = delegate (object x) { return ((FTPInfo)x).IP; };
            OlvPort.AspectGetter = delegate (object x) { return ((FTPInfo)x).Port; };
            OlvUserName.AspectGetter = delegate (object x) { return ((FTPInfo)x).UserName; };
            OlvAddTime.AspectGetter = delegate (object x) { return ((FTPInfo)x).AddTimeStr; };
        }
        #region 需要修改的内容
        private void RySearch1_OnSearch(object sender, EventArgs e)
        {
            //需要修改此处
            LoadDb("(name like @SearchText or des like @SearchText)");
        }

        private void CtlMyPage1_OnPageChange(object sender, EventArgs e)
        {
            #region 重新载入数据
            LvCbResult.ClearObjects();
            List<FTPInfo> list = new List<FTPInfo>();
            IDbInterface db = UseDb;
            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];
                list.Add(new FTPInfo()
                {
                    Id = row["id"].ToInt(),
                    Name = row["name"].ToString(),
                    IP = row["ip"].ToString(),
                    Port = row["port"].ToInt(),
                    RemoteDir = row["remoteDir"].ToString(),
                    UserName = row["username"].ToString(),
                    Pwd = row["pwd"].ToString(),
                    Encrypt = row["encrypt"].ToInt(0, 2, 0),
                    AddTime = row["AddTime"].ToDateTime()
                });
            }
            LvCbResult.AddObjects(list);
            #endregion
        }
        private void GetRow(int id, int index)
        {
            #region 重新载入数据
            DataProvider mydb = new DataProvider();
            IDbInterface db =UseDb;
            DataSet ds = db.ReadData("select * from " + tableName + " where id=" + id);
            if (mydb.HaveData(ds))
            {
                DataRow row = ds.Tables[0].Rows[0];
                var item = (FTPInfo)LvCbResult.GetModelObject(index);
                item.Name = row["name"].ToString();
                item.IP = row["ip"].ToString();
                item.Port = row["port"].ToInt();
                item.RemoteDir = row["remoteDir"].ToString();
                item.UserName = row["username"].ToString();
                item.Pwd = row["pwd"].ToString();
                item.Encrypt = row["encrypt"].ToInt(0, 2, 0);
                //添加剩余的项目
                //添加完毕
                LvCbResult.UpdateObject(item);
            }
            #endregion
        }
        #endregion
        private void LoadDb(string whereSQL)
        {
            string _whereSQL = whereSQL;
            IDbInterface db = UseDb;
            #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);
            #endregion
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            FrmAddFTP frm = new FrmAddFTP()
            {
                Text = "添加"+ titleName,
                Icon = Icon,
                isAdd = 1,
                UseDb=UseDb
            };
            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 BtnEdit_Click(object sender, EventArgs e)
        {
            if (LvCbResult.SelectedObject == null) { return; }
            var item = (FTPInfo)LvCbResult.SelectedObject;
            var selectId = item.Id;
            FrmAddFTP frm = new FrmAddFTP()
            {
                Text = "修改"+ titleName,
                Icon = Icon,
                isAdd = 0,
                UseDb = UseDb
            };
            frm.GetInfo(selectId.ToString());
            frm.mr = new ModalForm(this, frm);
            frm.mr.OnDialogResult += new ModalForm.DialogResultHandler((object t, DialogResult dg) => {
                if (dg == DialogResult.OK)
                {
                    GetRow(selectId, LvCbResult.SelectedIndex);
                }
            });
            frm.mr.ShowModal();
        }

        private void BtnDel_Click(object sender, EventArgs e)
        {
            if (LvCbResult.SelectedObject == null) { MessageBox.Show("请先选择要删除的项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; }
            var item = (FTPInfo)LvCbResult.SelectedObject;
            var selectId = item.Id;
            switch (MessageBox.Show("确定要删除该项吗？一旦删除将不可恢复。", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
            {
                case DialogResult.No:
                    return;
            }
            //DataProvider mydb = new DataProvider();
            IDbInterface db =UseDb;
            db.DelById(tableName, selectId.ToString());
            LvCbResult.RemoveObject(item);
        }

        private void FrmView_Load(object sender, EventArgs e)
        {
            Text = "浏览"+ titleName;
            添加数据ToolStripMenuItem.Text = "添加"+ titleName;
            修改数据ToolStripMenuItem.Text = "修改" + titleName;
            删除数据ToolStripMenuItem.Text = "删除" + titleName;
            rySearch1.PerformClick();
        }

        private void 添加数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnAdd.PerformClick();
        }

        private void 修改数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnEdit.PerformClick();
        }

        private void 删除数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnDel.PerformClick();
        }

        private void 复制并添加数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LvCbResult.SelectedObject == null) { return; }
            var item = (FTPInfo)LvCbResult.SelectedObject;
            var selectId = item.Id;
            FrmAddFTP frm = new FrmAddFTP()
            {
                Text = "添加" + titleName,
                Icon = Icon,
                isAdd = 1,
                UseDb=UseDb
            };
            frm.GetInfo(selectId.ToString());
            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();
        }
        public bool SelectModeOn { get; set; } = false;
        public FTPInfo SelectedItem { get; set; }
        private void LvCbResult_DoubleClick(object sender, EventArgs e)
        {
            if(SelectModeOn)
            {
                if (LvCbResult.SelectedObject == null) { return; }
                SelectedItem = (FTPInfo)LvCbResult.SelectedObject;
                DialogResult=DialogResult.OK;
            }
        }
    }
}
