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

namespace HighWayIot.Winform.UserControlPages.ParamConfigPages
{
    public partial class EquipParamSettingPage : UserControl
    {
        ZxMaterialService zxMaterialService = ZxMaterialService.Instance;

        ZxOpenMixMaterialService zxOpenMixMaterialService = ZxOpenMixMaterialService.Instance;

        List<string> zxMaterialEntities = new List<string>();

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

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            zxMaterialEntities = zxMaterialService.GetMaterialInfos().Select(x => x.MaterialName).ToList();
            zxMaterialEntities.Insert(0, "");
            OpenMixMaterial1.DataSource = zxMaterialEntities;
            List<string> zxMaterialEntities1 = new List<string>(zxMaterialEntities);
            OpenMixMaterial2.DataSource = zxMaterialEntities1;
            List<string> zxMaterialEntities2 = new List<string>(zxMaterialEntities);
            OpenMixMaterial3.DataSource = zxMaterialEntities2;
            List<string> zxMaterialEntities3 = new List<string>(zxMaterialEntities);
            OpenMixMaterial4.DataSource = zxMaterialEntities3;
            List<string> zxMaterialEntities4 = new List<string>(zxMaterialEntities);
            OpenMixMaterial5.DataSource = zxMaterialEntities4;
            
            List<ZxOpenMixMaterialEntity> list = zxOpenMixMaterialService.GetInfos();
            if (list != null && list.Count > 0)
            {
                OpenMixMaterial1.SelectedItem = list[0].MaterialName;
                OpenMixMaterial2.SelectedItem = list[1].MaterialName;
                OpenMixMaterial3.SelectedItem = list[2].MaterialName;
                OpenMixMaterial4.SelectedItem = list[3].MaterialName;
                OpenMixMaterial5.SelectedItem = list[4].MaterialName;
            }
        }

        /// <summary>
        /// 保存开炼机物料配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveOpenMixMaterial_Click(object sender, EventArgs e)
        {
            List<ZxOpenMixMaterialEntity> list = new List<ZxOpenMixMaterialEntity>()
            {
                new ZxOpenMixMaterialEntity()
                {
                    StationNo = 1,
                    MaterialName = OpenMixMaterial1.SelectedItem.ToString()
                },
                new ZxOpenMixMaterialEntity()
                {
                    StationNo = 2,
                    MaterialName = OpenMixMaterial2.SelectedItem.ToString()
                },
                new ZxOpenMixMaterialEntity()
                {
                    StationNo = 3,
                    MaterialName = OpenMixMaterial3.SelectedItem.ToString()
                },
                new ZxOpenMixMaterialEntity()
                {
                    StationNo = 4,
                    MaterialName = OpenMixMaterial4.SelectedItem.ToString()
                },
                new ZxOpenMixMaterialEntity()
                {
                    StationNo = 5,
                    MaterialName = OpenMixMaterial5.SelectedItem.ToString()
                }
            };

            if (zxOpenMixMaterialService.UpDateInfos(list))
            {
                MessageBox.Show("开炼机物料配置信息保存成功");
            }
            else
            {
                MessageBox.Show("开炼机物料配置信息保存失败");
            }
        }

        /// <summary>
        /// 刷新开炼机物料配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RefreshOpenMixMaterial_Click(object sender, EventArgs e)
        {
            Init();
        }
    }
}
