<template>
  <div id="prescriptionPrint2">
    <div
      id="prescription-print"
      v-if="item.advices.length == 0"
      class="prescription-print"
      v-for="(item, index) in advicePrint"
      :key="index"
    >
      <span v-if="org_id == 10138">
        <img
          src="https://kuyi.shengws.com/bailin/bltotle.jpg"
          style="width: 100%"
          alt=""
      /></span>
      <span v-if="org_id == 10278">
        <img
          src="https://kuyi.shengws.com/beierlog.png"
          style="width: 100%"
          alt=""
      /></span>
      <div class="printTitle">
        治疗单
        <span
          v-if="item.med_type == '1111'"
          style="margin-left: 50px; border: 1px solid #000"
          >精一</span
        >
        <span
          v-if="item.med_type == '1112'"
          style="margin-left: 50px; border: 1px solid #000"
          >精二</span
        >
      </div>
      <div style="display: flex">
        <div style="width: 33%; display: flex">
          费别：<span class="under_line">{{
            faber && faber.transBody
              ? getName(faber.transBody.outputlist1)
              : "自费"
          }}</span>
        </div>
        <div style="width: 33%; display: flex">
          电脑号：<span class="under_line">{{
            faber && faber.transBody ? faber.transBody.aac999 : ""
          }}</span>
        </div>
        <div style="width: 33%; display: flex">
          处方编号：<span class="under_line">{{
            item.prescription_number.substring(
              item.prescription_number.length - 6
            )
          }}</span>
        </div>
      </div>

      <div class="infoTitle">
        <div>
          姓名：{{
            item.patient.name
              ? item.patient.name.indexOf("（") > -1
                ? item.patient.name.substring(
                    0,
                    item.patient.name.indexOf("（")
                  )
                : item.patient.name
              : ""
          }}
        </div>
        <div>
          性别：
          <span v-if="item.patient.gender == 1">男</span>
          <span v-if="item.patient.gender == 2">女</span>
        </div>
        <div>
          年龄：{{
            getNewAge(item.patient.birthday)
              ? getNewAge(item.patient.birthday)
              : ""
          }}岁
        </div>
      </div>
      <div class="infoMain">
        <div style="margin-bottom: 10px; display: flex">
          门诊号码：<span class="under_line">{{
            hisPatient.number ? hisPatient.number : ""
          }}</span>
        </div>
        <div style="margin-bottom: 10px; display: flex">
          科别：<span class="under_line">{{
            item.info ? getDepart(item.info.departments) : ""
          }}</span>
        </div>
        <div style="margin-bottom: 10px; display: flex">
          临床诊断：{{ getDiagnosis(item.info.diagnosis) }}
        </div>
        <div style="margin-bottom: 10px; display: flex">
          自费药品签名：<span class="under_line"></span>
        </div>
        <!-- <div style="margin-bottom: 10px;display:flex;">医保卡号：{{item.hisPatient.number?item.hisPatient.number:""}}</div> -->
        <div style="margin-bottom: 10px; display: flex">
          联系方式：<span class="under_line">{{ item.patient.phone }}</span>
        </div>
        <div style="margin-bottom: 10px; display: flex">
          开具日期：<span class="under_line">{{
            getTime(item.pre_time) ? getTime(item.pre_time).split(" ")[0] : ""
          }}</span>
        </div>
        <!-- <div>地址：{{item.patient.home_address}}</div> -->
        <!-- <div style="display:flex;width:50%;">临床诊断：{{getDiagnosis(item.info.diagnosis)}}</div> -->
      </div>

      <!-- <div class="prescriptionBox">
            <table style="width:100%;text-align:center;line-height:25px;">
                <tr>
                    <td>序号</td>
                    <td>项目名称</td>
                    <td>数量</td>
                    <td>单位</td>
                    <td>单价</td>
                    <td>部位</td>
                    <td>备注</td>
                    <td>天数</td>
                </tr>
                <tr v-for="(it,index) in item.project" :key="index">
                    <td>{{index+1}}</td>
                    <td>{{it.type == 2 ?getProjectName(it.project_id):it.good_info.good_name}}</td>
                    <td>{{it.single_dose}}</td>
                    <td>{{getUnit(it.unit)}}</td>
                    <td>{{it.price}}</td>
                    <td></td>
                    <td>{{it.remark}}</td>
                    <td>{{it.day}}</td>

                </tr>

                <tr v-for="(it,index) in item.advices" :key="index">
                    <td>{{index+1}}</td>
                    <td>{{it.advice_name?it.advice_name:''}}</td>
                    <td>{{it.single_dose?it.single_dose:''}}</td>
                    <td>{{it.single_dose_unit?it.single_dose_unit:''}}</td>
                    <td>{{it.price}}</td>
                    <td></td>
                    <td>{{it.remark}}</td>
                    <td>{{it.day}}</td>

                </tr>
            </table>
        </div> -->
      <div class="prescriptionBox">
        <div class="Rp">Rp:</div>
        <!-- <div class="drugsBox" v-for="(it,index) in item.advices" :key="index">
                  <div class="drugsOne"><span style="font-weight:bold;">{{ index+1 + '.' }}</span>{{it.advice_name?it.advice_name:""}}&nbsp;&nbsp;<span v-if="it.drug.min_unit != it.drug.dose_unit">{{it.drug.dose}}{{it.drug.dose_unit}}&nbsp;* &nbsp;</span>{{it.drug.min_number}}{{it.drug.min_unit}}/{{it.drug.max_unit}}&nbsp;×&nbsp; {{it.prescribing_number}}{{it.prescribing_number_unit}}</div>
              </div> -->
        <div class="drugsBox" v-for="(it, index) in item.project" :key="index">
          <div class="drugsOne">
            <span style="font-weight: bold">{{ index + 1 + "." }}</span
            >{{
              it.type == 2 ? it.project.project_name : it.good_info.good_name
            }}&nbsp;&nbsp;×&nbsp; {{ it.count }}{{ unit }}
          </div>
        </div>
        <div style="text-align: center">(以下空白)</div>
      </div>
      <div style="display: flex">
        <div>
          医&nbsp;&nbsp;&nbsp;&nbsp;师：
          <span
            style="width: 100px; display: inline-block"
            v-if="item.creator == ''"
          >
            {{ item.doctor ? item.doctor : "" }}
          </span>
            <span
            style="width: 100px; display: inline-block"
            v-else-if="doc_name != ''"
          >
            {{ doc_name }}
          </span>
          <img
            style="height: 30px"
            :src="setAdminUserES(item.creator,item.doctor)"
            alt=""
            srcset=""
            v-else
          />
        </div>
        <!-- <div>药品金额：{{getTotalOne(item.id).toFixed(2)?getTotalOne(item.id).toFixed(2):''}}</div> -->
      </div>
      <div style="display: flex">
        <div style="display: flex">核对护士：</div>
      </div>
      <div style="display: flex">
        打印时间：<span style="border-bottom: 1px solid #000">{{
          getTime(new Date()) ? getTime(new Date()) : ""
        }}</span>
      </div>
      <span v-if="org_id == 10138"
        ><img
          src="https://kuyi.shengws.com/bailin/blend.jpg"
          style="width: 100%"
          alt=""
      /></span>
      <span v-if="org_id == 10278"
        ><img
          src="https://kuyi.shengws.com/bailinbeier.png"
          style="width: 100%"
          alt=""
      /></span>
      <div style="page-break-after: always"></div>
    </div>

    <!-- <div id='prescriptionPrint'>

        <div v-for="(item,index) in advicePrint" :key="index">
            <div id='prescription-print' class="prescription-print" style="page-break-after: always;">
                <div style="display:flex;justify-content: space-between;font-weight:bold;">
                    <div style="width:200px;display:flex;justify-content: space-between;font-weight:bold;">
                        <span>费别</span>
                        <span>自费</span>
                        <span>公费</span>
                        <span>保险</span>
                        <span>其他</span>
                    </div>
                    <div>就诊日期：{{getTime(item.ctime)}}</div>
                </div>

                <div class="printTitle">血液透析中心</div>

                <div class="printTitle">处方、治疗单</div>

                <div class="infoTitle">
                    <div>门诊编号：</div>
                    <div>电脑号：</div>
                </div>

                <div class="infoMain">
                    <div style="margin-bottom: 10px;width:25%">姓名：{{item.patient.name}}</div>
                    <div style="margin-bottom: 10px;width:25%">性别：
                        <span v-if="item.patient.gender == 1">男</span>
                        <span v-if="item.patient.gender == 2">女</span>
                    </div>
                    <div style="margin-bottom: 10px;width:25%">年龄：
                        <span>{{item.patient.age}}</span>
                    </div>
                    <div style="margin-bottom: 10px;width:25%">参保类型：</div>
                    <div style="margin-bottom: 10px;width:50%;">联系电话：{{item.patient.phone}}</div>
                    <div style="width:50%;">地址：{{item.patient.unit_address}}</div>
                    <div style="width:100%;">诊断：{{item.patient.diagnose}}</div>
                </div>

                <div class="prescriptionBox">
                    <table style="width:100%;text-align:center;line-height:25px;">
                        <tr>
                            <td>序号</td>
                            <td>项目名称</td>
                            <td>数量</td>
                            <td>单位</td>
                            <td>单价</td>
                            <td>部位</td>
                            <td>备注</td>
                            <td>天数</td>
                        </tr>
                        <tr v-for="(it,index) in item.project" :key="index">
                            <td>{{index+1}}</td>
                            <td>{{getProjectName(it.project_id)}}</td>
                            <td>{{it.single_dose}}</td>
                            <td>{{getUnit(it.unit)}}</td>
                            <td>{{it.price}}</td>
                            <td></td>
                            <td>{{it.remark}}</td>
                            <td>{{it.day}}</td>

                        </tr>
                    </table>
                </div>
                <div class="infoTitle">
                    <div>开单医生：{{getDoctor(item.doctor)?getDoctor(item.doctor):""}}</div>
                    <div>签章：</div>
                </div>
                <div class="actionBar">
                    <div>执行医生：{{getDoctor(item.doctor)?getDoctor(item.doctor):""}}</div>
                    <div>费用：</div>
                </div>
                <table style="width:100%;text-align:center;line-height:25px;border-collapse: collapse;" border="1">
                    <tr>
                        <td style="width:16%;">治疗时间</td>
                        <td style="width:16%;"></td>
                        <td style="width:16%;"></td>
                        <td style="width:16%;"></td>
                        <td style="width:16%;"></td>
                        <td style="width:16%;"></td>
                    </tr>
                    <tr>
                        <td style="width:16%;">医生签字</td>
                        <td style="width:16%;"></td>
                        <td style="width:16%;"></td>
                        <td style="width:16%;"></td>
                        <td style="width:16%;"></td>
                        <td style="width:16%;"></td>
                    </tr>
                    <tr>
                        <td style="width:16px;">患者签字</td>
                        <td style="width:16px;"></td>
                        <td style="width:16px;"></td>
                        <td style="width:16px;"></td>
                        <td style="width:16px;"></td>
                        <td style="width:16px;"></td>
                    </tr>
                </table>
            </div>

        </div>

    </div> -->
  </div>
</template>
<script>
import { jsGetAge, uParseTime } from "@/utils/tools";
import { getAllDoctorList, getPrescriptionPrint } from "@/api/project/project";
import { getInitData } from "@/api/his/his";
export default {
  props: {
    patient_id: Number,
    record_date: String,
    prescription_id: Number,
    ids: String,
  },
  data() {
    return {
      doctorList_1: [],
      operatorMaps: {},
      doctorList: [],
      advicePrint: {},
      doc_name:"",
      patient: {},
      tableData: [],
      prescriptionInfo: [],
      projectList: [],
      prescriptions: [],
      settlement: [
        { value: 1, label: "医保" },
        { value: 2, label: "自费" },
        { value: 3, label: "公费" },
        { value: 4, label: "农保" },
        { value: 5, label: "会员" },
        { value: 6, label: "职工" },
        { value: 7, label: "合同" },
      ],
      hisPatient: {},
      diagnoses: [],
      department: [],
      faber: {},
      org_id: 0,
    };
  },
  methods: {
    // 电子签名
    setAdminUserES(id,name) {
      if (id == 0) {
        return "";
      }
      if (id in this.operatorMaps) {
        return this.operatorMaps[id].url;
      } else {
        this.doc_name = name
        return "";
      }
    },

    getNewAge(birthday) {
      // var myDate = new Date(birthday * 1000)
      // var month = myDate.getMonth() + 1
      // var day = myDate.getDate()
      // var age = new Date().getFullYear() - myDate.getFullYear()
      // if (month < new Date().getMonth() + 1 || new Date().getMonth() + 1 == month && day <= new Date().getDate()) {
      //   age++
      // }
      // return age
      var nowTime = new Date().getTime();
      //一年毫秒数(365 * 86400000 = 31536000000)
      return Math.ceil((nowTime - birthday * 1000) / 31536000000);
    },
    getAge(patient) {
      var thisLen = patient.id_card_no.length;
      var birth = "";
      if (thisLen == 15) {
        birth = "19" + patient.id_card_no.substr(6, 6);
      } else {
        birth = patient.id_card_no.substr(6, 8);
      }
      var births =
        birth.substr(0, 4) +
        "-" +
        birth.substr(4, 2) +
        "-" +
        birth.substr(6, 2);
      return jsGetAge(births, "-");
    },
    printThisPage() {
      var ptime = Math.round(new Date().getTime() / 1000);
      this.print_time = uParseTime(ptime, "{y}-{m}-{d} {h}:{i}");

      const style =
        "@media print {.printTitle{font-size: 22px;text-align: center;font-weight: bold;}.infoTitle{display: flex;margin-top:10px;line-height: 1px;padding:0 10px;}.infoTitle p{width: 200px;}.otherInfo{display: flex;margin-top:10px;padding:0 10px;}.otherInfo span{display:inline-block;}.recordTitle{display: flex;margin-top:10px;line-height: 24px;padding:0 10px;border-bottom: 2px solid #000;}.recordTitle p{width: 200px;}}";
      printJS({
        printable: "prescription-print",
        type: "html",
        style: style,
        scanStyles: false,
      });
    },
    getDepart(id) {
      var name = "";
      for (let i = 0; i < this.department.length; i++) {
        if (id == this.department[i].id) {
          name = this.department[i].name;
        }
      }
      return name;
    },
    getAllDoctorList() {
      getAllDoctorList().then((response) => {
        if (response.data.state == 1) {
          var doctor = response.data.data.doctor;

          this.doctorList = doctor;
        }
      });
    },

    getDoctor(id) {
      var name = "";
      for (let i = 0; i < this.doctorList.length; i++) {
        if (id == this.doctorList[i].admin_user_id) {
          name = this.doctorList[i].user_name;
        }
      }
      return name;
    },
    getTime(value, temp) {
      if (value != undefined) {
        return uParseTime(value, temp);
      }
      return "";
    },
    getPrescriptionPrint() {
      var params = {
        patient_id: this.patient_id,
        record_date: this.record_date,
        prescription_id: this.prescription_id,
        ids: this.ids,
        p_type: 2,
      };
      console.log("999999", params);
      getPrescriptionPrint(params).then((response) => {
        if (response.data.state == 1) {
          var advicePrint = response.data.data.advicePrint;
          console.log("adviceprint", advicePrint);
          this.advicePrint = advicePrint;
          this.prescriptions = advicePrint;
          var projectlist = response.data.data.projectlist;
          console.log("所有项目列表", projectlist);
          this.projectList = projectlist;
          this.hisPatient = response.data.data.hisPatient;
          this.doctorList_1 = response.data.data.eles;
          console.log(this.doctorList_1, "医生列表");
          if (this.doctorList_1.length > 0) {
            var operatorsLen = this.doctorList_1.length;
            for (var index = 0; index < operatorsLen; index++) {
              this.$set(
                this.operatorMaps,
                this.doctorList_1[index].creator,
                this.doctorList_1[index]
              );
            }
          }
          let projectPrint = [];
          let total = 0;
          this.advicePrint.map((item) => {
            console.log(item.project.length);
            if (item.project.length > 0) {
              for (var i = item.project.length - 1; i >= 0; i--) {
                if (item.project[i].type == 2) {
                  if (item.project[i].project.cost_classify == 3) {
                    item.project.splice(i, 1);
                  }
                }
              }
            }
          });

          let outputlist1Name = JSON.parse(response.data.data.his.patient_info);
          this.faber = outputlist1Name;
        }
      });
    },
    getProjectName(id) {
      var project_name = "";
      for (let i = 0; i < this.projectList.length; i++) {
        if (id == this.projectList[i].id) {
          project_name = this.projectList[i].project_name;
        }
      }
      return project_name;
    },
    getUnit(id) {
      var unit = "";
      for (let i = 0; i < this.projectList.length; i++) {
        if (id == this.projectList[i].id) {
          unit = this.projectList[i].unit;
        }
      }
      return unit;
    },

    getTotalOne(id) {
      var total = 0;
      var addtotal = 0;
      for (let i = 0; i < this.prescriptions.length; i++) {
        if (id == this.prescriptions[i].id) {
          if (this.prescriptions[i].project != null) {
            for (let a = 0; a < this.prescriptions[i].project.length; a++) {
              total =
                total +
                this.prescriptions[i].project[a].price *
                  this.prescriptions[i].project[a].count;
            }
          }

          if (this.prescriptions[i].additionalcharge != null) {
            for (
              let a = 0;
              a < this.prescriptions[i].additionalcharge.length;
              a++
            ) {
              addtotal =
                addtotal +
                this.prescriptions[i].additionalcharge[a].price *
                  this.prescriptions[i].additionalcharge[a].count;
            }
          }
          addtotal = Math.floor(addtotal * 100) / 100;
        }
      }

      for (let i = 0; i < this.prescriptions.length; i++) {
        if (id == this.prescriptions[i].id) {
          if (this.prescriptions[i].advices != null) {
            for (let a = 0; a < this.prescriptions[i].advices.length; a++) {
              total =
                total +
                this.prescriptions[i].advices[a].price *
                  this.prescriptions[i].advices[a].prescribing_number;
            }
          }

          if (this.prescriptions[i].additionalcharge != null) {
            for (
              let a = 0;
              a < this.prescriptions[i].additionalcharge.length;
              a++
            ) {
              addtotal =
                addtotal +
                this.prescriptions[i].additionalcharge[a].price *
                  this.prescriptions[i].additionalcharge[a].count;
            }
          }
          addtotal = Math.floor(addtotal * 100) / 100;
        }
      }

      return total + addtotal;
    },
    getInitData() {
      getInitData().then((response) => {
        if (response.data.state == 1) {
          this.department = response.data.data.department;
          this.diagnoses = response.data.data.diagnose.sort(this.compare("id"));
          console.log("争端", this.diagnoses);
        }
      });
    },
    getDiagnosis(ids) {
      let newIds = ids.split(",").sort(function (a, b) {
        return a - b;
      });

      var name = "";
      let nameArr = [];
      for (let i = 0; i < this.diagnoses.length; i++) {
        // if(id == this.diagnoses[i].id){
        //    name = this.diagnoses[i].class_name
        // }

        if (newIds.indexOf(this.diagnoses[i].id.toString()) > -1) {
          // name += diagnoses[i].class_name + ' '
          nameArr.push(this.diagnoses[i].class_name);
        }
      }
      let newNameArr = [];
      nameArr.map((item, index) => {
        if (item == "尿毒症") {
          newNameArr.push(item);
          nameArr.splice(index, 1, "");
        }
      });
      newNameArr.push(...nameArr);
      return newNameArr.join(" ");
    },
    compare(property) {
      return function (a, b) {
        var value1 = a[property];
        var value2 = b[property];
        return value1 - value2; //升序排序
      };
    },
    getName(list) {
      console.log("list22", list);
      let new_list = [];
      for (let i = 0; i < list.length; i++) {
        if (list[i].aac031 == "1") {
          new_list.push(list[i]);
        }
      }

      switch (new_list[0].bcc334) {
        case "A31001":
          return "深圳医保1档";
          break;
        case "A31002":
          return "深圳医保2档";

          break;
        case "A31003":
          return "深圳医保3档";

          break;
        case "A31004":
          return "二档（少儿）";

          break;
        case "A31005":
          return "学生二档";

          break;
        case "A31006":
          return "大学生二档";

          break;
        case "A32001":
          return "在职公务员";
          break;
        case "A32002":
          return "在职驻深公务员";

          break;
        case "A39301":
          return "家属统筹医疗";

          break;
        case "A41001":
          return "工伤在职";

          break;
        case "A51001":
          return "生育在职";

          break;
        case "A52001":
          return "生育医疗一档";

          break;
        case "A52002":
          return "生育医疗一档";

          break;
        case "C31001":
          return "一档医疗退休";

          break;
        case "C31002":
          return "二档医疗退休";
          break;
      }
    },
  },
  created() {
    this.getInitData();
    this.getAllDoctorList();
    this.getPrescriptionPrint();
    var xtuser = this.$store.getters.xt_user;
    this.orgname = xtuser.org.org_name;
    this.org_id = this.$store.getters.xt_user.org.id;
  },
  watch: {
    patient_id: function (val) {
      console.log(
        "101000010101010",
        this.patient_id,
        this.record_date,
        this.prescription_id
      );
      //    this.getPrescriptionPrint()
    },
    ids: function (val) {
      this.ids = val;
      console.log("this.idsids", this.ids);
      this.getPrescriptionPrint();
    },
  },
};
</script>


<style lang="scss" scoped>
.prescription-print {
  -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27),
    0 0 60px rgba(0, 0, 0, 0.06) inset;
  -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27),
    0 0 40px rgba(0, 0, 0, 0.06) inset;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 40px rgba(0, 0, 0, 0.06) inset;
  margin-bottom: 20px;
  padding: 20px 10px;
}
.printTitle {
  font-size: 22px;
  text-align: center;
  font-weight: bold;
  margin-bottom: 10px;
}
.infoTitle {
  display: flex;
  margin-top: 10px;
  line-height: 24px;
}
.infoTitle div {
  width: 200px;
}
.infoMain {
  display: flex;
  flex-wrap: wrap;
  margin-top: 10px;
}
.infoMain div {
  width: 50%;
  line-height: 24px;
}
.prescriptionBox {
  padding: 0 10px;
  min-height: 400px;
}
.Rp {
  font-size: 22px;
  font-weight: bold;
}
.drugsBox {
  padding-left: 40px;
  margin-bottom: 10px;
}
.drugsBox div {
  line-height: 20px;
}
.drugsOne {
  line-height: 24px;
}
.drugsOne span {
  margin-right: 20px;
}
.doctorBox {
  display: flex;
  justify-content: space-between;
  padding: 0 10px;
  line-height: 24px;
  border-bottom: 2px solid #000;
}
.actionBar {
  display: flex;
  justify-content: space-between;
  line-height: 24px;
  padding: 0 10px;
}
.actionBar p {
  width: 150px;
}
.under_line {
  display: inline-block;
  border-bottom: 1px solid #000;
  flex: 1;
}
</style>
