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 DevExpress.XtraBars.ToolbarForm;
using DeviceRepairAndOptimization.Models;
using DeviceRepairAndOptimization.Business;
using DevExpress.XtraGrid.Views.Tile;
using DeviceRepairAndOptimization.Utils;
using DeviceRepairAndOptimization.Pages.DriveMaintenance;
using DeviceRepairAndOptimization.Data;

namespace DeviceRepairAndOptimization.Pages.DialogControl
{
    public partial class page_PlansDialog : ToolbarForm
    {
        private List<View_CurrentMonthPlanTips> lst;

        public page_PlansDialog()
        {
            InitializeComponent();
            this.Load += Page_PlansDialog_Load;
        }

        private void Page_PlansDialog_Load(object sender, EventArgs e)
        {
            BindDatas();
        }

        /// <summary>
        /// 数据获取并绑定
        /// </summary>
        void BindDatas()
        {
            APIResponseData apiResponseData = PlanManager.Instance.GetCurrentMonthPlanTips();
            lst = apiResponseData.ToDeserializeObject<List<View_CurrentMonthPlanTips>>();
            gridControl1.DataSource = lst;
        }

        private void btn_Ignore_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void tileView1_ContextButtonClick(object sender, DevExpress.Utils.ContextItemClickEventArgs e)
        {
            //当前选中对象
            View_CurrentMonthPlanTips currentEntity = (View_CurrentMonthPlanTips)tileView1.GetRow(((TileViewItem)e.DataItem).RowHandle);

            switch (e.Item.Name)
            {
                case "btn_Maintenance":
                    //保养一条
                    break;
                default:
                    // 忽略一条
                    ItemIgore(currentEntity);
                    break;
            }
        }


        /// <summary>
        /// 忽略该条保修集合
        /// </summary>
        /// <param name="item"></param>
        void ItemIgore(View_CurrentMonthPlanTips entity)
        {
            lst.Remove(entity);
            tileView1.RefreshData();
            gridControl1.RefreshDataSource();
        }

        /// <summary>
        /// 保养一条数据
        /// </summary>
        /// <param name="item"></param>
        void ItemMaintenence(View_CurrentMonthPlanTips entity)
        {
            try
            {
                if (!ApiHelper.Instance.RoleExistsAuth("BIZ_MAINTENANCE_ADD").ToBool())
                {
                    XtraMessageBoxHelper.Error($"当前账号缺少此操作的权限");
                    return;
                }

                this.Hide();

                if (entity.FormAutoID == 0)
                {
                    XtraMessageBox.Show("该设备未绑定点检表,请先绑定点检表。", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Show();
                    return;
                }

                // 通过点检表主键ID获取点检表并保存到缓存目录
                string templatePath = MaintenanceFormVersionMaintenance.Instance.GetFileSingle(entity.FormAutoID);
                if (string.IsNullOrWhiteSpace(templatePath))
                {
                    throw new Exception("获取点检表文件失败,请联系管理员处理！");
                }

                DeviceManager.Instance.DeviceQuerySingle(entity.EquipmentAutoID);


                //page_DriveMaintenance view = new page_DriveMaintenance(entity, entity.AutoID);
                //DialogResult dr = view.ShowDialog();
                //if (dr == DialogResult.OK)
                //{
                //    ItemIgore(item);
                //    XtraMessageBoxHelper.Info("操作成功！");
                //}
                //else if (dr == DialogResult.Abort)
                //{
                //    XtraMessageBoxHelper.Info(view.result.Message);
                //}
                this.Show();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Show();
            }
        }
    }
}