//#define isSysEidt
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
namespace ryPrint.Mod
{
    public partial class frmModView : Form
    {
        public frmModView()
        {
            InitializeComponent();
        }
        string m_SearchText = "";
        string m_SQLWhereText = "";
        private void btnSearch_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            #if isSysEidt
               ryCommon.clsDb myDb = new ryCommon.clsDb(clsPram.SysDbPath, clsPram.DbPwd);
            #else
               ryCommon.clsDb myDb = new ryCommon.clsDb(clsPram.UserExpressDbPath, clsPram.DbPwd);
            #endif
            myDb.ConnOrCreateDb();
            SQLiteCommand cmd = myDb.SQLite_cn.CreateCommand();
            m_SearchText = txtSearch.Text;
            m_SearchText = m_SearchText.Replace(":", " ").Replace("：", " ");
            ryCommon.clsLiteDb cLite = new ryCommon.clsLiteDb();
            m_SQLWhereText = cLite.GetSearchSQL(Table.s_Express.Name, m_SearchText);
            m_SQLWhereText += " order by " + Table.s_Express.AddTime + " desc";
            cmd.CommandText = "select count(*) from " + Table.s_Express.TableName + " where " + m_SQLWhereText;
            ctlMyPage1.T_Parameters = cLite.GetParameters();
            cmd.Parameters.AddRange(ctlMyPage1.T_Parameters);
            SQLiteDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ctlMyPage1.RecordCount = Convert.ToInt32(reader[0].ToString());
                break;
            }
            reader.Close();
            ctlMyPage1.SQLText = "select * from " + Table.s_Express.TableName + " where " + m_SQLWhereText + " limit {pagesize} offset {recordnum}";
            ctlMyPage1.SQLText2 = "select * from " + Table.s_Express.TableName + " where " + m_SQLWhereText + " limit {pagesize} offset {recordnum}";
            cmd.Dispose();
            ctlMyPage1.GotoPageIndex(1);
        }
        public bool isChanged = false;
        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch.PerformClick();
            }
        }

        private void ctlMyPage1_OnPageChange(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            #if isSysEidt
                ryCommon.clsDb myDb = new ryCommon.clsDb(clsPram.SysDbPath, clsPram.DbPwd);
            #else
                ryCommon.clsDb myDb = new ryCommon.clsDb(clsPram.UserExpressDbPath, clsPram.DbPwd);
            #endif
            myDb.ConnOrCreateDb();
            SQLiteCommand cmd = myDb.SQLite_cn.CreateCommand();
            cmd.CommandText = ctlMyPage1.GetSQLText;
            cmd.Parameters.AddRange(ctlMyPage1.T_Parameters);
            listView1.Columns[0].Text = "名称(正在读取)";
            SQLiteDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ListViewItem item;
                item = listView1.Items.Add(reader[Table.s_Express.Name].ToString());
                item.Tag = reader["id"].ToString();
                item.SubItems.Add(reader[Table.s_Express.Des].ToString());
                item.SubItems.Add(reader[Table.s_Express.SysId].ToString()=="0"?"用户模板":"系统模板");
                item.ForeColor = reader[Table.s_Express.SysId].ToString() == "0" ? Color.Black : Color.Blue;
                switch(reader[Table.s_Express.billType].ToString())
                {
                    case "0":
                        item.SubItems.Add("快递单");
                        break;
                    case "1":
                        item.SubItems.Add("发货单");
                        break;
                    default:
                        item.SubItems.Add("未知");
                        break;
                }
            }
            reader.Close();
            listView1.Columns[0].Text = "名称(" + listView1.Items.Count.ToString() + ")";
            cmd.Dispose();
        }

        private void 添加快递模板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAddMod frm = new frmAddMod();
            frm.isAdd = 1;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                isChanged = true;
                btnSearch.PerformClick();
            }
        }

        private void 修改快递模板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) { return; }
            #if isSysEidt
                ryCommon.clsDb myDb = new ryCommon.clsDb(clsPram.SysDbPath, clsPram.DbPwd);
            myDb.ConnOrCreateDb();
            #else
            #region 数据库
            string sysId = "0";
            ryCommon.clsDb myDb = new ryCommon.clsDb(clsPram.UserExpressDbPath, clsPram.DbPwd);
            myDb.ConnOrCreateDb();
            SQLiteCommand cmd2 = myDb.SQLite_cn.CreateCommand();
            cmd2.CommandText = "select * from " + Table.s_Express.TableName + " where id=@id";
            cmd2.Parameters.Add("@id", DbType.Int32);    //id
            cmd2.Parameters["@id"].Value = listView1.SelectedItems[0].Tag.ToString();
            SQLiteDataReader reader2 = cmd2.ExecuteReader();
            while (reader2.Read())
            {
                sysId = reader2[Table.s_Express.SysId].ToString();
                break;
            }
            reader2.Close();
            cmd2.Dispose();
            if (sysId != "0")
            {
                MessageBox.Show("该模板是系统模板,您不能修改。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            #endregion
            #endif
            frmAddMod frm = new frmAddMod();
            frm.Text = "修改模板";
            frm.isAdd = 0;
            frm.selectId = listView1.SelectedItems[0].Tag.ToString();
            frm.GetDataInfoById(frm.selectId);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                isChanged = true;
                try
                {
                    SQLiteCommand cmd = myDb.SQLite_cn.CreateCommand();
                    cmd.CommandText = "select * from " + Table.s_Express.TableName + " where id=@id";
                    cmd.Parameters.Add("@id", DbType.Int32);    //id
                    cmd.Parameters["@id"].Value = frm.selectId;
                    SQLiteDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        listView1.SelectedItems[0].SubItems[colExpressName.Index].Text= reader[Table.s_Express.Name].ToString();
                        listView1.SelectedItems[0].SubItems[colExpressDes.Index].Text = reader[Table.s_Express.Des].ToString();
                        string billtype = "未知";
                        switch (reader[Table.s_Express.billType].ToString())
                        {
                            case "0":
                                billtype="快递单";
                                break;
                            case "1":
                                billtype="发货单";
                                break;
                            default:
                                billtype="未知";
                                break;
                        }
                        listView1.SelectedItems[0].SubItems[colBillType.Index].Text = billtype;
                        break;
                    }
                    reader.Close();
                    cmd.Dispose();
                }
                catch { }
            }
        }

        private void 删除快递模板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) { return; }
            #region 数据库
            string sysId = "0";
            try
            {
                ryCommon.clsDb myDb2 = new ryCommon.clsDb(clsPram.UserExpressDbPath, clsPram.DbPwd);
                myDb2.ConnOrCreateDb();
                SQLiteCommand cmd = myDb2.SQLite_cn.CreateCommand();
                cmd.CommandText = "select * from " + Table.s_Express.TableName + " where id=@id";
                cmd.Parameters.Add("@id", DbType.Int32);    //id
                cmd.Parameters["@id"].Value = listView1.SelectedItems[0].Tag.ToString();
                SQLiteDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    sysId = reader[Table.s_Express.SysId].ToString();
                    break;
                }
                reader.Close();
                cmd.Dispose();
            }
            catch { }
            if (sysId != "0")
            {
                MessageBox.Show("该模板是系统模板,您不能删除。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
           #endregion
            if (MessageBox.Show("确定要删除该模板吗？一旦删除将不可恢复。", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
            {
                return;
            }
            if (listView1.SelectedItems.Count == 0) { return; }
            ryCommon.clsDb myDb = new ryCommon.clsDb(clsPram.UserExpressDbPath, clsPram.DbPwd);
            myDb.ConnOrCreateDb();
            myDb.ExecuteNonQuery("delete from " + Table.s_Express.TableName + " where id=" + listView1.SelectedItems[0].Tag.ToString());
            listView1.SelectedItems[0].Remove();
            listView1.Columns[0].Text = "名称(" + listView1.Items.Count.ToString() + ")";
            isChanged = true;
        }

        private void frmModView_Load(object sender, EventArgs e)
        {
            #if isSysEidt
            Text += "(调试模式)";
            #endif
            btnSearch.PerformClick();
        }

        private void 复制快递模板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) { return; }
            frmAddMod frm = new frmAddMod();
            frm.Text = "复制并添加模板";
            frm.isAdd = 1;
            frm.selectId = listView1.SelectedItems[0].Tag.ToString();
            frm.GetDataInfoById(frm.selectId);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                isChanged = true;
                btnSearch.PerformClick();
            }
        }
    }
}
