<template>
  <div class="deskRecord" style="margin-top:56px;">
    <!-- <div style="overflow:hidden;border-top:1px solid #e5e5e5;margin-top:5px;">
      <div class="mainCell" style="margin-bottom:10px;float:right">
        <el-button size="small" @click="open(1)" type="primary" style="margin-left:10px;">保存</el-button>
        <el-button size="small" @click="open(2)" type="primary">打印</el-button>
        <el-button size="small" @click="open(3)" type="primary">病历调用</el-button>
        <el-button size="small" @click="open(4)" type="primary">存模板</el-button>
      </div>
    </div> -->
    <div class="mainTitle">基本信息：</div>
    <el-form class="basicForm" :model="patientInfo" ref="form" label-width="120px">
      <el-form-item label="姓名:" prop="name">
        <el-input v-model="patientInfo.name" placeholder="" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="性别: " prop="name">
        <el-input v-model="patientInfo.gender" placeholder="" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="年龄: " prop="name">
        <el-input v-model="patientInfo.age" placeholder="" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="证件号:" prop="name">
        <el-input v-model="patientInfo.id_card_no" placeholder="" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="出生日期: " prop="name">
        <el-date-picker
          style="width:100%;"
          v-model="patientInfo.birth"
          type="date"
          :disabled="true"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="联系电话: " prop="name">
        <el-input v-model="patientInfo.phone" placeholder="" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="医保类型: " prop="name">
        <el-select v-model="delivery_way" style="width:100%;" placeholder="请选择">
         <el-option
            v-for="item in wayOptions"
           :key="item.id"
           :label="item.name"
           :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="医保证号: " prop="name">
        <el-input v-model="patientInfo.health_care_no" placeholder="" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="家庭住址: " prop="name">
        <el-input v-model="patientInfo.home_address" placeholder="" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="过敏史: " prop="name">
        <el-input v-model="allergic_history" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <div class="mainTitle">体格信息：</div>
    <el-form class="physiqueForm" :model="case_history" ref="form" label-width="80px">
      <el-form-item label="体温:" prop="temperature">
        <el-input v-model="case_history.temperature" placeholder=""></el-input>
        <span>℃</span>
      </el-form-item>
      <el-form-item label="脉搏: " prop="pulse">
        <el-input v-model="case_history.pulse" placeholder=""></el-input>
        (次/分)
      </el-form-item>
      <el-form-item label="呼吸: " prop="breathing">
        <el-input v-model="case_history.breathing" placeholder=""></el-input>
        (次/分)
      </el-form-item>
      <el-form-item label="血压:" prop="name">
        <el-input v-model="case_history.sbp" placeholder=""></el-input>
        /
        <el-input v-model="case_history.dbp" placeholder=""></el-input>
        mmHg
      </el-form-item>
    </el-form>
    <div class="mainTitle">病历信息：</div>
    <el-form class="recordForm" :model="case_history" ref="form" label-width="80px">
     <el-form-item filterable label="疾病类型: " prop="name" style="width:49%;">
        <el-select v-model="case_history.sick" style="width:100%;" placeholder="请选择">
          <el-option
            v-for="(item,index) in sick"
            :key="index"
            :label="item.class_name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="诊断: " prop="name" style="width:49%;">
        <el-select style="width:100%;" v-model="case_history.diagnose" placeholder="">
          <el-option
            v-for="(item,index) in diagnoses"
            :key="index"
            :label="item.class_name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="发病日期: " prop="name" style="width:49%;">
        <el-date-picker
          style="width:100%;"
          v-model="start_time"
          type="date"
          placeholder="选择日期"
           :picker-options="pickerOptions0">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="name" style="width:49%;">
        <el-checkbox v-model="case_history.is_infect">是否传染</el-checkbox>
      </el-form-item>
      <el-form-item label="主诉:" style="width:49%;">
        <el-select @change="changeMainTemplate" v-model="value" style="width:100%;margin-bottom:10px;">
            <el-option
              v-for="(item, index) in tabledata"
              :label="item.title"
              :value="item.content"
              :key="index"
            ></el-option>
        </el-select>
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="case_history.chief_conplaint">
        </el-input>
      </el-form-item>
      <el-form-item label="现病史:" style="width:49%;">
        <el-select @change="changeMainTemplateOne" v-model="valueOne" style="width:100%;margin-bottom:10px;">
          <el-option
            v-for="(item, index) in tabledataOne"
            :label="item.title"
            :value="item.content"
            :key="index"
          ></el-option>
        </el-select>
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="case_history.history_of_present_illness">
        </el-input>
      </el-form-item>
      <el-form-item label="既往史:" style="width:49%;">
        <el-select @change="changeMainTemplateTwo" v-model="valueTwo" style="width:100%;margin-bottom:10px;">
          <el-option
            v-for="(item, index) in tabledataTwo"
            :label="item.title"
            :value="item.content"
            :key="index"
          ></el-option>
        </el-select>
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="case_history.past_history">
        </el-input>
      </el-form-item>
      <el-form-item label="个人史:" style="width:49%;">
        <el-select @change="changeMainTemplateThree" v-model="valueThree" style="width:100%;margin-bottom:10px;">
          <el-option
            v-for="(item, index) in tabledataThree"
            :label="item.title"
            :value="item.content"
            :key="index"
          ></el-option>
        </el-select>
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="case_history.personal_history">
        </el-input>
      </el-form-item>
      <el-form-item  label="家族史:" style="width:49%;">
        <el-select @change="changeMainTemplateFour" v-model="valueFour" style="width:100%;margin-bottom:10px;">
          <el-option
            v-for="(item, index) in tabledataFour"
            :label="item.title"
            :value="item.content"
            :key="index"
          ></el-option>
        </el-select>
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="case_history.family_history">
        </el-input>
      </el-form-item>
      <el-form-item label="诊断信息:" style="width:49%;">
        <el-select @change="changeMainTemplateFive" v-model="valueFive" style="width:100%;margin-bottom:10px;">
          <el-option
            v-for="(item, index) in tabledataFive"
            :label="item.title"
            :value="item.content"
            :key="index"
          ></el-option>
        </el-select>
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="case_history.diagnostic">
        </el-input>
      </el-form-item>

      <el-form-item label="医嘱:" style="width:49%;">
        <el-select @change="changeMainTemplateSix" v-model="valueSix" style="width:100%;margin-bottom:10px;">
          <el-option
            v-for="(item, index) in tabledataSix"
            :label="item.title"
            :value="item.content"
            :key="index"
          ></el-option>
        </el-select>
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="case_history.doctor_advice">
        </el-input>
      </el-form-item>

      <el-form-item label ="备注"  prop="name" style="width:49%;">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="case_history.remark">
        </el-input>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
  const moment = require('moment');
  import medicalRecord from './medicalRecord'
  import saveRecordTemplate from './saveRecordTemplate'
  import { getDictionaryDataConfig } from "@/utils/data";
  import {  createCaseHistory,getInitData } from '@/api/his/his'
  import { getDataConfig } from "@/utils/data";
  import { getTemplateDetail,getMedicalTemplateList } from "@/api/project/project"
  export default {
    props: {
      case_history: Object,
      patientInfo: Object,
      hisPatientInfo: Object,
      record_date:String,
      detalid:Number,
    },
    components: {
      medicalRecord,
      saveRecordTemplate
    },
    data() {
      return {
        id:0,
        delivery_way:"",
        // case_history:{
        //   sick_type:'',
        //   symptom:"",
        //   sick_date:"",
        //   is_infect:"",
        //   chief_conplaint:"",
        //   history_of_present_illness:"",
        //   past_history:"",
        //   diagnostic:"",
        //   personal_history:"",
        //   family_history:"",

        // },
          pickerOptions0: {
          disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7;
          }
        },
        formValue:{
          temperature:"",
          blood_sugar:"",
          pulse:"",
          sbp:"",
          dbp:"",
          height:"",
          blood_fat:"",
        },
        form: {
          name: ''
        },
        wayOptions:[],
        start_time:moment(new Date()).add('year',0).format("YYYY-MM-DD"),
        doctor_advice:"",
        remark:"",
        tabledata:[],
        tabledataOne:[],
        tabledataTwo:[],
        tabledataThree:[],
        tabledataFour:[],
        tabledataFive:[],
        tabledataSix:[],
        tabledataSeven:[],
        value:'',
        valueOne:'',
        valueTwo:'',
        valueThree:'',
        valueFour:'',
        valueFive:'',
        valueSix:'',
        valueSeven:'',
        diagnoses:[],
        sick:[],
        allergic_history:"",
        form:{
          id:0,
        },
        watchSign:0
      }
    },
    methods: {
      getDictionaryDataConfig(module, filed_name) {
        return getDictionaryDataConfig(module, filed_name)

      },
      // setData(case_history,patientInfo,hisPatientInfo){
      //   this.form.name = patientInfo.name



      // },
      createCaseHistory(id) {
          if(this.patientInfo.id == undefined){
            this.$message.error("请先选择病人");
            return
          }
          if(this.case_history.is_infect == true){
            this.case_history.is_infect = 1
          }
          if(this.case_history.is_infect == false){
             this.case_history.is_infect = 2
          }
          let params = {
            "his_patient_id":this.hisPatientInfo.id,
            "delivery_way":this.delivery_way?"":0,
            "patient_id":id ? id : this.patientInfo.id,
            "temperature":this.case_history.temperature,
            "blood_sugar":this.case_history.blood_sugar,
            "pulse":this.case_history.pulse,
            "sbp":this.case_history.sbp,
            "dbp":this.case_history.dbp,
            "blood_fat":this.case_history.blood_fat,
            "height":this.case_history.height,
            "sick_type":this.case_history.sick_type,
            "symptom":this.case_history.symptom,
            "sick_date":this.start_time,
            "is_infect":this.case_history.is_infect,
            "chief_conplaint":this.case_history.chief_conplaint,
            "history_of_present_illness":this.case_history.history_of_present_illness,
            "past_history":this.case_history.past_history,
            "personal_history":this.case_history.personal_history,
            "family_history":this.case_history.family_history,
            "record_date":this.record_date,
            "diagnostic":this.case_history.diagnostic,
            "breathing":this.case_history.breathing,
            "doctor_advice":this.case_history.doctor_advice,
            "remark":this.case_history.remark,
            "sick":this.case_history.sick,
            "diagnose":this.case_history.diagnose,
            "allergic_history":this.allergic_history,
            "id":this.form.id,
          };
          console.log("prams222",params);
          createCaseHistory(params).then(response => {
            if (response.data.state == 0) {
              this.$message.error("患者病历已存在");
              this.watchSign = 0;
              return false
            } else {
              var caseHistory = response.data.data.caseHistory;
              console.log("caseHistory",caseHistory);
              this.$message.success("保存成功");
              this.doctor_advice = "";
              this.remark = "";
              this.watchSign = 0
            }
          })


      },


      getTemplateDetail(id){

          getTemplateDetail(id).then(response=>{
              if(response.data.state == 1){
                var templatedetail =  response.data.data.templateDetail;
                console.log("templatedetail",templatedetail);

                this.case_history.chief_conplaint =  templatedetail.chief_conplaint;
                this.case_history.history_of_present_illness =  templatedetail.history_of_present_illness;
                this.case_history.past_history =  templatedetail.past_history;
                this.case_history.personal_history =    templatedetail.personal_history;
                this.case_history.family_history = templatedetail.family_history;
                this.case_history.diagnostic = templatedetail.diagnostic;
                this.case_history.doctor_advice = templatedetail.doctor_advice;
                this.case_history.remark = templatedetail.remark;
                this.case_history.sick = templatedetail.sick;
                this.case_history.diagnose = templatedetail.diagnose

              }
          })
        },

        getlist(){
           getMedicalTemplateList().then(response=>{
           if(response.data.state == 1){
             var list =  response.data.data.list;
             console.log("list",list);
             this.tabledata = list;
             this.tabledataOne =  response.data.data.listOne;
             this.tabledataTwo = response.data.data.listTwo;
             this.tabledataThree = response.data.data.listThree;
             this.tabledataFour =  response.data.data.listFour;
             this.tabledataFive = response.data.data.listFive;
             this.tabledataSix = response.data.data.listSix;
             this.tabledataSeven =  response.data.data.listSeven
           }
        })
       },

       changeMainTemplate(values){
         if(this.case_history.chief_conplaint == ''){
           this.case_history.chief_conplaint = values
         } else {
          if (this.case_history.chief_conplaint.indexOf(values) == -1) {
            if (this.case_history.chief_conplaint.charAt(this.case_history.chief_conplaint.length - 1).indexOf('。') == -1) {
              this.case_history.chief_conplaint = this.case_history.chief_conplaint + ',' + values
            } else {
              this.case_history.chief_conplaint = this.case_history.chief_conplaint + values
            }
          }
        }
       },
       changeMainTemplateOne(values){
          if(this.case_history.history_of_present_illness == ''){
           this.case_history.history_of_present_illness = values
         } else {
          if (this.case_history.history_of_present_illness.indexOf(values) == -1) {
            if (this.case_history.history_of_present_illness.charAt(this.case_history.history_of_present_illness.length - 1).indexOf('。') == -1) {
              this.case_history.history_of_present_illness = this.case_history.history_of_present_illness + ',' + values
            } else {
              this.case_history.history_of_present_illness = this.case_history.history_of_present_illness + values
            }
          }
        }
      },
      changeMainTemplateTwo(values){
         if(this.case_history.past_history == ''){
           this.case_history.past_history = values
         } else {
          if (this.case_history.past_history.indexOf(values) == -1) {
            if (this.case_history.past_history.charAt(this.case_history.past_history.length - 1).indexOf('。') == -1) {
              this.case_history.past_history = this.case_history.past_history + ',' + values
            } else {
              this.case_history.past_history = this.case_history.past_history + values
            }
          }
        }
      },
      changeMainTemplateThree(values){
         if(this.case_history.personal_history == ''){
           this.case_history.personal_history = values
         } else {
          if (this.case_history.personal_history.indexOf(values) == -1) {
            if (this.case_history.personal_history.charAt(this.case_history.personal_history.length - 1).indexOf('。') == -1) {
              this.case_history.personal_history = this.case_history.personal_history + ',' + values
            } else {
              this.case_history.personal_history = this.case_history.personal_history + values
            }
          }
        }
      },
      changeMainTemplateFour(values){
          if(this.case_history.family_history == ''){
           this.case_history.family_history = values
         } else {
          if (this.case_history.family_history.indexOf(values) == -1) {
            if (this.case_history.family_history.charAt(this.case_history.family_history.length - 1).indexOf('。') == -1) {
              this.case_history.family_history = this.case_history.family_history + ',' + values
            } else {
              this.case_history.family_history = this.case_history.family_history + values
            }
          }
        }
      },

      changeMainTemplateFive(values){
          if(this.case_history.diagnostic == ''){
           this.case_history.diagnostic = values
         } else {
          if (this.case_history.diagnostic.indexOf(values) == -1) {
            if (this.case_history.diagnostic.charAt(this.case_history.diagnostic.length - 1).indexOf('。') == -1) {
              this.case_history.diagnostic = this.case_history.diagnostic + ',' + values
            } else {
              this.case_history.diagnostic = this.case_history.diagnostic + values
            }
          }
        }
      },
      changeMainTemplateSix(values){
          if(this.case_history.doctor_advice == ''){
           this.case_history.doctor_advice = values
         } else {
          if (this.case_history.doctor_advice.indexOf(values) == -1) {
            if (this.case_history.doctor_advice.charAt(this.case_history.doctor_advice.length - 1).indexOf('。') == -1) {
              this.case_history.doctor_advice = this.case_history.doctor_advice + ',' + values
            } else {
              this.case_history.doctor_advice = this.case_history.doctor_advice + values
            }
          }
        }
      },
      getInitData() {
        getInitData().then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false
          } else {
            this.sick = response.data.data.sick;
            this.diagnoses = response.data.data.diagnose

          }
        })

      },
    },
    created(){
       this.getlist();
       this.getInitData();
       this.wayOptions = getDataConfig("patient", "reimbursement_ways");
       console.log("232322323",this);
       if(this.patientInfo.gender = 1){
         this.patientInfo.gender = '男'
       }else if(this.patientInfo.gender = 2){
          this.patientInfo.gender = '女'
        }

    },
    watch:{
      detalid:function(val){
        this.getTemplateDetail(val)
      },
      'patientInfo.gender':{
        handler(newName, oldName){
          console.log('newName',newName);
          if(newName == 1){
            this.patientInfo.gender = '男'
          }else if(newName == 2){
            this.patientInfo.gender = '女'
          }
        },
        immediate: true,
        deep: true
      },
      case_history:{
        handler(val){
          console.log("南京人明",this.case_history);
          if(this.case_history.breathing == 0){
            this.case_history.breathing = ""
          }
          if(this.case_history.sick == 0){
            this.case_history.sick = ""
          }
          if(this.case_history.diagnose == 0){
            this.case_history.diagnose = ""
          }
          this.form.id = this.case_history.id;
          this.allergic_history = this.case_history.allergic_history;
          this.watchSign++
        },
        deep:true
      },
      allergic_history:function(){
        this.watchSign++
      }
    }
  }
</script>


<style lang="scss" scoped>
  .mainTitle {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 10px;
    color:#409EFF;
  }

  .backColor {
    background: #f6f8f9;
    height: 5px;
    margin-bottom: 5px;
  }

  .deskRecord {

  .basicForm {
    display: flex;
    flex-wrap: wrap;

  .el-form-item {
    width: 32%;
    margin-right: 1%;
  }

  }
  .physiqueForm {
    display: flex;
    flex-wrap: wrap;

  .el-form-item {
    width: 24%;
    margin-right: 1%;
  }

  }
  .recordForm {
    display: flex;
    flex-wrap: wrap;

  .el-form-item {
    width: 24%;
    margin-right: 1%;
  }

  }

  }
</style>

<style lang="scss">
  .physiqueForm {

  .el-form-item__content {
    display: flex;
  }

  .el-input {
    width: 80px;
    margin-right: 5px;
  }

  }
</style>
