import axios from 'axios'
export default {
    execute(context, param) {
        let subTable = param.subTable||'BXMXZB';
        let directGeneration = param.directGeneration||false;
        if (!param.billId || !param.billDefine) {
            context.dom.$Message.info("请配置查询单据参数");
            return;
        }
        if(context.selects.length==0){
            context.dom.$Message.info("请勾择需要生成的数据");
            return;
        }
        let that = this;
        let billDefine = param.billDefine.replace(/_E/ig, "").replace(/_R/ig, "").replace(/_A/ig, "");
        let arrApi1 = [];
        context.selects.forEach(v=>{
            arrApi1.push(that.searchBillObj(billDefine,v[param.billId]))
        })
        let arr = [];
        let arrApi = [];
        Promise.all(arrApi1).then((list) => {
            list.forEach((v,i)=>{
                let res = v;
                if(res && res[subTable]){
                    res[subTable].forEach(v=>{
                        let billDefine = v.BILLNAME && v.BILLNAME.replace(/_E/ig, "").replace(/_R/ig, "").replace(/_A/ig, "");
                        arrApi.push(that.searchBillObj(billDefine,v.BILLID,i))
                    })
                }
                arr.push({//直接生成
                    index:i,
                    data:res
                }) 
            })
            if(!directGeneration){//间接生成
                Promise.all(arrApi).then((subList) => {
                    arr.forEach(v=>{
                        v.detailData=[];
                        subList.forEach(f=>{
                            if(v.index==f.index){
                                v.detailData.push(f); 
                            }
                        })
                    })
                    that.generateVoucher(arr,context);
                }) 
            }else{
                that.generateVoucher(arr,context);
            }
        })   
    },
    generateVoucher(list,context){
        axios({
              method: 'POST',
              url:GMS.url+'/v1/reimbursePort/combineCreateVchr',
              headers: {
                  Authorization:GMS.token,
              },
              data:list
            }).then(res => {
              if (res.status === 200) {
                if(res.data.data.showVoucher){//展示凭证
                    GMS.vbus.$emit('previewVoucher',res.data.data);
                }else{
                    if(res.data.data.successVchrs && res.data.data.successVchrs.length > 0){
                        GMS.vbus.$emit('custom-query-refresh');
                        GMS.vbus.$emit('tab-count-refresh');
                    }
                    context.dom.$Modal.warning({
                        title: '信息提示',
                        content: res.data.data.processMsg,
                        onOk: () => {},
                    });
                }
              }
            })
    },
    searchBillObj(billDefine, billId ,index) {
        return new Promise((resolve, reject) => {
            GMS.$http.get(
                `/gms/bill/${billDefine}/${billId}?withSubs=true`
            ).then((data) => {
                if (data.status == 200 && data.data.code == 0) {
                    let obj = data.data.content
                    obj.billDefine = billDefine;
                    obj.index = index
                    resolve(obj);
                }
            }).catch((error)=>{
                reject(error)
            })
        })
    }
}