import { getDict } from '@/tumour/utils/dictionaries.js'

/**
 * 1肺癌 2乳腺癌 3结直肠癌 4肝癌 5宫颈癌 6甲状腺癌 7胃癌 8食管癌 9前列腺癌 10肾癌 11黑色素癌
 */

/**
 * 
 * @param {String} key 需要生成的问卷
 * @param {Object} userInfo 患者信息
 * @param {Object} detail 问卷选中的值
 * @returns 
 */
export function getQuestion(key, userInfo, detail) {

    /**
     * 设置问题title
     * @param {String} text title文字
     * @param {String} linkKey 关联的表单key
     * @returns 
     */
    const setTitle = (text, linkKey) => {
        return {
            text,
            linkKey
        }
    }

    const questions = { 
        q1: {
            title: '肺癌筛查量表',
            issue: [{
                key: 'question1',
                title: setTitle('1、年龄介于45~74岁之间'),
                answer: {
                    // radio单选 checkbox多选
                    type: 'radio',
                    // 选项中 其他选项 的value
                    other: null,
                    // 答案的选项 DC00001是否
                    cont: getDict('DC00001'),
                    // 禁用
                    disabled: true,
                },
                // 赋初始值
                init: () => {
                    return userInfo.age >= 45 && userInfo.age <= 74 ? 1 : 2
                }
            }, {
                title: setTitle('2、吸烟包年数≥20包年,包括曾经吸烟≥20包年,但戒烟不足15年'),
                key: 'question2',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('3、与条件2共同生活或同室工作被动吸烟>20年'),
                key: 'question3',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('4、患有慢性阻塞性肺疾病'),
                key: 'question4',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('5、有职业暴露史≥1年,包括暴露于石棉、氡、铍、铬、镉、硅、煤烟和煤烟灰'),
                key: 'question5',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('6、有一、二级亲属确诊肺癌'),
                key: 'question6',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }],
            // 高危判断条件
            check() {
                // console.log('question check', this.issue)
                return this.issue[0].value === 1 &&
                    this.issue.filter(e => e.value === 1).length >= 2
            },
            remark: '肺癌高风险人群建议:每年进行胸部低剂量CT平扫检查。如发现肺部结节,建议到胸外科、呼吸内科、肿瘤科等专科进行就诊。',
            sub: '肺癌高风险人群介于45~74岁,且2-6至少满足一项'
        },
        q2: {
            title: '乳腺癌筛查量表',
            issue: [{
                title: setTitle('1、既往乳腺3类及以上结节病史'),
                key: 'question4',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('2、既往有乳腺导管或小叶中重度不典型增生或小叶原位癌患者'),
                key: 'question2',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('3、既往有胸部放疗史的患者'),
                key: 'question3',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('4、一级亲属或本人携带有 BRCA1/BRCA2 基因突变'),
                key: 'question5',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('5、有2个或2个以上≤50岁二级亲属诊断为乳腺癌或卵巢癌或原发性腹膜癌'),
                key: 'question6',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }],
            check() {
                return this.issue.filter(e => e.value === 1).length >= 1
            },
            remark: `一般风险人群建议:一、20~39岁:(1)每月1次乳腺自我检查;(2)每1—3年1次临床检查。 
                                二、40~69岁:(1)每1~2年1次乳腺彩超或乳腺钼靶检查;(2)每月1次乳腺自我检查; (3)每年1次临床检查。
                                三、70岁以上:(1)有症状或可疑体征时进行影像学检查;(2)每月1次乳腺自我检查;(3)每年1次临床检查。\n乳腺癌高危人群建议:临床体检、B 超、乳腺钼靶检查、乳腺MRI、肿瘤标志物。`,
            sub: '满足任意一项乳腺癌高危人群'
        },
        q3: {
            title: '结直肠癌筛查量表',
            issue: [{
                title: setTitle('1、大便潜血阳性'),
                key: 'question1',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('2、一级亲属有结直肠癌病史'),
                key: 'question2',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('3、以往有肠道腺瘤史'),
                key: 'question3',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('4、本人有癌症史'),
                key: 'question4',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('5、有大便习惯的改变'),
                key: 'question5',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('6、符合以下任意2项者:', 'question7'),
                key: 'question6',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    // 禁用
                    disabled: true,
                },
                // 赋初始值
                init: () => {
                    return 2
                }
            }, {
                key: 'question7',
                index: 7,
                answer: {
                    type: 'checkbox',
                    cont: getDict('DC00096')
                },
                change: (that) => {
                    if (!that) return
                    console.log('that', that)
                    const item1 = that.getIssue('question7') || {}
                    const item2 = that.getIssue('question6') || {}
                    if (!item1 || !item2) return
                    const val = item1.value || []
                    item2.value = val.length >= 2 ? 1 : 2
                }
            }],
            check() {
                return this.issue.filter(e => e.value === 1 && e.title).length >= 1
            },
            remark: '一般风险人群建议:随访观察。\n结直肠癌高危人群建议:进一步进行生化全套、结直肠镜检查、腹部 CT、肿瘤标志物。',
            sub: '有任意一条者视为结直肠癌高危人群',
        },
        q4: {
            title: '肝癌筛查量表',
            issue: [{
                title: setTitle('1、年龄大于40岁'),
                key: 'question1',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    // 禁用
                    disabled: true,
                },
                // 赋初始值
                init: () => {
                    return userInfo.age > 40 ? 1 : 2
                }
            }, {
                title: setTitle('2、慢性乙型肝炎或者慢性丙型肝炎史'),
                key: 'question2',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('3、肝硬化病史'),
                key: 'question3',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('4、长期酗酒史(每日饮酒大于2两,饮酒史超过1年)'),
                key: 'question4',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('5、出血史:(出血倾向:鼻出血、牙龈及皮下瘀斑)'),
                key: 'question5',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('6、直系属亲属(父母、兄弟姐妹、子女)有肝癌病史'),
                key: 'question6',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('7、长期进食霉变食物或进食烟熏或盐腌的肉制品'),
                key: 'question7',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('8、有肝区疼痛史:(肝区疼痛:右上腹或中上腹持续性或间歇性钝痛、刺痛或胀痛,夜间或劳累后加重)'),
                key: 'question8',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('9、符合以下任意2项者:', 'question10'),
                key: 'question9',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    // 禁用
                    disabled: true,
                },
                // 赋初始值
                init: () => {
                    return 2
                }
            }, {
                key: 'question10',
                index: 10,
                answer: {
                    type: 'checkbox',
                    cont: getDict('DC00095')
                },
                change: (that) => {
                    if (!that) return
                    const item1 = that.getIssue('question10')
                    const item2 = that.getIssue('question9')
                    if (!item1 || !item2) return
                    const val = item1.value || []
                    item2.value = val.length >= 2 ? 1 : 2
                }
            }],
            check() {
                return this.issue[0].value === 1 &&
                    this.issue.filter(e => e.value === 1 && e.title).length >= 2
            },
            remark: '一般风险人群建议:随访观察。\n肝癌高危人群建议:进一步检查彩色多普勒腹部超声、甲胎蛋白测定。',
            sub: '肝癌高风险人群年龄40岁以上,且2-9至少满足一项'
        },
        q5: {
            title: '宫颈癌筛查量表',
            issue: [{
                title: setTitle('1、已婚或有性生活史3年及以上的女性'),
                key: 'question1',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('2、家族中有人患有宫颈癌或其他妇科癌症'),
                key: 'question2',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('3、曾经被诊断为宫颈炎症、宫颈糜烂或宫颈息肉'),
                key: 'question3',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('4、有阴异常道分泌物增多,非经期流血'),
                key: 'question4',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('5、感染过人类乳头瘤病毒(HPV)'),
                key: 'question5',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('6、同房后阴道出血或非经期流血'),
                key: 'question6',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }],
            check() {
                return this.issue[0].value === 1 &&
                    this.issue.filter(e => e.value === 1).length >= 2
            },
            remark: '宫颈癌高风险人群建议:行阴道超声、宫颈涂片、HPV检查。',
            sub: '已婚或有性生活史3年及以上的女性,且至少符合条件之一'
        },
        q6: {
            title: '甲状腺癌筛查量表',
            issue: [{
                title: setTitle('1、年龄40岁以上'),
                key: 'question1',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    // 禁用
                    disabled: true
                },
                // 赋初始值
                init: () => {
                    return userInfo.age > 40 ? 1 : 2
                }
            }, {
                title: setTitle('2、有甲状腺疾病:甲状腺炎、甲状腺结节'),
                key: 'question2',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('3、有甲状腺癌家族史'),
                key: 'question3',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('4、长期接触辐射或化学物质'),
                key: 'question4',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('5、颈部放疗史'),
                key: 'question5',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('6、有其他身体不适或异常表现,如体重变化、颈部增粗'),
                key: 'question6',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }],
            check() {
                return this.issue[0].value === 1 &&
                    this.issue.filter(e => e.value === 1).length >= 2
            },
            remark: '甲状腺高风险人群:建议行甲状腺超声、甲状腺功能',
            sub: '40岁以上的人群,且至少符合条件之一'
        },
        q7: {
            title: '胃癌筛查量表',
            issue: [{
                title: setTitle('1、年龄40岁以上,男女不限'),
                key: 'question1',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    disabled: true
                },
                // 赋初始值
                init: () => {
                    return userInfo.age > 40 ? 1 : 2
                }
            }, {
                title: setTitle('2、胃癌高发地区人群'),
                key: 'question2',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('3、幽门螺杆菌(H.pylori)感染者'),
                key: 'question3',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('4、既往患有慢性萎缩性胃炎、胃溃疡、胃息肉、手术后残胃、肥厚性胃炎、恶性贫血等胃癌前疾病'),
                key: 'question4',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('5、胃癌患者一级亲属'),
                key: 'question5',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                title: setTitle('6、存在胃癌其他高危因素(高盐、腌制饮食、吸烟、过度饮酒等)'),
                key: 'question6',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }],
            check() {
                return this.issue[0].value === 1 &&
                    this.issue.filter(e => e.value === 1).length >= 2
            },
            remark: '一般风险人群建议:随访观察。\n胃癌筛查高危人群建议:进一步进行胃十二指肠镜检查、肿瘤标志物、腹部CT。',
            sub: '胃癌高风险人群年龄40岁以上,且2-6至少满足一项'
        },
        q8: {
            title: '食管癌筛查量表',
            issue: [{
                key: 'question1',
                title: setTitle('1、年龄40岁以上'),
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    // 禁用
                    disabled: true
                },
                // 赋初始值
                init: () => {
                    return userInfo.age > 40 ? 1 : 2
                }
            }, {
                key: 'question2',
                title: setTitle('2、经常食用过烫、粗硬或腌制的食物,吃饭吞食快、暴饮暴食或经常吸烟 与饮酒等不良生活习惯'),
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                key: 'question3',
                title: setTitle('3、被HIV、HPV、单纯疱疹等病毒感染'),
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                key: 'question4',
                title: setTitle('4、生活在食管癌高发地区的人群:如河北、山西、河南等地'),
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                key: 'question5',
                title: setTitle('5、近期有吞咽困难、胸背部疼痛、食管异物感等症状'),
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                key: 'question6',
                title: setTitle('6、食管癌家族史'),
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }, {
                key: 'question7',
                title: setTitle('7、行胃镜检查诊断为肠上皮化生的Barrett食管的患者'),
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001')
                }
            }],
            check() {
                return this.issue[0].value === 1 &&
                    this.issue.filter(e => e.value === 1).length >= 2
            },
            remark: '一般人群建议:随访观察。\n食管癌高危人群建议:进一步行胃镜、食管内镜超声、肿瘤标记物或食管CT扫描、胸部增强CT检查。',
            sub: '40岁以上的人群,且至少符合2项'
        },
        q9: {
            title: '前列腺癌筛查量表',
            issue: [{
                title: setTitle(`1、尿频或排尿困难`),
                key: 'question10',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }, {
                title: setTitle(`2、尿流变小或中断`),
                key: 'question11',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }, {
                title: setTitle(`3、尿液中出现血丝`),
                key: 'question12',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }, {
                title: setTitle(`4、尿液浑浊或有异味`),
                key: 'question13',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }, {
                title: setTitle(`5、阴茎勃起困难或全`),
                key: 'question14',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }, {
                title: setTitle(`6、疼痛或不适感`),
                key: 'question15',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }, {
                title: setTitle('7、您是否有家族中有人患有前列腺癌'),
                key: 'question2',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }, {
                title: setTitle('8、你是否曾经被诊断为前列腺疾病'),
                key: 'question3',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }, {
                title: setTitle('9、你是否常食用高脂食品?(如肥肉、蛋黄、猪脑,蟹黄等高脂食品)'),
                key: 'question5',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }, {
                title: setTitle('10、您是否长期坐着工作或生活'),
                key: 'question8',
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }],
            check() {
                let score = 0
                this.issue.forEach(e => {
                    score = score + (e.answer.score[e.value] || 0)
                })
                this.score = score
                return score > 4
            },
            remark: '一般人群建议:随访观察\n高危人群建议:进一步检查前列腺特异性抗原、前列腺B超、盆腔磁共振成像(MRI)',
            sub: '计分方式:每个条目1分,结果以总分计算,也就是以十个条目的分值相加,总分范围是0—10分。0-4分属于一般人群,5-10分属于高危人群'
        },
        q10: {
            title: '肾癌筛查量表',
            issue: [{
                key: 'question1',
                title: setTitle('1、近期出现血尿,肾区疼痛等症状(5分)'),
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 5 }
                }
            }, {
                key: 'question2',
                title: setTitle('2、长期吸烟、酗酒及长期熬夜(1分)'),
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }, {
                key: 'question3',
                title: setTitle('3、家族有肾癌患者(2分)'),
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 2 }
                }
            }, {
                key: 'question4',
                title: setTitle('4、患有高血压、糖尿病、肥胖症等慢性病(1分)'),
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }, {
                key: 'question5',
                title: setTitle('5、职业因素:长期接触石油、金属等产品(1分)'),
                answer: {
                    type: 'radio',
                    cont: getDict('DC00001'),
                    score: { 1: 1 }
                }
            }],
            check() {
                let score = 0
                this.issue.forEach(e => {
                    score = score + (e.answer.score[e.value] || 0)
                })
                this.score = score
                return score > 2
            },
            remark: '0-2分一般人群建议:随访观察。\n3-10分高危群建议:进一步行尿常规、肝功能、肾功能、肿瘤标记物、泌尿系彩超、CT等检查。',
            sub: '计分方式:总分范围是0—10分。 0-2分一般人群,3-10分高危群'
        }
    }
    const result = questions['q' + key]
    return result ? new Question(result, detail) : {}
}

class Question {
    issue = []
    check = null
    result = null
    remark = null
    sub = null
    highRisk = null
    highRiskTrans = null
    score = null

    constructor({
        title = '',
        remark = '',
        sub = '',
        issue = [],
        check = () => {}
    } = {}, detail = {}) {
        console.log('class Question', detail)
        this.issue = issue.map(e => {
            let value = detail[e.key] || (e.init && e.init())
            if (!e.title && value) {
                value = value.split(',').map(i => +i)
            }
            return {
                ...e,
                value,
                valueTrans: detail[e.key + 'Trans'],
                valueOther: null,
            }
        })
        // 处理title中有选项的
        console.log('this.issue', this.issue)
        const arr = this.issue.filter(e => e.title && e.title.linkKey)
        arr.forEach(e => {
            const linkKey = e.title.linkKey
            e.titleExtend = this.getIssue(linkKey)
        })

        this.title = title
        this.remark = remark
        this.sub = sub
        this.check = check
        this.highRisk = detail?.highRisk
        this.highRiskTrans = detail?.highRiskTrans
        this.score = detail?.score
    }

    getIssue(key) {
        return this.issue.find(e => e.key === key)
    }
}