<template>
  <risp-template :reportTitle="reportTitle" :baseInfo="baseInfo" class="timeline-risp-cap-page">
    <template>
      <div class="cap-wrapper">
        <div class="item-con" v-for="item of histologicList" :key="item.id">
          <div class="val-title">{{ item.question }}:</div>
          <div class="val-text">{{ item.answer }}</div>
        </div>
      </div>
    </template>
  </risp-template>
</template>


<script>
import RispTemplate from "../risp-template.vue";
import { mixins } from "vue-common-risp";

export default {
  mixins: [mixins.Form, mixins.FormDataElements],
  components: {
    RispTemplate
  },
  props: {
    templates: Array,
    dataNodes: Array,

    rispData: Object,
    baseInfo: Array,
    rispReport: Object
  },

  provide() {
    return {
      rispDatas: this.rispData,
      rispReports: this.rispReport,
    };
  },
  computed: {
    histologicList() {
      let list = [];

      let capPdfData = this.handleSaveaCapPdfData();
      if (Array.isArray(capPdfData)) {
        list = capPdfData.map(item => {
          let question = item.question.value.trim();

          let answer = "";
          if (Array.isArray(item.answer)) {
            item.answer.forEach((option, i) => {
              answer += option.value;
              if (i != item.answer.length - 1) {
                answer += ";";
              }
            });
          } else {
            answer = item.answer;
          }

          return {
            id: item.question.id,
            question: question,
            answer: answer
          };
        });
      }

      return list;
    },
    reportTitle() {
      return this.rispData.parentId ?"补充CAP报告":"CAP报告";
    }
  },
  methods: {
        test(parent, ele, obj, resArr) {
      if (ele[parent].elementType == "Question") {
        // 这是一个问题
        if (ele[parent].title == "") {
          this.test(ele[parent].parent, ele, obj, resArr);
        } else {
          obj.question = ele[parent];
          obj.question.key = parent;
          resArr.push(obj);
        }
      }
    },
        // 处理resData 的数据 并保存到store/risP saveaCapPdfData
    handleResData() {
      let resArr = [];
      let ele = this.dataElements;

      for (let key in this.data) {
        // 判断 这是一个问题还是一个答案
        if (ele[key].item == true) {
          // 是一个选项
          // 就向上查找 他的父级
          // 判断是否是多选 及是否为同一个父级
          let parentKey = ele[key].parent;
          let flag = false;

          // 判断当前 选项是否存在 子集
          // 单选多选后面 如果是一个输入框并且有单位 那就把单位加上
          if (ele[key].type) {
            let title = "";
            if (ele[key].chinese && ele[key].chinese.title) {
              title = ele[key].chinese.title;
            } else if (ele[key].myTitle) {
              if (ele[key].title) {
                title = ele[key].myTitle.replace(ele[key].title, ":");
              }
            }

            ele[key].text =
              title + " " + (this.data[key].value ? this.data[key].value : "");

            if (ele[key].suffix) {
              ele[key].text = `${ele[key].text}-${ele[key].suffix}`;
 
            }
          }

          for (let prop of resArr) {
            if(!prop.answer) {
              continue
            }
            for (let i = 0; i < prop.answer.length; i++) {
              if (
                prop.answer[i].parent == parentKey ||
                (prop.answer[i].child &&
                  prop.answer[i].child.indexOf(parentKey) != -1)
              ) {
                prop.answer.push(ele[key]);
                flag = true;
                break;
              }
            }
          }
          if (!flag) {
            let obj = {};
            obj.answer = [ele[key]];
            this.test(ele[key].parent, ele, obj, resArr);
          }
        } else {
          // 是一个输入框
          let obj = {};
          if (this.data[key].value) {
            obj.answer =
              this.data[key].value +
              "" +
              (ele[key].suffix ? ele[key].suffix : "");
          }
          // 判断title为空 和 title 为 x
          if (
            ele[key].title == "" ||
            ele[key].title == "x" ||
            ele[key].title == "X" ||
            ele[key].title == "or"
          ) {
            // 向上查找 他的parent 找到后将这个 parent设置为 他的question
            let question = ele[ele[key].parent];
            // 循环这个数组 resArr 判断是否已经存在这个 question
            let flag = false;
            resArr.forEach((prop, i) => {
              if (prop.question.id == question.id) {
                // 存在这个 question 改变obj的answer
                obj.answer = `${
                  prop.answer + "" == "undefined" ? "" : prop.answer
                } ${prop.answer + "" == "undefined" ? "" : ele[key].title} ${
                  this.data[key].value
                }${ele[key].suffix ? ele[key].suffix : ""}`;
                resArr[i].answer = obj.answer;
                flag = true;
              }
            });
            if (!flag) {
              // 不存在这个question
              obj.question = question;
              obj.question.key = ele[key].parent;
              resArr.push(obj);
            }
          } else {
            obj.question = ele[key];
            obj.question.key = key;
            resArr.push(obj);
          }
        }
      }
      return resArr;
    },
    hanleToCapPdfData(tableData) {
      let res = [];
      for (let prop of tableData) {
        let tempObj = {};
        tempObj.question = {};
        tempObj.question.value = `${prop.question.chinese.reportText}}`;
        tempObj.question.value = `${
          prop.question.chinese
            ? prop.question.chinese.title || prop.question.chinese.reportText
            : ""
        }`;
        tempObj.question.id = prop.question.id;
        // 判断answer 是 单选 多选 字符串
        if (typeof prop.answer == "object") {
          // 单选 多选
          // multiple.max != 1 为多选
          if (
            (prop.question.multiple && prop.question.multiple.max != 1) ||
            prop.answer.length > 1
          ) {
            // 多选
            tempObj.answer = [];
            for (let item of prop.answer) {
              let obj = {
                value: item.text
                  ? item.text
                  : `${
                      item.chinese
                        ? item.chinese.title || item.chinese.reportText
                        : ""
                    }`,
                id: item.id
              };
              obj.value =
                obj.value === "undefined" || obj.value === "{no text}/{no text}"
                  ? ""
                  : obj.value;
              tempObj.answer.push(obj);
            }
            tempObj.id = "multiple";

          } else {
            // 单选

            // tempObj.answer = (prop.answer[0].chinese && prop.answer[0].chinese.reportText !== '{no text}') ?`${prop.answer[0].chinese ? prop.answer[0].chinese.reportText || prop.answer[0].chinese.title : ""}/${prop.answer[0].reportText || prop.answer[0].title} ${value}11` : `${prop.answer[0].text}`

            if (
              prop.answer[0].chinese &&
              prop.answer[0].chinese.reportText !== "{no text}"
            ) {
              let data = [];
              let value = "";
              for (let key in this.data) {
                data.push(this.data[key]);
              }
              let result = data.find(item => item.id == prop.answer[0].id);

              if (result && result.value) {
                value =
                  result.value +
                  (prop.answer[0].suffix ? prop.answer[0].suffix : "");
              }

              tempObj.answer = `${
                prop.answer[0].chinese
                  ? prop.answer[0].chinese.title ||
                    prop.answer[0].chinese.reportText
                  : ""
              }${value}`;
            } else {
              tempObj.answer = prop.answer[0].text;
            }

            tempObj.answer =
              tempObj.answer === "undefined" ? "" : tempObj.answer;
            tempObj.id = "single";
          }
        } else {
          tempObj.answer = prop.answer;
          tempObj.answer = tempObj.answer === "undefined" ? "" : tempObj.answer;
          tempObj.id = "str";
        }
        res.push(tempObj);
      }
      return res;
    },
    handleSaveaCapPdfData() {
      let res = this.handleResData();
      let result = this.hanleToCapPdfData(res);

     return result
    }
  },
  created() {
    this.initForm();
  }
};
</script>


<style lang="less" scoped>

@import "./common.less";
  .diagose-bodys-con {
    // display: flex;
    // flex-direction: column;
    .item-con {
      // flex: 1;
      // display: flex;
      // flex-wrap: wrap;
      // align-items: center;
      // margin: 14px 0;
      // letter-spacing: 0.01px;
      // line-height: 20px;
    }
    span {
      font-size: 14px;
      color: #000;
    }
  }

.val-title {
  // width: 100%;
}
.val-text {
  margin-left: 20px;
  // padding: 4px 0;
}
.cap-wrapper {
  margin-top: 20px;
  .item-con {
    padding: 10px 0;
  }
}
.val-title {
    line-height: 30px;
    font-size: 14px;
    font-family: "SimSun";
    color: #000;
}

.val-text {
    line-height: 30px;
    font-size: 14px;
    font-family: "SimSun";
    color: #000;
}

@media print {

    .val-text {
        white-space: pre-line;
        margin-left: 20px;
        font-size: 18px;
        letter-spacing: 0.01px;
        line-height: 30px;
        font-family: "SimSun";
        color: #000;
    }

    .val-title {
        font-size: 20px;
        font-family: "SimSun";
        color: #000;
        font-family: "SimSun";
    }
}
</style>