using System;
using DevExpress.XtraEditors;
using UAS_PLCDataReader.Entity;
using System.Data;
using UAS_PLCDataReader.DataOperate;
using UAS_PLCDataReader.PublicMethod;

namespace UAS_PLCDataReader.Device.Command
{
    public partial class FormNewCommand : XtraForm
    {

        string brand = "";

        public FormNewCommand()
        {
            InitializeComponent();
        }

        public FormNewCommand(string BrandCode, string BrandName)
        {
            InitializeComponent();
            brand = BrandCode;
            Text = "新增指令(" + BrandName + ")";
        }

        /// <summary>
        /// 设置下拉框的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormNewCommand_Load(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)SystemInf.dh.ExecuteSql("select * from CommandFunctionSetting where cfs_debrand='" + brand + "' order by cfs_id ", "select");
            BaseUtil.FillComBoxEditWidthDataTable(CodeClientAddress, "cfs_name", "cfs_value", BaseUtil.ToDataTable(dt.Select("cfs_type='CA'")));
            BaseUtil.FillComBoxEditWidthDataTable(CodeFunction, "cfs_name", "cfs_value", BaseUtil.ToDataTable(dt.Select("cfs_type='CF'")));
            BaseUtil.FillComBoxEditWidthDataTable(CodeComplement, "cfs_name", "cfs_value", BaseUtil.ToDataTable(dt.Select("cfs_type='CC'")));
            BaseUtil.FillComBoxEditWidthDataTable(CodeEnd, "cfs_name", "cfs_value", BaseUtil.ToDataTable(dt.Select("cfs_type='CE'")));
        }

        /// <summary>
        /// 插入生成的指令,根据选择的功能码+地址位+长度+补码+结束码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddCommand_Click(object sender, EventArgs e)
        {
            string code = BaseUtil.GetComboxEditValue(CodeClientAddress) + BaseUtil.GetComboxEditValue(CodeFunction) + CodeAddress.Text + CodeLength.Text;
            //添加指令的补码
            code = code + BaseUtil.getLRC(code);
            code = code + BaseUtil.GetComboxEditValue(CodeEnd);
            SystemInf.dh.ExecuteSql("insert into devicecommand(dc_id,dc_name,dc_code,dc_command,dc_brand) values (devicecommand_seq.nextval,'" + dc_name.Text + "','" + dc_code.Text + "','" + code + "','" + brand + "')", "insert");
            XtraMessageBox.Show("指令保存成功");
            Close();
        }
    }
}