using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DbCommon.Enties.DbModels;
using DbCommon.BusinessCore.BaseCore;

namespace ProjectManagementSystem.UI
{
    public partial class LocationAddUI : UserControl, IDisplayUI
    {
        public LocationAddUI()
        {
            InitializeComponent();
        }

        public bool Init()
        {
            try
            {
                WareHouseManager wareHouseManager = new WareHouseManager();
                DataTable dt = wareHouseManager.QueryWareHouse();

                if (dt == null
                    || dt.Rows.Count == 0)
                {
                    return false;
                }

                this.comboBox1.ValueMember = "WareHouseCode";
                this.comboBox1.DisplayMember = "WareHouseName";
                this.comboBox1.DataSource = dt;

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"初始化异常，{ex.Message}");
            }

            return false;
        }

        public void UpdateDisplay()
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //批量插入
            try
            {
                string wHouse = this.comboBox1.SelectedValue.ToString();
                string wHouseName = this.comboBox1.Text;
                string preLocation = this.textBox1.Text;
                int start = int.Parse(this.textBox2.Text);
                int end = int.Parse(this.textBox3.Text);

                List<LocationProperty> locationList = new List<LocationProperty>();
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                for (int i = start; i <= end; i++)
                {
                    LocationProperty location = new LocationProperty();
                    location.WareHouseCode = wHouse;
                    location.LocationCode = $"{preLocation}{i}";

                    location.Status = LocationStatus.Empty;
                    location.Active = "N";
                    location.ModifyUser = "xjh";
                    location.ModifyTime = time;
                    location.Area = wHouseName;
                    locationList.Add(location);
                }

                LocationPropertyManager manager = new LocationPropertyManager();
                var result = manager.Insert(locationList);

                if(result)
                {
                    MessageBox.Show("批量插入完成");
                }
                else
                {
                    MessageBox.Show("批量插入失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"批量插入异常，{ex.Message}");
            }

        }

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

                //string wHouse = this.comboBox1.SelectedValue.ToString();
                string preLocation = this.textBox1.Text;
                int start = int.Parse(this.textBox2.Text);
                int end = int.Parse(this.textBox3.Text);

                LocationPropertyManager manager = new LocationPropertyManager();
                List<string> locationList = new List<string>();
                for (int i = start; i <= end; i++)
                {
                    locationList.Add($"{preLocation}{i}");
                }

                var result = manager.Delete(locationList.ToArray());
                if (result)
                {
                    MessageBox.Show("批量删除完成");
                }
                else
                {
                    MessageBox.Show("批量删除失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"批量删除异常，{ex.Message}");
            }

        }
    }
}
