using CasunCommon.BaseUI;
using Pms.Common.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PMSUI
{
    public partial class CStepTemplateForm : CasunForm
    {
        public CStepTemplateForm()
        {
            InitializeComponent();
        }

        private void CStepTemplateForm_Load(object sender, EventArgs e)
        {
            CasunUILib.ButtonHelps(btHelps);

            gp_Read.Location = new Point(btHelps.Location.X, gp_Read.Location.Y);
            gp_Opt.Location = new Point(btHelps.Location.X, gp_Opt.Location.Y);

            CasunUILib.GroupBoxReadModel(gp_Read, PermissionLevel);
            CasunUILib.GroupBoxReadWriteModel(gp_Opt, PermissionLevel);
            CasunUILib.GroupBoxReadWriteModel(gp_Opt1, PermissionLevel);

            CasunUILib.splitContainerModel(splitContainer1);
            Init();
        }

        private void cbTemplateID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbTemplateID.SelectedIndex != -1)
            {
                DGVTaskTemplate.Rows.Clear();
                DGVTaskTemplateChange(cbTemplateID.SelectedItem.ToString());

                tbStepID.Text = "";
                tbBehaviour.Text = "";
                tbAGVBehaviour.Text = "";
                tbNextSpcialStep.Text = "";
                tbNextDefaultStep.Text = "";
            }
        }

        private void DGVTaskTemplateChange(string TemplateID)
        {
            //List<PmsCoreDB.Model.PMS_bas_BookTemplate> Lt_stasktemplate = CasunModelDB.CDBManager.Instance.PMS_bas_TaskTemplate.GetTaskTemplatebyTemplateID(TemplateID);
            List<PmsCoreDB.Model.PMS_bas_StepBookTemplate> Lt_stasktemplate = PmsCoreDB.DBHost.Instance.StepBookTemplate.GetTemplateListByTemplateID(TemplateID);

            foreach (PmsCoreDB.Model.PMS_bas_StepBookTemplate stasktemplate in Lt_stasktemplate)
            {
                string[] rowData = new string[5];
                rowData[0] = stasktemplate.StepID.ToString();
                rowData[1] = stasktemplate.Behavior.ToString();
                rowData[2] = stasktemplate.AgvBehavior.ToString();
                rowData[3] = stasktemplate.NextDefaultStep.ToString();
                rowData[4] = stasktemplate.NextSpecialStep.ToString();
                DGVTaskTemplate.Rows.Add(rowData);
            }
        }

        private void Init()
        {
            CasunUILib.DgvModel(DGVTaskTemplate);

            //List<string> LTemplateID = CasunModelDB.CDBManager.Instance.PMS_bas_TaskTemplate.GetTemplateID();
            //List<string> LTemplateID = PmsCoreDB.DBHost.Instance.BookTemplate.GetTemplateID();
            List<string> LTemplateID = PmsCoreDB.DBHost.Instance.StepBookTemplate.GetTemplateID();

            cbTemplateID.Items.AddRange(LTemplateID.ToArray());
            if (LTemplateID.Count > 0)
            {
                cbTemplateID.SelectedIndex = 0;
            }

        }


        private void btAddTemplate_Click(object sender, EventArgs e)
        {

            if (!cbTemplateID.Items.Contains(cbTemplateID.Text.ToString()))
            {
                cbTemplateID.Items.Add(cbTemplateID.Text);
            }
            else
            {
                string strMsg = CLanguageManager.LanguageChange("模板")
                    + "：【" + cbTemplateID.Text.ToString() + "】"
                    + CLanguageManager.LanguageChange("已存在")
                    + "！";
                MessageBox.Show(strMsg);
            }
        }

        private void btDeleteTaskTemplate_Click(object sender, EventArgs e)
        {

            if (cbTemplateID.SelectedIndex == -1)
            {
                return;
            }
            string strMsg = CLanguageManager.LanguageChange("是否删除模板") + "：【" + cbTemplateID.Text.ToString() + "】\r\n"
                + CLanguageManager.LanguageChange("请谨慎操作")
                + "！";
            if (MessageBox.Show(strMsg, CLanguageManager.LanguageChange("删除"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                //if (CasunModelDB.CDBManager.Instance.PMS_bas_TaskTemplate.DeleteTaskTemplateDate(cbTemplateID.Text.ToString()))
                //if (PmsCoreDB.DBHost.Instance.BookTemplate.DeleteTaskTemplateDate(cbTemplateID.Text.ToString()))
                if (PmsCoreDB.DBHost.Instance.StepBookTemplate.DeleteTaskTemplateDate(cbTemplateID.Text.ToString()))
                {
                    cbTemplateID.Items.Remove(cbTemplateID.Text.ToString());
                    DGVTaskTemplate.Rows.Clear();
                }
                else
                {

                }
            }
        }

        private void btAddStep_Click(object sender, EventArgs e)
        {


            if (tbStepID.Text != null && tbBehaviour.Text != null && tbAGVBehaviour.Text != null)
            {
                string[] data = new string[5] { tbStepID.Text.ToString(), tbBehaviour.Text.ToString(), tbAGVBehaviour.Text.ToString(), tbNextDefaultStep.Text.ToString(), tbNextSpcialStep.Text.ToString() };
                DGVTaskTemplate.Rows.Add(data);
            }
        }

        private void btDeleteStep_Click(object sender, EventArgs e)
        {

            if (DGVTaskTemplate.CurrentRow == null)
            {
                return;
            }
            int currentIndex = DGVTaskTemplate.CurrentRow.Index;
            if (currentIndex != -1)
            {
                DGVTaskTemplate.Rows.RemoveAt(currentIndex);
            }

        }

        private void btModfiyStep_Click(object sender, EventArgs e)
        {
            if (DGVTaskTemplate.CurrentRow == null)
            {
                return;
            }
            int currentIndex = DGVTaskTemplate.CurrentRow.Index;
            if (currentIndex != -1)
            {
                string[] data = new string[5] { tbStepID.Text.ToString(), tbBehaviour.Text.ToString(), tbAGVBehaviour.Text.ToString(), tbNextDefaultStep.Text.ToString(), tbNextSpcialStep.Text.ToString() };
                DGVTaskTemplate.Rows[currentIndex].Cells[0].Value = data[0];
                DGVTaskTemplate.Rows[currentIndex].Cells[1].Value = data[1];
                DGVTaskTemplate.Rows[currentIndex].Cells[2].Value = data[2];
                DGVTaskTemplate.Rows[currentIndex].Cells[3].Value = data[3];
                DGVTaskTemplate.Rows[currentIndex].Cells[4].Value = data[4];
            }
        }

        private void btSaveStep_Click(object sender, EventArgs e)
        {
            if (cbTemplateID.SelectedItem != null)
            {
                string TemplateID = cbTemplateID.SelectedItem.ToString();

                DataTable dt = new DataTable();
                dt.Columns.Add("StepID");
                dt.Columns.Add("Behavior");
                dt.Columns.Add("AgvBehavior");
                dt.Columns.Add("NextDefaultStep");
                dt.Columns.Add("NextSpecialStep");
                string strMsg = "";
                for (int r = 0; r < DGVTaskTemplate.Rows.Count; r++)
                {
                    string[] data = new string[5] {
                          DGVTaskTemplate.Rows[r].Cells[0].Value.ToString()
                        , DGVTaskTemplate.Rows[r].Cells[1].Value.ToString()
                        , DGVTaskTemplate.Rows[r].Cells[2].Value.ToString()
                        , DGVTaskTemplate.Rows[r].Cells[3].Value.ToString()
                        , DGVTaskTemplate.Rows[r].Cells[4].Value.ToString()};
                    strMsg += "【" + data[0] + "、" + data[1] + "、" + data[2] + "、" + data[3] + "、" + data[4] + "】";
                    dt.Rows.Add(data);
                }

                //if (CasunModelDB.CDBManager.Instance.PMS_bas_TaskTemplate.SaveTaskTemplateDate(TemplateID, dt))
                if (PmsCoreDB.DBHost.Instance.StepBookTemplate.SaveTaskTemplateDate(TemplateID, dt))
                {
                    string strOptLog = string.Format("【{0}】:TemplateID={1},{2}={3}"
                        , CLanguageManager.LanguageChange("保存步骤模板")
                        , TemplateID
                        , CLanguageManager.LanguageChange("信息")
                        , strMsg);
                    COperateLog.Instance.InsertOperaterLogAsync(UserID, strOptLog, DateTime.Now);

                    MessageBox.Show(CLanguageManager.LanguageChange("保存成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("保存失败") + "！");
                }
            }

        }

        private void DGVTaskTemplate_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (DGVTaskTemplate.CurrentRow == null)
            {
                return;
            }
            int DGVCurrentRow = DGVTaskTemplate.CurrentRow.Index;
            if (DGVCurrentRow != -1 && DGVTaskTemplate.Rows[DGVCurrentRow] != null)
            {
                if (DGVTaskTemplate.Rows[DGVCurrentRow].Cells[0].Value != null)
                {
                    tbStepID.Text = DGVTaskTemplate.Rows[DGVCurrentRow].Cells[0].Value.ToString();
                }

                if (DGVTaskTemplate.Rows[DGVCurrentRow].Cells[1].Value != null)
                {
                    tbBehaviour.Text = DGVTaskTemplate.Rows[DGVCurrentRow].Cells[1].Value.ToString();
                }

                if (DGVTaskTemplate.Rows[DGVCurrentRow].Cells[2].Value != null)
                {
                    tbAGVBehaviour.Text = DGVTaskTemplate.Rows[DGVCurrentRow].Cells[2].Value.ToString();
                }

                if (DGVTaskTemplate.Rows[DGVCurrentRow].Cells[3].Value != null)
                {
                    tbNextDefaultStep.Text = DGVTaskTemplate.Rows[DGVCurrentRow].Cells[3].Value.ToString();
                }

                if (DGVTaskTemplate.Rows[DGVCurrentRow].Cells[4].Value != null)
                {
                    tbNextSpcialStep.Text = DGVTaskTemplate.Rows[DGVCurrentRow].Cells[4].Value.ToString();
                }
            }
        }

        private void btHelps_Click(object sender, EventArgs e)
        {
            MessageBox.Show(PMSUI.Config.CConfigManager.gs_FormHelpConfig.GetFormHelp(this.Name));

        }
    }
}
