using DbCommon.BusinessCore.BaseCore;
using DbCommon.Enties.DbModels;
using ProjectManagementSystem.Common.Extenions;
using ProjectManagementSystem.Common.WebApi;
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 ProjectManagementSystem.Forms
{
    public partial class FormPmsTaskInfo : Form
    {
        public PmsTaskInfo PmsTask { get; set; }
        public bool IsAppointment { get; set; }
        public string CurrentButtonText { get; set; }


        private WareHouseManager wareHouseManager = new WareHouseManager();
        private LocationPropertyManager locationManager = new LocationPropertyManager();
        private MaterialDetailManager materialDetailManager = new MaterialDetailManager();

        public FormPmsTaskInfo()
        {
            InitializeComponent();
        }

        public void Init(object sender, LocationProperty locationData)
        {
            CurrentButtonText = (sender as Control).Text;
            if(CurrentButtonText.Contains("预约"))
            {
                IsAppointment = true;
            }

            if(CurrentButtonText.Contains("叫料"))
            {
                InitCallMaterial(sender, locationData);
            }
            else if(CurrentButtonText.Contains("转运"))
            {
                InitTransferMaterial(sender, locationData);
            }
        }

        private void InitWareHouse(ComboBox cmb, string[] wareHouseCodes = null)
        {
            var dt = wareHouseCodes == null ?
                wareHouseManager.GetList() : wareHouseManager.QueryWareHouseList(wareHouseCodes);

            if (dt != null
                && dt.Count > 0)
            {
                cmb.ValueMember = "WareHouseCode";
                cmb.DisplayMember = "WareHouseName";
                cmb.DataSource = dt;
            }

            button1.Text = CurrentButtonText;
        }

        private bool InitCallMaterial(object sender, LocationProperty locationData)
        {
            PmsTask = new PmsTaskInfo();
            PmsTask.TargetWareHouseCode = locationData.WareHouseCode;
            PmsTask.TargetLocationCode = locationData.LocationCode;

            InitWareHouse(cmbTargetWareHouseCode);

            this.cmbTargetWareHouseCode.SelectedValue = PmsTask.TargetWareHouseCode;
            this.cmbTargetLocationCode.SelectedItem = PmsTask.TargetLocationCode;

            var data = wareHouseManager.GetById(PmsTask.TargetWareHouseCode);
            string[] wareHouseCodes = data.SourceWareHouseCode.ToValueArray<string>();

            InitWareHouse(cmbWareHouseCode, wareHouseCodes);

            this.label8.Enabled = false;
            this.label9.Enabled = false;
            this.cmbTargetWareHouseCode.Enabled = false;
            this.cmbTargetLocationCode.Enabled = false;
            return true;
        }

        private bool InitTransferMaterial(object sender, LocationProperty locationData)
        {
            PmsTask = new PmsTaskInfo();
            PmsTask.WareHouseCode = locationData.WareHouseCode;
            PmsTask.LocationCode = locationData.LocationCode;

            InitWareHouse(cmbWareHouseCode);

            this.cmbWareHouseCode.SelectedValue = PmsTask.WareHouseCode;
            this.cmbLocationCode.SelectedItem = PmsTask.LocationCode;

            var data = wareHouseManager.GetById(PmsTask.WareHouseCode);
            string[] wareHouseCodes = data.TargetWareHouseCode.ToValueArray<string>();

            InitWareHouse(cmbTargetWareHouseCode, wareHouseCodes);

            this.label5.Enabled = false;
            this.label7.Enabled = false;
            this.cmbWareHouseCode.Enabled = false;
            this.cmbLocationCode.Enabled = false;

            this.label6.Enabled = false;
            this.cmbMaterialId.Enabled = false;
            return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string text = $"确定要{button1.Text}？";
                var dialogResult = MessageBox.Show(text, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (dialogResult != DialogResult.OK)
                {
                    return;
                }

                string apiName = IsAppointment ? "TaskAppointment" : "TaskAdd";

                PmsTaskInfo data = new PmsTaskInfo();
                data.WareHouseCode = cmbWareHouseCode.SelectedValue?.ToString();
                data.LocationCode = cmbLocationCode.Text;
                data.TargetWareHouseCode = cmbTargetWareHouseCode.SelectedValue?.ToString();
                data.TargetLocationCode = cmbTargetLocationCode.Text;
                data.MaterialId = cmbMaterialId.Text;

                var result = PmsApi.CustomApiPost(apiName, null, data);
                if (result?.code == 20000)
                {
                    ShowMsg($"{button1.Text}成功，{result?.message}");
                    this.Close();
                }
                else
                {
                    ShowErrMsg($"{button1.Text}失败，{result?.message}");
                }
            }
            catch (Exception ex)
            {
                ShowErrMsg($"{CurrentButtonText}过程发生异常：{ex.Message}");
            }
        }

        private void ShowMsg(string text)
        {
            MessageBox.Show(text, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void ShowErrMsg(string text)
        {
            MessageBox.Show(text, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void cmbWareHouseCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            string wHouse = cmbWareHouseCode.SelectedValue.ToString();
            var nameList = locationManager.QueryDataList(wHouse)?.Select(d => d.LocationCode).ToList();
            nameList.Add(string.Empty);
            cmbLocationCode.DataSource = nameList;

            cmbLocationCode.SelectedIndex = cmbLocationCode.Items.Count - 1;

            //获取取货仓库所有的物料
            var materialList = locationManager.GetList(d => d.WareHouseCode == wHouse && !string.IsNullOrEmpty(d.MaterialId))?
                .Select(d => d.MaterialId)
                .Distinct()
                .ToList();

            var materialList2 = materialDetailManager.GetList()
                .Select(d => d.MaterialId)
                .Distinct()
                .ToList();

            materialList.AddRange(materialList2);
            materialList = materialList.Distinct().ToList();

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

        private void cmbTargetWareHouseCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            string wHouse = cmbTargetWareHouseCode.SelectedValue.ToString();
            var nameList = locationManager.QueryDataList(wHouse)?.Select(d => d.LocationCode).ToList();
            nameList.Add(string.Empty);
            cmbTargetLocationCode.DataSource = nameList;

            cmbTargetLocationCode.SelectedIndex = cmbTargetLocationCode.Items.Count - 1;
        }

        private void cmbLocationCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cmbLocationCode.Text))
            {
                cmbMaterialId.Text = locationManager.GetById(cmbLocationCode.Text)?.MaterialId;
            }
        }
    }
}
