using Models;
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 AssistClient
{
    public partial class FrmStepList : Form
    {
        Service.StepService service=new Service.StepService();
        public FrmStepList()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false; 
        }
        private void initDataView()
        {
            var list = service.GetListNav(1);
            tsslCount.Text = $"共 {list.Count} 条记录";
            dataGridView1.DataSource = new BindingSource(list, null);
        } 

        private void FrmStepList_Load(object sender, EventArgs e)
        {
            initDataView();
        } 

        private void tsbtnAdd_Click(object sender, EventArgs e)
        {
            FrmStepInfo frm = new FrmStepInfo();
            frm.ShowDialog();
            initDataView();
        }

        private void tsbtnDel_Click(object sender, EventArgs e)
        {
            try
            {
                if(this.dataGridView1.CurrentRow==null)
                    return;

                var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<Step>;
                int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引   
                if (service.InUse(list[liIndex].Id))
                    throw new Exception("此流程已有产品在使用中,不可删除！");
                if (MessageBox.Show($"确认删除？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                { 
                    if (!service.DelNav(list[liIndex]))
                        throw new Exception("删除失败！");
                    MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    initDataView();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void tsbtnClone_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow == null)
            {
                MessageBox.Show("请选择要克隆的流程！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<Step>;
            int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引   

            Step newStep = new Step()
            {
                Tag = 1,
                Code = list[liIndex].Code + "_clone",
                Name = $"{list[liIndex].Name} (克隆)",
                StartTimeIndex = list[liIndex].StartTimeIndex,
                ModifyUserCode = Config.loginUser.Code,
                CreateUserCode = Config.loginUser.Code,
                ProcessList = new List<StepProcess>()
            };
            foreach(var item in list[liIndex].ProcessList)
            {
                newStep.ProcessList.Add(new StepProcess()
                {
                    Order = item.Order,
                    ProcessCode=item.ProcessCode,
                    ProcessName = item.ProcessName,
                    ProcessParams=item.ProcessParams,
                    ModifyUserCode = Config.loginUser.Code,
                    CreateUserCode = Config.loginUser.Code
                });
            }

            try
            {
                bool result = service.InsertNav(newStep);
                if (result)
                {
                    MessageBox.Show("克隆成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    initDataView();
                }
                else
                    throw new Exception("克隆失败！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void tsbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<Step>;
            int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引    
            FrmStepInfo frm = new FrmStepInfo(list[liIndex]);
            frm.ShowDialog(this);
            initDataView();
        }

       
    }
}
