using DeviceRepair.Models;
using DeviceRepair.Models.History;
using DeviceRepair.Models.Logs;
using DeviceRepairAndOptimization.Biz;
using DeviceRepairAndOptimization.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace DeviceRepairAndOptimization.Pages.Log
{
    public partial class page_UserLoginLog : FormBase
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartDate
        {
            get
            {
                if (dtStart.EditValue == null)
                    dtStart.EditValue = DateTime.Today.AddMonths(-1);
                return Convert.ToDateTime(dtStart.EditValue);
            }
        }

        /// <summary>
        /// 截至时间
        /// </summary>
        public DateTime EndDate
        {
            get
            {
                if (dtEnd.EditValue == null)
                    dtEnd.EditValue = DateTime.Today.AddDays(1).AddMilliseconds(-1);
                return Convert.ToDateTime(dtEnd.EditValue);
            }
        }

        /// <summary>
        /// 用户编号
        /// </summary>
        public string UserCode
        {
            get
            {
                return (txtUserCode.EditValue + "").Trim();
            }
        }

        /// <summary>
        /// 操作类型
        /// </summary>
        public string ContentType
        {
            get
            {
                if (cbContentType.EditValue + "" == "全部")
                    return "";
                return cbContentType.EditValue + "";
            }
        }

        /// <summary>
        /// 客户端名称
        /// </summary>
        public string ClientName
        {
            get
            {
                return (txt_ClientName.EditValue + "").Trim();
            }
        }

        public OperationFilterModel operationFilterModel
        {
            get
            {
                return new OperationFilterModel
                {
                    sDate = StartDate.Year < 1753 ? new DateTime(1753,1,1): StartDate,
                    eDate = EndDate,
                    LoginCode = UserCode,
                    OperationType = ContentType,
                    ClientName = ClientName
                };
            }
        }

        public page_UserLoginLog()
        {
            InitializeComponent();
        }

        private void btn_Query_Click(object sender, EventArgs e)
        {
            try
            {
                APIResponseData result = OperationManager.Instance.UserLoginContent(operationFilterModel);
                if (result.Code == -1)
                {
                    XtraMessageBoxHelper.Error(result.Message);
                    return;
                }
                List<UserLogin> Datas = result.ToDeserializeObject<List<UserLogin>>();
                gridControl1.DataSource = Datas;
                // 设置行号列宽度
                SizeF size = this.CreateGraphics().MeasureString(Datas.Count.ToString(), this.Font);
                gridView1.IndicatorWidth = Convert.ToInt32(size.Width) + 20;
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView1.DataRowCount == 0)
                {
                    throw new Exception("当前没有数据可供操作！");
                }
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Title = "导出Excel";
                saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
                DialogResult dialogResult = saveFileDialog.ShowDialog(this);
                if (dialogResult == DialogResult.OK)
                {

                    DevExpress.XtraPrinting.XlsxExportOptionsEx options = new DevExpress.XtraPrinting.XlsxExportOptionsEx();
                    options.ExportType = DevExpress.Export.ExportType.WYSIWYG;
                    gridControl1.ExportToXlsx(saveFileDialog.FileName.ToString(), options);
                    XtraMessageBoxHelper.Info("导出成功！");
                }
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.Error(ex.Message);
            }
        }
    }
}
