using HighWayIot.Log4net;
using HighWayIot.Repository.service;
using HighWayIot.Winform.Business;
using HighWayIot.Winform.UserControlPages.SysConfigPages;
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;
using System.Windows.Forms.VisualStyles;

namespace HighWayIot.Winform.UserControlPages
{
    public partial class MaterialConfigPage : UserControl
    {
        LogHelper logHelper = LogHelper.Instance;

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

        /// <summary>
        /// 物料子类型数组
        /// </summary>
        string[] MaterialChildTypeArray;

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

        /// <summary>
        /// 物料类型数组
        /// </summary>
        string[] MaterialTypeArray;

        /// <summary>
        /// 物料信息服务类实例
        /// </summary>
        ZxMaterialService zxMaterialService = ZxMaterialService.Instance;

        /// <summary>
        /// 物料信息列表
        /// </summary>
        List<ZxMaterialEntity> Lists;

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

        private void Init()
        {
            MaterialDataGridView.AutoGenerateColumns = false;

            //绑定物料类型
            MaterialTypeArray = zxMaterialTypeService.GetMaterialTypeInfos().Select(x => x.MaterialTypeName).ToArray();
            MaterialTypeArray = GeneralUtils.HeadAddEmptyString(MaterialTypeArray);

            MaterialTypeComboBox.DataSource = MaterialTypeArray;

            Lists = zxMaterialService.GetMaterialInfos();

            MaterialDataGridView.DataSource = null;
            MaterialDataGridView.DataSource = Lists;
        }
 
        /// <summary>
        /// 添加物料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddMaterial_Click(object sender, EventArgs e)
        {
            MaterialAddForm form = new MaterialAddForm();
            form.ShowDialog();
            Lists = zxMaterialService.GetMaterialInfos();
            MaterialDataGridView.DataSource = null;
            MaterialDataGridView.DataSource = Lists;
        }

        /// <summary>
        /// 更新物料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateMaterial_Click(object sender, EventArgs e)
        {
            ZxMaterialEntity entity = new ZxMaterialEntity();
            int a = MaterialDataGridView.CurrentRow.Index;
            try
            {
                entity.Id = int.Parse(MaterialDataGridView.Rows[a].Cells["Id"].Value.ToString().Trim());
            }
            catch (Exception ex)
            {
                 MessageBox.Show("物料更新ID转换发生错误" + ex.Message);
            }
            entity.MaterialCode = MaterialDataGridView.Rows[a].Cells["MaterialCode"].Value.ToString();
            entity.MaterialName = MaterialDataGridView.Rows[a].Cells["MaterialName"].Value.ToString();
            entity.MaterialType = MaterialDataGridView.Rows[a].Cells["MaterialType"].Value.ToString();
            entity.ChildType = MaterialDataGridView.Rows[a].Cells["ChildType"].Value.ToString();
            bool.TryParse(MaterialDataGridView.Rows[a].Cells["IsUse"].Value.ToString(), out bool b);
            entity.IsUse = b;
            entity.IsDeleted = false;
            MaterialUpdateForm form = new MaterialUpdateForm(entity);
            form.ShowDialog();
            Lists = zxMaterialService.GetMaterialInfos();
            MaterialDataGridView.DataSource = null;
            MaterialDataGridView.DataSource = Lists;
        }

        /// <summary>
        /// 删除物料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteMaterial_Click(object sender, EventArgs e)
        {
            int a = MaterialDataGridView.CurrentRow.Index;
            string s = MaterialDataGridView.Rows[a].Cells["MaterialCode"].Value.ToString();
            int id;
            try
            {
                id = int.Parse(MaterialDataGridView.Rows[a].Cells["Id"].Value.ToString());
            }
            catch (Exception ex)
            {
                logHelper.Error("物料删除ID转换发生错误", ex);
                return;
            }

            if (MessageBox.Show($"是否要删除编号为{s}的物料?", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }

            if (!zxMaterialService.DeleteMaterialInfoById(id))
            {
                MessageBox.Show($"编号为{s}的物料删除失败", "提示");
            }

            Lists = zxMaterialService.GetMaterialInfos();
            MaterialDataGridView.DataSource = null;
            MaterialDataGridView.DataSource = Lists;
        }

        /// <summary>
        /// 查询物料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectMaterial_Click(object sender, EventArgs e)
        {
            Lists = zxMaterialService.GetMaterialInfos(MaterialNameTextbox.Text.Trim(), MaterialTypeComboBox.Text.Trim(), ChildComboBox.Text.Trim(), IsAvailableUseCheckBox.Checked, IsUseCheckBox.Checked);
            MaterialDataGridView.DataSource = null;
            MaterialDataGridView.DataSource = Lists;
        }

        /// <summary>
        /// 物料类型更改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MaterialTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MaterialChildTypeArray = zxMaterialChildTypeService.GetMaterialChildTypeInfos(x => x.MaterialTypeName == MaterialTypeComboBox.Text).Select(x => x.MaterialChlidTypeName).ToArray();
            MaterialChildTypeArray = GeneralUtils.HeadAddEmptyString(MaterialChildTypeArray);

            ChildComboBox.DataSource = MaterialChildTypeArray;
        }
    }
}
