using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace HH.YiDaSyncNC.Helper
{
    public class HttpRequestHelper
    {
        public static string _Static_NCApporeApiUrl = ConfigurationManager.AppSettings["NCApporeApiUrl"];

        public class ApporeDataModel
        {
            public int state { get; set; }
            public string msg { get; set; }
            public string result { get; set; }
        }

        #region 销售发票审核
        public class ApporeNCCSOBillParamModel
        {
            public string billdate { get; set; }
            public string csaleinvoiceid { get; set; }
            public string usercode { get; set; }
        }

        public static ApporeDataModel ApporeNCCSOBill(string billNo)
        {
            var param = new ApporeNCCSOBillParamModel()
            {
                billdate = DateTime.Now.ToString("yyyy-MM-dd"),
                csaleinvoiceid = billNo,
                usercode = "OA-NC"
            };

            string apiUrl = string.Format(_Static_NCApporeApiUrl + "service/~lxbxy_ext/nc.api.lxbxy.httpservice.ApporeNCCSOBill");
            string result = HttpPost(apiUrl, JsonConvert.SerializeObject(param));

            return JsonConvert.DeserializeObject<ApporeDataModel>(result);
        }
        #endregion

        #region 付款单审核
        public class ApproveNCFKBillParamModel
        {
            public string billdate { get; set; }
            public string paybill { get; set; }
            public string usercode { get; set; }
        }

        public static ApporeDataModel ApproveNCFKBill(string billNo)
        {
            var param = new ApproveNCFKBillParamModel()
            {
                billdate = DateTime.Now.ToString("yyyy-MM-dd"),
                paybill = billNo,
                usercode = "OA-NC"
            };

            string apiUrl = string.Format(_Static_NCApporeApiUrl + "service/~lxbxy_ext/nc.api.lxbxy.httpservice.ApproveNCFKBill");
            string result = HttpPost(apiUrl, JsonConvert.SerializeObject(param));
            return JsonConvert.DeserializeObject<ApporeDataModel>(result);
        }
        #endregion


        #region POST请求（原始方法）
        public static string HttpPost(string url, string postDataStr)
        {
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(postDataStr);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                var host = request.RequestUri.Scheme + "://" + request.RequestUri.Host + request.RequestUri.AbsolutePath;

                request.Method = "POST";
                request.ContentType = "application/json";
                request.ContentLength = bytes.Length;
                Stream writer = request.GetRequestStream();
                writer.Write(bytes, 0, bytes.Length);
                writer.Flush();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string retString = reader.ReadToEnd();
                response.Close();

                return retString;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
