using DevExpress.XtraEditors;
using DeviceRepair.Models;
using DeviceRepair.Models.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using TsSFCDevice.Client.Biz.Impl;

namespace TsSFCDevice.Client.Launch.Device
{
    public partial class Page_FormVersionDialog : XtraForm
    {
        public int SelectValue = 0;
        public string SelectText = string.Empty;

        private EnumDeviceBelong Belong = EnumDeviceBelong.PM;

        public Page_FormVersionDialog() : this(0)
        {

        }

        public Page_FormVersionDialog(int AutoID, EnumDeviceBelong belong = EnumDeviceBelong.PM)
        {
            InitializeComponent();
            SelectValue = AutoID;
            Belong = belong;
        }

        private void btn_Submit_Click(object sender, EventArgs e)
        {
            int[] hands = gridView1.GetSelectedRows();
            if (hands.Length == 1)
            {
                MaintenanceFormVersionInfo entity = (MaintenanceFormVersionInfo)gridView1.GetRow(hands[0]);
                if (entity != null && entity.AutoID > 0)
                {
                    SelectValue = entity.AutoID;
                    SelectText = entity.VersionCode + "-" + entity.VersionRev;
                    DialogResult = DialogResult.OK;
                    return;
                }
            }
            else if (hands.Length == 0)
            {
                if (XtraMessageBoxHelper.AskYesNo($"当前没有选择任何数据,请问是否继续？") == DialogResult.Yes)
                {
                    SelectValue = 0;
                    SelectText = "";
                    DialogResult = DialogResult.OK;
                    return;
                }
            }
            else if (hands.Length > 1)
            {
                XtraMessageBoxHelper.Error($"选中项只能存在1行！");
                return;
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// 程序加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Page_FormVersionDialog_Load(object sender, EventArgs e)
        {
            // 关闭列头右键菜单
            gridView1.OptionsMenu.EnableColumnMenu = false;

            BindDatas();

            if (SelectValue != 0)
            {
                // 遍历每一行，找到符合条件的行并选择
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    int v = 0;

                    if (int.TryParse(gridView1.GetRowCellValue(i, "AutoID") + "", out v) && v == SelectValue)
                    {
                        gridView1.SelectRow(i);
                        break; // 可以选择中断循环，如果只需要选择第一行
                    }
                }
            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private void BindDatas()
        {
            try
            {
                List<MaintenanceFormVersionInfo> result = CheckFormRepository.Instance.GetDatas()?.ToList();
                List<MaintenanceFormVersionInfo> lst = result.Where(x => x.FormStatus == true && (x.FormBelong ?? "").Trim().Equals(Belong.ToString().Trim(), StringComparison.OrdinalIgnoreCase))?.ToList();
                gridControl1.DataSource = lst;
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }

        }

        private void gridView1_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
        {
            if (e.Column != null && (e.Column.Caption == "Selection" || e.Column.FieldName == "DX$CheckboxSelectorColumn"))
            {
                e.Info.Caption = "选择";
                e.Appearance.Font = new System.Drawing.Font("Tahoma", 12F);
            }
        }

        /// <summary>
        /// 选择一行数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Column.Caption == "Selection")
            {
                if (gridView1.SelectedRowsCount > 0)
                {
                    for (int i = 0; i < this.gridView1.RowCount; i++)
                    {
                        if (e.Clicks == 1 && this.gridView1.IsRowSelected(i) && this.gridView1.FocusedRowHandle.ToString().Equals(i.ToString()) == false)
                        {
                            this.gridView1.UnselectRow(i);
                        }

                    }
                }
            }
        }
    }
}
