using DbCommon.Enties.DbModels;
using DbCommon.BusinessCore.BaseCore;
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 ProjectManagementSystem.Common.Config;
using ProjectManagementSystem.Common.Service;

namespace ProjectManagementSystem.Forms
{
    public partial class FormSetting : Form
    {
        public LocationProperty LocationProperty { get; set; }

        public FormSetting()
        {
            InitializeComponent();
        }

        public virtual void Init()
        { 
            MaterialDetailManager materialDetailManager = new MaterialDetailManager();
            var materialList = materialDetailManager.GetList()
                .Select(d => d.MaterialId)
                .Distinct()
                .ToList();

            if(!string.IsNullOrEmpty(LocationProperty.MaterialId))
            {
                materialList.Add(LocationProperty.MaterialId);
            }

            materialList = materialList.Distinct().ToList();
            materialList.Add(string.Empty);
            cmbMaterial.DataSource = materialList;
            cmbMaterial.SelectedIndex = cmbMaterial.Items.Count - 1;
        }

        public virtual void UpdateDisplay()
        {
            if (LocationProperty == null) return;

            this.labArea.Text = LocationProperty.Area;
            this.labWareHouse.Text = LocationProperty.WareHouseCode;
            this.labLocation.Text = LocationProperty.LocationCode;

            if (string.IsNullOrEmpty(LocationProperty.MaterialId)) return;

            this.cmbMaterial.SelectedItem = LocationProperty.MaterialId;
        }

        private void FormSetting_Shown(object sender, EventArgs e)
        {
        }

        protected virtual void button1_Click(object sender, EventArgs e)
        {
            LocationStatus status = LocationProperty.Status;
            if(status == LocationStatus.Locked)
            {
                if(PmsTaskService.Instance.LocationHasTask(LocationProperty.LocationCode))
                {
                    string msg = string.Format("状态为锁定，不能修改（需要强制修改请使用WMS）");
                    MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            
            string material = this.cmbMaterial.Text;
            if(!string.IsNullOrEmpty(material))
            {
                status = LocationStatus.Filled;
            }

            var m_locationManager = new LocationPropertyManager();
            var data = m_locationManager.GetById(LocationProperty.LocationCode);
            data.Status = status;
            data.MaterialId = material;

            var result = m_locationManager.Update(data);
            if (result)
            {
                MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }
    }
}
