using DefaultNamespace;
using TMPro;
using UnityEngine;
using Framework.Dto;
using Framework.Manager;
using MotionFramework;
///
/// 表单选择下拉框处理器
/// 监听TMP_Dropdown选择变化,判断formselection是表单1还是表单2,并打印选择的值
///
public class FormSelectionDropdownHandler : MonoBehaviour
{
[Header("调试设置")] [Tooltip("是否启用调试日志")] [SerializeField]
private bool enableDebugLog = true;
[Tooltip("下拉框组件")] [SerializeField] private TMP_Dropdown tmpDropdown;
public GameObject plane1;
public GameObject plane2;
public TMP_Text plane2Text;
private InventoryReversalVoucherAnalyzer returnRevVoucherAnalyzer;
///
/// 初始化组件
///
private void Start()
{
// 获取InventoryReversalVoucherAnalyzer实例
returnRevVoucherAnalyzer = (InventoryReversalVoucherAnalyzer)MotionEngine.GetModule().materialTaskObj;
// 如果没有手动指定下拉框,尝试自动获取
if (tmpDropdown == null)
{
tmpDropdown = GetComponent();
}
// 检查组件是否存在
if (tmpDropdown == null)
{
Debug.LogError($"FormSelectionDropdownHandler: 在 {gameObject.name} 中未找到 TMP_Dropdown 组件!");
return;
}
if (returnRevVoucherAnalyzer == null)
{
Debug.LogError($"FormSelectionDropdownHandler: 无法获取 InventoryReversalVoucherAnalyzer 实例!");
return;
}
// 监听下拉框选择变化事件
tmpDropdown.onValueChanged.AddListener(OnDropdownValueChanged);
if (enableDebugLog)
{
Debug.Log($"FormSelectionDropdownHandler: 初始化完成 - {gameObject.name}");
Debug.Log($"当前formselection值: {returnRevVoucherAnalyzer.formselection}");
}
}
///
/// 下拉框选择变化回调
///
/// 选择的索引
private void OnDropdownValueChanged(int value)
{
// 检查索引是否有效
if (value < 0 || value >= tmpDropdown.options.Count)
{
if (enableDebugLog)
{
Debug.LogWarning($"FormSelectionDropdownHandler: 选择无效,索引: {value}");
}
return;
}
// 获取选择的文本值
string selectedValue = tmpDropdown.options[value].text;
// 判断formselection是表单1还是表单2
// 打印判断结果
PrintSelectionResult(selectedValue, returnRevVoucherAnalyzer.formselection);
}
///
/// 打印选择结果
///
/// 选择的值
/// 表单类型
private void PrintSelectionResult(string selectedValue, string formType)
{
string logMessage = $"=== 下拉框选择结果 ===\n" +
$"选择的值: {selectedValue}\n" +
$"formselection: {returnRevVoucherAnalyzer.formselection}\n" +
$"判断结果: {formType}\n" +
$"下拉框名称: {gameObject.name}";
// 也可以输出到控制台
print(logMessage);
plane1.SetActive(false);
plane2.SetActive(true);
if (returnRevVoucherAnalyzer.formselection == "表单1")
{
switch (selectedValue)
{
case "1805L01111":
plane2Text.text = "目前1805L01111批次生产中";
break;
case "1805L01099":
plane2Text.text = "目前1805L01099批次物资已发送";
break;
case "1805L01095":
plane2Text.text = "目前1805L01095批次生产中";
break;
case "1804L00346":
plane2Text.text = "目前1804L00346批次物资已到货";
break;
case "1804L00345":
plane2Text.text = "目前1804L00345批次图纸已完成确认";
break;
}
}
else
{
switch (selectedValue)
{
case "1805L01111":
plane2Text.text = "目前1805L01111批次生产中";
break;
case "1805L01099":
plane2Text.text = "目前1805L01099批次物资已发送";
break;
case "1805L01095":
plane2Text.text = "目前1805L01095批次生产中";
break;
case "1804L00346":
plane2Text.text = "目前1804L00346批次物资已到货";
break;
case "1804L00345":
plane2Text.text = "目前1804L00345批次图纸已到货";
break;
}
}
if (enableDebugLog)
{
Debug.Log(logMessage);
}
}
///
/// 手动触发判断(用于测试)
///
public void TriggerManualCheck()
{
if (tmpDropdown != null && returnRevVoucherAnalyzer != null)
{
OnDropdownValueChanged(tmpDropdown.value);
}
}
///
/// 设置是否启用调试日志
///
/// 是否启用
public void SetDebugLogEnabled(bool enabled)
{
enableDebugLog = enabled;
if (enableDebugLog)
{
Debug.Log($"FormSelectionDropdownHandler: 调试日志 {(enabled ? "启用" : "禁用")} - {gameObject.name}");
}
}
///
/// 获取当前formselection值
///
/// formselection值
public string GetCurrentFormSelection()
{
return returnRevVoucherAnalyzer?.formselection ?? "未获取到";
}
///
/// 获取当前选择的文本
///
/// 当前选择的文本
public string GetCurrentSelectedValue()
{
if (tmpDropdown != null && tmpDropdown.value >= 0 && tmpDropdown.value < tmpDropdown.options.Count)
{
return tmpDropdown.options[tmpDropdown.value].text;
}
return "无选择";
}
///
/// 组件销毁时清理事件监听
///
private void OnDestroy()
{
if (tmpDropdown != null)
{
tmpDropdown.onValueChanged.RemoveListener(OnDropdownValueChanged);
}
}
}