using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using AIMSBLL;
using AIMSExtension;
using AIMSModel;
using DCSoftDotfuscate;
using static System.Windows.Forms.AxHost;
namespace AIMS.PublicUI.UI
{
    public partial class frmApplianceSelect : Form
    {
        /// 
        /// 可用的器械
        /// 
        public DataTable dt;
        /// 
        /// 已选择的器械
        /// 
        public DataTable ydt;
        /// 
        /// 当前页数
        /// 
        int currentPage = 0;
        /// 
        /// 总数
        /// 
        int total = 0;
        /// 
        /// 总页数
        /// 
        int pages = 0;
        /// 
        /// 
        /// 
        public int _applianceUsetypeId;
        private ApplianceUseType _applianceUseType;
        public frmApplianceSelect()
        {
            InitializeComponent();
        }
        private void frmApplianceSelect_Load(object sender, EventArgs e)
        {
            _applianceUseType = BApplianceUseType.SelectSingle(_applianceUsetypeId);
            lblUseName.Text = _applianceUseType.Name;
            dgvD.AutoGenerateColumns = false;
            dgvY.AutoGenerateColumns = false;
            dt = BAppliance.GetApplianiceByName("", -1);
            ydt = BAppliance.GetApplianiceNumberByIds(_applianceUseType.TheApplianceId, _applianceUseType.ApplianceNumber);
            BindDgvY(ydt);
            cboType.Text = "器械";
            cboType_SelectedIndexChanged(null, null);
        }
        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)
            {
                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();
                this.dgvY.Rows[index].Cells["ApplianceNumber"].Value = dr["ApplianceNumber"].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();
            }
        }
        private void cboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentPage = 0;
            if (cboType.SelectedIndex >= 0)
            {
                int type = -1;
                if (cboType.Text.Equals("器械"))
                {
                    type = 0;
                }
                else if (cboType.Text.Equals("敷料"))
                {
                    type = 1;
                }
                else if (cboType.Text.Equals("其它"))
                {
                    type = 9;
                }
                dt = BAppliance.GetApplianiceByName("", type);
                SetPageText(dt);
                BindDgv(GetTableByCurrentPage(currentPage, dt));
            }
        }
        private void SetPageText(DataTable table)
        {
            total = table.Rows.Count;
            if (total % 25 == 0)
            {
                pages = total / 25;
            }
            else
            {
                pages = total / 25 + 1;
            }
            lblPage.Text = currentPage + 1 + "/" + pages + ",共" + total + "条";
        }
        /// 
        /// 得到某页的DataTable
        /// 
        /// 当前页
        /// 获取数据的DataTable
        /// 某一页的DataTable
        private DataTable GetTableByCurrentPage(int currPage, DataTable dt)
        {
            DataTable pdt = dt.Clone();
            int index = currPage * 25;
            for (int i = index; i < index + 25; i++)
            {
                if (i == total)
                {
                    break;
                }
                DataRow dr = pdt.NewRow();
                dr["Id"] = Convert.ToInt32(dt.Rows[i]["Id"]);
                dr["Name"] = dt.Rows[i]["Name"].ToString();
                pdt.Rows.Add(dr);
            }
            return pdt;
        }
        private void txtQuery_TextChanged(object sender, EventArgs e)
        {
            int type = -1;
            if (cboType.Text.Equals("器械"))
            {
                type = 0;
            }
            else if (cboType.Text.Equals("敷料"))
            {
                type = 1;
            }
            else if (cboType.Text.Equals("其它"))
            {
                type = 9;
            }
            dt = BAppliance.GetApplianiceByName(txtQuery.Text, type);
            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 row in dgvD.Rows)
            {
                foreach (DataGridViewRow yRow in dgvY.Rows)
                {
                    if (row.Cells["Selectc"].EditedFormattedValue.ToString() == "True" && row.Cells["oName"].Value.ToString() == yRow.Cells["yName"].Value.ToString())
                    {
                        MessageBox.Show("已经选过了!");
                        foreach (DataGridViewRow drow in dgvD.Rows)
                        {
                            if (drow.Cells["Selectc"].EditedFormattedValue.ToString() == "True")
                            {
                                drow.Cells["Selectc"].Value = false;
                            }
                        }
                        return;
                    }
                }
            }
            List list = new List();
            foreach (DataGridViewRow row in dgvD.Rows)
            {
                if (row.Cells["Selectc"].EditedFormattedValue.ToString() == "True")
                {
                    list.Add(Convert.ToInt32(row.Cells["Id"].Value));
                }
            }
            foreach (int id in list)
            {
                DataRow ydr = ydt.NewRow();
                foreach (DataRow dr in dt.Rows)
                {
                    if (Convert.ToInt32(dr["Id"]) == id)
                    {
                        ydr["Id"] = Convert.ToInt32(dr["Id"]);
                        ydr["Name"] = dr["Name"].ToString();
                        ydr["ApplianceNumber"] = "0";
                        ydt.Rows.Add(ydr);
                        break;
                    }
                }
            }
            BindDgvY(ydt);
            txtQuery.Text = "";
            txtQuery.Focus();
        }
        private void btnCancelAll_Click(object sender, EventArgs e)
        {
            List list = new List();
            foreach (DataGridViewRow row in dgvY.Rows)
            {
                if (row.Cells["ySelect"].EditedFormattedValue.ToString() == "True")
                {
                    list.Add(Convert.ToInt32(row.Cells["yId"].Value));
                }
            }
            DataTable dt = new DataTable();
            foreach (int id in list)
            {
                foreach (DataGridViewRow row in dgvY.Rows)
                {
                    if (Convert.ToInt32(row.Cells["yId"].Value) == id)
                    {
                        dgvY.Rows.Remove(row);
                        DataRow[] dr = ydt.Select(" Id=" + id);
                        if (dr.Length > 0)
                        {
                            ydt.Rows.Remove(dr[0]);
                        }
                        break;
                    }
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            List list = new List();
            List number = new List();
            foreach (DataGridViewRow row in dgvY.Rows)
            {
                list.Add(Convert.ToInt32(row.Cells["yId"].Value));
                number.Add(Convert.ToInt32(row.Cells["ApplianceNumber"].Value));
            }
            string applianceId = string.Join(",", list.ToArray());
            string _ApplianceNumber = string.Join(",", number.ToArray());
            _applianceUseType.TheApplianceId = applianceId;
            _applianceUseType.ApplianceNumber = _ApplianceNumber;
            int num = BApplianceUseType.Update(_applianceUseType);
            if (num > 0)
            {
                MessageBox.Show("保存成功!");
                //this.Close();
            }
        }
        private void dgvD_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataRow ydr = ydt.NewRow();
            int id = Convert.ToInt32(dgvD.SelectedRows[0].Cells["Id"].Value);
            foreach (DataRow row in ydt.Rows)
            {
                if (Convert.ToInt32(row["Id"]) == id)
                {
                    MessageBox.Show("已选择!");
                    return;
                }
            }
            foreach (DataRow row in dt.Rows)
            {
                if (Convert.ToInt32(row["Id"]) == id)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        ydr["Id"] = row["Id"];
                        ydr["Name"] = row["Name"];
                        ydr["ApplianceNumber"] = "0";
                    }
                    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 btnSelectUp_Click(object sender, EventArgs e)
        {
            int rowIndex = dgvY.SelectedRows[0].Index;//选中行索引
            if (rowIndex > 0)
            {
                List