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.SysConfigPages
{
    public partial class ShiftTimePage : UserControl
    {
        /// <summary>
        /// 班组时间业务实例
        /// </summary>
        SysShiftTimeService _sysShiftTimeService = SysShiftTimeService.Instance;

        /// <summary>
        /// 数据流转
        /// </summary>
        private List<SysShiftTimeEntity> _list;

        public ShiftTimePage()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            ShiftTimeDataGridView.AutoGenerateColumns = false;
            _list = _sysShiftTimeService.GetShiftInfos();
            ShiftTimeDataGridView.DataSource = null;
            ShiftTimeDataGridView.DataSource = _list;
        }

        private void RefreshShiftTime_Click(object sender, EventArgs e)
        {
            _list = _sysShiftTimeService.GetShiftInfos();
            ShiftTimeDataGridView.DataSource = null;
            ShiftTimeDataGridView.DataSource = _list;
        }

        private void UpdateShiftTimeButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认要修改班次信息？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }

            foreach (DataGridViewRow row in ShiftTimeDataGridView.Rows)
            {
                if (row.IsNewRow)
                {
                    continue;
                }


                SysShiftTimeEntity shiftTimeEntity = new SysShiftTimeEntity();

                if (row.Cells[1].Value.ToString().Length > 1 ||
                    row.Cells[2].Value.ToString().Length > 8 ||
                    row.Cells[3].Value.ToString().Length > 8 ||
                    row.Cells[4].Value.ToString().Length > 1
                    )
                {
                    MessageBox.Show("输入信息长度有误，班次内容、班次类型为单字，班次时间格式为 时时:分分:秒秒 \" 08:00:00 \"");
                    return;
                }


                    shiftTimeEntity.ShiftId = row.Cells[0].Value.ToString();
                shiftTimeEntity.ShiftName = row.Cells[1].Value.ToString();
                shiftTimeEntity.ShiftStartTime = row.Cells[2].Value.ToString();
                shiftTimeEntity.ShiftEndTime = row.Cells[3].Value.ToString();
                shiftTimeEntity.ShiftClass = row.Cells[4].Value.ToString();

                _sysShiftTimeService.UpdateShiftInfo(shiftTimeEntity);
            }

            _list = _sysShiftTimeService.GetShiftInfos();
            ShiftTimeDataGridView.DataSource = null;
            ShiftTimeDataGridView.DataSource = _list;
        }
    }
}
