using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DeviceRepair.Models;
using DeviceRepair.Utils;
using DeviceRepairAndOptimization.Common;
using DeviceRepair.Models.Device;
using DeviceRepairAndOptimization.Pages.Maintenance;
using DeviceRepairAndOptimization.Pages.DriveMaintenance;

namespace DeviceRepairAndOptimization.Pages.Plan
{
    public partial class pageDevicePlans : XtraForm
    {
        int m_SelectedCurrentRowIndex = 0;
        int CurrentEquipmentID = 0;
        int CurrentYear = 0;
        DeviceAnnPlanView CurrentData = null;

        public pageDevicePlans(int EquipmentID, int Year)
        {
            InitializeComponent();

            CurrentEquipmentID = EquipmentID;
            CurrentYear = Year;

            this.Load += PageDevicePlans_Load;
        }

        private void PageDevicePlans_Load(object sender, EventArgs ee)
        {
            try
            {
                APIResponseData apiResponseData = Biz.PlanManager.Instance.GetDeviceInformationPlans(CurrentYear, CurrentEquipmentID);
                if (!apiResponseData.IsSuccess)
                    throw new Exception(apiResponseData.Message);

                CurrentData = apiResponseData.ToDeserializeObject<DeviceAnnPlanView>();

                foreach (DriveMaintencePlanInfo item in CurrentData.Plans)
                {
                    MaintenanceRecordInfo record = CurrentData.Records?.FirstOrDefault(x => x.PlanPrimaryID == item.AutoID);
                    item.CompleteDate = record?.CreateDate ?? null;
                    item.EquipmentDisplayID = CurrentData.Dev.EquipmentID;
                    item.FormDisplayCode = CurrentData.CurrentFormCode;
                }

                gridView1.IndicatorWidth = 50;
                gridControl1.DataSource = CurrentData.Plans;
                gridView1.BestFitColumns();

                // 自增长行号
                gridView1.CustomDrawRowIndicator += (s, e) =>
                {
                    if (e.Info.IsRowIndicator && e.RowHandle >= 0)
                    {
                        e.Info.DisplayText = (e.RowHandle + 1).ToString();
                        e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                        e.Appearance.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                        SizeF size = e.Graphics.MeasureString((e.RowHandle + 1).ToString(), e.Appearance.Font);
                    }
                };
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
                this.Close();
            }

        }


        private void repositoryItemButtonEdit1_Click(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            try
            {


                m_SelectedCurrentRowIndex = gridView1.FocusedRowHandle;
                if (!gridView1.IsValidRowHandle(m_SelectedCurrentRowIndex))
                {
                    throw new Exception("请先选择计划所在行！");
                }

                DriveMaintencePlanInfo CurrentSelectRowData = gridView1.GetRow(m_SelectedCurrentRowIndex) as DriveMaintencePlanInfo;
                if (CurrentSelectRowData == null)
                {
                    throw new Exception("请先选择计划所在行！");
                }

                if (e.Button.Caption.Equals("查看"))
                {
                    if (!GlobalInfo.HasRole("BIZ_MAINTENANCE_LOG_VIEW"))
                    {
                        throw new Exception($"当前账号缺少此操作的权限");
                    }

                    if (CurrentSelectRowData.CompleteDate.HasValue)
                    {
                        MaintenanceRecordInfo record = CurrentData.Records?.FirstOrDefault(x => x.PlanPrimaryID == CurrentSelectRowData.AutoID);
                        if (record == null)
                            throw new Exception("未查询到设备保养记录信息，请重试！");
                        page_MaintenancePdfView view = new page_MaintenancePdfView("查看", new int[] { record.AutoID }, false);
                        view.ShowDialog();
                    }
                    else
                    {
                        throw new Exception("当前计划不存在保养信息！");
                    }
                }
                else if (e.Button.Caption.Equals("保养记录修改"))
                {
                    if (!GlobalInfo.HasRole("BIZ_MAINTENANCE_EDIT"))
                    {
                        throw new Exception($"当前账号缺少此操作的权限");
                    }
                    MaintenanceRecordInfo record = CurrentData.Records?.FirstOrDefault(x => x.PlanPrimaryID == CurrentSelectRowData.AutoID);
                    if (record == null)
                        throw new Exception("未查询到设备保养记录信息，请重试！");
                    page_DriveMaintenance view = new page_DriveMaintenance(CurrentData.Dev, CurrentSelectRowData.AutoID, record.AutoID, true);

                    if (view.ShowDialog() == DialogResult.OK)
                    {
                        XtraMessageBoxHelper.Info("操作成功!");
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(view.apiResponseData.Message))
                            throw new Exception(view.apiResponseData.Message);
                    }
                }
                else if (e.Button.Caption.Equals("设备保养"))
                {
                    if (CurrentSelectRowData.CompleteDate.HasValue)
                    {
                        throw new Exception("当前计划已保养！");
                    }

                    if (!GlobalInfo.HasRole("BIZ_MAINTENANCE_ADD"))
                    {
                        throw new Exception($"当前账号缺少此操作的权限");
                    }

                    page_DriveMaintenance view = new page_DriveMaintenance(CurrentData.Dev, CurrentSelectRowData.AutoID);
                    if (view.ShowDialog() == DialogResult.OK)
                    {
                        XtraMessageBoxHelper.Info("操作成功!");
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(view.apiResponseData.Message))
                            throw new Exception(view.apiResponseData.Message);
                    }
                }
                else if (e.Button.Caption.Equals("打印"))
                {
                    if (CurrentSelectRowData.CompleteDate.HasValue)
                    {
                        MaintenanceRecordInfo record = CurrentData.Records?.FirstOrDefault(x => x.PlanPrimaryID == CurrentSelectRowData.AutoID);
                        page_MaintenancePdfView view = new page_MaintenancePdfView("打印", new int[] { record.AutoID }, true);
                        view.ShowDialog();
                    }
                    else
                    {
                        throw new Exception("当前计划不存在保养信息！");
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }
    }
}