import XML_LOAD from '@/utils/xml-load'

/**
 * 创建红字发票申请清单对象
 */
function createObjRedInvReqBill() {
    return {
        ReqBillNo: "",              // 红字申请编号
        ResBillNo: "",              // 红字请求编号
        StatusDM: "",
        StatusMC: "",
        BillType: "",
        TypeCode: "",               // 发票代码
        InvNo: "",                  // 发票号码
        Szlb: "",
        IsMutiRate: "",
        Date: "",                   // 填开日期
        BuyerName: "",              // 购方名称
        BuyTaxCode: "",             // 购方税号
        SellerName: "",             // 销方名称
        SellTaxCode: "",            // 销方税号
        Amount: "",                 // 总金额
        TaxRate: "",                // 税率
        Tax: "",                    // 总税额
        ReqMemo: "",
        SPBMBBH: "",
        SLBZ: "",
        RedInvReqBillMx: []     // 商品明细列表
    }
}

/**
 * 创建红字发票商品明细对象
 */
function createObjGoodsMx() {
    return {
        GoodsName: "",              // 商品名称
        GoodsUnit: "",              // 商品单位
        GoodsPrice: "",             // 商品单价
        GoodsTaxRate: "",           // 商品税率
        GoodsGgxh: "",              // 商品规格型号
        GoodsNum: "",               // 商品数量
        GoodsJE: "",                // 商品金额
        GoodsSE: "",                // 商品税额
        HS_BZ: "",                  // 含税标识
        SPBM: "",                   // 商品编码
        QYSPBM: "",
        SYYHZCBZ: "", // 优惠政策标识
        YHZC: "",
        LSLBZ: "", // 零税率标识
    }
}

const INVOICE_RED_TABLE_XML_V2 = {
    createObjRedInvReqBill: createObjRedInvReqBill,
    createObjGoodsMx: createObjGoodsMx,

    /**
     * 获取红字发票申请清单
     * @param {String} xmlFileText 红字信息表导出XML文件文本
     */
    getRedInvReqBill: function (xmlFileText) {
        let getNodeValue = function (dom) {
            if (dom && dom.childNodes.length > 0) {
                return dom.childNodes[0].nodeValue;
            }
            return "";
        }
        let result = createObjRedInvReqBill();
        var xmlDoc = XML_LOAD.loadXML(xmlFileText);
        var domRedInvReqBill = xmlDoc.getElementsByTagName("RedInvReqBill")[0];
        result.ReqBillNo = getNodeValue(domRedInvReqBill.getElementsByTagName('ReqBillNo')[0]);
        result.ResBillNo = getNodeValue(domRedInvReqBill.getElementsByTagName('ResBillNo')[0]);
        result.StatusDM = getNodeValue(domRedInvReqBill.getElementsByTagName('StatusDM')[0]);
        result.StatusMC = getNodeValue(domRedInvReqBill.getElementsByTagName('StatusMC')[0]);
        result.BillType = getNodeValue(domRedInvReqBill.getElementsByTagName('BillType')[0]);
        result.TypeCode = getNodeValue(domRedInvReqBill.getElementsByTagName('TypeCode')[0]);
        result.InvNo = getNodeValue(domRedInvReqBill.getElementsByTagName('InvNo')[0]);
        result.Szlb = getNodeValue(domRedInvReqBill.getElementsByTagName('Szlb')[0]);
        result.IsMutiRate = getNodeValue(domRedInvReqBill.getElementsByTagName('IsMutiRate')[0]);
        result.Date = getNodeValue(domRedInvReqBill.getElementsByTagName('Date')[0]);
        result.BuyerName = getNodeValue(domRedInvReqBill.getElementsByTagName('BuyerName')[0]);
        result.BuyTaxCode = getNodeValue(domRedInvReqBill.getElementsByTagName('BuyTaxCode')[0]);
        result.SellerName = getNodeValue(domRedInvReqBill.getElementsByTagName('SellerName')[0]);
        result.SellTaxCode = getNodeValue(domRedInvReqBill.getElementsByTagName('SellTaxCode')[0]);
        result.Amount = getNodeValue(domRedInvReqBill.getElementsByTagName('Amount')[0]);
        result.TaxRate = getNodeValue(domRedInvReqBill.getElementsByTagName('TaxRate')[0]);
        result.Tax = getNodeValue(domRedInvReqBill.getElementsByTagName('Tax')[0]);
        result.ReqMemo = getNodeValue(domRedInvReqBill.getElementsByTagName('ReqMemo')[0]);
        result.SPBMBBH = getNodeValue(domRedInvReqBill.getElementsByTagName('SPBMBBH')[0]);
        result.SLBZ = getNodeValue(domRedInvReqBill.getElementsByTagName('SLBZ')[0]);

        console.log('XML数据：');
        console.log(result);

        var domGoodsMxList = xmlDoc.getElementsByTagName("GoodsMx");
        for (let i = 0; i < domGoodsMxList.length; i++) {
            const element = domGoodsMxList[i];
            let tempGoodMx = createObjGoodsMx();
            tempGoodMx.GoodsName = getNodeValue(element.getElementsByTagName('GoodsName')[0]);
            tempGoodMx.GoodsUnit = getNodeValue(element.getElementsByTagName('GoodsUnit')[0]);
            tempGoodMx.GoodsPrice = getNodeValue(element.getElementsByTagName('GoodsPrice')[0]);
            tempGoodMx.GoodsTaxRate = getNodeValue(element.getElementsByTagName('GoodsTaxRate')[0]);
            tempGoodMx.GoodsGgxh = getNodeValue(element.getElementsByTagName('GoodsGgxh')[0]);
            tempGoodMx.GoodsNum = getNodeValue(element.getElementsByTagName('GoodsNum')[0]);
            tempGoodMx.GoodsJE = getNodeValue(element.getElementsByTagName('GoodsJE')[0]);
            tempGoodMx.GoodsSE = getNodeValue(element.getElementsByTagName('GoodsSE')[0]);
            tempGoodMx.HS_BZ = getNodeValue(element.getElementsByTagName('HS_BZ')[0]);
            tempGoodMx.SPBM = getNodeValue(element.getElementsByTagName('SPBM')[0]);
            tempGoodMx.QYSPBM = getNodeValue(element.getElementsByTagName('QYSPBM')[0]);
            tempGoodMx.SYYHZCBZ = getNodeValue(element.getElementsByTagName('SYYHZCBZ')[0]);
            tempGoodMx.YHZC = getNodeValue(element.getElementsByTagName('YHZC')[0]);
            tempGoodMx.LSLBZ = getNodeValue(element.getElementsByTagName('LSLBZ')[0]);
            result.RedInvReqBillMx[i] = tempGoodMx;
        }

        console.log('XML数据：');
        console.log(result);

        return result;
    },

    /**
     * 创建红字发票导出 XML 文件文本
     * @param {Object} dataRedInvReqBill 红字发票申请清单对象
     */
    createXMLFileText: function (dataRedInvReqBill) {
        let xmlTemplate = `<?xml version="1.0" encoding="UTF-8"?><FPXT><OUTPUT><CODE>0000</CODE><MESS>处理成功</MESS><DATA/></OUTPUT></FPXT>`
        var xmlDoc = XML_LOAD.loadXML(xmlTemplate);
        var domData = xmlDoc.getElementsByTagName("DATA")[0];

        var domRedInvReqBillMx = xmlDoc.createElement("RedInvReqBillMx");
        for (const item of dataRedInvReqBill.RedInvReqBillMx) {
            var domGoodsMx = xmlDoc.createElement("GoodsMx");

            var domGoodsName = xmlDoc.createElement("GoodsName");
            domGoodsName.appendChild(xmlDoc.createTextNode(item.GoodsName));
            var domGoodsUnit = xmlDoc.createElement("GoodsUnit");
            domGoodsUnit.appendChild(xmlDoc.createTextNode(item.GoodsUnit));
            var domGoodsPrice = xmlDoc.createElement("GoodsPrice");
            domGoodsPrice.appendChild(xmlDoc.createTextNode(item.GoodsPrice));
            var domGoodsTaxRate = xmlDoc.createElement("GoodsTaxRate");
            domGoodsTaxRate.appendChild(xmlDoc.createTextNode(item.GoodsTaxRate));
            var domGoodsGgxh = xmlDoc.createElement("GoodsGgxh");
            domGoodsGgxh.appendChild(xmlDoc.createTextNode(item.GoodsGgxh));
            var domGoodsNum = xmlDoc.createElement("GoodsNum");
            domGoodsNum.appendChild(xmlDoc.createTextNode(item.GoodsNum));
            var domGoodsJE = xmlDoc.createElement("GoodsJE");
            domGoodsJE.appendChild(xmlDoc.createTextNode(item.GoodsJE));
            var domGoodsSE = xmlDoc.createElement("GoodsSE");
            domGoodsSE.appendChild(xmlDoc.createTextNode(item.GoodsSE));
            var domHS_BZ = xmlDoc.createElement("HS_BZ");
            domHS_BZ.appendChild(xmlDoc.createTextNode(item.HS_BZ));
            var domSPBM = xmlDoc.createElement("SPBM");
            domSPBM.appendChild(xmlDoc.createTextNode(item.SPBM));
            var domQYSPBM = xmlDoc.createElement("QYSPBM");
            domQYSPBM.appendChild(xmlDoc.createTextNode(item.QYSPBM));
            var domSYYHZCBZ = xmlDoc.createElement("SYYHZCBZ");
            domSYYHZCBZ.appendChild(xmlDoc.createTextNode(item.SYYHZCBZ));
            var domYHZC = xmlDoc.createElement("YHZC");
            domYHZC.appendChild(xmlDoc.createTextNode(item.YHZC));
            var domLSLBZ = xmlDoc.createElement("LSLBZ");
            domLSLBZ.appendChild(xmlDoc.createTextNode(item.LSLBZ));

            domGoodsMx.appendChild(domGoodsName);
            domGoodsMx.appendChild(domGoodsUnit);
            domGoodsMx.appendChild(domGoodsPrice);
            domGoodsMx.appendChild(domGoodsTaxRate);
            domGoodsMx.appendChild(domGoodsGgxh);
            domGoodsMx.appendChild(domGoodsNum);
            domGoodsMx.appendChild(domGoodsJE);
            domGoodsMx.appendChild(domGoodsSE);
            domGoodsMx.appendChild(domHS_BZ);
            domGoodsMx.appendChild(domSPBM);
            domGoodsMx.appendChild(domQYSPBM);
            domGoodsMx.appendChild(domSYYHZCBZ);
            domGoodsMx.appendChild(domYHZC);
            domGoodsMx.appendChild(domLSLBZ);

            domRedInvReqBillMx.appendChild(domGoodsMx);
        }

        var domRedInvReqBill = xmlDoc.createElement("RedInvReqBill");

        var domReqBillNo = xmlDoc.createElement("ReqBillNo");
        domReqBillNo.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.ReqBillNo));
        var domResBillNo = xmlDoc.createElement("ResBillNo");
        domResBillNo.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.ResBillNo));
        var domStatusDM = xmlDoc.createElement("StatusDM");
        domStatusDM.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.StatusDM));
        var domStatusMC = xmlDoc.createElement("StatusMC");
        domStatusMC.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.StatusMC));
        var domBillType = xmlDoc.createElement("BillType");
        domBillType.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.BillType));
        var domTypeCode = xmlDoc.createElement("TypeCode");
        domTypeCode.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.TypeCode));
        var domInvNo = xmlDoc.createElement("InvNo");
        domInvNo.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.InvNo));
        var domSzlb = xmlDoc.createElement("Szlb");
        domSzlb.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.Szlb));
        var domIsMutiRate = xmlDoc.createElement("IsMutiRate");
        domIsMutiRate.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.IsMutiRate));
        var domDate = xmlDoc.createElement("Date");
        domDate.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.Date));
        var domBuyerName = xmlDoc.createElement("BuyerName");
        domBuyerName.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.BuyerName));
        var domBuyTaxCode = xmlDoc.createElement("BuyTaxCode");
        domBuyTaxCode.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.BuyTaxCode));
        var domSellerName = xmlDoc.createElement("SellerName");
        domSellerName.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.SellerName));
        var domSellTaxCode = xmlDoc.createElement("SellTaxCode");
        domSellTaxCode.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.SellTaxCode));
        var domAmount = xmlDoc.createElement("Amount");
        domAmount.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.Amount));
        var domTaxRate = xmlDoc.createElement("TaxRate");
        domTaxRate.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.TaxRate));
        var domTax = xmlDoc.createElement("Tax");
        domTax.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.Tax));
        var domReqMemo = xmlDoc.createElement("ReqMemo");
        domReqMemo.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.ReqMemo));
        var domSPBMBBH = xmlDoc.createElement("SPBMBBH");
        domSPBMBBH.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.SPBMBBH));
        var domSLBZ = xmlDoc.createElement("SLBZ");
        domSLBZ.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.SLBZ));

        domRedInvReqBill.appendChild(domReqBillNo);
        domRedInvReqBill.appendChild(domResBillNo);
        domRedInvReqBill.appendChild(domStatusDM);
        domRedInvReqBill.appendChild(domStatusMC);
        domRedInvReqBill.appendChild(domBillType);
        domRedInvReqBill.appendChild(domTypeCode);
        domRedInvReqBill.appendChild(domInvNo);
        domRedInvReqBill.appendChild(domSzlb);
        domRedInvReqBill.appendChild(domIsMutiRate);
        domRedInvReqBill.appendChild(domDate);
        domRedInvReqBill.appendChild(domBuyerName);
        domRedInvReqBill.appendChild(domBuyTaxCode);
        domRedInvReqBill.appendChild(domSellerName);
        domRedInvReqBill.appendChild(domSellTaxCode);
        domRedInvReqBill.appendChild(domAmount);
        domRedInvReqBill.appendChild(domTaxRate);
        domRedInvReqBill.appendChild(domTax);
        domRedInvReqBill.appendChild(domReqMemo);
        domRedInvReqBill.appendChild(domSPBMBBH);
        domRedInvReqBill.appendChild(domSLBZ);
        domRedInvReqBill.appendChild(domRedInvReqBillMx);

        var domAllCount = xmlDoc.createElement("ALLCOUNT")
        domAllCount.appendChild(xmlDoc.createTextNode(dataRedInvReqBill.RedInvReqBillMx.length));
        var domVersion = xmlDoc.createElement("Version")
        domVersion.appendChild(xmlDoc.createTextNode("2.0"));

        domData.appendChild(domAllCount);
        domData.appendChild(domVersion);
        domData.appendChild(domRedInvReqBill);

        return XML_LOAD.convertXML2String(xmlDoc);
    }
}

export default INVOICE_RED_TABLE_XML_V2
