using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CowainHmi
{
    public partial class ucRfidCtrl : UserControl
    {
        private string[] nameList;
        [Category("PCHMI"), Description("RFID名称列表")]
        public string[] NameList
        {
            get
            {
                return this.nameList;
            }
            set
            {
                this.nameList = value;
            }
        }

        private string idAddr = "";
        [Category("PCHMI"), Description("编号地址")]
        public string IdAddr
        {
            get
            {
                return idAddr;
            }
            set
            {
                idAddr = value;
            }
        }


        private string db = "DB900";
        [Category("PCHMI"), Description("DB地址")]
        public string DataAddr
        {
            get
            {
                return db;
            }
            set
            {
                db = value;
            }
        }

        public ucRfidCtrl()
        {
            InitializeComponent();
        }
        private int id = 0;
        private string rfidName;
        private void ProgramAdds()
        {
            id = this.cbxCySelect.SelectedIndex;
            rfidName = this.cbxCySelect.SelectedText;
            this.lblTp.指示灯地址 = $"{DataAddr}.{id * 22 + 12}.0";
            this.lblRd.指示灯地址 = $"{DataAddr}.{id * 22 + 12}.1";
            this.lblErr.指示灯地址 = $"{DataAddr}.{id * 22 + 12}.4";
            this.lblReadDone.指示灯地址 = $"{DataAddr}.{id * 22 + 12}.3";
            this.lblWriteDone.指示灯地址 = $"{DataAddr}.{id * 22 + 12}.2";
            this.txtReadTime.监控地址 = $"{DataAddr}.{id * 22 + 18}";
            this.txtWriteTime.监控地址 = $"{DataAddr}.{id * 22 + 20}";

            this.txtStartAdd.监控地址 = $"{DataAddr}.{id * 22 + 0}";
            this.txtLength.监控地址 = $"{DataAddr}.{id * 22 + 2}";
            this.txtLength.互锁.互锁地址 = $"{DataAddr}.{id * 22 + 10}.2";
            this.btnRead.开关功能.地址[0] = $"{DataAddr}.{id * 22 + 10}.0";
            this.btnRead.指示灯地址 = $"{DataAddr}.{id * 22 + 10}.0";
            this.btnWrite.开关功能.地址[0] = $"{DataAddr}.{id * 22 + 10}.1";
            this.btnWrite.指示灯地址 = $"{DataAddr}.{id * 22 + 10}.1";
            this.swAutoLength.开关功能.地址[0] = $"{DataAddr}.{id * 22 + 10}.2";
            this.swAutoLength.监控地址 = $"{DataAddr}.{id * 22 + 10}.2";


        }



        private void ucPressCtrl_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(IdAddr))
            {
                return;
            }
            id = PCHMI.VL.GET_UINT16(0, IdAddr) - 1;//读气缸编号
            if (id < 0)
            {
                PCHMI.RT.SEND_UINT16(0, IdAddr, 1);
                id = 0;
            }
            this.cbxCySelect.DataSource = NameList;
            this.cbxCySelect.SelectedIndexChanged += CbxRfidSelect_SelectedIndexChanged;
            //ProgramAdds();
            this.cbxCySelect.SelectedIndex = id;
        }

        private async void CbxRfidSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            ProgramAdds();
            string addr = $"{DataAddr}.{id * 22 + 4}";
            PCHMI.VL.GET_INT16(0, addr);
            await Task.Delay(300);
            int dbno = PCHMI.VL.GET_INT16(0, addr);//读数据db值
            if (dbno > 0)
            {
                this.dgvData.DB = dbno;
            }
           
        }

    }
}
