using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace HH.YiDaSyncNC.Models
{
    class FK_CLBX_SyncServiceModel
    {
        public class DataModel
        {
            public string 集团 { get; set; } = "0";
            public string 组织 { get; set; }

            public string 支付人 { get; set; }
            public string 支付日期 { get; set; }

            public string 创建人 { get; set; }
            public string 创建时间 { get; set; }
            public string 制单人 { get; set; }

            public string 单据类型 { get; set; } = "F3";
            public string 付款类型 { get; set; } = "F3-Cxx-011";
            public string 单据大类 { get; set; } = "fk";
            public string 业务流程 { get; set; } = "AP02";

            public string 单据号 { get; set; }
            public string 单据日期 { get; set; }

            public string 单据所属系统 { get; set; } = "1";
            public string 单据来源系统 { get; set; } = "1";
            public string 单据状态 { get; set; } = "-1";
            public string 结算状态 { get; set; } = "1";
            public string 生效状态 { get; set; } = "10";

            public string 生效人 { get; set; } 
            public string 生效日期 { get; set; }

            public string 发票号 { get; set; }

            public string 供应商 { get; set; } = "999"; // 虚拟供应商999
            public string 币种 { get; set; } = "CNY";

            public string 往来对象 { get; set; } = "1";
            public string 付款银行账户 { get; set; }

            public string 价税合计 { get; set; }

            public string 年份 { get; set; }
            public string 月份 { get; set; }
            
            public string 起算日期 { get; set; }

            public string 汇率 { get; set; } = "1";
            public string 折扣 { get; set; } = "100";

            public string 部门 { get; set; }
            public string 项目 { get; set; }

            public List<Item> 明细 { get; set; }

            public class Item
            {
                public string 摘要 { get; set; }
                public string 科目 { get; set; }
                public string 收支项目 { get; set; }

                public string 价税合计 { get; set; }
                public string 无税金额 { get; set; }
                public string 税额 { get; set; }
            }
        }

        public class ImportXml
        {
            /// <summary>
            /// 模板头部份
            /// </summary>
            /// <returns></returns>
            public static XElement TemplateHeade()
            {

                return new XElement(new XElement("ufinterface", new object[]
                {
                new XAttribute("sender","dingding"),
                new XAttribute("roottag",""),
                new XAttribute("replace","Y"),
                new XAttribute("isexchange","Y"),
                new XAttribute("groupcode","0"),
                new XAttribute("filename",""),
                new XAttribute("billtype","F3"),
                new XAttribute("proc","add"),
                new XAttribute("account","develop")
                }));
            }

            /// <summary>
            /// 组建表头xml
            /// </summary>
            /// <param name="item">表头参数</param>
            /// <returns></returns>
            public static XElement Header(DataModel item, string id)
            {
                var bodys = new List<XElement>();

                if (item.明细 != null && item.明细.Count > 0)
                {
                    var crowno = 10; // 行号

                    foreach (var itemData in item.明细)
                    {
                        bodys.Add(new XElement("item", new object[] {
                            new XElement("pk_group",item.集团),
                            new XElement("sett_org",item.组织),
                            new XElement("pk_org",item.组织),
                            new XElement("pk_fiorg",item.组织),
                            new XElement("prepay","0"),
                            new XElement("supplier",item.供应商),
                            new XElement("checkdirection","ap"),
                            new XElement("billdate",item.单据日期),

                            new XElement("pk_billtype",item.单据类型),
                            new XElement("billclass",item.单据大类),
                            new XElement("pk_tradetype",item.付款类型),
                            new XElement("busidate",item.起算日期),

                            new XElement("billno",item.单据号),
                            new XElement("objtype",item.往来对象),
                            new XElement("rowno",crowno.ToString()),
                            new XElement("direction","1"),

                            new XElement("pk_recpaytype","001"),

                            new XElement("pk_subjcode",itemData.收支项目),
                            new XElement("subjcode",itemData.科目),
                            new XElement("pk_currtype",item.币种),
                            new XElement("rate",item.汇率),
                            new XElement("pk_deptid",item.部门),
                            new XElement("project",item.项目),

                            new XElement("money_de",itemData.价税合计),
                            new XElement("local_money_de",itemData.价税合计),
                            new XElement("local_money_bal",itemData.价税合计),
                            new XElement("notax_de",itemData.无税金额),
                            new XElement("local_notax_de",itemData.价税合计),

                            new XElement("payaccount",item.付款银行账户),
                            // new XElement("invoiceno",item.发票号),

                            new XElement("def12",itemData.税额),
                            new XElement("def11",itemData.无税金额),
                            new XElement("occupationmny",itemData.价税合计),

                            new XElement("grouprate","0"),
                            new XElement("globalrate","0"),
                            new XElement("groupdebit","0"),
                            new XElement("globaldebit","0"),
                            new XElement("groupbalance","0"),
                            new XElement("globalbalance","0"),
                            new XElement("groupnotax_de","0"),
                            new XElement("globalnotax_de","0"),

                        }));

                        crowno = crowno + 10;
                    }
                }

                return new XElement("bill", new object[]
                {
                "",
                new XAttribute("id",id),
                new XElement("billhead", new object[]
                {
                    new XElement("pk_group",item.集团),
                    new XElement("pk_fiorg",item.组织),
                    new XElement("pk_org",item.组织),
                    new XElement("sett_org",item.组织),
                    new XElement("pk_corp",item.组织),

                    new XElement("isreded","N"),

                    new XElement("payman",item.支付人),
                    new XElement("paydate",item.支付日期),
                    new XElement("isonlinepay","N"),

                    new XElement("creationtime",item.创建时间),
                    new XElement("creator",item.创建人),

                    new XElement("pk_billtype",item.单据类型),
                    new XElement("pk_tradetype",item.付款类型),
                    new XElement("billclass",item.单据大类),
                    new XElement("pk_busitype",item.业务流程),
                    new XElement("ismandatepay","N"),
                    new XElement("accessorynum","0"),
                    new XElement("isflowbill","N"),
                    new XElement("isinit","N"),

                    new XElement("billno",item.单据号),
                    new XElement("billdate",item.单据日期),

                    new XElement("syscode",item.单据所属系统),
                    new XElement("src_syscode",item.单据来源系统),
                    new XElement("billstatus",item.单据状态),

                    new XElement("billmaker",item.制单人),
                    new XElement("isnetpayready","N"),

                    new XElement("money",item.价税合计),
                    new XElement("local_money",item.价税合计),
                    new XElement("billyear",item.年份),
                    new XElement("billperiod",item.月份),

                    new XElement("settleflag",item.结算状态),
                    new XElement("effectstatus",item.生效状态),
                    new XElement("effectuser",item.生效人),
                    new XElement("effectdate",item.生效日期),

                    new XElement("def2",item.发票号),
                    //  new XElement("pk_deptid",item.发票号),

                    new XElement("supplier",item.供应商),
                    new XElement("pk_currtype",item.币种),
                    new XElement("objtype",item.往来对象),
                    new XElement("payaccount",item.付款银行账户),

                    new XElement("busidate",item.起算日期),

                    new XElement("bodys",bodys),
                }),
                });
            }
        }
    }
}
