export default {
    execute: function (bill, param) {
        let dataObject = bill.getMasterData();
        let id = dataObject.getValue('id');
        let rq = dataObject.getValue('RQ');
        let djlb = dataObject.getValue('DJLB').code;
        GMS.$http.post(`/rbc/bill/action/extractCash`, {
            id:id,
            date:rq,
            djlb:djlb
        }).then((res) => {
            if(res.data.code===0){
                this.setSubTableData(bill,'RIMXZZB',res.data.content)
            }else if(res.data.code == -1 && res.data.message){
                GAMS.Common.messagePrompt(res.data.message);
            }
        })
    },
    setSubTableData(bill,tableName,datas) {
        //清空子表
        let subData = bill.getSubData(tableName)
        let dataLength = subData.length
        if (dataLength > 0) {
            for (let i = 0; i < dataLength; i++) {
                bill.deleteSubData(tableName, 0,null)
            }
        }
        //子表字段
        let subField = this.getSubField(bill,tableName)
        let allFormula = null
        //赋值子表
        datas.forEach((data, index) => {
            const subData = bill.addSubData(tableName,index)
            for (const key in subField) {
                if(key == 'BZ' || key == 'DJLB' || key == 'BXBM' || key == 'SFKR'){
                    const idKey = key + 'ID'
                    const codeKey = key + 'CODE'
                    const nameKey = key + 'NAME'
                    const baseDataValue = { id: data[idKey],code:data[codeKey], name: data[nameKey] }
                    subData.setValue(key, baseDataValue)
                }else if(key == 'RQ'){
                    subData.setValue(key, new Date(data[key]).getTime())
                }else{
                    subData.setValue(key, data[key])
                }
            }
            //调用子表公式
            if (!allFormula) {
                allFormula = bill.getSubFormula(tableName)
            }
            this.funSubTableFormula(bill,allFormula.execution[tableName])
        })
        bill.refreshSubDataRow(tableName)
        if (datas.length > 0) {
            GMS.vbus.$Message.success('提取成功');
        } else {
            GMS.vbus.$Message.success('没有数据');
        }
    },
    getSubField(bill,tableName) {
        let fieldObj = {}
        if (bill.getAllSubFiledByName(tableName)) {
          fieldObj = JSON.parse(JSON.stringify(bill.getAllSubFiledByName(tableName)))
        } else {
          this.$message.error('子表字段获取失败')
        }
        return fieldObj
    },
    funSubTableFormula(bill,formuals){
      var subFormulaList = this.getSubFormulas(formuals);
      if(subFormulaList&&subFormulaList.length > 0){
          for (const _s_f_n of subFormulaList) {
              bill.runSubSumFormula(_s_f_n);
          }
      }
    },
    getSubFormulas(formuals){
        var subFormulaList = [];
        for (const _f in formuals) {
            for (const iterator of formuals[_f]) {
                subFormulaList.push(iterator);
            }
        }
        return Array.from(new Set(subFormulaList));
    }
}