import { $http } from '../../../../containers/config/https';
import { $loadTemplate,$loopPrint } from'../../../lodop/Common/Print.js';

//打印处方
export function $PrintPresc(props) {
    let prop = JSON.parse(JSON.stringify(props))
    let prescno=prop.prescno;
    let data = {};
    data={
        params: [{
            ordPrescNo: prescno,
            flag: 'Presc',
        }]
    }
    $http.post('urlS', this, {
        code: "02030059",
        data: data,
        success: function (res) {
            if (res.errorCode === '0') {
                let template=res.result.template;
                prop.template=template;
                let herbalFlag=res.result.herbalFlag;
                if (herbalFlag==="Y") {
                    $PrintZCYPresc(prop);
                }else{
                    $PrintXYPresc(prop);
                }
            }
        }
    })
}

//打印标签
export function $PrintLabel(props) {
    let prop = JSON.parse(JSON.stringify(props))
    let prescno=prop.prescno;
    let data = {};
    data={
        params: [{
            ordPrescNo: prescno,
            flag: 'Label',
        }]
    }
    $http.post('urlS', this, {
        code: "02030059",
        data: data,
        success: function (res) {
            if (res.errorCode === '0') {
                let template=res.result.template;
                prop.template=template;
                let herbalFlag=res.result.herbalFlag;
                if (herbalFlag==="Y") {
                    
                }else{
                    $PrintXYLabel(prop);
                }
            }
        }
    })
}

//打印配药单
export function $PrintPYD(props) {
    let prop = JSON.parse(JSON.stringify(props))
    let prescno=prop.prescno;
    let data = {};
    data={
        params: [{
            ordPrescNo: prescno,
            flag: 'PYD',
        }]
    }
    $http.post('urlS', this, {
        code: "02030059",
        data: data,
        success: function (res) {
            if (res.errorCode === '0') {
                let template=res.result.template;
                prop.template=template;
                let herbalFlag=res.result.herbalFlag;
                if (herbalFlag==="Y") {
                    $PrintZCYPYD(prop);
                }else{
                    $PrintXYPYD(prop);
                }
            }
        }
    })
}


//打印西药处方
export function $PrintXYPresc(props) {
    let prop = JSON.parse(JSON.stringify(props))
    let dispID=prop.dispID;
    let prescno=prop.prescno;
    let phFlag=prop.phFlag;
    let data = {};
    data={
        params: [{
            dispID: dispID,
            ordPrescNo: prescno,
            phFlag: phFlag,
        }]
    }
    $http.post('urlS', this, {
        code: "02030060",
        data: data,
        success: function (res) {
            if (res.errorCode === '0') {
                //加载模板
                for(var j = 0; j < res.result.length; j++) {
                    $loadTemplate(props.template,res.result[j].printStr);
                }
            }
        }
    })
}

//打印中草药处方
export function $PrintZCYPresc(props) {
    let prop = JSON.parse(JSON.stringify(props))
    let dispID=prop.dispID;
    let prescno=prop.prescno;
    let phFlag=prop.phFlag;
    let data = {};
    data={
        params: [{
            dispID: dispID,
            ordPrescNo: prescno,
            phFlag: phFlag,
        }]
    }
    $http.post('urlS', this, {
        code: "02030061",
        data: data,
        success: function (res) {
            if (res.errorCode === '0') {
                //加载模板
                for(var j = 0; j < res.result.length; j++) {
                    $loadTemplate(props.template,res.result[j].printStr);
                }
            }
        }
    })
}

//打印西药标签
export function $PrintXYLabel(props) {
    let prop = JSON.parse(JSON.stringify(props))
    let dispID=prop.dispID;
    let prescno=prop.prescno;
    let phFlag=prop.phFlag;
    let data = {};
    data={
        params: [{
            dispID: dispID,
            ordPrescNo: prescno,
            phFlag: phFlag,
        }]
    }
    $http.post('urlS', this, {
        code: "02030062",
        data: data,
        success: function (res) {
            if (res.errorCode === '0') {
                //循环打印
                for(var j = 0; j < res.result.length; j++) {
                    let printLogData={
                        params:[
                            {
                                prescno : prescno,
                                printCount : j,
                                templateStr: res.result[j].templateStr,
                                printStr : res.result[j].printStr,
                                printNote : "PrintXYLabel"
                            }
                        ]
                    }
                    $SavePrintLog(printLogData);
                    $loopPrint(res.result[j].templateStr,res.result[j].printStr);
                }
                // //加载模板
                // let templateHead=props.template.split("^")[0];
                // $loadTemplate(templateHead,res.result[0].printStr);
                // let template=props.template.split("^")[1];
                // for(var j = 1; j < res.result.length; j++) {
                //     $loadTemplate(template,res.result[j].printStr);
                // }
            }
        }
    })
}


//打印中草药配药单
export function $PrintZCYPYD(props) {
    let prop = JSON.parse(JSON.stringify(props))
    let dispID=prop.dispID;
    let prescno=prop.prescno;
    let phFlag=prop.phFlag;
    let data = {};
    data={
        params: [{
            dispID: dispID,
            ordPrescNo: prescno,
            phFlag: phFlag,
        }]
    }
    $http.post('urlS', this, {
        code: "02030075",
        data: data,
        success: function (res) {
            if (res.errorCode === '0') {
                //加载模板
                for(var j = 0; j < res.result.length; j++) {
                    $loadTemplate(props.template,res.result[j].printStr);
                }
            }
        }
    })
}

//打印西药配药单
export function $PrintXYPYD(props) {
    let prop = JSON.parse(JSON.stringify(props))
    let dispID=prop.dispID;
    let prescno=prop.prescno;
    let phFlag=prop.phFlag;
    let data = {};
    data={
        params: [{
            dispID: dispID,
            ordPrescNo: prescno,
            phFlag: phFlag,
        }]
    }
    console.log(11111111111111111111111111111)
    $http.post('urlS', this, {
        code: "02030125",
        data: data,
        success: function (res) {
            if (res.errorCode === '0') {
                //加载模板
                for(var j = 0; j < res.result.length; j++) {
                    $loadTemplate(props.template,res.result[j].printStr);
                }
            }
        }
    })
}

//保存打印日志
export function $SavePrintLog(data) {
    $http.post('urlS', this, {
        code: "02030077",
        data: data,
        success: function (res) {

        }
    })
}