// 主表预计付款日期
const yjfksj = WfForm.convertFieldNameToId('yjfksj');
// 主表比例
const bl = WfForm.convertFieldNameToId('bcfkbl');
// 明细要求付款日期
const detail3Yqfkrq =  WfForm.convertFieldNameToId('yqfkrq',"detail_3");
// 明细本次付款比例
const detailBl = WfForm.convertFieldNameToId('bcfkbl',"detail_3");
// 关联订单
const relationOrder =  WfForm.convertFieldNameToId("gldd");
// 明细4施工合同字段
const detail4SgField = WfForm.convertFieldNameToId("sght","detail_4");
// 明细4采购合同字段
const detail4CgField = WfForm.convertFieldNameToId("cght","detail_4");
// 明细4发票字段
const detail4FpField = WfForm.convertFieldNameToId("fpje","detail_4");
// 明细4合同发票金额和
let detail4Map = new Map();
// 明细3施工合同字段
const detail3SgField = WfForm.convertFieldNameToId("sght","detail_3");
// 明细3采购合同字段
const detail3CgField = WfForm.convertFieldNameToId("cght","detail_3");
// 明细3 含税金额
const detail3TaxField = WfForm.convertFieldNameToId("ddhsje","detail_3");
// 明细3以收票金额
const detail3YspField = WfForm.convertFieldNameToId("yspje","detail_3");

const detail3ApplyMoneyField =  WfForm.convertFieldNameToId("bcfkje", "detail_3");
const detail3CgId = WfForm.convertFieldNameToId("cgddzbid", "detail_3");
const detail3Bl = WfForm.convertFieldNameToId('bcfkbl', "detail_3");
let detailBlMap = new Map();
const detail1Bl = WfForm.convertFieldNameToId('bcljfkbl', "detail_1");
const detail1CgId = WfForm.convertFieldNameToId("cgddzbid", "detail_1");

WfForm.registerCheckEvent(WfForm.OPER_ADDROW + "3", function (callback) {
    callback();
    setTimeout(()=>{
        initDeatail3Date();
    },5)
});
// // 关联订单变化时将明细四的合同-金额map进行初始化
// WfForm.bindFieldChangeEvent(relationOrder, function(obj,id,value){
//   setTimeout(()=>{
//     initDetail4Map();
//     changeDetail3SpMoney();
//   },2000);
// });

WfForm.bindFieldChangeEvent(relationOrder, function (obj, id, value) {
    setTimeout(() => {
        initDetail4Map();
        changeDetail3SpMoney();
        changeDetail1ApplyAMount();
    }, 2000);
});
WfForm.bindDetailFieldChangeEvent(detail3ApplyMoneyField, function () {
    setTimeout(() => {
        changeDetail1ApplyAMount();
    }, 2000);
});

WfForm.bindFieldChangeEvent(`${yjfksj},${bl}`, function(obj,id,value){
    initDeatail3Date();
});

// 计算明细三开票金额
function changeDetail3SpMoney(){
    let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(",");
    for (let i = 0; i < detail3RowArr.length; i++) {
        let rowIndex = detail3RowArr[i];
        if (rowIndex !== "") {
            let detail3SgFieldVal = WfForm.getFieldValue(`${detail3SgField}_${rowIndex}`);
            let detail3CgFieldVal = WfForm.getFieldValue(`${detail3CgField}_${rowIndex}`);
            let fpje = detail4Map.get(detail3SgFieldVal) ?? detail4Map.get(detail3CgFieldVal);
            let mapVal = detail4Map.get(detail3SgFieldVal);
            let key = detail3SgFieldVal;
            if(!mapVal){
                key = detail3CgFieldVal;
            }
            console.log('key ', key);
            if(fpje){
                console.log('fpje => ', fpje);
                // 0.1 0.3
                let detail3TaxVal = WfForm.getFieldValue(`${detail3TaxField}_${rowIndex}`);
                console.log('detail3TaxVal => ', detail3TaxVal);
                let tempJe = fpje - detail3TaxVal;
                console.log('tempJe => ', tempJe);
                if(fpje > detail3TaxVal){
                    WfForm.changeFieldValue(`${detail3YspField}_${rowIndex}`,{value: detail3TaxVal});
                }else if(tempJe <= 0 && fpje == 0){
                    WfForm.changeFieldValue(`${detail3YspField}_${rowIndex}`,{value: 0});
                }else if(tempJe < 0){
                    WfForm.changeFieldValue(`${detail3YspField}_${rowIndex}`,{value: fpje});
                    tempJe = 0;
                }
                detail4Map.set(key, tempJe);
                console.log('detail4Map ', detail4Map)
            }else{
                WfForm.changeFieldValue(`${detail3YspField}_${rowIndex}`,{value: 0});
            }
        }
    }
}


function initDeatail3Date(){
    let dateVal = WfForm.getFieldValue(yjfksj);
    let blVal = WfForm.getFieldValue(bl);
    let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(",");
    for (let i = 0; i < detail3RowArr.length; i++) {
        let rowIndex = detail3RowArr[i];
        if (rowIndex !== "") {
            WfForm.changeFieldValue(`${detail3Yqfkrq}_${rowIndex}`,{value: dateVal});
            WfForm.changeFieldValue(`${detailBl}_${rowIndex}`,{value: blVal});
        }
    }
}

function initDetail4Map(){
    let detail4RowArr = WfForm.getDetailAllRowIndexStr('detail_4').split(",");
    console.log('detail4RowArr ', detail4RowArr);
    for (let i = 0; i < detail4RowArr.length; i++) {
        let rowIndex = detail4RowArr[i];
        if (rowIndex !== "") {
            let detail4SgFieldVal = WfForm.getFieldValue(`${detail4SgField}_${rowIndex}`);
            let detail4CgFieldVal = WfForm.getFieldValue(`${detail4CgField}_${rowIndex}`);
            let detail4FpFieldVal = WfForm.getFieldValue(`${detail4FpField}_${rowIndex}`);
            let key = -1;
            console.log('detail4SgFieldVal ', detail4SgFieldVal);
            console.log('detail4CgFieldVal ', detail4CgFieldVal);
            console.log('detail4FpFieldVal ', detail4FpFieldVal);
            if(!detail4FpFieldVal){
                continue;
            }
            key = detail4CgFieldVal ?? detail4SgFieldVal;
            console.log('key ', key);
            let tempVal = parseFloat(detail4Map.get(key) ?? 0);
            tempVal += parseFloat(detail4FpFieldVal);
            detail4Map.set(key, tempVal);
        }
    }
    console.log('detail4Map ', detail4Map)
}


WfForm.bindDetailFieldChangeEvent(detail3ApplyMoneyField,  (id, rowIndex, value) => {
    setTimeout(()=>{
        let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(",");
        detailBlMap = new Map();
        for (let i = 0; i < detail3RowArr.length; i++) {
            let rowIndex = detail3RowArr[i];
            if (rowIndex !== "") {
                let blStr = WfForm.getFieldValue(`${detail3Bl}_${rowIndex}`);
                if(blStr == ''){
                    console.log('111')
                    continue;
                }
                let bl = parseFloat(blStr);
                console.log('bl => ' , bl);
                let cg = WfForm.getFieldValue(`${detail3CgId}_${rowIndex}`);
                let mapVal = detailBlMap.get(cg);
                if(!mapVal){
                    mapVal = 0;
                }
                mapVal+= bl;
                detailBlMap.set(cg, mapVal);
            }
        }
        console.log('detailBlMap ', detailBlMap)
        let detail1RowArr = WfForm.getDetailAllRowIndexStr('detail_1').split(",");
        for (let i = 0; i < detail1RowArr.length; i++) {
            let rowIndex = detail1RowArr[i];
            if (rowIndex !== "") {
                let cg = WfForm.getFieldValue(`${detail1CgId}_${rowIndex}`);
                let val = detailBlMap.get(cg) === '' ?  0 : detailBlMap.get(cg);
                WfForm.changeFieldValue(`${detail1Bl}_${rowIndex}`, {value: val})
            }
        }
    },300);
});




// 明细3合同总金额
const detail3AmountField = WfForm.convertFieldNameToId("htzje", "detail_3");
// 明细3订单号字段
const detail3OrderField = WfForm.convertFieldNameToId("ddh","detail_3");
// 明细1本次付款金额字段
const detail1ApplyAmountField = WfForm.convertFieldNameToId("bcsqfkje","detail_1");
// 明细1含税金额
const detail1AmountTaxField = WfForm.convertFieldNameToId("ddmxxj","detail_1");
// 明细1订单号
const detail1OrderField = WfForm.convertFieldNameToId("ddh","detail_1");
// 明细1预计付款日期
const detail1ApplyDate = WfForm.convertFieldNameToId("yjfkrq","detail_1");
function changeDetail1ApplyAMount(){
    let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(",");
    // 明细3 订单号-合同总金额map
    let detail3ContractAmountMap = new Map();
    let detail3ApplyAmountMap = new Map();
    let detail3ApplyMoneyField = WfForm.convertFieldNameToId("bcfkje", "detail_3");
    for (let i = 0; i < detail3RowArr.length; i++) {
        let rowIndex = detail3RowArr[i];
        if (rowIndex !== "") {
            let detail3Order = WfForm.getFieldValue(`${detail3OrderField}_${rowIndex}`);
            if(!detail3ContractAmountMap.has(detail3Order)){
                detail3ContractAmountMap.set(detail3Order, parseFloat(WfForm.getFieldValue(`${detail3AmountField}_${rowIndex}`)));
            }
            let detail3Amount = parseFloat(WfForm.getFieldValue(`${detail3ApplyMoneyField}_${rowIndex}`));
            let sum = detail3ApplyAmountMap.get(detail3Order) ?? 0;
            detail3ApplyAmountMap.set(detail3Order, sum + detail3Amount);
        }
    }
    console.log('detail3ContractAmountMap : ', detail3ContractAmountMap);
    console.log('detail3ApplyAmountMap : ', detail3ApplyAmountMap);
    let detail1RowArr = WfForm.getDetailAllRowIndexStr('detail_1').split(",");
    for (let i = 0; i < detail1RowArr.length; i++) {
        let rowIndex = detail1RowArr[i];
        if (rowIndex !== "") {
            let detail1Order =WfForm.getFieldValue(`${detail1OrderField}_${rowIndex}`);
            console.log('detail1Order ', detail1Order);
            // 含税金额
            let detail1AmountTax = parseFloat(WfForm.getFieldValue(`${detail1AmountTaxField}_${rowIndex}`) ?? 0);
            console.log('detail1AmountTax ', detail1AmountTax);
            // 本次付款金额和
            let applySum = parseFloat(detail3ApplyAmountMap.get(detail1Order) ?? 0);
            console.log('applySum ', applySum);
            // 合同总金额
            let contractAmount = parseFloat(detail3ContractAmountMap.get(detail1Order) ?? 0);
            console.log('contractAmount ', contractAmount);
            let val = (applySum / contractAmount) * detail1AmountTax;
            console.log('计算金额 : ', val)
            WfForm.changeFieldValue(`${detail1ApplyAmountField}_${rowIndex}`, {value: val})
        }
    }
}
WfForm.bindDetailFieldChangeEvent(detail3Yqfkrq,()=>{
    initDetail1ApplyDate();
});
function initDetail1ApplyDate(){
    let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(",");
    let detail3DateMap = new Map();
    for (let i = 0; i < detail3RowArr.length; i++) {
        let rowIndex = detail3RowArr[i];
        if (rowIndex !== "") {
            let detail3Order = WfForm.getFieldValue(`${detail3OrderField}_${rowIndex}`);
            if(!detail3DateMap.has(detail3Order)){
                detail3DateMap.set(detail3Order, WfForm.getFieldValue(`${detail3Yqfkrq}_${rowIndex}`));
            }
        }
    }
    console.log('detail3DateMap ', detail3DateMap);
    let detail1RowArr = WfForm.getDetailAllRowIndexStr('detail_1').split(",");
    for (let i = 0; i < detail1RowArr.length; i++) {
        let rowIndex = detail1RowArr[i];
        if (rowIndex !== "") {
            let detail1Order =WfForm.getFieldValue(`${detail1OrderField}_${rowIndex}`);
            WfForm.changeFieldValue(`${detail1ApplyDate}_${rowIndex}`, {value: detail3DateMap.get(detail1Order)})
        }
    }
}