using AIRS.usercontrol;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AIRS
{
    /// <summary>
    /// HistoryGrade.xaml 的交互逻辑
    /// </summary>
    public partial class HistoryGrade : Window
    {
        private int m_Pyte = 0;//打开窗体类型 0历史评级  1检查报告

        #region 分页变量
        /// <summary>
        /// 当前页码
        /// </summary>
        private int m_Page = 1;
        /// <summary>
        /// 总条数
        /// </summary>
        private int m_Total = 0;
        /// <summary>
        /// 总页数
        /// </summary>
        private int m_Count = 1;
        /// <summary>
        /// 每页条数
        /// </summary>
        private int m_Page_Count = 15;
        #endregion

        /// <summary>
        /// 数据库
        /// </summary>
        SQLiteConnection conn = null;
        SQLiteConnectionStringBuilder connstr = null;

        /// <summary>
        /// 程序工作目录
        /// </summary>
        private string path = Directory.GetCurrentDirectory();

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="type">0历史评级   1检查报告</param>
        public HistoryGrade(int type)
        {
            m_Pyte = type;
            InitializeComponent();
        }

        /// <summary>
        /// 关闭窗体事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        /// <summary>
        /// 窗体加载完成后执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_ContentRendered(object sender, EventArgs e)
        {
            if (m_Pyte == 1)
            {
                //切换到检查报告
                this.HisorReport.Content = "检查报告";
                this.HisGrad.Visibility = Visibility.Collapsed;//隐藏历史评级
                this.CheckReport.Visibility = Visibility.Visible;//显示检查报告
                this.CheckBtnSave.Visibility = Visibility.Visible;//显示报告另存按钮
                this.btn_Query.Visibility = Visibility.Collapsed;//隐藏查询按钮
                this.lable1.Visibility = Visibility.Collapsed;//隐藏lable
                this.startdatePickerCtl.Visibility = Visibility.Collapsed;//隐藏开始时间
                this.enddatePickerCtl.Visibility = Visibility.Collapsed;//隐藏结束时间

                //#region 隐藏分页按钮
                //this.first_btn.Visibility = Visibility.Collapsed;
                //this.last_btn.Visibility = Visibility.Collapsed;
                //this.next_btn.Visibility = Visibility.Collapsed;
                //this.pro_btn.Visibility = Visibility.Collapsed; 
                //#endregion

            }
            else if (m_Pyte == 0)
            {
                //切换到历史评级
                this.HisorReport.Content = "历史评级";
                this.HisGrad.Visibility = Visibility.Visible;//显示历史评级
                this.CheckReport.Visibility = Visibility.Collapsed;//隐藏检查报告
                this.CheckBtnSave.Visibility = Visibility.Collapsed;//隐藏报告另存按钮
                this.btn_Query.Visibility = Visibility.Visible;//显示查询按钮
                this.lable1.Visibility = Visibility.Visible;//显示lable
                this.startdatePickerCtl.Visibility = Visibility.Visible;//显示开始时间
                this.enddatePickerCtl.Visibility = Visibility.Visible;//显示结束时间

                //#region 显示分页按钮
                //this.first_btn.Visibility = Visibility.Visible;
                //this.last_btn.Visibility = Visibility.Visible;
                //this.next_btn.Visibility = Visibility.Visible;
                //this.pro_btn.Visibility = Visibility.Visible;
                //#endregion

                GetGrade();
            }
        }

        /// <summary>
        /// 根据路径获取报告
        /// </summary>
        /// <param name="path"></param>
        private void GetReport(string path)
        {
            try
            {
                string strSql = "SELECT * FROM t_grade where \"图像目录\"=='" + path + "'";
                conn = new SQLiteConnection();
                connstr = new SQLiteConnectionStringBuilder();
                connstr.DataSource = path + "\\datas.db";
                conn.ConnectionString = connstr.ToString();
                conn.Open();
                SQLiteDataAdapter da = new SQLiteDataAdapter(strSql, conn);
                DataTable ds = new DataTable();
                da.Fill(ds);
                
                if (ds.Rows.Count > 0)
                {

                }
                else
                {
                    MessageBox.Show("没有该图像路径的检查结果！");
                }

            }
            catch (Exception ex)
            {

            }
            finally
            {

            }
        }

        /// <summary>
        /// 获取历史评级数据
        /// </summary>
        /// <returns></returns>
        private void GetGrade()
        {
            try
            {
                string startTime = "";
                string endTime = "";
                try
                {
                    startTime = DateTime.Parse(startdatePickerCtl.Text.Trim()).ToString("yyyy-MM-dd 00:00:00");
                    endTime = DateTime.Parse(enddatePickerCtl.Text.Trim()).ToString("yyyy-MM-dd 23:59:59");
                }
                catch
                {
                    MessageBox.Show("时间格式不正确，请重新填写！");
                    return;
                }
                string strSql = "SELECT * FROM t_grade where 1=1";
                string strcountsql = "SELECT count(*) as count FROM t_grade where 1=1";
                if (startTime.Length > 0)
                {
                    strSql += " and \"评级时间\">'" + startTime + "'";
                    strcountsql += " and \"评级时间\">'" + startTime + "'";
                }
                if (endTime.Length > 0)
                {
                    strSql += " and \"评级时间\"<'" + endTime + "'";
                    strcountsql += " and \"评级时间\"<'" + endTime + "'";
                }//desc
                strSql += "order by \"评级时间\" desc limit " + ((m_Page - 1) * m_Page_Count) + ","+ m_Page_Count + "";
                conn = new SQLiteConnection();
                connstr = new SQLiteConnectionStringBuilder();
                connstr.DataSource = path + "\\datas.db";
                conn.ConnectionString = connstr.ToString();
                conn.Open();
                SQLiteDataAdapter da = new SQLiteDataAdapter(strSql, conn);
                DataTable ds = new DataTable();
                da.Fill(ds);
                da = new SQLiteDataAdapter(strcountsql, conn);
                DataTable dsCount = new DataTable();
                da.Fill(dsCount);

                m_Total = Convert.ToInt32(dsCount.Rows[0][0]);//总条数
                m_Count = (int)Math.Ceiling(((double)m_Total / (double)m_Page_Count));

                ObservableCollection<HisData> list = new ObservableCollection<HisData>();
                for (int i = 0; i < ds.Rows.Count; i++)
                {
                    HisData data = new HisData();
                    //data.num = (i + 1);
                    data.gradeproject = ds.Rows[i]["评级项目"] == null ? "" : ds.Rows[i]["评级项目"].ToString();
                    data.gradetime = ds.Rows[i]["评级时间"] == null ? "" : ds.Rows[i]["评级时间"].ToString(); // ds.Rows[i]["评级时间"].ToString();
                    data.operation = ds.Rows[i]["操作员"] == null ? "" : ds.Rows[i]["操作员"].ToString(); //ds.Rows[i]["操作员"].ToString();
                    data.checke = ds.Rows[i]["复合员"] == null ? "" : ds.Rows[i]["复合员"].ToString(); //ds.Rows[i]["复合员"].ToString();
                    data.imgurl = ds.Rows[i]["图像目录"] == null ? "" : ds.Rows[i]["图像目录"].ToString();
                    data.catagory = ds.Rows[i]["catagory"] == null ? "" : ds.Rows[i]["catagory"].ToString();//类别
                    data.gangzhong = ds.Rows[i]["钢种"] == null ? "" : ds.Rows[i]["钢种"].ToString();//钢种
                    data.imgcount = ds.Rows[i]["图像数量"] == null ? "" : ds.Rows[i]["图像数量"].ToString();//图像数量
                    data.pingji = ds.Rows[i]["评级标准"] == null ? "" : ds.Rows[i]["评级标准"].ToString();//评级标准
                    data.instrument = ds.Rows[i]["检测仪器"] == null ? "" : ds.Rows[i]["检测仪器"].ToString();//图像数量
                    list.Add(data);
                }
                date_grid.DataContext = list;
                //this.pageuser.FontSize = 36;
                this.pageuser.Page = m_Page;
                //this.pageuser.PageCount = m_Count;
                this.pageuser.PageSize = m_Page_Count;
                this.pageuser.RecordCount = m_Total;
            }
            catch (Exception ex)
            {

            }
            finally
            {
                try
                {
                    conn.Close();                    
                }
                catch 
                {
                }
            }
        }

        /// <summary>
        /// 序号列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void date_grid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        /// <summary>
        /// 生成/查看报告单击事件(表格内按钮单击事件)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(((Button)sender).Tag == null ? "" : ((Button)sender).Tag.ToString());
            try
            {
                string imgUrl = ((Button)sender).Tag == null ? "" : ((Button)sender).Tag.ToString();
                conn = new SQLiteConnection();
                connstr = new SQLiteConnectionStringBuilder();
                connstr.DataSource = path + "\\datas.db";
                conn.ConnectionString = connstr.ToString();
                conn.Open();
                string strSql = "select * from t_grade where \"图像目录\"='" + imgUrl + "'";
                SQLiteDataAdapter da = new SQLiteDataAdapter(strSql, conn);
                DataTable ds = new DataTable();
                da.Fill(ds);
                if (ds.Rows.Count > 0)
                {
                    string check_Project = "";// 评级项目/检测项目
                    string standard = "";//检测标准及技术文件
                    string device = "";//检测使用设备
                    string result = "";//结果
                    string check_user = "";//检查员
                }
            }
            catch
            {
            }
            finally
            {
                try
                {
                    conn.Close();
                }
                catch
                {
                }
            }
        }

        #region 分页事件
        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pro_btn_Click(object sender, RoutedEventArgs e)
        {
            if (m_Page == 1)
            {
                MessageBox.Show("已经是第一页！");
                return;
            }
            m_Page--;
            GetGrade();
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void first_btn_Click(object sender, RoutedEventArgs e)
        {
            if (m_Page == 1)
            {
                MessageBox.Show("已经是第一页！");
                return;
            }
            m_Page = 1;
            GetGrade();
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void next_btn_Click(object sender, RoutedEventArgs e)
        {
            if (m_Page >= m_Count)
            {
                MessageBox.Show("已经是最后一页！");
                return;
            }
            m_Page++;
            GetGrade();
        }
        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void last_btn_Click(object sender, RoutedEventArgs e)
        {
            if (m_Page >= m_Count)
            {
                MessageBox.Show("已经是最后一页！");
                return;
            }
            m_Page = m_Count;
            GetGrade();
        }
        #endregion

        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Query_Click(object sender, RoutedEventArgs e)
        {
            m_Page = 1;
            GetGrade();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.pageuser.PageChanged += new EventHandler<PageChangedEventArgs>(Eventpage);
        }
        /// <summary>
        /// 自定义分页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="a"></param>
        public void Eventpage(object sender, PageChangedEventArgs a)
        {
            m_Page = a.Page;
            GetGrade();
        }
    }

    #region 显示列类
    /// <summary>
    /// 显示列类
    /// </summary>
    public class HisData
    {
        //public int num { get; set; }
        /// <summary>
        /// 评级项目
        /// </summary>
        public string gradeproject { get; set; }
        /// <summary>
        /// 评级时间
        /// </summary>
        public string gradetime { get; set; }
        /// <summary>
        /// 操作员
        /// </summary>
        public string operation { get; set; }
        /// <summary>
        /// 复核员
        /// </summary>
        public string checke { get; set; }
        /// <summary>
        /// 图像目录
        /// </summary>
        public string imgurl { get; set; }
        /// <summary>
        /// 类别
        /// </summary>
        public string catagory { get; set; }
        /// <summary>
        /// 钢种
        /// </summary>
        public string gangzhong { get; set; }
        /// <summary>
        /// 图片数量
        /// </summary>
        public string imgcount { get; set; }
        /// <summary>
        /// 评级标准
        /// </summary>
        public string pingji { get; set; }
        /// <summary>
        /// 检测仪器
        /// </summary>
        public string instrument { get;set;}
        //public string 查看报告 { get; set; }
    }

    #endregion

}
