using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HighWayIot.Winform.UserControlPages.ParamConfigPages
{
    public partial class RFIDParamSettingPage : UserControl
    {
        private ZxReaderSettingService zx_rfid_reader_settingService = ZxReaderSettingService.Instance;

        private ZxTagSettingService zx_rfid_tag_settingService = ZxTagSettingService.Instance;

        public RFIDParamSettingPage()
        {
            InitializeComponent();
            RefreshReaderGridView();
            RefreshTagGridView();
            ReaderDataGridView.AutoGenerateColumns = false;
            TagDataGridView.AutoGenerateColumns = false;
        }

        /// <summary>
        /// 刷新读写器前端页面
        /// </summary>
        private void RefreshReaderGridView()
        {
            List<ZxReaderSettingEntity> list = zx_rfid_reader_settingService.GetReaderInfos();

            ReaderDataGridView.DataSource = null;
            ReaderDataGridView.DataSource = list;
        }

        /// <summary>
        /// 刷新标签前端页面
        /// </summary>
        private void RefreshTagGridView()
        {
            List<ZxTagSettingEntity> list = zx_rfid_tag_settingService.GetTagInfos();

            TagDataGridView.DataSource = null;
            TagDataGridView.DataSource = list;
        }

        /// <summary>
        /// 查询读写器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetReaderButton_Click(object sender, EventArgs e)
        {
            RefreshReaderGridView();
        }

        /// <summary>
        /// 查询RFID
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectRfidButton_Click(object sender, EventArgs e)
        {
            RefreshTagGridView();
        }

        /// <summary>
        /// 更改读写器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateReaderButton_Click(object sender, EventArgs e)
        {
            List<ZxReaderSettingEntity> lists = new List<ZxReaderSettingEntity>();
            DataGridViewRowCollection rows = ReaderDataGridView.Rows;
            foreach (DataGridViewRow row in rows)
            {
                ZxReaderSettingEntity entity = new ZxReaderSettingEntity()
                {
                    Id = int.Parse(row.Cells["Id"].Value.ToString()),
                    RfidIp = row.Cells["RfidIp"].Value.ToString(),
                    WorkstationNo = row.Cells["WorkstationNo"].Value.ToString()
                };
                lists.Add(entity);
            }

            zx_rfid_reader_settingService.UpdateRangeReaderInfo(lists);

            RefreshReaderGridView();

            MessageBox.Show("读写器信息修改成功");
        }

        /// <summary>
        /// 更改标签
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateRFIDButton_Click(object sender, EventArgs e)
        {
            List<ZxTagSettingEntity> lists = new List<ZxTagSettingEntity>();
            DataGridViewRowCollection rows = TagDataGridView.Rows;
            foreach (DataGridViewRow row in rows)
            {
                ZxTagSettingEntity entity = new ZxTagSettingEntity()
                {
                    Id = int.Parse(row.Cells["RfidId"].Value.ToString()),
                    RfidEpc = row.Cells["RfidEpc"].Value.ToString(),
                    DeviceNo = row.Cells["DeviceNo"].Value.ToString()
                };
                lists.Add(entity);
            }

            zx_rfid_tag_settingService.UpdateRangeTagInfo(lists);

            RefreshReaderGridView();

            MessageBox.Show("标签信息修改成功");
        }
    }
}
