using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using HighWayIot.Winform.Business;
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;

namespace HighWayIot.Winform.UserControlPages.SysConfigPages
{
    public partial class MaterialAddForm : Form
    {
        /// <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;

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

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

            MaterialTypeComboBox.DataSource = MaterialTypeArray;
        }

        /// <summary>
        /// 确认添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfrimAddButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(MaterialCodeTextBox.Text.Trim()))
            {
                MessageBox.Show("物料编号不能为空！");
                return;
            }

            string s = MaterialCodeTextBox.Text.Trim();

            if(zxMaterialService.GetEntityByMaterialCode(s).Count > 0)
            {
                MessageBox.Show("已存在相同物料编号，添加失败");
                return;
            }

            ZxMaterialEntity entity = new ZxMaterialEntity()
            {
                MaterialCode = s,
                MaterialName = MaterialNameTextBox.Text.Trim(),
                MaterialType = MaterialTypeComboBox.Text.Trim(),
                ChildType = ChildComboBox.Text,
                IsUse = IsUseCheckBox.Checked,
                IsDeleted = false
            };

            if (!zxMaterialService.InsertMaterialInfo(entity))
            {
                MessageBox.Show("物料信息添加失败！");
                return;
            }

            this.Close();
            this.Dispose();
        }

        /// <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;
        }
    }
}
