using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using DeviceRepair.Models;
using DeviceRepairAndOptimization.Common;
using DeviceRepairAndOptimization.Biz;

namespace DeviceRepairAndOptimization.Pages.FormVersion
{
    public partial class Page_FormVersionDialog : XtraForm
    {
        public int SelectValue = 0;
        public string SelectText = string.Empty;

        public Page_FormVersionDialog(int AutoID)
        {
            InitializeComponent();
            SelectValue = AutoID;
        }

        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;
                }
            }

            if (hands.Length == 0)
            {
                if (XtraMessageBoxHelper.AskYesNo($"当前没有选择任何数据,请问是否继续？") == DialogResult.Yes)
                {
                    SelectValue = 0;
                    SelectText = "";
                    DialogResult = DialogResult.OK;
                    return;
                }
            }
            XtraMessageBoxHelper.Error($"选中项只能存在1行！");
        }

        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)
        {
            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
            {
                APIResponseData apiResponseData = FormManager.Instance.GetQuery("");//List<MaintenanceFormVersionInfo> result
                if (!apiResponseData.IsSuccess)
                    throw new Exception(apiResponseData.Message);

                if (apiResponseData.Data == null)
                {
                    XtraMessageBoxHelper.Error($"获取数据出错！");
                    return;
                }

                List<MaintenanceFormVersionInfo> result = apiResponseData.ToDeserializeObject<List<MaintenanceFormVersionInfo>>();
                List<MaintenanceFormVersionInfo> lst = result.Where(x => x.FormStatus == true)?.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.Info.Caption = "选择";
            }
        }

        /// <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);
                        }

                    }
                }
            }
        }
    }
}
