import Api from "~/service/Api";
import Utils from "~/utils";

export async function declareTrial(params, model, secModel, thrdModel) {
  const declareTrial = await Api.post("v1/declare/trial", params)
  if (declareTrial.code == '000000') {
    let formatedData = Utils.toDepthObject(declareTrial.data)
    //汇出登记
    if (params.head.txCode == "040101") {
      if (model != undefined) {
        Object.assign(model.decmstOrtBaseVo, formatedData.decmstOrtBaseVo)
        Object.assign(model.decdtlOrtDeclrVo, formatedData.decdtlOrtDeclrVo)
        Object.assign(model.decdtlOrtManageVo, formatedData.decdtlOrtManageVo)
      }
      if (secModel != undefined) {
        Object.assign(secModel.decmstDrtBaseVo, formatedData.decmstDrtBaseVo)
        Object.assign(secModel.decdtlDrtManageVo, formatedData.decdtlDrtManageVo)
      }
      if (thrdModel != undefined) {
        Object.assign(thrdModel, formatedData.decmstExpnVo)
      }
    }
    //汇入汇款登记解付/后续解付
    if (params.head.txCode == "040201" || params.head.txCode == "040203") {
      if (model != undefined) {
        Object.assign(model.decmstOinBaseVo, formatedData.decmstOinBaseVo)
        Object.assign(model.decdtlOinDeclrVo, formatedData.decdtlOinDeclrVo)
      }
      if (secModel != undefined) {
        Object.assign(secModel.decmstDinBaseVo, formatedData.decmstDinBaseVo)
        Object.assign(secModel.decdtlDinManageVo, formatedData.decdtlDinManageVo)
      }
      if (thrdModel != undefined) {
        Object.assign(thrdModel, formatedData.decmstIncomeVo)
      }
    }
    //出口托收收汇
    if (params.head.txCode == "050205") {
      if (model != undefined) {
        Object.assign(model.decmstOinBaseVo, formatedData.decmstOinBaseVo)
        Object.assign(model.decdtlOinDeclrVo, formatedData.decdtlOinDeclrVo)
      }
      if (secModel != undefined) {
        Object.assign(secModel.decmstDinBaseVo, formatedData.decmstDinBaseVo)
        Object.assign(secModel.decdtlDinManageVo, formatedData.decdtlDinManageVo)
      }
      if (thrdModel != undefined) {
        Object.assign(thrdModel, formatedData.decmstIncomeVo)
      }
    }
    //进口代收付汇
    if (params.head.txCode == "050105") {
      if (model != undefined) {
        //对外付款/承兑通知书
        Object.assign(model.decmstOpyBaseVo, formatedData.decmstOpyBaseVo)
        Object.assign(model.decdtlOpyDeclrVo, formatedData.decdtlOpyDeclrVo)
        Object.assign(model.decdtlOpyManageVo, formatedData.decdtlOpyManageVo)
      }

      if (secModel != undefined) {
        //境内付款/承兑通知书
        Object.assign(secModel.decmstDpyBaseVo, formatedData.decmstDpyBaseVo)
        Object.assign(secModel.decdtlDpyManageVo, formatedData.decdtlDpyManageVo)
      }
      if (thrdModel != undefined) {
        Object.assign(thrdModel, formatedData.decmstExpnVo)
      }
    }
      //出口托收登记
      if(params.head.txCode == "050201"){         
      if(model != undefined){
        Object.assign(model, formatedData.decmstbankdocsetVo)            
      }  
    }
            //进口代收登记
        if(params.head.txCode == "050101"){         
        if(model != undefined){
          Object.assign(model, formatedData.decmstbankdocsetVo)            
        }  
      }



    //国际保函备用信用证开立
    if (params.head.txCode == "030301") {
      if (model != undefined) {
        Object.assign(model, formatedData.decmstExtGuarSignVo)
        //反担保人信息  
        if (model.cgs.length === formatedData.decmstExtGuarSignVo.fdbpts.length) {
          model.cgs.forEach((item, index) => {
            for (var inr in formatedData.decmstExtGuarSignVo.fdbpts[index]) {
              if (inr === "cgcode" || inr === "cgname" || inr === "cgnamen" || inr === "cgcountrycode") {
                if (formatedData.decmstExtGuarSignVo.fdbpts[index][inr] != "") {
                  Object.assign(item, formatedData.decmstExtGuarSignVo.fdbpts[index])
                }
              }
            }
          });
        } else {
          model.cgs = formatedData.decmstExtGuarSignVo.fdbpts.map(item => {
            return {
              cgcode: item.cgcode,
              cgname: item.cgname,
              cgnamen: item.cgnamen,
              cgcountrycode: item.cgcountrycode
            }
          });
        }
        //受益人信息
        if (formatedData.bfcys != undefined) {
          var benefitType = null;
          if (model.bfcys[0] != null) {
            benefitType = model.bfcys[0].bentype;
          }
          var list = {
            bencode: formatedData.bfcys.bencode, //受益人代码
            bename: formatedData.bfcys.bename, //受益人中文名称
            benamen: formatedData.bfcys.benamen, //受益人英文文名称
            bentype: benefitType,
            bencountrycode: formatedData.bfcys.bencountrycode //受益人国别/地区

          };
          if (list.bencode != undefined || list.bename != undefined || list.benamen != undefined || list.bencountrycode != undefined) {
            model.bfcys = [list];
          }
        }
        //被担保人
        if (formatedData.gueds != undefined) {
          var guedincode = null;
          var guedtyp = null;
          if (model.gueds[0] != null) {
            guedincode = model.gueds[0].guedindustrycode;
            guedtyp = model.gueds[0].guedtype;
          }
          var list1 = {
            guedcode: formatedData.gueds.guedcode, //被担保人代码
            guedname: formatedData.gueds.guedname, //被担保人中文名称
            guednamen: formatedData.gueds.guednamen, //被担保人英文名称
            guedtype: guedtyp, //被担保人类型
            guedcouncode: formatedData.gueds.guedcouncode, //被担保人国别/地区代码
            guedparecompname: formatedData.gueds.guedparecompname, //被担保人境内母公司中文名称
            guedparecompnamen: formatedData.gueds.guedparecompnamen, //被担保人境内母公司英文名称
            guedparecompcode: formatedData.gueds.guedparecompcode, //被担保人境内母公司代码
            issuecountrycode: formatedData.gueds.issuecountrycode, //发行/上市国家与地区
            guedindustrycode: guedincode
          };
          if (list1.guedcode != undefined || list1.guedname != undefined || list1.guednamen != undefined || list1.guedcouncode != undefined ||
            list1.guedparecompname != undefined || list1.guedparecompnamen != undefined || list1
            .guedparecompcode != undefined || list1.issuecountrycode != undefined
          ) {
            model.gueds = [list1];
          }
        }
      }
      if (secModel != undefined) {
        Object.assign(secModel, formatedData.decdtlExtGuarBalVo)
      }
      if (thrdModel != undefined) {
        Object.assign(thrdModel, formatedData.decmstGuaenrInfoVo)
      }
    }
    //进口信用证来单付汇
    if (params.head.txCode == "020314") {
      if (model != undefined) {
        //对外付款/承兑通知书
        Object.assign(model.decmstOpyBaseVo, formatedData.decmstOpyBaseVo)
        Object.assign(model.decdtlOpyDeclrVo, formatedData.decdtlOpyDeclrVo)
        Object.assign(model.decdtlOpyManageVo, formatedData.decdtlOpyManageVo)
      }
      if (secModel != undefined) {
        //境内付款/承兑通知书
        Object.assign(secModel.decmstDpyBaseVo, formatedData.decmstDpyBaseVo)
        Object.assign(secModel.decdtlDpyManageVo, formatedData.decdtlDpyManageVo)
      }
      if (thrdModel != undefined) {
        Object.assign(thrdModel, formatedData.decmstExpnVo)
      }
    }
    //出口信用证收汇
    if (params.head.txCode == "020412") {
      if (model != undefined) {
        Object.assign(model.decmstOinBaseVo, formatedData.decmstOinBaseVo)
        Object.assign(model.decdtlOinDeclrVo, formatedData.decdtlOinDeclrVo)
      }
      if (secModel != undefined) {
        Object.assign(secModel.decmstDinBaseVo, formatedData.decmstDinBaseVo)
        Object.assign(secModel.decdtlDinManageVo, formatedData.decdtlDinManageVo)
      }
      if (thrdModel != undefined) {
        Object.assign(thrdModel, formatedData.decmstIncomeVo)
      }
    }
    return Promise.resolve(true)
  }
  return Promise.resolve(false)
}