using HKLib.Dto;
using HKLib.Interfaces;
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;
//using BPA.Helper;

namespace HKCardManager.UserPages
{
    public partial class CancellationPage : UserControl
    {
        public CancellationPage()
        {
            InitializeComponent();
            //this.SizeChanged += CancellationPage_SizeChanged;
        }

        private void Ref()
        {
            var res = HKLibHelper.GetUserListSync("");
            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()
                {
                    Num = item.Num,
                    UserName = item.Name,
                    Phone = item.Phone,
                    OrgName = item.OrgInfo?.Count > 0 ? item.OrgInfo.ElementAt(0).Name : "",
                    CardNum = cardNum,
                    State = status
                });
            });
        }

        private async void button2_Click(object sender, EventArgs e)
        {

            string name = textBox1.Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("提示！！！\n 请输入用户名...");
                MessageLogNotify.GetInstance.ShowWarning("请输入用户名");
                return;
            }
            string TagName = this.Tag.ToString();
            if (string.IsNullOrEmpty(TagName))
            {
                MessageBox.Show("提示！！！\n 功能名称为空，操作失败，请重试...");
                MessageLogNotify.GetInstance.ShowError("功能名称为空，操作失败，请重试");
            }
            else
            {
                int status = 0;
                if (TagName == "销户") status = 3;
                else if (TagName == "挂失") status = 2;
                else if (TagName == "解挂") status = 1;
                //Task.Factory.StartNew(() =>
                //{
                //this.Invoke(() => { button2.Enabled = false; });
                button2.Enabled = false;
                Ref();
                var temp = Global.UserListDtos.FirstOrDefault(p => p.UserName == name);
                if (temp != null)
                {
                    if (temp.State == "作废")
                    {
                        MessageBox.Show($"用户【{temp.UserName}】卡片已作废，不允许进行解卦和挂失操作");
                        return;
                    }


                    if (status == 3 && temp.State == "挂失")
                    {
                        MessageBox.Show($"用户【{temp.UserName}】挂失状态不能销户");
                        return;
                    }


                    if (!string.IsNullOrEmpty(temp.CardNum))
                    {
                        if (MessageBox.Show($"确认是否[{TagName}]用户[{temp.UserName}]!", "操作", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            var res = await HKLibHelper.CardStutasChange(new CardStutasDto()
                            {
                                keywrod = name,
                                Stutas = status
                            });
                            if (status == 3)
                            {
                                await HKLibHelper.DisableUser(name);
                            }
                            if (res)
                            {
                                MessageBox.Show($"用户【{temp.UserName}】 {TagName} 成功");
                                MessageLogNotify.GetInstance.Show($"用户【{temp.UserName}】 {TagName} 成功");
                            }
                            else
                            {
                                MessageBox.Show($"用户【{temp.UserName}】 {TagName} 失败，请检查名称是否正常");
                                MessageLogNotify.GetInstance.ShowError($"用户【{temp.UserName}】 {TagName} 失败，请检查名称是否正常");
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("提示！！！\n 该用户未注册卡号，请重试...");

                        MessageLogNotify.GetInstance.ShowError("该用户未注册卡号");
                    }
                }
                else
                {
                    MessageBox.Show("提示！！！\n 该用户不存在...");
                    MessageLogNotify.GetInstance.ShowError("该用户不存在");
                }
                //this.Invoke(() => { button2.Enabled = true; });
                button2.Enabled = true;
                //});
            }
        }
    }
}
