using DevExpress.XtraBars;
using DeviceRepair.Models;
using DeviceRepair.Models.Enum;
using DeviceRepair.Models.Record;
using System;
using System.Collections.Generic;
using System.Linq;
using TsSFCDevice.Client.Biz.Base.Utils;
using TsSFCDevice.Client.Biz.Impl;
using TsSFCDevice.Client.Launch.Preserve;

namespace TsSFCDevice.Client.Launch.Plan
{
    public partial class pageRecordView : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private EnumDeviceBelong Belong = EnumDeviceBelong.PM;

        public List<MaintenanceRecordHistoryModel> Datas;
        public MaintenanceRecordHistoryModel CurrentModel
        {
            get
            {
                return cfMaintenanceRecord1.CurrentModel;
            }
        }

        public pageRecordView() : this(EnumDeviceBelong.PM)
        {
        }

        public pageRecordView(EnumDeviceBelong belong)
        {
            InitializeComponent();
            Belong = belong;
        }

        private void pageRecordView_Load(object sender, EventArgs e)
        {
            cfMaintenanceRecord1.DataSearch += CfMaintenanceRecord1_DataSearch;
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="filter"></param>
        private void CfMaintenanceRecord1_DataSearch(MaintenanceFilterModel filter)
        {
            try
            {
                if (filter.StartDate > filter.EndDate)
                    throw new Exception($"结束时间不允许大于开始时间!");

                if (filter.EquipmentName.Length > 200)
                    filter.EquipmentName = filter.EquipmentName.Substring(2, 200);

                Datas = PreserveRepository.Instance.Get_PreserveDetail_HisView(filter, Belong);
                this.cfMaintenanceRecord1.Datas = Datas;
                this.cfMaintenanceRecord1.RefreshGrid();
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barBtnDetail_ItemClick(object sender, ItemClickEventArgs e)
        {
            barBtnDetail.Enabled = false;
            try
            {
                if (Belong == EnumDeviceBelong.PM)
                {
                    if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.PM_PRESERVE_Watch))
                    {
                        throw new Exception($"当前账号缺少此操作的权限");
                    }
                }
                else if (Belong == EnumDeviceBelong.AM)
                {
                    if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.AM_PRESERVE_Watch))
                    {
                        throw new Exception($"当前账号缺少此操作的权限");
                    }
                }
                else
                {
                    throw new ArgumentException($"传入的保养类型(AM/PM)参数不正确！");
                }

                if (CurrentModel == null)
                {
                    throw new Exception("请先选择数据所在行！");
                }

                page_MaintenancePdfView view = new page_MaintenancePdfView("查看", new int[] { CurrentModel.PlanID }, false);
                view.ShowDialog(this);
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
            barBtnDetail.Enabled = true;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barBtnEdit_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (Belong == EnumDeviceBelong.PM)
                {
                    if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.PM_PRESERVE_Edit))
                    {
                        throw new Exception($"当前账号缺少此操作的权限");
                    }
                }
                else if (Belong == EnumDeviceBelong.AM)
                {
                    if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.AM_PRESERVE_Edit))
                    {
                        throw new Exception($"当前账号缺少此操作的权限");
                    }
                }
                else
                {
                    throw new ArgumentException($"传入的保养类型(AM/PM)参数不正确！");
                }

                if (CurrentModel == null)
                {
                    throw new Exception("请先选择数据所在行！");
                }

                DeviceInformationInfo dev = Utility.SystemRuntimeInfo.CurrentDeviceCaches.FirstOrDefault(x => x.AutoID == CurrentModel.EquipmentPrimaryID);

                page_DriveMaintenance view = new page_DriveMaintenance(dev, Belong, CurrentModel.PlanID, CurrentModel.AutoID, true);
                if (view.ShowDialog(this) == System.Windows.Forms.DialogResult.Abort)
                {
                    throw new Exception(view.apiResponseData.Message);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barBtnPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            barBtnPrint.Enabled = false;
            try
            {
                if (Belong == EnumDeviceBelong.PM)
                {
                    if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.PM_PRESERVE_Watch))
                    {
                        throw new Exception($"当前账号缺少此操作的权限");
                    }
                }
                else if (Belong == EnumDeviceBelong.AM)
                {
                    if (!Utility.SystemRuntimeInfo.AuthValidate(OperationAuthConstValue.AM_PRESERVE_Watch))
                    {
                        throw new Exception($"当前账号缺少此操作的权限");
                    }
                }
                else
                {
                    throw new ArgumentException($"传入的保养类型(AM/PM)参数不正确！");
                }

                if (CurrentModel == null)
                {
                    throw new Exception("请先选择数据所在行！");
                }

                page_MaintenancePdfView view = new page_MaintenancePdfView("打印", new int[] { CurrentModel.PlanID }, true);
                view.ShowDialog(this);
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
            barBtnPrint.Enabled = true;
        }
    }
}