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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace HighWayIot.Winform.UserControlPages.SysConfigPages
{
    public partial class MaterialUpdateForm : Form
    {
        /// <summary>
        /// 物料子类型服务类实例
        /// </summary>
        ZxMaterialChildTypeService zxMaterialChildTypeService = ZxMaterialChildTypeService.Instance;

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

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

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

        /// <summary>
        /// 物料数据库业务实例
        /// </summary>
        private ZxMaterialService zxMaterialService = ZxMaterialService.Instance;

        /// <summary>
        /// 要修改的实体
        /// </summary>
        private ZxMaterialEntity _entity;

        public MaterialUpdateForm(ZxMaterialEntity entity)
        {
            InitializeComponent();
            _entity = entity;

            Init();
        }

        //初始化选择
        private void Init()
        {
            MaterialNameTextBox.Text = _entity.MaterialName;
            MaterialCodeTextBox.Text = _entity.MaterialCode;

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

            MaterialTypeComboBox.DataSource = MaterialTypeArray;

            for (int i = 0; i < MaterialTypeComboBox.Items.Count; i++)
            {
                if (MaterialTypeComboBox.GetItemText(MaterialTypeComboBox.Items[i]) == _entity.MaterialType)
                {
                    MaterialTypeComboBox.SelectedIndex = i;
                }
            }

            for (int i = 0; i < ChildComboBox.Items.Count; i++)
            {
                if (ChildComboBox.GetItemText(ChildComboBox.Items[i]) == _entity.ChildType)
                {
                    ChildComboBox.SelectedIndex = i;
                }
            }

            IsUseCheckBox.Checked = _entity.IsUse.Value;
        }

        /// <summary>
        /// 确认修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfrimAddButton_Click(object sender, EventArgs e)
        {
            ZxMaterialEntity entity = new ZxMaterialEntity()
            {
                Id = _entity.Id,
                MaterialName = MaterialNameTextBox.Text.Trim(),
                MaterialCode = MaterialCodeTextBox.Text.Trim(),
                MaterialType = MaterialTypeComboBox.Text.Trim(),
                ChildType = ChildComboBox.Text.Trim(),
                IsUse = IsUseCheckBox.Checked,
                IsDeleted = _entity.IsDeleted.Value,
            };

            if (string.IsNullOrEmpty(MaterialCodeTextBox.Text.Trim()))
            {
                MessageBox.Show("物料信息修改失败！物料编号不能为空！", "提示");
                return;
            }

            if(zxMaterialService.GetEntityByMaterialCode(MaterialCodeTextBox.Text.Trim()).Count > 0 && MaterialCodeTextBox.Text.Trim() != _entity.MaterialCode)
            {
                MessageBox.Show("物料信息修改失败！物料编号不能重复！", "提示");
                return;
            }

            if (zxMaterialService.UpdateMaterialInfo(entity))
            {
                MessageBox.Show("物料信息修改成功！", "提示");
            }
            else
            {
                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;
        }
    }
}
