const $ = require("jquery");
const {get, post, config, getUrlArgObject, openNewWin, titleConfig} = require('./promise.js');
const targetUrl = '/lantone/index';
let treatUrl = targetUrl;
$(function () {
    let mrId = getUrlArgObject('mrId')
    $(".knowledge .a-link").click(function () {
        const code = $(this).attr("code");
        openNewWin(window.encodeURI(targetUrl + '?page=' + code), true);
    })
});

function renderConfig(config) {
    const classMap = {
        'conditionder': 'go-push',
        'treatment': 'recipe',
        'tcmknowledge': 'knowledge',
    };
    config.map((it) => {
        if (+it.status === 1) {
            $(".tcmWarp ." + classMap[it.code]).show();
        }
    })
}

function getTcmMr(mrId, config) {
    renderConfig(config.planDetails || []);
    if (!mrId) {    //没有mrId时不显示推送模块
        $(".recipe").hide();
        $("#goPush").on("click", function () {
            openNewWin(window.encodeURI(targetUrl + '?page=bztd&jibing='), true);
        });
    }else{
        post('/sys/mr/getTcmMr', { mrId: mrId }).then((res) => {
            //console.log(res)
            let data = res.data.data || {}
            const { tcmdisease, tcmsyndrome } = data;
            if (res.data.code === '0') {
                const diag = (tcmdisease || {}).uniqueName;
                $("#goPush").on("click", function () {
                    openNewWin(window.encodeURI(targetUrl + '?page=bztd&jibing=' + diag), true);
                });
                getTcmData({ tcmDiag: diag, tcmSyndrome: (tcmsyndrome || {}).uniqueName }, config)
            } else {

            }
        })
    }
    
}

//获取处方推荐
function getTcmData(param, config) {
    const item = config.planDetails.filter((it) => it.code === "treatment");
    const isHor = window.location.pathname.indexOf("cdssHorizontal") > -1;
    const num = isHor ? 3 : ((item[0] || {}).number || 3);
    const {tcmDiag, tcmSyndrome} = param;
    if (!tcmDiag) {     //没有推送入参时不显示推送模块
        $(".recipe").hide();
        return;
    }
    $("#jibing").text(tcmDiag + "-" + tcmSyndrome);
    $('#bingming').text(tcmDiag);
    $('#zhenghou').text(tcmSyndrome);
    treatUrl = targetUrl + '?jibing=' + tcmDiag + "&zhengxing=" + tcmSyndrome;
    //处方推荐
    get(window.encodeURI('/lantone/private/zycftj?jibing=' + tcmDiag + "&zhengxing=" + tcmSyndrome)).then((res) => {
        console.log("窗口地址：", treatUrl)
        let data = res.data.data || []
        if (!data.length) {   //没有处方时不显示推送模块
            $(".recipe").hide();
            return;
        }
        if (res.data.status) {
            renderTcm(data, num)
        } else {

        }
    })
    //治法推荐-暂时不用
    /*get('/lantone/private/zhongyizx?type=1&code=' + tcmDiag + '&value=' + tcmSyndrome).then((res) => {
        console.log(res)
        let data = res.data.data || {}
        if (res.data.status) {
            //renderTcm(data)
        } else {

        }
    })*/
}

function renderTcm(data, num = 3) {
    let str = ``, it;
    $("#goTreat").hide();
    const n = num < data.length ? num : data.length;
    for (let i = 0; i < n; i++) {
        it = data[i];
        str += `<li class="clearfix"><div class="label">处方${i + 1}：</div>
                    <div class="val">
                    <p class="name">${it.name}</p>
                    <p class="desc" title="${it.effect}">${it.effect}</p>
                </div></li>`;
    }

    if (data.length > num) {
        if ($("#goTreat").length) {     //竖版显示即可不用新增
            str += ``;
            $("#goTreat").show();
        } else {
            str += `<a class="go go-push" id="goTreat">更多 <img class="icon" src="./images/go.png" alt=""></a>`;
            $("#goTreat").show();
        }
    }
    $('.detail ul').html(str);
    if(data.length > num){
        $(".horizontal-detail .val:nth-child(2) .desc").css("width",'420px')
    }
    //更多治法
    $("#goTreat").on('click', function () {
        openNewWin(window.encodeURI(treatUrl), true);
    });
}

module.exports = {
    getTcmMr
}