using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Kill.Managers;
using Kill.Network;
public class SelectCountry : MonoBehaviour
{
public GameObject countryPrefab;
public Transform countryParent;
public Button submitButton;
public InputField searchInput;
LanguageManager.Country selectCountry;
private bool _hasSelection;
///
/// 未选中 选中
///
public Color[] textColors;
private List _items = new List();
public System.Action onConfirm;
// private void Start()
// {
// Init("");
// }
public void Init(string code)
{
countryPrefab.SetActive(false);
// 清理旧 item
foreach (var item in _items)
Destroy(item.gameObject);
_items.Clear();
_hasSelection = false;
if (LanguageManager.Instance == null || LanguageManager.Instance.countryList == null)
return;
foreach (var country in LanguageManager.Instance.countryList)
{
var go = Instantiate(countryPrefab, countryParent);
var item = go.AddComponent();
item.Init(this, country);
_items.Add(item);
item.gameObject.SetActive(true);
}
// 绑定搜索
if (searchInput != null)
{
searchInput.onValueChanged.RemoveAllListeners();
searchInput.onValueChanged.AddListener(OnSearchChanged);
}
// 设置默认选中(code 为空时不选中)
if (!string.IsNullOrEmpty(code))
SelectCountryByCode(code);
UpdateSubmitButton();
}
private void OnSearchChanged(string keyword)
{
foreach (var item in _items)
{
item.gameObject.SetActive(item.MatchKeyword(keyword));
}
}
public void SelectCountryByCode(string code)
{
_hasSelection = !string.IsNullOrEmpty(code);
foreach (var item in _items)
{
bool isSelected = _hasSelection && item.Country.code == code;
item.SetSelected(isSelected);
if (isSelected)
selectCountry = item.Country;
}
UpdateSubmitButton();
}
public string GetSelectedCode()
{
return _hasSelection ? selectCountry.code : null;
}
private void UpdateSubmitButton()
{
if (submitButton != null)
{
submitButton.gameObject.SetActive(_hasSelection);
submitButton.onClick.RemoveAllListeners();
submitButton.onClick.AddListener(OnSubmit);
}
}
private async void OnSubmit()
{
if (!_hasSelection) return;
await UpdateCountryCodeAsync(selectCountry.code);
}
///
/// 更新用户国家代码
///
private async System.Threading.Tasks.Task UpdateCountryCodeAsync(string code)
{
try
{
var requestData = new CountryCodeRequest
{
country_code = code
};
var response = await NetworkCtrl.Instance.Post("/api/v1/user/country", requestData);
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
if (DataManager.Instance.userInfo != null)
DataManager.Instance.userInfo.country_code = code;
onConfirm?.Invoke(code);
},
onError: null
);
}
catch (Exception ex)
{
Debug.LogError($"更新国家代码失败: {ex.Message}");
}
}
}
internal class SelectCountryItem : MonoBehaviour
{
public LanguageManager.Country Country { get; private set; }
private SelectCountry _parent;
private Text _nameText;
private GameObject _selectGo;
private Color _selectedColor;
private Color _unselectedColor;
public void Init(SelectCountry parent, LanguageManager.Country country)
{
_parent = parent;
Country = country;
_nameText = transform.Find("name")?.GetComponent();
_selectGo = transform.Find("选中")?.gameObject;
_selectedColor = parent.textColors.Length > 0 ? parent.textColors[0] : Color.white;
_unselectedColor = parent.textColors.Length > 1 ? parent.textColors[1] : Color.gray;
// 显示国家名称(根据当前语言)
bool isChinese = LanguageManager.Instance != null && LanguageManager.Instance.languageType == LanguageManager.LanguageType.Chinese;
if (_nameText != null)
_nameText.text = isChinese ? country.name_zh : country.name_en;
// 点击事件
var btn = GetComponent