<template>
<div>
  <el-dialog
    title="详情"
    width="1000px"
    :visible.sync="visible"
    :before-close="_close"
    class="detailDialog"
  >
    <div style="position: absolute;right:20px;z-index:99">
      <el-button v-if="activeName == 'first'" @click="open(1)" type="primary">打印</el-button>
      <el-button v-if="activeName == 'second'" @click="open(2)" type="primary">打印</el-button>
    </div>
    <el-tabs v-model="activeName">

      <el-tab-pane label="处方详情" name="first">
        <el-tabs class="preTabs" v-model="editableTabsValue" type="card" @tab-click="tabclickEvent">
          <el-tab-pane
            v-for="(item, index) in prescriptions"
            :key="index"
            :label="item.name"
            :name="item.name"
          >
          </el-tab-pane>

          <div class="RP">
            Rp
          </div>
          <new-prescription-table ref="prescription_tables" :prescription="curPrescriptions"></new-prescription-table>
        </el-tabs>

      </el-tab-pane>
      <el-tab-pane label="病历详情" name="second">
        <div class="commonCell">
          <p style="color:#409EFF;">基本信息</p>
          <div class="detailMain">
            <span style="width:270px;">处方号：{{order.prescription_number}}</span>
            <span style="width:180px;">姓名：{{order.patient.name}}</span>
            <span style="width:240px;">证件号：{{order.patient.id_card_no}}</span>
            <span style="width:180px;">联系电话：{{order.patient.phone}}</span>
            <span style="width:270px;">医生：{{order.doctor}}</span>
            <span style="width:180px;">科室：{{getDepartMent(order.departments)}}</span>
            <span style="width:240px;">创建时间： {{getTimes(order.ctime,"{y}-{m}-{d} {h}:{i}")}}</span>
            <!-- <span style="width:180px;">疾病名称：{{case_history.diagnostic}}</span>
            <span style="width:180px;">症状：{{case_history.diagnostic}}</span> -->


          </div>
        </div>
        <div class="commonCell">
          <p style="color:#409EFF;">体格信息</p>
          <div class="detailMain">
            <span style="width:140px;">体温：{{case_history.temperature}}℃</span>
            <span style="width:140px;">呼吸：{{case_history.breathing}} 次/分</span>

            <span style="width:140px;">脉搏：{{case_history.pulse}} 次/分</span>
            <span style="width:200px;">血压：{{case_history.sbp}}~{{case_history.dbp}} mmHg</span>
          </div>
        </div>
        <div class="commonCell">
          <p style="color:#409EFF;">病历信息</p>
          <div class="detailMain">
            <span style="width:260px;" v-if="case_history.is_infect== 1">是否传染：是</span>
            <span style="width:260px;" v-if="case_history.is_infect != 1">是否传染：否</span>
            <span style="width:180px;">发病日期：{{getTimes(case_history.sick_date,"{y}-{m}-{d}")}}</span>
          </div>
        </div>
        <div class="commonCell">
          <p>主诉</p>
          <div>{{case_history.chief_conplaint}}</div>
        </div>
        <div class="commonCell">
          <p>现病史</p>
          <div>{{case_history.history_of_present_illness}}</div>
        </div>
        <div class="commonCell">
          <p>门诊诊断</p>
          <div>{{case_history.diagnostic}}</div>
        </div>
        <div class="commonCell">
          <p>门诊医嘱</p>
          <div>{{case_history.doctor_advice}}</div>
        </div>
        <div class="commonCell">
          <p>过敏史</p>
          <div>{{case_history.past_history}}</div>
        </div>
        <div class="commonCell">
          <p>既往史</p>
          <div>{{case_history.past_history}}</div>
        </div>
        <div class="commonCell">
          <p>个人史</p>
          <div>{{case_history.personal_history}}</div>
        </div>
        <div class="commonCell">
          <p>家族史</p>
          <div>{{case_history.family_history}}</div>
        </div>
      </el-tab-pane>
    </el-tabs>
    <div slot="footer" class="dialog-footer">
      <el-button @click="hide">取 消</el-button>
      <!--<el-button type="primary" :loading="submitLoading" @click="hide">确定</el-button>-->
    </div>
    <el-dialog
      class="centerDialog detailDialog"
      width="600px"
      title="打印"
      :visible.sync="innerVisible"
      append-to-body>

      <print v-if="activeName == 'first'" :paramsObj='paramsObj'></print>
      <recordPrint v-if="activeName == 'second'" :patientid="patientid"></recordPrint>
    </el-dialog>
  </el-dialog>
</div>
</template>



<script>
  import { getDictionaryDataConfig} from "@/utils/data";
  import { getHisPrescriptionInfo } from '@/api/his/his'
  import NewPrescriptionTable from './newPrescriptionTable'
  import { uParseTime } from '@/utils/tools'
  import { getAllDoctorList } from "@/api/project/project"
  import moment from 'moment';
  import print from '../print'
  import recordPrint from '../recordPrint'

  export default {
    components: { NewPrescriptionTable,print,recordPrint },
    data() {
      return {
        visible: false,
        activeName: 'first',
        curPrescriptions: {},
        prescriptions: [],
        record_date: '',
        editableTabsValue: '处方1',
        loadingone: false,
        editableTabs: [{
          title: '处方1',
          name: '1'
        }],
        tabIndex: 1,
        hisPatientInfo: {},
        loadingtwo: false,
        patientTableData: [{
          name: '杨美英',
          mdtrt_id: '1709946'
        }],
        patientInfo: { id: 0 },
        doctor: {},
        total: 0,
        state: '未收费',
        radio: 1,
        radioStatus: 1,
        search_input: '',
        case_history:{},
        order:{},
        doctorList:[],
        departmentList:[],
        innerVisible:false,
        paramsObj:{},
        patientid:''
      }
    },
    methods: {
      open(index){
        if(index == 1){
           var arr= [];
           for(let i=0;i<this.prescriptions.length;i++){
              arr.push(this.prescriptions[i].id)
           }
           var prescription_id = arr[0];
           var record_date = this.format(this.order.record_date);
           var ids = arr.toString();

           let obj = {
             record:record_date,
             prescription_id:prescription_id,
             ids:ids,
             patient_id:this.patientInfo.id
           };
           this.paramsObj = obj;
           this.innerVisible = true
          //  this.$router.push("/outpatientDoctorStation/print?record="+record_date+"&prescription_id="+prescription_id+"&ids="+ids+"&patient_id="+this.patientInfo.id)

        }else if(index == 2){
          var record_date = this.format(this.case_history.sick_date);
          this.patientid = this.patientInfo.id;
          this.innerVisible = true
          // this.$router.push("/outpatientDoctorStation/recordPrint?record="+record_date+"&patient_id="+this.case_history.patient_id)
        }
      },
      getTimes(time,temp) {
        return uParseTime(time, temp);
      },
      tabclickEvent(val) {
        for (let i = 0; i < this.prescriptions.length; i++) {
          if (this.prescriptions[i].name == val.name) {
            this.curPrescriptions = {};
            var temp = this.deepClone(this.prescriptions[i]);
            this.curPrescriptions = temp
          }
        }
      },
      deepClone(source) {
        if (!source && typeof source !== 'object') {
          throw new Error('error arguments', 'shallowClone')
        }
        const targetObj = source.constructor === Array ? [] : {};
        Object.keys(source).forEach((keys) => {
          if (source[keys] && typeof source[keys] === 'object') {
            targetObj[keys] = this.deepClone(source[keys])
          } else {
            targetObj[keys] = source[keys]
          }
        });
        return targetObj
      },

      moreState(tab, event) {
        if (tab == 'more') {
          return false
        }
      },

      _close: function(done) {
        // this.clear()
        done()
      },
      clear: function() {
        this.form.id = 0;
        this.form.name = '';
        this.form.intro = ''
      },
      show(id) {
        let params={
          id:id,
        };
        getHisPrescriptionInfo(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false
          } else {
            this.patientInfo = response.data.data.order.patient;
            console.log("患者信息",this.patientInfo);
            this.hisPatientInfo = response.data.data.order.his_patient;
            this.case_history = response.data.data.order.case_history;
            console.log("历史详情",this.case_history);
            this.order = response.data.data.order;
            console.log('this.order',this.order);


            this.prescriptions = [];
            for (let i = 0; i < response.data.data.prescription.length; i++) {
              var prescription = response.data.data.prescription[i];
              let tempAdvice = [];
              let tempProject = [];
              for (let b = 0; b < prescription.advices.length; b++) {
                let obj = {
                  advice_id: prescription.advices[b].id,
                  drug_name: prescription.advices[b].advice_name,
                  single_dose: prescription.advices[b].single_dose,
                  delivery_way: prescription.advices[b].delivery_way,
                  execution_frequency: prescription.advices[b].execution_frequency,
                  retail_price: prescription.advices[b].price.toString(),
                  remark: prescription.advices[b].remark,
                  day: prescription.advices[b].day,
                  prescribing_number: prescription.advices[b].prescribing_number.toString(),
                  single_dose_unit: prescription.advices[b].single_dose_unit,
                  prescribing_number_unit: prescription.advices[b].prescribing_number_unit,
                  medical_insurance_number: prescription.advices[b].med_list_codg

                };
                tempAdvice.push(obj)
              }

              for (let b = 0; b < prescription.project.length; b++) {
                let obj = {
                  id: prescription.project[b].id,
                  project_id: prescription.project[b].project.id,
                  project_name: prescription.project[b].project.project_name,
                  statistical_classification: prescription.project[b].project.statistical_classification,
                  single_dose: prescription.project[b].single_dose,
                  delivery_way: prescription.project[b].delivery_way,
                  execution_frequency: prescription.project[b].execution_frequency,
                  number_days: prescription.project[b].day,
                  total: prescription.project[b].count.toString(),
                  price: prescription.project[b].price,
                  remark: prescription.project[b].remark,
                  medical_code: prescription.project[b].project.medical_code,
                  unit:prescription.project[b].project.unit,
                };
                if(prescription.project[b].type == 2){
                  obj['statistical_classification'] =  prescription.project[b].project.statistical_classification
                  obj['medical_code'] =  prescription.project[b].project.medical_code
                  obj['project_name'] =  prescription.project[b].project.project_name
                  obj['type'] =  2



                }else if(prescription.project[b].type == 3){
                  obj['statistical_classification'] = ""
                  obj['medical_code'] =  prescription.project[b].good_info.medical_insurance_number
                  obj['project_name'] =  prescription.project[b].good_info.good_name
                  obj['type'] =  3

                }
                tempProject.push(obj)
              }

              console.log(tempProject)
              let index = i + 1;
              let obj = {
                id: prescription.id,
                name: '处方' + index,
                advices: tempAdvice,
                project: tempProject,
                type: response.data.data.prescription[i].type
              };
              this.prescriptions.push(obj);

              this.curPrescriptions = this.prescriptions[0]



            }
          }
        });
        // this.clear()
        this.visible = true



      },
      hide() {
        // this.clear()
        this.visible = false
      },
     getAllDoctorList(){
      getAllDoctorList().then(response=>{
        if(response.data.state == 1){
            var doctor =  response.data.data.doctor;
            this.doctorList = doctor;
             var department = response.data.data.department;
             console.log("department",department);
             this.departmentList = department
            }
         })
      },
      getDepartMent(id){
         var name = "";
         for(let i=0;i<this.departmentList.length;i++){
           if(id == this.departmentList[i].id){
              name = this.departmentList[i].name
           }
         }
         return name
      },

      format(time){
        moment.locale();
        var times = time*1000;
        times = moment(times).format('YYYY-MM-DD');
        return times
      }
    },
    created(){
      this.getAllDoctorList()
    }
  }
</script>

<style lang="scss">
  .detailDialog {

  .el-dialog__body {
    padding: 0 20px 30px;
    position: relative;
  }

  .detailMain {
    flex: 1;
  span {
    display: inline-block;
  }

  }
  .commonCell {
    display: flex;
    line-height: 40px;

  > p {
    font-weight: bold;
    color: #000;
    width: 80px;
  }

  }
  }
</style>

