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 frmReply : Form
    {
        string _ID = "", _Creator = "", _Principal = "", _Complete = "";

        public frmReply(string sID, string sComplete, string sCreator, string sPrincipal, string sQuestion)
        {
            _ID = sID;
            _Complete = sComplete;
            _Creator = sCreator;
            _Principal = sPrincipal;

            InitializeComponent();

            txtQuestion.Text = sQuestion;
        }

        void doReply()
        {
            if (chbxComplete.Checked == false && txtReplyInfo.Text.Trim() == "")
            {
                txtReplyInfo.Focus();
                MessageBox.Show("请输入回复内容！");
                return;
            }
            string[] sArgs = new string[6];
            sArgs[0] = _ID;
            sArgs[1] = ClientCommon._UserInfo.UserName;
            sArgs[2] = ClientCommon._UserInfo.UserDepName;
            sArgs[3] = txtReplyInfo.Text.Trim();
            sArgs[4] = (chbxComplete.Enabled) ? ((chbxComplete.Checked) ? "1" : "0") : "0";
            sArgs[5] = Dns.Resolve(Dns.GetHostName()).AddressList[0].ToString();
            string err = "";
            object obj = ClientCommon._RemotingHelp.ExecuteMethod("PurviewManager", "Core.Mes.PurviewManager.QuestionReply", "addReply",
                new object[] { sArgs },
                out err);
            if ((int)obj > 0)
            {
                DataSet dset = (DataSet)ultraGrid1.DataSource;
                dset.Tables[0].Rows.Add(new object[] { _ID, int.Parse(err), sArgs[1], sArgs[3], DateTime.Now, sArgs[5], sArgs[4], sArgs[2] });
                ultraGrid1.Rows[ultraGrid1.Rows.Count - 1].Activate();
                foreach (Infragistics.Win.UltraWinGrid.UltraGridRow arow in ultraGrid1.Selected.Rows)
                    arow.Selected = false;

                _Complete = sArgs[4];
                ultraToolbarsManager1.Tools["tlbtnReply"].SharedProps.Visible = (_Complete == "0");
                ultraToolbarsManager1.Tools["tlbtnDelete"].SharedProps.Visible = (_Complete == "0");
            }
            else
                MessageBox.Show(err);
        }

        void doEdit()
        {
        }

        void doDelete()
        { 
            Infragistics.Win.UltraWinGrid.UltraGridRow arow = ultraGrid1.ActiveRow;
            if (arow == null)
                return;
            if (MessageBox.Show("您确定要删除？", "选择", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) ==
                DialogResult.No)
                return;
            string[] sArgs = new string[2];
            sArgs[0] = _ID;
            sArgs[1] = arow.Cells["XH"].Value.ToString();
            string err = "";
            object obj = ClientCommon._RemotingHelp.ExecuteMethod("PurviewManager", "Core.Mes.PurviewManager.QuestionReply", "delReply",
                new object[] { sArgs },
                out err);
            if ((int)obj > 0)
            {
                int idx = arow.Index;
                arow.Delete(false);
                if (idx < ultraGrid1.Rows.Count)
                {
                    ultraGrid1.Rows[idx].Activate();
                }
                else if (idx == ultraGrid1.Rows.Count && ultraGrid1.Rows.Count > 0)
                {
                    ultraGrid1.Rows[idx - 1].Activate();
                }
            }
            else
                MessageBox.Show(err);
        }

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

                case "tlbtnEdit":    // ButtonTool
                    break;

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

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

            }
        }

        private void frmReply_Load(object sender, EventArgs e)
        {
            string err = "";
            object obj = ClientCommon._RemotingHelp.ExecuteMethod("PurviewManager", "Core.Mes.PurviewManager.QuestionReply", "getReplyData",
                new object[] { _ID },
                out err);
            if (obj != null)
            {
                ultraGrid1.DataSource = (DataSet)obj;
            }
            ultraToolbarsManager1.Tools["tlbtnReply"].SharedProps.Visible = (_Complete == "0");
            //ultraToolbarsManager1.Tools["tlbtnEdit"].SharedProps.Visible = (_Complete == "0");
            ultraToolbarsManager1.Tools["tlbtnDelete"].SharedProps.Visible = (_Complete == "0");
            chbxComplete.Enabled = (ClientCommon._UserInfo.UserName == _Creator || ClientCommon._UserInfo.UserName == _Principal);
        }

        private void ultraGrid1_AfterRowActivate(object sender, EventArgs e)
        {
            Infragistics.Win.UltraWinGrid.UltraGridRow arow = ultraGrid1.ActiveRow;
            //ultraToolbarsManager1.Tools["tlbtnEdit"].SharedProps.Enabled = (ClientCommon._UserInfo.UserName == arow.Cells["ANSWERER"].Text.Trim() &&
            //    arow.Index == ultraGrid1.Rows.Count-1);
            ultraToolbarsManager1.Tools["tlbtnDelete"].SharedProps.Enabled = (ClientCommon._UserInfo.UserName == arow.Cells["ANSWERER"].Text.Trim() &&
                arow.Index == ultraGrid1.Rows.Count - 1);

            txtReplyInfo.Text = "";
            chbxComplete.Checked = false;
        }

        private void ultraGrid1_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs e)
        {
            //txtReplyInfo.Text = e.Row.Cells["REPLYINFO"].Text;
            //chbxComplete.Checked = (e.Row.Cells["REPLYINFO"].Value.ToString() == "1");
        }
    }//class
}