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 ProductionControl
{
    public partial class FrmRoleRight : Form
    { 
        Service.RoleService service = new Service.RoleService();
        private List<Right> rightList;
        public FrmRoleRight()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView2.AutoGenerateColumns = false;
        }
        private void initDataView()
        {
            rightList = service.GetRightItems();
            dataGridView2.DataSource = new BindingSource(rightList, null);

            var list = service.GetListNav();
            //tsslCount.Text = $"共 {list.Count} 条记录";
            dataGridView1.DataSource = new BindingSource(list, null); 
        }
          
        private void FrmRoleRight_Load(object sender, EventArgs e)
        {
            initDataView(); 
        }

        private void tsbtnSave_Click(object sender, EventArgs e)
        {
            dataGridView2.CurrentCell = null;//更新当前编辑单元格

            var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<Role>;
            int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引   
            foreach (var item in rightList)
            {
                if(list[liIndex].RightList.FirstOrDefault(m => m.Id == item.Id) == null)
                {
                    if(item.check)
                        list[liIndex].RightList.Add(item);
                }
                else
                {
                    if (!item.check)
                        list[liIndex].RightList.Remove(item);
                }
            }
            if(service.UpdateNav(list[liIndex]))
                MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("保存失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        } 
        private void tsbtnAdd_Click(object sender, EventArgs e)
        {
            FrmRoleInfo frm=new FrmRoleInfo();
            if(frm.ShowDialog()== DialogResult.OK)
                initDataView();
        } 
        private void tsbtnDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.CurrentRow == null)
                    return;

                var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<Role>;
                int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引    
                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 tsbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<Role>;
            int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引    
            FrmRoleInfo frm = new FrmRoleInfo(list[liIndex]);
            if(frm.ShowDialog(this)== DialogResult.OK)
                initDataView();
        }
          
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            var list = ((BindingSource)dataGridView1.DataSource).DataSource as List<Role>;
            int liIndex = this.dataGridView1.CurrentRow.Index;//获取当前选中行的索引   
            foreach(var item in rightList) 
                item.check = list[liIndex].RightList.FirstOrDefault(m => m.Id == item.Id) != null;

            dataGridView2.Refresh();
        }
    }
}
