using System;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Core.Mes.ClientFrameWork;

namespace Core.Mes.ClientPurviewManager
{
    public partial class frmQuestionFeedback : Core.Mes.ClientFrameWork.FrmBase
    {
        public frmQuestionFeedback()
        {
            InitializeComponent();
        }

        void doQuery()
        {
            string[] sArgs = new string[4];
            if (!chbxTime_Q.Checked && !chbxCreator_Q.Checked)
            {
                MessageBox.Show("至少选择1个查询条件！系统自动选择1个条件。");
                chbxTime_Q.Checked = true;
            }

            if (chbxTime_Q.Checked)
            {
                if (uldtpTime1_Q.DateTime > uldtpTime2_Q.DateTime)
                {
                    uldtpTime1_Q.Focus();
                    MessageBox.Show("请选择合理的时间范围！");
                    return;
                }

                TimeSpan tspan = uldtpTime2_Q.DateTime - uldtpTime1_Q.DateTime;
                if (tspan.Days > 31)
                {
                    uldtpTime1_Q.Focus();
                    MessageBox.Show("不能查时间范围超过1个月的数据！");
                    return;
                }

                sArgs[0] = uldtpTime1_Q.Text + " 00:00:00";
                sArgs[1] = uldtpTime2_Q.Text + " 23:59:59";
            }

            if (chbxCreator_Q.Checked)
            {
                if (txtCreator_Q.Text.Trim() == "")
                {
                    txtCreator_Q.Focus();
                    MessageBox.Show("请输入查询内容！");
                    return;
                }
                sArgs[2] = txtCreator_Q.Text.Trim();
            }

            if (rdbtnAll.Checked)
                sArgs[3] = "All";
            else if (rdbtnNoReply.Checked)
                sArgs[3] = "NoReply";
            else if (rdbtnNoComplete.Checked)
                sArgs[3] = "NoComplete";
            else if (rdbtnComplete.Checked)
                sArgs[3] = "Complete";

            string err = "";
            object obj = ClientCommon._RemotingHelp.ExecuteMethod("PurviewManager", "Core.Mes.PurviewManager.QuestionReply", "getAllData",
                new object[] { sArgs },
                out err);
            if (obj != null)
            {
                ultraGrid1.DataSource = (DataSet)obj;
            }
        }

        void doAdd()
        {
            if (txtQuestion.Text.Trim() == "")
            {
                txtQuestion.Focus();
                MessageBox.Show("请输入内容！");
                return;
            }
            string[] sArgs = new string[8];
            sArgs[0] = ClientCommon._UserInfo.UserName;
            sArgs[1] = ClientCommon._UserInfo.UserDepName;
            sArgs[2] = txtQuestion.Text.Trim();
            sArgs[3] = ulcmeDepartment.Text.Trim();
            sArgs[4] = txtPrincipal.Text.Trim();
            sArgs[5] = ClientCommon.GetIp();
            sArgs[6] = mtxtPhono.Text;
            sArgs[7] = ulcmeQuestionType.Text;

            string err = "";
            object obj = ClientCommon._RemotingHelp.ExecuteMethod("PurviewManager", "Core.Mes.PurviewManager.QuestionReply", "addQuestion",
                new object[] { sArgs },
                out err);
            if ((int)obj > 0)
            {
                DataSet dset = (DataSet)ultraGrid1.DataSource;
                if (!rdbtnComplete.Checked)
                {
                    DataRow newRow = dset.Tables[0].NewRow();
                    newRow.ItemArray = new object[] { err, sArgs[0], sArgs[1], sArgs[2], sArgs[3], sArgs[4], DateTime.Now, "", null, sArgs[5], '0', sArgs[6], sArgs[7] };
                    dset.Tables[0].Rows.InsertAt(newRow, 0);
                    ultraGrid1.Rows[0].Activate();

                    foreach (Infragistics.Win.UltraWinGrid.UltraGridRow arow in ultraGrid1.Selected.Rows)
                        arow.Selected = false;

                    int idx = -1;
                    if (ultraGrid1.Selected.Cells.Count > 0)
                        idx = ultraGrid1.Selected.Cells[0].Column.Index;
                    for (int i = 0; i < ultraGrid1.Selected.Cells.Count; i++)
                    {
                        ultraGrid1.Selected.Cells[i].Selected = false;
                    }
                    if (idx > -1)
                        ultraGrid1.Rows[0].Cells[idx].Selected = true;
                }
                else
                {
                    MessageBox.Show("操作成功！请更改查询条件后重新查询。");
                }
            }
        }

        void doEdit()
        {
            if (curTopRow == null)
                return;

            if (txtQuestion.Text.Trim() == "")
            {
                txtQuestion.Focus();
                MessageBox.Show("请输入内容！");
                return;
            }
            string[] sArgs = new string[6];
            sArgs[0] = curTopRow.Cells["ID_"].Text;
            sArgs[1] = txtQuestion.Text.Trim();
            sArgs[2] = ulcmeDepartment.Text.Trim();
            sArgs[3] = txtPrincipal.Text.Trim();
            sArgs[4] = mtxtPhono.Text;
            sArgs[5] = ulcmeQuestionType.Text;

            string err = "";
            object obj = ClientCommon._RemotingHelp.ExecuteMethod("PurviewManager", "Core.Mes.PurviewManager.QuestionReply", "editQuestion",
                new object[] { sArgs },
                out err);
            if ((int)obj > 0)
            {
                curTopRow.Cells["QUESTION"].Value = sArgs[1];
                curTopRow.Cells["DEPARTMENT"].Value = sArgs[2];
                curTopRow.Cells["PRINCIPAL"].Value = sArgs[3];
                curTopRow.Cells["PHONE"].Value = sArgs[4];
                curTopRow.Cells["QUESTIONTYPE"].Value = sArgs[5];
            }
        }

        void doDelete()
        {
            if (curTopRow == null)
                return;

            if (MessageBox.Show("您确定要删除？", "选择", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) ==
                DialogResult.No)
                return;

            string[] sArgs = new string[1];
            sArgs[0] = curTopRow.Cells["ID_"].Text;
            string err = "";
            object obj = ClientCommon._RemotingHelp.ExecuteMethod("PurviewManager", "Core.Mes.PurviewManager.QuestionReply", "delQuestion",
                new object[] { sArgs },
                out err);
            if ((int)obj > 0)
            {
                int idx = curTopRow.Index;
                curTopRow = null;
                doQuery();
                if (idx < ultraGrid1.Rows.Count)
                {
                    ultraGrid1.Rows[idx].Activate();
                }
                else if (idx == ultraGrid1.Rows.Count && ultraGrid1.Rows.Count > 0)
                {
                    ultraGrid1.Rows[idx - 1].Activate();
                }
            }
        }

        private void frmQuestionFeedback_Load(object sender, EventArgs e)
        {
            ulcmeQuestionType.SelectedIndex = 0;

            ultraGrid1.DisplayLayout.Override.MinRowHeight = 21;
            ultraGrid1.DisplayLayout.Bands[0].Override.HeaderAppearance.BackColor = System.Drawing.Color.LightSteelBlue;
            ultraGrid1.DisplayLayout.Bands[0].Override.RowSelectorAppearance.BackColor = System.Drawing.Color.LightSteelBlue;
            ultraGrid1.DisplayLayout.Bands[0].Override.RowAppearance.BackColor = Color.LightGray;

            ultraGrid1.DisplayLayout.Bands[1].Override.HeaderAppearance.BackColor = Color.FromArgb(233, 242, 199);
            ultraGrid1.DisplayLayout.Bands[1].Override.HeaderAppearance.BackColor2 = Color.FromArgb(170, 184, 131);
            ultraGrid1.DisplayLayout.Bands[1].Override.HeaderAppearance.BackGradientStyle = Infragistics.Win.GradientStyle.Vertical;

            ultraGrid1.DisplayLayout.Bands[1].Override.RowSelectorAppearance.BackColor = Color.FromArgb(233, 242, 199);
            ultraGrid1.DisplayLayout.Bands[1].Override.RowSelectorAppearance.BackColor2 = Color.FromArgb(170, 184, 131);
            ultraGrid1.DisplayLayout.Bands[1].Override.RowSelectorAppearance.BackGradientStyle = Infragistics.Win.GradientStyle.Vertical;

            ultraGrid1.DisplayLayout.Bands[1].Override.RowAppearance.BorderColor = Color.FromArgb(170, 184, 131);

            string err = "";
            object obj = ClientCommon._RemotingHelp.ExecuteMethod("PurviewManager", "Core.Mes.PurviewManager.UAM_DEPARTMENT", "SelectUAM_DEPARTMENT",
                new object[] { "" },
                out err);
            if (obj != null)
            {
                DataSet dset = (DataSet)obj;
                foreach (DataRow drow in dset.Tables[0].Rows)
                {
                    ulcmeDepartment.Items.Add(drow["DEPARTMENTNAME"].ToString());
                }
            }

            uldtpTime1_Q.DateTime = uldtpTime1_Q.DateTime.AddDays(-2);
            doQuery();
        }

        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "tlbtnQuery":    // ButtonTool
                    doQuery();
                    break;

                case "tlbtnAdd":    // ButtonTool
                    doAdd();
                    break;

                case "tlbtnEdit":    // ButtonTool
                    doEdit();
                    break;

                case "tlbtnDelete":    // ButtonTool
                    doDelete();
                    break;

                case "tlbtnClose":    // ButtonTool
                    Close();
                    break;
            }
        }

        Infragistics.Win.UltraWinGrid.UltraGridRow curTopRow = null;
        private void ultraGrid1_AfterRowActivate(object sender, EventArgs e)
        {
            Infragistics.Win.UltraWinGrid.UltraGridRow arow = ultraGrid1.ActiveRow;
            if (arow.Band.Index == 0)
            {
                if (arow != curTopRow)
                {
                    curTopRow = arow;
                }
            }
            else
            {
                curTopRow = arow.ParentRow;
            }
            ultraToolbarsManager1.Tools["tlbtnEdit"].SharedProps.Enabled = (curTopRow.Cells["CREATOR"].Text.Trim() == ClientCommon._UserInfo.UserName && curTopRow.Cells["LASTANSWERER"].Text.Trim() == "");
            ultraToolbarsManager1.Tools["tlbtnDelete"].SharedProps.Enabled = (curTopRow.Cells["CREATOR"].Text.Trim() == ClientCommon._UserInfo.UserName && curTopRow.Cells["LASTANSWERER"].Text.Trim() == "");
            txtQuestion.Text = curTopRow.Cells["QUESTION"].Text.Trim();
            ulcmeDepartment.Text = curTopRow.Cells["DEPARTMENT"].Text.Trim();
            txtPrincipal.Text = curTopRow.Cells["PRINCIPAL"].Text.Trim();
            mtxtPhono.Text = curTopRow.Cells["PHONE"].Text.Trim();
            ulcmeQuestionType.Text = curTopRow.Cells["QUESTIONTYPE"].Text.Trim();
        }

        private void ultraGrid1_DoubleClickCell(object sender, Infragistics.Win.UltraWinGrid.DoubleClickCellEventArgs e)
        {
            if (curTopRow != null)
            {
                frmReply frmReply_1 = new frmReply(curTopRow.Cells["ID_"].Text, curTopRow.Cells["FLAG"].Value.ToString(), curTopRow.Cells["CREATOR"].Text, curTopRow.Cells["PRINCIPAL"].Text, curTopRow.Cells["QUESTION"].Text);
                frmReply_1.ShowDialog();

                string sID = curTopRow.Cells["ID_"].Text;
                doQuery();
                foreach (Infragistics.Win.UltraWinGrid.UltraGridRow arow in ultraGrid1.Rows)
                {
                    if (arow.Cells["ID_"].Text == sID)
                    {
                        arow.Activate();
                        break;
                    }
                }
            }
        }

        private void ultraGrid1_InitializeRow(object sender, Infragistics.Win.UltraWinGrid.InitializeRowEventArgs e)
        {
            if (e.Row.Band.Index == 0)
            {
                if (e.Row.Cells["FLAG"].Value.ToString() == "1")
                    e.Row.Cells["QUESTION"].Appearance.ForeColor = Color.Green;
                else
                    e.Row.Cells["QUESTION"].Appearance.ForeColor = Color.Black;
            }
        }
    }//class
}

