using DevExpress.XtraBars;
using DeviceRepair.Models;
using DeviceRepair.Models.Record;
using DeviceRepairAndOptimization.Common;
using DeviceRepairAndOptimization.Pages.DriveMaintenance;
using DeviceRepairAndOptimization.Pages.Maintenance;
using System;
using System.Collections.Generic;

namespace DeviceRepairAndOptimization.Pages.AM.Preserve
{
    public partial class pageRecordView : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public List<MaintenanceRecordHistoryModel> Datas;
        public MaintenanceRecordHistoryModel CurrentModel
        {
            get
            {
                return cfMaintenanceRecord1.CurrentModel;
            }
        }

        public pageRecordView()
        {
            //cfMaintenanceRecord1.filterModel = filter;
            InitializeComponent();
        }

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

                APIResponseData apiResponseData = Biz.PreserveManager.Instance.GetRecordHisView(filter);
                if (!apiResponseData.IsSuccess)
                {
                    throw new Exception(apiResponseData.Message);
                }

                Datas = apiResponseData.ToDeserializeObject<List<MaintenanceRecordHistoryModel>>();
                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 (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_PRESERVE_LOG_AM_01))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

                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 (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_PRESERVE_LOG_AM_02))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

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

                //DeviceInformationInfo
                APIResponseData apiResponseData = Biz.DeviceManager.Instance.GetDataByAutoID(CurrentModel.EquipmentPrimaryID);
                if (!apiResponseData.IsSuccess)
                {
                    throw new Exception(apiResponseData.Message);
                }

                DeviceInformationInfo dev = apiResponseData.ToDeserializeObject<DeviceInformationInfo>();
                page_DriveMaintenance view = new page_DriveMaintenance(dev, 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 (!GlobalInfo.HasRole(AuthCodeConstValue.BIZ_PRESERVE_LOG_AM_03))
                {
                    throw new Exception($"当前账号缺少此操作的权限");
                }

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