using AIMSBLL;
using AIMSExtension;
using AIMSModel;
using DrawGraph;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace AIMS.PublicUI.UI
{
    public partial class frmChargSelect : Form
    {
        /// 
        /// 可用的收费
        /// 
        public DataTable dt;
        /// 
        /// 已选择的收费
        /// 
        public DataTable ydt;
        /// 
        /// 当前页数
        /// 
        int currentPage = 0;
        /// 
        /// 总数
        /// 
        int total = 0;
        /// 
        /// 总页数
        /// 
        int pages = 0;
        public ChargsTemplate chargsTemplate;
        public string DeptId;
        public frmChargSelect()
        {
            InitializeComponent();
        }
        public frmChargSelect(int chargsTemplateId)
        {
            InitializeComponent();
            chargsTemplate = BChargsTemplate.SelectSingle(chargsTemplateId);
            if (chargsTemplate.TemplateType == "麻醉")
            {
                DeptId = "300001";
            }
            if (chargsTemplate.TemplateType == "护士")
            {
                DeptId = "300002";
            }
        }
        private void frmApplianceSelect_Load(object sender, EventArgs e)
        {
            dgvD.AutoGenerateColumns = false;
            dgvY.AutoGenerateColumns = false;
            BindCharsDICT();
            ydt = BCharges.GetChargsByCodes(chargsTemplate.ConnectId, chargsTemplate.DefaultValue);
            BindDgvY(ydt);
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void BindDgvY(DataTable dt)
        {
            dgvY.AutoGenerateColumns = false;
            dgvY.Rows.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                bool isOnly = true;
                foreach (DataGridViewRow item1 in dgvY.Rows)
                {
                    if (item1.Cells["yId"].Value.ToString() == dr["Id"].ToString())
                    {
                        isOnly = false;
                    }
                }
                if (isOnly == true)
                {
                    int index = this.dgvY.Rows.Add();
                    this.dgvY.Rows[index].Cells["yId"].Value = dr["Id"].ToString();
                    this.dgvY.Rows[index].Cells["yName"].Value = dr["Name"].ToString() + " " + dr["Bill"].ToString();
                    this.dgvY.Rows[index].Cells["yCode"].Value = dr["Code"].ToString();
                    this.dgvY.Rows[index].Cells["yxmbm"].Value = dr["Code"].ToString();
                    string ITEMPRICE = dr["Price"].ToString();
                    try
                    {
                        if (ITEMPRICE != null && ITEMPRICE != "")
                        {
                            ITEMPRICE = Double.Parse(ITEMPRICE).ToString();
                        }
                        this.dgvY.Rows[index].Cells["price"].Value = ITEMPRICE;
                    }
                    catch (Exception)
                    {
                    }
                    this.dgvY.Rows[index].Cells["Number"].Value = dr["Number"].ToString();
                }
            }
        }
        private void BindDgv(DataTable dt)
        {
            dgvD.AutoGenerateColumns = false;
            dgvD.Rows.Clear();
            int num = 1;
            foreach (DataRow dr in dt.Rows)
            {
                int index = this.dgvD.Rows.Add();
                this.dgvD.Rows[index].Cells["Id"].Value = dr["Id"].ToString();
                this.dgvD.Rows[index].Cells["Index"].Value = num;
                num++;
                this.dgvD.Rows[index].Cells["oName"].Value = dr["Name"].ToString() + " " + dr["Bill"].ToString(); ;
                this.dgvD.Rows[index].Cells["oCode"].Value = dr["Code"].ToString();
                this.dgvD.Rows[index].Cells["oxmbm"].Value = dr["Code"].ToString();
                string ITEMPRICE = dr["Price"].ToString();
                try
                {
                    if (ITEMPRICE != null && ITEMPRICE != "")
                    {
                        ITEMPRICE = Double.Parse(ITEMPRICE).ToString();
                    }
                    this.dgvD.Rows[index].Cells["oPrice"].Value = ITEMPRICE;
                }
                catch (Exception)
                {
                }
            }
        }
        private void BindCharsDICT()
        {
            currentPage = 0;
            dt = BCharges.SelectByIdName("", DeptId);
            SetPageText(dt);
            BindDgv(GetTableByCurrentPage(currentPage, dt));
        }
        private void SetPageText(DataTable table)
        {
            total = table.Rows.Count;
            if (total % 20 == 0)
            {
                pages = total / 20;
            }
            else
            {
                pages = total / 20 + 1;
            }
            lblPage.Text = currentPage + 1 + "/" + pages + ",共" + total + "条";
        }
        /// 
        /// 得到某页的DataTable
        /// 
        /// 当前页
        /// 获取数据的DataTable
        /// 某一页的DataTable
        private DataTable GetTableByCurrentPage(int currPage, DataTable dt)
        {
            DataTable pdt = dt.Clone();
            int index = currPage * 20;
            for (int i = index; i < index + 20; i++)
            {
                if (i == total)
                {
                    break;
                }
                DataRow dr = pdt.NewRow();
                dr["Id"] = dt.Rows[i]["ID"];
                dr["Name"] = dt.Rows[i]["Name"].ToString();
                dr["Code"] = dt.Rows[i]["Code"].ToString();
                dr["xmbm"] = dt.Rows[i]["xmbm"].ToString();
                dr["Price"] = dt.Rows[i]["Price"].ToString();
                pdt.Rows.Add(dr);
            }
            return pdt;
        }
        private void txtQuery_TextChanged(object sender, EventArgs e)
        {
            dt = BCharges.SelectByIdName(txtQuery.Text, DeptId);
            currentPage = 0;
            SetPageText(dt);
            BindDgv(GetTableByCurrentPage(currentPage, dt));
        }
        private void lkUp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (total == 0 || currentPage == 0)
            {
                return;
            }
            currentPage--;
            SetPageText(dt);
            BindDgv(GetTableByCurrentPage(currentPage, dt));
        }
        private void lkDown_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (total == 0 || (currentPage + 1) == pages)
            {
                return;
            }
            currentPage++;
            SetPageText(dt);
            BindDgv(GetTableByCurrentPage(currentPage, dt));
        }
        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow yRow in dgvY.Rows)
            {
                if (dgvD.CurrentRow.Cells["Id"].Value.ToString() == yRow.Cells["yId"].Value.ToString())
                {
                    MessageBox.Show("已经选过了!", "系统提示");
                    return;
                }
            }
            List list = new List();
            list.Add(dgvD.CurrentRow.Cells["Id"].Value.ToString());
            foreach (string id in list)
            {
                DataRow ydr = ydt.NewRow();
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["Id"].ToString() == id)
                    {
                        ydr["Id"] = dr["Id"].ToString();
                        ydr["Name"] = dr["Name"].ToString();
                        ydr["Code"] = dr["Code"].ToString();
                        ydr["Price"] = dr["Price"].ToString();
                        ydr["Number"] = "1";
                        ydt.Rows.Add(ydr);
                        break;
                    }
                }
            }
            BindDgvY(ydt);
        }
        private void btnCancelAll_Click(object sender, EventArgs e)
        {
            List list = new List();
            list.Add(dgvY.CurrentRow.Cells["yId"].Value.ToString());
            foreach (string id in list)
            {
                foreach (DataRow dr in ydt.Rows)
                {
                    if (dr["Id"].ToString() == id)
                    {
                        ydt.Rows.Remove(dr);
                        break;
                    }
                }
            }
            BindDgvY(ydt);
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            List list = new List();
            List number = new List();
            foreach (DataGridViewRow row in dgvY.Rows)
            {
                list.Add(row.Cells["yId"].Value.ToString());
                number.Add(Convert.ToInt32(row.Cells["Number"].Value));
            }
            string applianceId = string.Join("','", list.ToArray());
            applianceId = "'" + applianceId + "'";
            string _Number = string.Join(",", number.ToArray());
            chargsTemplate.ConnectId = applianceId;
            chargsTemplate.DefaultValue = _Number;
            chargsTemplate.OperatorId = PublicMethod.OperatorId;
            chargsTemplate.OperatorTime = DateTime.Now;
            int num = BChargsTemplate.Update(chargsTemplate);
            if (num > 0)
            {
                new frmMessageBox().Show();
                this.Close();
            }
        }
        private void dgvD_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataRow ydr = ydt.NewRow();
            string id = dgvD.SelectedRows[0].Cells["Id"].Value.ToString();
            foreach (DataRow row in ydt.Rows)
            {
                if (row["Id"].ToString() == id)
                {
                    MessageBox.Show("已选择!", "系统提示");
                    return;
                }
            }
            foreach (DataRow row in dt.Rows)
            {
                if (row["Id"].ToString() == id)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        ydr["Id"] = row["Id"];
                        ydr["Name"] = row["Name"].ToString();
                        ydr["Code"] = row["Code"].ToString();
                        ydr["Price"] = row["Price"].ToString();
                        ydr["Number"] = "1";
                    }
                    ydt.Rows.Add(ydr);
                    break;
                }
            }
            BindDgvY(ydt);
        }
        public DataGridViewTextBoxEditingControl dgvTxt = null; // 声明 一个文本 CellEdit
        public DataGridViewTextBoxEditingControl dgvTxtName = null; // 声明 一个文本 CellEdit
        private void dgvY_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            //判断类型
            if (dgvY.CurrentCell != null)
            {
                if (dgvY.CurrentCell.ColumnIndex == 3)
                {
                    dgvTxtName = (DataGridViewTextBoxEditingControl)e.Control; // 得到单元格
                    dgvTxtName.KeyPress -= new KeyPressEventHandler(dgvTxt_KeyPress); // 绑定事件
                    dgvTxtName.KeyPress += new KeyPressEventHandler(dgvTxt_KeyPress); // 绑定事件
                }
            }
        }
        void dgvTxt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < 48 || e.KeyChar > 57)
            {
                if (e.KeyChar != 46 && e.KeyChar != 8 && e.KeyChar != 13)
                {
                    e.Handled = true;
                }
            }
            TextBox tb = sender as TextBox;
            if (e.KeyChar == 46)
            {
                int n = tb.Text.LastIndexOf(".");
                if (n > 0) e.Handled = true;
            }
        }
        private void dgvY_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //List list = new List();
            //list.Add(dgvY.CurrentRow.Cells["yId"].Value.ToString());
            //for (int id = 0; id < list.Count; id++)
            //{
            //    for (int i = ydt.Rows.Count - 1; i >= 0; i--)
            //    {
            //        if (ydt.Rows[i]["ChargCode"].ToString() == list[id])
            //        {
            //            ydt.Rows.Remove(ydt.Rows[i]);
            //        }
            //    }
            //}
            //dgvY.Rows.Remove(dgvY.CurrentRow);
        }
        private void txtQuery_Click(object sender, EventArgs e)
        {
            txtQuery.Text = "";
        }
    }
}