<template>
  <div style="margin-top: -15px;">
    <el-tabs v-model="tabChoosed" style="height: 510px;">
      <el-tab-pane label="基本信息" name="0">
        <OccDiseaseBase />
      </el-tab-pane>
      <el-tab-pane label="既往病史" name="1">
        <OccDiseasePrevious />
      </el-tab-pane>
      <el-tab-pane label="接害因素" name="2">
        <OccDiseasePoison :height="465" />
      </el-tab-pane>

      <el-tab-pane label="职业史" name="3">
        <OccDiseaseHistory />
      </el-tab-pane>
      <el-tab-pane label="症状" name="4">
        <OccDiseaseSymptom />
      </el-tab-pane>
      <el-tab-pane label="体征" name="5">
        <CheckDetails />
      </el-tab-pane>
      <el-tab-pane label="图文报告" name="6">
        <ImageTextReport />
      </el-tab-pane>
      <el-tab-pane label="职业健康检查结论" name="7">
        <OccDiseaseConclusion />
      </el-tab-pane>
    </el-tabs>
    <div style="display: flex;justify-content: space-between;">
      <div></div>
      <div>
        <el-button v-if="false" @click="btnTest">测试</el-button>
        <el-button class="deleteButton" @click="btnDel"
          :disabled="dataTransOpts.tableS.patient_register.completeFlag == '3'">删除</el-button>
        <el-button class="commonbutton" @click="btnOk">确定</el-button>
        <el-button class="commonbutton" @click="btnClose">关闭</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import { getapi, postapi, putapi, deletapi } from "@/api/api";
import { dddw, deepCopy } from "../../utlis/proFunc";

import OccDiseaseBase from "./OccDiseaseBase.vue";
import OccDiseasePrevious from "./OccDiseasePrevious.vue";
import OccDiseasePoison from "./OccDiseasePoison.vue";
import OccDiseaseHistory from "./OccDiseaseHistory.vue";
import OccDiseaseSymptom from "./OccDiseaseSymptom.vue";
import CheckDetails from "./CheckDetails.vue";
import ImageTextReport from "./ImageTextReport.vue";
import OccDiseaseConclusion from "./OccDiseaseConclusion.vue";

export default {
  components: {
    OccDiseaseBase,
    OccDiseasePrevious,
    OccDiseasePoison,
    OccDiseaseHistory,
    OccDiseaseSymptom,
    CheckDetails,
    ImageTextReport,
    OccDiseaseConclusion
  },
  props: ["patientRegisterId"],
  data() {
    return {
      tabChoosed: "0",
    };
  },

  created() {

  },

  //挂载完成
  async mounted() {
    await this.dictInit();

    this.initOccDiseaseData()

  },

  computed: {
    ...mapState([
      "window",
      "dict",
      "dataTransOpts",
      "dialogWin"
    ]),
  },

  methods: {

    //数据初始化
    async dictInit() {

      //职业病 接害因素
      let res = await postapi("/api/app/Poison/GetPoisonWithTypeList")
      if (res.code > -1) {
        this.dict.poison = res.data;
        this.dict.poisonList = []
        this.dict.poison.forEach(e => {
          e.poisonDtos.forEach(e2 => {
            this.dict.poisonList.push({
              poisonTypeName: e.poisonTypeName,
              id: e2.id,
              displayName: e2.displayName,
              simpleCode: e2.simpleCode
            })
          });
        });
      }

      // 职业病--症状列表
      let res2 = await postapi("/api/app/Symptom/GetList")
      if (res2.code > -1) {
        this.dict.symptom = res2.data;
      }


    },

    initOccDiseaseData() {
      let patientRegisterId = this.dataTransOpts.tableS.patient_register.id
      postapi('/api/app/OccupationalDisease/GetOccupationalDiseaseWithDetailByPatientRegisterId', { patientRegisterId })
        .then(res => {
          if (res.code > -1) {
            this.dataTransOpts.tableS.patient_occupational_disease = deepCopy(res.data['patientOccupationalDisease']) || { patientRegisterId }
            this.dataTransOpts.tableM.patient_past_medical_history = deepCopy(res.data['patientPastMedicalHistoryDtos'])
            this.dataTransOpts.tableM.patient_occupational_medical_history = deepCopy(res.data['patientOccupationalMedicalHistoryDtos'])
            this.dataTransOpts.tableM.patient_poison = deepCopy(res.data['patientPoisonDtos'])
            // this.dataTransOpts.tableM.patient_poison.forEach(e => {
            //   e.poisonName = dddw(this.dict.poisonList,'id',e.poisonId,'displayName')
            // });
            this.dataTransOpts.tableM.patient_occupational_history = deepCopy(res.data['patientOccupationalHistoryDtos'])
            if (Array.isArray(res.data['patientSymptomDtos']) && res.data['patientSymptomDtos'].length > 0) {
              this.dataTransOpts.tableM.patient_symptom = deepCopy(res.data['patientSymptomDtos'])
            } else {
              this.dataTransOpts.tableM.patient_symptom = []
              this.dict.symptom.forEach(e => {
                // 男性 不显示月经/经期相关症状                
                if (!(this.dataTransOpts.tableS.patient_register.sexId != 'F' && (e.displayName.indexOf('月经') > -1 || e.displayName.indexOf('经期') > -1))) {
                  this.dataTransOpts.tableM.patient_symptom.push({ symptomId: e.id, symptomName: e.displayName, degree: '-' })
                }
              });
            }
          }
        })
    },

    btnTest() {
      console.log('this.dataTransOpts.tableS.patient_occupational_disease', this.dataTransOpts.tableS.patient_occupational_disease)
      console.log('this.dataTransOpts.tableM.patient_poison', this.dataTransOpts.tableM.patient_poison)
      console.log('this.dataTransOpts.tableM.patient_occupational_history', this.dataTransOpts.tableM.patient_occupational_history)
      console.log('this.dataTransOpts.tableM.patient_symptom', this.dataTransOpts.tableM.patient_symptom)
    },

    btnDel() {
      this.$confirm("此操作确定后将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "是",
        cancelButtonText: "否",
        type: "warning",
      }).then(() => {
        return postapi('/api/app/OccupationalDisease/DeleteOccupationalDiseaseWithDetail', { patientRegisterId: this.dataTransOpts.tableS.patient_register.id })
      }).then(res => {
        if (res && res.code > -1) {
          this.dialogWin.OccDisease = false
        }
      })
    },

    // 数据验证
    validiteData(){
      let err = ''
      let disease = this.dataTransOpts.tableS.patient_occupational_disease
      if(!disease.ocCheckTypeId){
        err = '请填写 检查类别'
        return err
      }
      if(!disease.jobType){
        err = '请填写 工种'
        return err
      }

      if(!disease.riskFactors){
        err = '请填写 基本信息-接害因素'
        return err
      }

      if(disease.smokeFlag && disease.smokeFlag != '0'){
        if(!disease.smokeNum){
          err = '请填写 吸烟频次'
          return err
        }
        if(!disease.smokeYears){
          err = '请填写 烟龄'
          return err
        }
      }

      if(disease.drinkFlag && disease.drinkFlag != '0'){
        if(!disease.drinkNum){
          err = '请填写 饮酒频次'
          return err
        }
        if(!disease.drinkYears){
          err = '请填写 酒龄'
          return err
        }
      }

      this.dataTransOpts.tableM.patient_poison.forEach((e, i) => {
        if (!e.poisonId) err = `接害因素，第 ${i + 1} 行，不能为空`
        if (!e.occupationalAbnormalId) e.occupationalAbnormalId = null
      });

      // 日期为空串的，换成 null
      this.dataTransOpts.tableM.patient_past_medical_history.forEach(e => {
        if(!e.diagnosisDate) e.diagnosisDate = null
      });
      this.dataTransOpts.tableM.patient_occupational_medical_history.forEach(e => {
        if(!e.diagnosisDate) e.diagnosisDate = null
      });
      this.dataTransOpts.tableM.patient_occupational_history.forEach(e => {
        if(!e.beginDate) e.beginDate = null
        if(!e.endDate) e.endDate = null
      });

      return err
    },
    // 点击确定
    btnOk() {
      let message = this.validiteData()
      // console.log('this.dataTransOpts.tableM.patient_poison', this.dataTransOpts.tableM.patient_poison)
      if (message) {
        this.$message.warning({ showClose: true, message })
        return
      }

      let body = {
        patientRegisterId: this.dataTransOpts.tableS.patient_register.id,
        patientOccupationalDisease: this.dataTransOpts.tableS.patient_occupational_disease,
        patientPastMedicalHistorys: this.dataTransOpts.tableM.patient_past_medical_history,
        patientOccupationalMedicalHistorys: this.dataTransOpts.tableM.patient_occupational_medical_history,
        patientPoisons: this.dataTransOpts.tableM.patient_poison,
        patientOccupationalHistorys: this.dataTransOpts.tableM.patient_occupational_history,
        patientSymptoms: this.dataTransOpts.tableM.patient_symptom
      }

      postapi('/api/app/OccupationalDisease/CreateOccupationalDiseaseWithDetail', body)
        .then(res => {
          if (res.code > -1) {
            this.$message.success({ showClose: true, message: "操作成功！" })
            this.dataTransOpts.tableS.patient_occupational_disease.patientRegisterId = this.dataTransOpts.tableS.patient_register.id
            return postapi('/api/app/OccupationalDisease/UpdateOccupationalDiseaseInspectionConclusion', this.dataTransOpts.tableS.patient_occupational_disease)
          } else {
            this.$message.warning({ showClose: true, message: res.message })
          }
        }).then(res => {
          if (res && res.code > -1) {
            this.dataTransOpts.plus.OccDisease++
          }
        })
    },

    // 点击关闭
    btnClose() {
      this.dialogWin.OccDisease = false
    },

  },

  //监听事件()
  watch: {
    "dataTransOpts.plus.OccDisease": {
      // immediate:true,
      handler(newVal, oldVal) {
        console.log(`watch 职业病 newVal: ${newVal}, oldVal: ${oldVal} `);
        if (newVal != oldVal) this.initOccDiseaseData()
      }
    },

  },
};
</script>
<style scoped>
@import '../../assets/css/global_card.css';
@import '../../assets/css/global_input.css';
@import '../../assets/css/global_table.css';
@import '../../assets/css/global.css';
</style>
