using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Tile;
using DeviceRepair.Models;
using DeviceRepairAndOptimization.Biz;
using DeviceRepairAndOptimization.Common;
using DeviceRepairAndOptimization.Pages.DriveMaintenance;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace DeviceRepairAndOptimization.Pages
{
    public partial class page_PlanDialog : Form
    {
        public List<View_CurrentMonthPlanTips> Datas { get; set; }

        public page_PlanDialog()
        {
            SetStyle(ControlStyles.UserPaint, true);
            InitializeComponent();
        }

        /// <summary>
        /// 程序加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void page_PlanDialog_Load(object sender, EventArgs e)
        {
            DataBind();
            //if (Datas.IsNullOrZero())
            //    this.Close();
            //gridControl1.DataSource = Datas;
        }

        void DataBind()
        {
            #region 数据库处理

            APIResponseData apiResponseData = PlanManager.Instance.GetCurrentMonthPlanTips();
            List<View_CurrentMonthPlanTips> lst = apiResponseData.ToDeserializeObject<List<View_CurrentMonthPlanTips>>();
            Datas = lst;

            #endregion

            //如果tips数据为空则跳过
            if (lst == null || lst.Count == 0)
                this.DialogResult = DialogResult.OK;

            gridControl1.DataSource = Datas;
        }

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

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {
            Rectangle tang = this.ClientRectangle; //获取窗口矩形 为了下面得到窗口的宽高
            Graphics g3 = e.Graphics; //新建一个画布
            Color c3 = Color.FromArgb(183, 183, 183); //声明一个 颜色
            Pen p3 = new Pen(c3); //新建一支画笔
            g3.SmoothingMode = SmoothingMode.HighQuality; //抗锯齿 使得线条变柔顺  在画斜线或者曲线的时候使用
            g3.InterpolationMode = InterpolationMode.HighQualityBicubic; //使得画出来的效果高质量
            g3.CompositingQuality = CompositingQuality.HighQuality; //高质量画图
            g3.DrawLine(p3, 0, 0, 0, tang.Height - 1); //在（0，0）和（tang.Width - 1, 0）这两点间画一条直线
            g3.DrawLine(p3, 0, tang.Height - 1, tang.Width - 1,
                tang.Height -
                1); //注意必须减1 不然显示不出来  因为 如果假设窗口的高度是3像素 我们知道（0，0）位置代表 窗口最左上角的像素点  那么最左下角的像素点应该是（0，2） 而不是（0，3） 因为0，1，2 已经三个像素点了
            g3.DrawLine(p3, tang.Width - 1, tang.Height - 1, tang.Width - 1, 0);
            g3.DrawLine(p3, tang.Width - 1, 0, 0, 0);
            //操作栏 上 边框线
            g3.DrawLine(p3, tang.Width - 1, tableLayoutPanel1.Height - 60, 0, tableLayoutPanel1.Height - 60); //上
        }

        /// <summary>
        /// 标题栏 - 鼠标拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lb_Caption_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Win32ApiHelper.ReleaseCapture();
                Win32ApiHelper.PostMessage((int)this.Handle, 0x0112, 0xf017, 0);
            }
        }

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

        private void tileView1_ContextButtonClick(object sender, DevExpress.Utils.ContextItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                //忽略一条数据
                case "btn_Igore":
                    ItemIgore((TileViewItem)e.DataItem);
                    break;
                //保养一条数据
                case "btn_Maintenance":
                    ItemMaintenence((TileViewItem)e.DataItem);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 忽略该条保修集合
        /// </summary>
        /// <param name="item"></param>
        void ItemIgore(TileViewItem item)
        {
            int handle = item.RowHandle;
            View_CurrentMonthPlanTips dmp = (View_CurrentMonthPlanTips)tileView1.GetRow(handle);
            Datas.Remove(dmp);
            if (Datas.Count == 0)
            {
                DialogResult = DialogResult.OK;
                this.Close();
            }

            tileView1.RefreshData();
            gridControl1.RefreshDataSource();
        }

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

                int handle = item.RowHandle;
                View_CurrentMonthPlanTips dmp = (View_CurrentMonthPlanTips)tileView1.GetRow(handle);

                if (dmp != null)
                {
                    this.Hide();
                    //此处应通过webpi下载xls逻辑
                    APIResponseData apiResponseData = DeviceManager.Instance.GetModelByEquipmentID(dmp.EquipmentID);
                    if (!apiResponseData.IsSuccess)
                        throw new Exception(apiResponseData.Message);
                    DeviceInformationInfo entity = apiResponseData.ToDeserializeObject<DeviceInformationInfo>();

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

                    page_DriveMaintenance view = new page_DriveMaintenance(entity, dmp.AutoID);
                    DialogResult dr = view.ShowDialog();
                    if (dr == DialogResult.OK)
                    {
                        ItemIgore(item);
                        XtraMessageBoxHelper.Info("操作成功！");
                    }
                    else if (dr == DialogResult.Abort)
                    {
                        XtraMessageBoxHelper.Info(view.apiResponseData.Message);
                    }
                    this.Show();
                }
                else
                {
                    XtraMessageBox.Show("未能获取到设备信息,请重试。", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Show();
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Show();
            }
        }
    }
}