using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace MesWork.Pages
{
    /// <summary>
    /// 通用分页状态栏控件 — 嵌入到任何页面底部
    /// 包含：记录数标签 | 每页条数下拉 | 上一页 | 页码信息 | 下一页
    /// </summary>
    public class PagerBar : Panel
    {
        public event EventHandler PageChanged;

        private Label lbl_RecordCount;
        private ComboBox cmb_PageSize;
        private Button btn_Prev;
        private Label lbl_PageInfo;
        private Button btn_Next;

        private int _currentPage = 1;
        private int _totalPage = 1;
        private int _totalCount = 0;
        private int _pageSize = 100;

        public int CurrentPage => _currentPage;
        public int PageSize => _pageSize;
        public int TotalCount => _totalCount;

        public PagerBar()
        {
            this.BackColor = Color.White;
            this.Dock = DockStyle.Bottom;
            this.Height = 40;

            var tbl = new TableLayoutPanel
            {
                Dock = DockStyle.Fill,
                ColumnCount = 2,
                RowCount = 1
            };
            tbl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
            tbl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
            tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
            this.Controls.Add(tbl);

            // 左侧 — 记录数
            lbl_RecordCount = new Label
            {
                AutoSize = true,
                Anchor = AnchorStyles.Left,
                Font = new Font("微软雅黑", 10F),
                ForeColor = Color.FromArgb(55, 65, 81),
                Margin = new Padding(16, 0, 0, 0),
                Text = "共 0 条记录"
            };
            tbl.Controls.Add(lbl_RecordCount, 0, 0);

            // 右侧 — 分页控件 (RightToLeft)
            var flow = new FlowLayoutPanel
            {
                Dock = DockStyle.Fill,
                FlowDirection = FlowDirection.RightToLeft,
                WrapContents = false
            };
            tbl.Controls.Add(flow, 1, 0);

            // 下一页
            btn_Next = CreatePageButton("下一页 ▶");
            btn_Next.Click += (s, e) => { if (_currentPage < _totalPage) { _currentPage++; PageChanged?.Invoke(this, EventArgs.Empty); } };
            flow.Controls.Add(btn_Next);

            // 页码信息
            lbl_PageInfo = new Label
            {
                AutoSize = true,
                Font = new Font("微软雅黑", 10F),
                ForeColor = Color.FromArgb(55, 65, 81),
                Margin = new Padding(4, 10, 4, 0),
                Text = "第 1 页 / 共 1 页"
            };
            flow.Controls.Add(lbl_PageInfo);

            // 上一页
            btn_Prev = CreatePageButton("◀ 上一页");
            btn_Prev.Click += (s, e) => { if (_currentPage > 1) { _currentPage--; PageChanged?.Invoke(this, EventArgs.Empty); } };
            flow.Controls.Add(btn_Prev);

            // 每页条数下拉
            cmb_PageSize = new ComboBox
            {
                DropDownStyle = ComboBoxStyle.DropDownList,
                Font = new Font("微软雅黑", 9F),
                Size = new Size(70, 26),
                Margin = new Padding(4, 7, 8, 0)
            };
            cmb_PageSize.Items.AddRange(new object[] { "50", "100", "200", "500", "1000" });
            cmb_PageSize.SelectedItem = "100";
            cmb_PageSize.SelectedIndexChanged += (s, e) =>
            {
                _pageSize = int.Parse(cmb_PageSize.Text);
                _currentPage = 1;
                PageChanged?.Invoke(this, EventArgs.Empty);
            };
            flow.Controls.Add(cmb_PageSize);

            var lblSize = new Label
            {
                AutoSize = true,
                Font = new Font("微软雅黑", 9F),
                ForeColor = Color.FromArgb(100, 116, 139),
                Margin = new Padding(4, 11, 0, 0),
                Text = "每页"
            };
            flow.Controls.Add(lblSize);
        }

        /// <summary>
        /// 重置到第1页（查询按钮调用）
        /// </summary>
        public void ResetPage() => _currentPage = 1;

        /// <summary>
        /// 更新分页状态（SP调用后调用）
        /// </summary>
        public void UpdateState(SqlParameter pageCountParam, SqlParameter itemCountParam)
        {
            _totalPage = pageCountParam.Value != DBNull.Value ? Convert.ToInt32(pageCountParam.Value) : 1;
            _totalCount = itemCountParam.Value != DBNull.Value ? Convert.ToInt32(itemCountParam.Value) : 0;
            if (_totalPage < 1) _totalPage = 1;

            lbl_RecordCount.Text = $"共 {_totalCount} 条记录";
            lbl_PageInfo.Text = $"第 {_currentPage} 页 / 共 {_totalPage} 页";
            btn_Prev.Enabled = _currentPage > 1;
            btn_Next.Enabled = _currentPage < _totalPage;
        }

        /// <summary>
        /// 简易更新（直接传总条数，不分页的模块用）
        /// </summary>
        public void UpdateCount(int count)
        {
            lbl_RecordCount.Text = $"共 {count} 条记录";
        }

        /// <summary>
        /// 创建分页 Output 参数对
        /// </summary>
        public static (SqlParameter pageCount, SqlParameter itemCount) CreateOutputParams()
        {
            return (
                new SqlParameter("@PageCount", SqlDbType.Int) { Direction = ParameterDirection.Output },
                new SqlParameter("@ItemCount", SqlDbType.Int) { Direction = ParameterDirection.Output }
            );
        }

        private Button CreatePageButton(string text)
        {
            var btn = new Button
            {
                BackColor = Color.FromArgb(74, 144, 217),
                Cursor = Cursors.Hand,
                FlatStyle = FlatStyle.Flat,
                Font = new Font("微软雅黑", 9F, FontStyle.Bold),
                ForeColor = Color.White,
                Margin = new Padding(4, 6, 4, 0),
                Size = new Size(72, 28),
                Text = text,
                UseVisualStyleBackColor = false
            };
            btn.FlatAppearance.BorderSize = 0;
            return btn;
        }
    }
}
