using Newtonsoft.Json;
using System;
using System.Net.Http.Headers;
using System.Net.Http;
using UnityEngine;
using UnityEngine.UI;
using static System.Net.Mime.MediaTypeNames;
using System.Threading.Tasks;
/// <summary>
/// 端口-编辑
/// </summary>
public class RedactPort : MonoBehaviour
{
    public Root root;
    public Button save_bt;

    public Dropdown port;
    public InputField portCode;
    public InputField portName;
    public InputField portType;
    public InputField deviceId;
    public InputField portModel;
    public Dropdown status;
    public Dropdown conDevice;
    public Dropdown conPort;

    private void Start()
    {
        save_bt.onClick.AddListener(async () => await saveJson(""));
    }


    private void Update()
    {
        if (gameObject.activeSelf)
        {
            root.port = port.value.ToString();
            root.portCode = portCode.text;
            root.portName = portName.text;
            root.portType = portType.text;
            root.deviceId = deviceId.text;
            root.portModel = portModel.text;
            root.status = int.Parse(status.value.ToString());
            root.conDevice = conDevice.value.ToString();
            root.conPort = conPort.value.ToString();

        }
    }

    /// <summary>
    /// 端口-编辑保存
    /// </summary>
    /// <param name="token"></param>
    public async Task saveJson(string token)
    {
        if (string.IsNullOrEmpty(root.id)) return;
        string json = JsonConvert.SerializeObject(root);
        Debug.Log(json);

        var client = new HttpClient();
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri("http://wu4ifs.natappfree.cc/machineRoom/port/updateById"),
            Headers =
    {
        { "X-Token", token },
    },
            Content = new StringContent(json)
            {
                Headers =
        {
            ContentType = new MediaTypeHeaderValue("application/json")
        }
            }
        };
        using (var response = await client.SendAsync(request))
        {
            response.EnsureSuccessStatusCode();
            var body = await response.Content.ReadAsStringAsync();
            Debug.Log(body);
        }
    }

    #region JSON
    [System.Serializable]
    public class Root
    {
        /// <summary>
        /// id
        /// </summary>
        public string id;
        /// <summary>
        /// 端口
        /// </summary>
        public string port;
        /// <summary>
        /// 编号
        /// </summary>
        public string portCode;
        /// <summary>
        /// 名称
        /// </summary>
        public string portName;
        /// <summary>
        /// 类型
        /// </summary>
        public string portType;
        /// <summary>
        /// 所属设备
        /// </summary>
        public string deviceId;
        /// <summary>
        /// 型号
        /// </summary>
        public string portModel;
        /// <summary>
        /// 是否启用：1-正常；0-禁用
        /// </summary>
        public int status;
        /// <summary>
        /// 对联设备
        /// </summary>
        public string conDevice;
        /// <summary>
        /// 对联端口
        /// </summary>
        public string conPort;
        /// <summary>
        /// 备注
        /// </summary>
        public string remark;
    }

    #endregion

}
