using HKLib.DB.Model.Entity;
using HKLib.Dto;
using HKLib.Interfaces;
using HKLib.Logic;
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 HKCardManager.UserPages
{
    public partial class PersonnelEntryPage : UserControl
    {
        //List<string> OrgList = new List<string>();
        List<OrgTable> orgTables = new List<OrgTable>();

        public PersonnelEntryPage()
        {
            InitializeComponent();

            #region 禁止更改宽高 
            // 禁止用户改变DataGridView的所有列的列宽   
            dataGridView1.AllowUserToResizeColumns = false;
            //禁止用户改变DataGridView所有行的行高   
            dataGridView1.AllowUserToResizeRows = false;
            // 禁止用户改变列头的高度   
            dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            // 禁止用户改变列头的宽度   
            dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            #endregion 


            this.SizeChanged += PersonnelEntryPage_SizeChanged;
            dataGridView1.AutoGenerateColumns = false;
            comboBox1.SelectedIndex = 0;
            orgTables = ServiceHandler.GetOrgList();
            Global.OrgList.Clear();
            orgTables?.ForEach(item =>
            {
                Global.OrgList.Add(item.Name);
            });
            comboBox2.DataSource = Global.OrgList;
            Task.Factory.StartNew(() =>
            {
                var res = HKLibHelper.GetUserList("");
                Global.UserListDtos.Clear();
                res?.ToList()?.ForEach(item =>
                 {
                     string status = "";
                     string cardNum = "";
                     if (item.Cards != null && item.Cards.Count > 0)
                     {
                         cardNum = item.Cards.ElementAt(0).CardNum;
                         status = ((CarStatus)item.Cards.ElementAt(0).State).ToString();
                     }
                     Global.UserListDtos.Add(new UserInfoModel()
                     {
                         UserName = item.Name,
                         Phone = item.Phone,
                         OrgName = item.OrgInfo?.Count > 0 ? item.OrgInfo.ElementAt(0).Name : "",
                         CardNum = cardNum,
                         State = status
                     });
                 });
                //TestData();
                this.Invoke(() => { dataGridView1.DataSource = null; dataGridView1.DataSource = Global.UserListDtos; });
            });
        }

        private void TestData()
        {
            for (int i = 0; i < 10; i++)
            {
                Global.UserListDtos.Add(new UserInfoModel()
                {
                    CardNum = Guid.NewGuid().ToString(),
                    State = CarStatus.正常.ToString(),
                    OrgName = "研发部",
                    Phone = "13545893363",
                    UserName = $"张{i}"
                });
            }
        }

        private void PersonnelEntryPage_SizeChanged(object? sender, EventArgs e)
        {
            //panel1.Left = (this.Width - panel1.Width) / 2;
            //panel1.Top = (this.Height - panel1.Height) / 2;
            var tt1 = dataGridView1.Size;
            var tt = this.Size;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                MessageLogNotify.GetInstance.ShowWarning("请输入姓名");
                return;
            }
            if (!string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                if (!textBox2.Text.Trim().IsMobile())
                {
                    MessageLogNotify.GetInstance.ShowWarning("手机号格式不正确");
                    return;
                }
            }
            UserDto userDto = new UserDto()
            {
                Name = textBox1.Text.Trim(),
                Phone = textBox2.Text.Trim(),
                OrgId = orgTables.FirstOrDefault(p => p.Name == comboBox2.Text)?.SId
            };
            string OrgName = comboBox2.Text;
            Task.Factory.StartNew(() =>
            {
                this.Invoke(() => { button2.Enabled = false; });
                if (HKLibHelper.AddUser(userDto))
                {
                    Global.UserListDtos.Add(new UserInfoModel()
                    {
                        OrgName = OrgName,
                        Phone = userDto.Phone,
                        UserName = userDto.Name
                    });
                    this.Invoke(() =>
                    {
                        dataGridView1.DataSource = null;
                        dataGridView1.DataSource = Global.UserListDtos;
                    });
                    MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 添加成功");
                }
                else
                    MessageLogNotify.GetInstance.ShowError($"用户 【{userDto.Name}】 添加失败，请重试！");
                this.Invoke(() => { button2.Enabled = true; });
            });

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dataGridView1.CurrentRow.Index;
            if (index <= dataGridView1.Rows.Count - 1 && dataGridView1.CurrentCell.Value.ToString() == "删除")
            {
                MessageLogNotify.GetInstance.Show($"用户名称：{dataGridView1.Rows[index].Cells[0].Value }");
            }
        }
    }
}
