using System;
using System.Windows.Forms;
using 贴标机标签打印.CustomControl;
using System.Drawing;
using System.Text;
using System.Data;

namespace 贴标机标签打印
{
    public partial class CodeSoftPrintParam : Form
    {
        public CodeSoftPrintParam()
        {
            InitializeComponent();
        }
        //模板参数的名称集合
        private string[] ParamName;
        //标签的客户编号ID
        private string Cl_ID;
        //Label表的主键,关联LabelParameter查询SQL
        private string LA_ID;

        private int LabelHeight;

        private int LabelWidth;
        //打开的标签文件
        private Document Doc;

        DataTable dt;

        DataHelper dh;

        StringBuilder sql = new StringBuilder();

        public CodeSoftPrintParam(string cl_id, string la_id, string[] paramname, Document doc)
        {
            InitializeComponent();
            ParamName = paramname;
            Cl_ID = cl_id;
            LA_ID = la_id;
            Doc = doc;
            LabelHeight = doc.Format.LabelHeight;
            LabelWidth = doc.Format.LabelWidth;
        }

        private void CodeSoftPrintParam_Load(object sender, EventArgs e)
        {
            dh = new DataHelper();
            dt = dh.getFieldsDatasByCondition("labelparameter", new string[] { "lp_id", "lp_name", "lp_sql", "lp_laid" }, "lp_laid=" + (LA_ID == "" ? "0" : LA_ID));
            int y = 100;
            for (int i = 0; i < ParamName.Length; i++)
            {
                //变量的名称Label
                Label Param = new Label();
                Param.Text = ParamName[i];
                Param.Anchor = AnchorStyles.Left;
                Param.Anchor = AnchorStyles.Top;
                Param.Location = new Point(200, y + 5);
                this.Controls.Add(Param);
                //维护变量的打印测试参数
                EnterTextBox TempParam = new EnterTextBox();
                TempParam.Anchor = AnchorStyles.Left;
                TempParam.Anchor = AnchorStyles.Top;
                TempParam.Name = "Temp" + ParamName[i];
                TempParam.Text = Doc.Variables.FreeVariables.Item(ParamName[i]).Value;
                TempParam.Location = new Point(300, y);
                y = y + 30;
                TempParam.Size = new Size(150, 22);
                this.Controls.Add(TempParam);
                //变量的SQL标签
                Label ParamSQL = new Label();
                ParamSQL.Text = ParamName[i] + "取值SQL";
                ParamSQL.Anchor = AnchorStyles.Left;
                ParamSQL.Anchor = AnchorStyles.Top;
                ParamSQL.AutoSize = false;
                ParamSQL.Location = new Point(200, y + 5);
                this.Controls.Add(ParamSQL);
                //维护SQL输入框  
                EnterTextBox SQL = new EnterTextBox();
                SQL.Anchor = AnchorStyles.Left;
                SQL.Anchor = AnchorStyles.Top;
                SQL.Name = ParamName[i];
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    if (dt.Rows[j]["lp_name"].ToString() == ParamName[i])
                    {
                        SQL.Text = dt.Rows[j]["lp_sql"].ToString();
                        //只要维护个这个参数就会有lp_laid这个属性,判断该SQL是否需要插入
                        SQL.Tag = dt.Rows[j]["lp_id"].ToString();
                    }
                }
                SQL.Location = new Point(300, y);
                this.Controls.Add(SQL);
                SQL.Size = new Size(250, 22);
                NormalButton Test = new NormalButton();
                Test.Text = "检测";
                Test.Tag = ParamName[i];
                Test.Click += CheckSQL;
                Test.Location = new Point(570, y);
                Test.Size = new Size(60, 22);
                this.Controls.Add(Test);
                y = y + 30;
            }
            NormalButton nb = new NormalButton();
            nb.Text = "保存";
            nb.Click += Save_Click;
            nb.Location = new Point(330, y + 10);
            nb.Size = new Size(80, 22);
            this.Controls.Add(nb);
            Height = y + 150;
        }

        private void Save_Click(object sender, EventArgs e)
        {
            //将标签信息插入Label表,将SQL和标签其他信息插入LabelParameter
            //如果此标签没有维护过则LA_ID字段的值为空
            string[] psql = new string[ParamName.Length];
            for (int i = 0; i < ParamName.Length; i++)
            {
                Doc.Variables.FreeVariables.Item(ParamName[i]).Value = "Temp" + Controls[ParamName[i]].Text;
                psql[i] = Controls[ParamName[i]].Text;
            }
            //未维护参数的时候从序列取一个新的ID
            if (LA_ID == "" || LA_ID == null)
            {
                string la_id = dh.GetSEQ("label_seq");
                dh.ExecuteSql("insert into label(la_id,la_code,la_name,la_indate,la_inman) select '" + la_id + "',cl_custcode,cu_name,sysdate,'管理员' from customerlabel left join customer on cu_code=cl_custcode where cl_id=" + Cl_ID, "insert");
                sql.Clear();
                sql.Append("insert into labelparameter (lp_id,lp_laid,lp_name,lp_software,lp_height,lp_width,lp_sql) ");
                sql.Append("values(labelparameter_seq.nextval,'" + la_id + "',:lp_name,'CodeSoft','" + LabelHeight + "','" + LabelWidth + "',:lp_sql)");
                //保存标签参数的值和获取需要插入的SQL的数组
                dh.BatchInsert(sql.ToString(), new string[] { "lp_name", "lp_sql" }, ParamName, psql);
            }
            //如果维护过变标签的时候就使用传进来的LA_ID作为参数
            if (LA_ID != "")
            {
                //存在ID直接更新的数据
                ArrayList<string> UpdateName = new ArrayList<string>();
                ArrayList<string> UpdateSQL = new ArrayList<string>();
                ArrayList<string> UpdateID = new ArrayList<string>();
                //不存在ID需要新插入的数据
                ArrayList<string> InsertName = new ArrayList<string>();
                ArrayList<string> InsertSQL = new ArrayList<string>();
                for (int i = 0; i < ParamName.Length; i++)
                {
                    //ID为空需要插入的数据
                    if (Controls[ParamName[i]].Tag == null || Controls[ParamName[i]].Tag.ToString() == "")
                    {
                        InsertName.Add(ParamName[i]);
                        InsertSQL.Add(Controls[ParamName[i]].Text);
                    }
                    //存在ID需要更新的数据
                    else {
                        UpdateName.Add(ParamName[i]);
                        UpdateSQL.Add(Controls[ParamName[i]].Text);
                        UpdateID.Add(Controls[ParamName[i]].Tag.ToString());
                    }
                }
                //保存标签参数的值和获取需要插入的SQL的数组
                sql.Clear();
                sql.Append("insert into labelparameter (lp_id,lp_laid,lp_name,lp_software,lp_height,lp_width,lp_sql) ");
                sql.Append("values(labelparameter_seq.nextval,'" + LA_ID + "',:lp_name,'CodeSoft','" + LabelHeight + "','" + LabelWidth + "',:lp_sql)");
                dh.BatchInsert(sql.ToString(), new string[] { "lp_name", "lp_sql" }, InsertName.ToArray(), InsertSQL.ToArray());
                //更新存在ID的参数
                sql.Clear();
                sql.Append("update labelparameter set lp_sql=:lp_sql , lp_name=:lp_name where lp_laid=:lp_laid");
                dh.BatchInsert(sql.ToString(), new string[] { "lp_sql", "lp_name", "lp_laid" }, UpdateSQL.ToArray(), UpdateName.ToArray(), UpdateID.ToArray());
            }
            Doc.Save();
            MessageBox.Show("保存成功");
        }

        //通过Tag获取到输入框的SQL,执行测试的方法
        private void CheckSQL(object sender, EventArgs e)
        {
            NormalButton nbt = sender as NormalButton;
            string ErrorMessage = "";
            if (dh.CheckSQL(this.Controls[nbt.Tag.ToString()].Text, out ErrorMessage))
            {
                MessageBox.Show("检测通过");
            }
            else
            {
                MessageBox.Show(ErrorMessage);
            }
        }
    }
}