using CowainHmi.Helper;
using PCHMI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace CowainHmi.Common
{
    public partial class DlgEditPlcValue : Form
    {
        private PlcTagColumn tag;
        public DlgEditPlcValue(PlcTagColumn tag)
        {
            InitializeComponent();
            this.txtTag.小数位数 = (uint)tag.DecimalPlaces;
            this.txtTag.字符串长度 = (uint)tag.StringLength;
            this.txtTag.数据类型 = (数据显示器.DatType)tag.DatType;
            this.txtTag.Text = tag.VarValue;
            this.tag = tag;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("确定要修改变量吗？", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result != DialogResult.Yes)
            {
                return;
            }
            if (tag.DatType == 字段.DatType.INT32 || tag.DatType == 字段.DatType.UINT32 || tag.DatType == 字段.DatType.UINT16 || tag.DatType == 字段.DatType.INT16)
            {
                if (int.TryParse(this.txtTag.Text, out int val))
                {
                    this.tag.VarValue = tag.DecimalPlaces > 0 ? (val * tag.DecimalPlaces * 10).ToString() : val.ToString();
                }

            }
            else
            {
                this.tag.VarValue = this.txtTag.Text;
            }

            PlcValueHelper.SetValue(this.tag);
            this.DialogResult = DialogResult.OK;
        }
    }
}
