using HighWayIot.Repository.service;
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;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HighWayIot.Winform.UserControlPages.MaterialConfigPages
{
    public partial class MaterialTypeConfigPage : UserControl
    {

        /// <summary>
        /// 物料子类型服务类实例
        /// </summary>
        ZxMaterialChildTypeService zxMaterialChildTypeService = ZxMaterialChildTypeService.Instance;

        /// <summary>
        /// 物料类型服务类实例
        /// </summary>
        ZxMaterialTypeService zxMaterialTypeService = ZxMaterialTypeService.Instance;

        /// <summary>
        /// 物料子类型列表
        /// </summary>
        List<ZxMaterialChildTypeEntity> zxMaterialChildTypeList;

        /// <summary>
        /// 物料类型列表
        /// </summary>
        List<ZxMaterialTypeEntity> zxMaterialTypeList;

        public MaterialTypeConfigPage()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            MaterialTypeDataGridView.AutoGenerateColumns = false;
            MaterialChildTypeDataGridView.AutoGenerateColumns = false;

            zxMaterialTypeList = zxMaterialTypeService.GetMaterialTypeInfos();

            MaterialTypeDataGridView.DataSource = null;
            MaterialTypeDataGridView.DataSource = zxMaterialTypeList;

        }

        /// <summary>
        /// 物料类型添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddMaterialTypeButton_Click(object sender, EventArgs e)
        {
            string material = MaterialTypeTextBox.Text.Trim();

            if (string.IsNullOrEmpty(material))
            {
                MessageBox.Show("物料类型不能为空！", "提示");
                return;
            }

            if (zxMaterialTypeService.GetMaterialTypeInfos(x => x.MaterialTypeName == material).Count != 0)
            {
                MessageBox.Show("物料类型重复！", "提示");
                return;
            }

            zxMaterialTypeService.InsertMaterialTypeInfo(new ZxMaterialTypeEntity()
            {
                MaterialTypeName = material,
            });

            zxMaterialTypeList = zxMaterialTypeService.GetMaterialTypeInfos();

            MaterialTypeDataGridView.DataSource = null;
            MaterialTypeDataGridView.DataSource = zxMaterialTypeList;
        }

        /// <summary>
        /// 物料子类型添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddMaterialChildTypeButton_Click(object sender, EventArgs e)
        {
            //获取选择的物料类型行
            int a = MaterialTypeDataGridView.CurrentRow.Index;

            string s = MaterialTypeDataGridView.Rows[a].Cells["MaterialTypeName"].Value.ToString();

            string material = MaterialChildTypeTextBox.Text.Trim();

            if (string.IsNullOrEmpty(material))
            {
                MessageBox.Show("物料子类型不能为空！", "提示");
                return;
            }

            if (zxMaterialChildTypeService.GetMaterialChildTypeInfos(x => x.MaterialTypeName == s && x.MaterialChlidTypeName == material).Count != 0)
            {
                MessageBox.Show("物料子类型重复！", "提示");
                return;
            }

            zxMaterialChildTypeService.InsertMaterialChildTypeInfo(new ZxMaterialChildTypeEntity()
            {
                MaterialTypeName = s,
                MaterialChlidTypeName = material,
            });

            zxMaterialChildTypeList = zxMaterialChildTypeService.GetMaterialChildTypeInfos(x => x.MaterialTypeName == s);

            MaterialChildTypeDataGridView.DataSource = null;
            MaterialChildTypeDataGridView.DataSource = zxMaterialChildTypeList;
        }

        /// <summary>
        /// 物料类型删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteMaterialTypeButton_Click(object sender, EventArgs e)
        {
            //获取选择的物料类型行
            int a = MaterialTypeDataGridView.CurrentRow.Index;

            string s = MaterialTypeDataGridView.Rows[a].Cells["MaterialTypeName"].Value.ToString();

            if (MessageBox.Show($"确定要删除{s}吗", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }

            int[] intArray = zxMaterialChildTypeService.GetMaterialChildTypeInfos(x => x.MaterialTypeName == s).Select(x => x.Id).ToArray();
            if (intArray.Length != 0)
            {
                if (MessageBox.Show($"是否要删除其关联的所有子物料类型", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (!zxMaterialChildTypeService.DeleteGroup(intArray))
                    {
                        MessageBox.Show("关联子物料类型删除失败！", "提示");
                    }
                }
            }

            int id = int.Parse(MaterialTypeDataGridView.Rows[a].Cells["Id"].Value.ToString().Trim());

            if (zxMaterialTypeService.DeleteMaterialTypeInfoById(id))
            {
                MessageBox.Show("删除成功！", "提示");
            }

            zxMaterialTypeList = zxMaterialTypeService.GetMaterialTypeInfos();

            MaterialTypeDataGridView.DataSource = null;
            MaterialTypeDataGridView.DataSource = zxMaterialTypeList;
        }

        /// <summary>
        /// 物料子类型删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteMaterialChildTypeButton_Click(object sender, EventArgs e)
        {
            //获取选择的物料子类型行
            int a = MaterialChildTypeDataGridView.CurrentRow.Index;

            if (MessageBox.Show($"确定要删除{MaterialChildTypeDataGridView.Rows[a].Cells["MaterialChildTypeName"].Value.ToString()}吗", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }

            int id = int.Parse(MaterialChildTypeDataGridView.Rows[a].Cells["ChildId"].Value.ToString().Trim());

            if (zxMaterialChildTypeService.DeleteMaterialChildTypeInfoById(id))
            {
                MessageBox.Show("删除成功！", "提示");
            }

            //获取选择的物料类型行
            a = MaterialTypeDataGridView.CurrentRow.Index;

            string s = MaterialTypeDataGridView.Rows[a].Cells["MaterialTypeName"].Value.ToString();

            zxMaterialChildTypeList = zxMaterialChildTypeService.GetMaterialChildTypeInfos(x => x.MaterialTypeName == s);

            MaterialChildTypeDataGridView.DataSource = null;
            MaterialChildTypeDataGridView.DataSource = zxMaterialChildTypeList;
        }

        /// <summary>
        /// 所选内容更改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MaterialTypeDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            int a = MaterialTypeDataGridView.CurrentRow.Index;
            string s = MaterialTypeDataGridView.Rows[a].Cells["MaterialTypeName"].Value.ToString();

            zxMaterialChildTypeList = zxMaterialChildTypeService.GetMaterialChildTypeInfos(x => x.MaterialTypeName == s);

            MaterialChildTypeDataGridView.DataSource = null;
            MaterialChildTypeDataGridView.DataSource = zxMaterialChildTypeList;
        }
    }
}
