<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb :crumbs='crumbs'></bread-crumb>
    </div>
    <div class="app-container ">
      
      <div class="dataTitle">透析模板</div>

      <div
        class="app-container"
        style="margin-left: -20px;margin-top: -20px"
        v-loading="pageLoading"
        element-loading-text="拼命加载中"
      >
        <el-row :gutter="20">
          <el-col :span="7">
            <el-table
              :header-cell-style="{
                backgroundColor: 'rgb(245, 247, 250)',
                color: '#606266'
              }"
              height="450"
              :row-style="{ color: '#303133' }"
              :data="treatment_mode"
              border
              style="width: 100%"
              highlight-current-row
              :row-class-name="tableRows"
              @row-click="onRowClicks"
              ref="singleTable"
            >
              <el-table-column label="透析模式" align="center">
                <template slot-scope="scope">
                  {{ scope.row.name }}
                </template>
              </el-table-column>

              <el-table-column label="状态" width="80" align="center">
                <template slot-scope="scope">
                  {{ getModeStatus(scope.row.id) }}
                </template>
              </el-table-column>
            </el-table>
          </el-col>
          <el-col :span="17" v-loading="itemLoading">
            <div class="filter-container">
              <el-row :gutter="20">
                <el-col :span="16">
                  <div>{{ mode_name }}</div>
                </el-col>
                <el-col :span="8">
                  <el-button
                    style="float: right;visibility: hidden"
                    size="small"
                    class="filter-item"
                    type="primary"
                    icon="el-icon-edit-outline"
                    @click="openEdit()"
                    >修改
                  </el-button>
                </el-col>
              </el-row>
            </div>
            <div class="filter-container" style="float:right">
              <el-form
                ref="addPlan"
                :model="addPlan"
                label-width="160px"
                style="border: black"
              >
                <el-row :gutter="20">
                  <el-col :span="8">
                    <el-form-item label="透析时长（h）:" prop="dialysis_duration">
                      <el-time-picker
                        v-model="dialysisTimeShow"
                        :picker-options="{
                          selectableRange: '00:01:00 - 23:59:00'
                        }"
                        placeholder="透析时长"
                        style="width:100%;"
                        value-format="H:m"
                        format="H:m"
                        @change="selectnDialysisTime"
                      >
                      </el-time-picker>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item
                      label="血流量(ml/min):"
                      prop="blood_flow_volume"
                    >
                      <el-input v-model="addPlan.blood_flow_volume"></el-input>
                    </el-form-item>
                  </el-col>
                  <!--<el-col :span="8" v-if="this.$store.getters.xt_user.template_info.template_id == 6">-->
                  <!--<el-form-item label="目标超滤量(ml) : ">-->
                  <!--<el-input v-model="addPlan.target_ultrafiltration" type="number" ></el-input>-->
                  <!--</el-form-item>-->
                  <!--</el-col>-->

                  <!--<el-col :span="8" v-if="this.$store.getters.xt_user.template_info.template_id != 6">-->
                  <!--<el-form-item label="目标超滤量(L) : ">-->
                  <!--<el-input v-model="addPlan.target_ultrafiltration" type="number" ></el-input>-->
                  <!--</el-form-item>-->
                  <!--</el-col>-->

                  <el-col :span="8">
                    <el-form-item label="透析液配方:">
                      <el-select v-model="addPlan.dialysate_formulation">
                        <el-option
                          v-for="(item, index) in dialysate_formulation"
                          :label="item.name"
                          :key="index"
                          :value="item.id"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="抗凝剂:" prop="anticoagulant">
                      <el-select
                        v-model="addPlan.anticoagulant"
                        @change="changeThisAnticoagulant"
                      >
                        <el-option
                          v-for="(item, index) in anticoagulantsConfit"
                          :label="item.name"
                          :key="index"
                          :value="item.id"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8" v-if="anticoagulant.shouji != -1">
                    <el-form-item
                      :label="'首剂（' + anticoagulant.shouji_unit + '）: '"
                      prop="anticoagulant_shouji"
                    >
                      <el-input
                        v-model="addPlan.anticoagulant_shouji"
                        :disabled="anticoagulant.shouji == 1 ? false : true"
                        type="number"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8" v-if="anticoagulant.weichi != -1">
                    <el-form-item
                      :label="'维持（' + anticoagulant.weichi_unit + '）: '"
                      prop="anticoagulant_weichi"
                    >
                      <el-input
                        v-model="addPlan.anticoagulant_weichi"
                        :disabled="anticoagulant.weichi == 1 ? false : true"
                        type="number"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8" v-if="anticoagulant.zongliang != -1">
                    <el-form-item
                      :label="'总量（' + anticoagulant.zongliang_unit + '）: '"
                      prop="anticoagulant_zongliang"
                    >
                      <el-input
                        v-model="addPlan.anticoagulant_zongliang"
                        :disabled="anticoagulant.zongliang == 1 ? false : true"
                        type="number"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8" v-if="anticoagulant.gaimingcheng != -1">
                    <el-form-item
                      label="钙名称:"
                      prop="anticoagulant_gaimingcheng"
                    >
                      <el-input
                        v-model="addPlan.anticoagulant_gaimingcheng"
                        :disabled="anticoagulant.gaimingcheng == 1 ? false : true"
                        placeholder="钙名称"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8" v-if="anticoagulant.gaijiliang != -1">
                    <el-form-item
                      :label="
                        '钙剂量（' + anticoagulant.gaimingcheng_unit + '）: '
                      "
                      prop="anticoagulant_gaijiliang"
                    >
                      <el-input
                        v-model="addPlan.anticoagulant_gaijiliang"
                        :disabled="anticoagulant.gaijiliang == 1 ? false : true"
                        placeholder="钙剂量"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col
                    :span="8"
                    v-if="
                      current_select == 2 ||
                        current_select == 5 ||
                        current_select == 12
                    "
                  >
                    <el-form-item label="置换量(L) : " prop="replacement_total">
                      <el-input
                        v-model="addPlan.replacement_total"
                        type="number"
                      ></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="钾（mmol/L）: " prop="kalium">
                      <el-input v-model="addPlan.kalium"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="钠（mmol/L）: " prop="sodium">
                      <el-input v-model="addPlan.sodium"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="钙（mmol/L）: " prop="calcium">
                      <el-input v-model="addPlan.calcium"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="透析器/灌流器：">

                      <el-input
                        v-model="addPlan.dialyzer_perfusion_apparatus" @focus="showInnerDialog('5')"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="碳酸氢盐（mmol/L）: " prop="bicarbonate">
                      <el-input v-model="addPlan.bicarbonate"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="葡萄糖（mmol/L）: " prop="glucose">
                      <el-input v-model="addPlan.glucose"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item
                      label="透析液流量（ml/min）: "
                      prop="dialysate_flow"
                    >
                      <el-input v-model="addPlan.dialysate_flow"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item
                      label="透析液温度（℃）: "
                      prop="dialysate_temperature"
                    >
                      <el-input
                        v-model="addPlan.dialysate_temperature"
                      ></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="电导度（mS/cm）: " prop="conductivity">
                      <el-input v-model="addPlan.conductivity"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="体液过多症状:">
                      <el-select v-model="addPlan.body_fluid">
                        <el-option
                          v-for="(item, index) in body_fluid_option"
                          :label="item.name"
                          :key="index"
                          :value="item.id"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="体液过多其他症状">
                      <el-input v-model="addPlan.body_fluid_other"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="透析前使用特殊药物">
                      <el-select
                        v-model="addPlan.special_medicine"
                        placeholder="请选择"
                        style="width:100%;"
                      >
                        <el-option :key="0" label="请选择" :value="0"></el-option>
                        <el-option
                          v-for="(item, index) in special_medicine_option"
                          :label="item.name"
                          :key="index"
                          :value="item.id"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="使用其他特殊药物">
                      <el-input
                        v-model="addPlan.special_medicine_other"
                      ></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item
                      label="置换液："
                      v-if="
                        (current_select == 2 ||
                          current_select == 5 ||
                          current_select == 12) &&
                          this.$store.getters.xt_user.template_info.template_id !=
                            6
                      "
                    >
                      <el-select
                        v-model="addPlan.displace_liqui_part"
                        placeholder="请选择"
                      >
                        <el-option :key="0" label="请选择" :value="0"></el-option>
                        <el-option
                          v-for="(option, index) in displace_liqui_part_option"
                          :key="index"
                          :label="option.name"
                          :value="option.id"
                        ></el-option>
                      </el-select>
                    </el-form-item>

                    <el-form-item
                      label="置换方式："
                      v-if="
                        current_select == 2 &&
                          this.$store.getters.xt_user.template_info.template_id ==
                            6
                      "
                    >
                      <el-select
                        v-model="addPlan.displace_liqui_part"
                        placeholder="请选择"
                      >
                        <el-option :key="0" label="请选择" :value="0"></el-option>
                        <el-option
                          v-for="(option, index) in displace_liqui_part_option"
                          :key="index"
                          :label="option.name"
                          :value="option.id"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col
                    :span="8"
                    v-if="
                      current_select == 2 ||
                        current_select == 5 ||
                        current_select == 12
                    "
                  >
                    <el-form-item label="置换液总量(L)">
                      <el-input v-model="addPlan.displace_liqui_value"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="血管通路：">
                      <el-select
                        v-model="addPlan.blood_access"
                        placeholder="请选择"
                      >
                        <el-option :key="0" label="请选择" :value="0"></el-option>

                        <el-option
                          v-for="(option, index) in blood_access_option"
                          :key="index"
                          :label="option.name"
                          :value="option.id"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <!-- </el-row>
                  <el-row :gutter="20" > -->
                  <el-col :span="8">
                    <el-form-item label="实际超滤量(L)">
                      <el-input v-model="addPlan.ultrafiltration"></el-input>
                    </el-form-item>
                  </el-col>

                  <el-col :span="8">
                    <el-form-item label="目标KT/V">
                      <el-input v-model="addPlan.target_ktv"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
              <el-button
                style="float: right"
                size="small"
                class="filter-item"
                type="primary"
                icon="el-icon-edit-outline"
                @click="savePrescription()"
                >保存
              </el-button>
            </div>
          </el-col>
        </el-row>
      </div>
      <multi-select-box
        :propsForm="InnerDialogProps"
        v-on:dialog-comfirm="innerDialogComfirm"
        v-on:dialog-cancle="innerDialogCancle"
      >
      </multi-select-box>
    </div>
  </div>
</template>

<script>
import BreadCrumb from '@/xt_pages/components/bread-crumb'
import { calculateAnticoagulantZL, uParseTime } from "@/utils/tools";
import {
  getAllSystemPrescription,
  getSystemPrescription,
  postSystemPrescription,
  updateSystemPrescription
} from "@/api/config";
import { getDataConfig } from '@/utils/data'
import MultiSelectBox from '../dialysis/details/dialog/MultiSelectBox/index'


export default {
  name: "prescription",
  components: { MultiSelectBox,BreadCrumb },

  data() {
    return {
      crumbs: [
        { path: false, name: '配置管理' },
        { path: false, name: '处方模板' }
      ],
      InnerDialogProps: {
        values: [],
        visibility: false,
        isShowTextArea: true,
        customContent: '',
        titles: '',
        type: '' // 不同弹框类型，用来匹配数据
      },
      dialysisTimeShow: new Date(2018, 1, 1, 3, 0),
      modeOptions: null,
      current_select: 0,
      mode_name: "HD",
      pageLoading: false,
      itemLoading: false,
      dialyzerPerfusionApparatus:[],
      treatment_mode: [
        // 治疗模式
        {
          id: 1,
          name: "HD",
          dialysis_duration: 1,
          replacement_way: 2,
          hemodialysis_machine: 1,
          blood_filter: 2,
          perfusion_apparatus: 2,
          blood_flow_volume: 1,
          dialysate_flow: 1,
          kalium: 1,
          sodium: 1,
          calcium: 1,
          bicarbonate: 1
        },
        {
          id: 2,
          name: "HDF",
          dialysis_duration: 1,
          replacement_way: 1,
          hemodialysis_machine: 2,
          blood_filter: 1,
          perfusion_apparatus: 2,
          blood_flow_volume: 1,
          dialysate_flow: 1,
          kalium: 1,
          sodium: 1,
          calcium: 1,
          bicarbonate: 1
        },
        {
          id: 3,
          name: "HD+HP",
          dialysis_duration: 1,
          replacement_way: 2,
          hemodialysis_machine: 1,
          blood_filter: 2,
          perfusion_apparatus: 1,
          blood_flow_volume: 1,
          dialysate_flow: 1,
          kalium: 1,
          sodium: 1,
          calcium: 1,
          bicarbonate: 1
        },
        {
          id: 4,
          name: "HP",
          dialysis_duration: 1,
          replacement_way: 2,
          hemodialysis_machine: 2,
          blood_filter: 2,
          perfusion_apparatus: 1,
          blood_flow_volume: 1,
          dialysate_flow: 2,
          kalium: 2,
          sodium: 2,
          calcium: 2,
          bicarbonate: 2
        },
        {
          id: 5,
          name: "HF",
          dialysis_duration: 1,
          replacement_way: 1,
          hemodialysis_machine: 2,
          blood_filter: 1,
          perfusion_apparatus: 2,
          blood_flow_volume: 1,
          dialysate_flow: 2,
          kalium: 1,
          sodium: 1,
          calcium: 1,
          bicarbonate: 1
        },
        {
          id: 6,
          name: "SCUF",
          dialysis_duration: 1,
          replacement_way: 2,
          hemodialysis_machine: 1,
          blood_filter: 2,
          perfusion_apparatus: 2,
          blood_flow_volume: 1,
          dialysate_flow: 2,
          kalium: 2,
          sodium: 2,
          calcium: 2,
          bicarbonate: 2
        },
        {
          id: 7,
          name: "IUF",
          dialysis_duration: 1,
          replacement_way: 2,
          hemodialysis_machine: 1,
          blood_filter: 2,
          perfusion_apparatus: 2,
          blood_flow_volume: 2,
          dialysate_flow: 2,
          kalium: 2,
          sodium: 2,
          calcium: 2,
          bicarbonate: 2
        },
        {
          id: 8,
          name: "HFHD",
          dialysis_duration: 1,
          replacement_way: 2,
          hemodialysis_machine: 1,
          blood_filter: 2,
          perfusion_apparatus: 2,
          blood_flow_volume: 1,
          dialysate_flow: 1,
          kalium: 1,
          sodium: 1,
          calcium: 1,
          bicarbonate: 1
        },
        {
          id: 9,
          name: "HFHD+HP",
          dialysis_duration: 1,
          replacement_way: 2,
          hemodialysis_machine: 1,
          blood_filter: 2,
          perfusion_apparatus: 1,
          blood_flow_volume: 1,
          dialysate_flow: 1,
          kalium: 1,
          sodium: 1,
          calcium: 1,
          bicarbonate: 1
        },
        {
          id: 10,
          name: "PHF",
          dialysis_duration: 1,
          replacement_way: 1,
          hemodialysis_machine: 2,
          blood_filter: 1,
          perfusion_apparatus: 2,
          blood_flow_volume: 1,
          dialysate_flow: 1,
          kalium: 1,
          sodium: 1,
          calcium: 1,
          bicarbonate: 1
        },
        {
          id: 11,
          name: "HFR",
          dialysis_duration: 1,
          replacement_way: 2,
          hemodialysis_machine: 2,
          blood_filter: 1,
          perfusion_apparatus: 1,
          blood_flow_volume: 1,
          dialysate_flow: 1,
          kalium: 1,
          sodium: 1,
          calcium: 1,
          bicarbonate: 1
        },
        {
          id: 12,
          name: "HDF+HP",
          dialysis_duration: 1,
          replacement_way: 1,
          hemodialysis_machine: 2,
          blood_filter: 1,
          perfusion_apparatus: 1,
          blood_flow_volume: 1,
          dialysate_flow: 1,
          kalium: 1,
          sodium: 1,
          calcium: 1,
          bicarbonate: 1
        },
        {
          id: 13,
          name: "CRRT",
          dialysis_duration: 1,
          replacement_way: 1,
          hemodialysis_machine: 2,
          blood_filter: 1,
          perfusion_apparatus: 2,
          blood_flow_volume: 1,
          dialysate_flow: 2,
          kalium: 1,
          sodium: 1,
          calcium: 1,
          bicarbonate: 1
        },
        {
          id: 14,
          name: "腹水回输",
          dialysis_duration: 1,
          replacement_way: 2,
          hemodialysis_machine: 1,
          blood_filter: 2,
          perfusion_apparatus: 2,
          blood_flow_volume: 2,
          dialysate_flow: 2,
          kalium: 2,
          sodium: 2,
          calcium: 2,
          bicarbonate: 2
        },
        // {
        //   id: 15,
        //   name: "HD前置换",
        //   dialysis_duration: 1,
        //   replacement_way: 2,
        //   hemodialysis_machine: 1,
        //   blood_filter: 2,
        //   perfusion_apparatus: 2,
        //   blood_flow_volume: 1,
        //   dialysate_flow: 1,
        //   kalium: 1,
        //   sodium: 1,
        //   calcium: 1,
        //   bicarbonate: 1
        // },
        // {
        //   id: 16,
        //   name: "HD后置换",
        //   dialysis_duration: 1,
        //   replacement_way: 2,
        //   hemodialysis_machine: 1,
        //   blood_filter: 2,
        //   perfusion_apparatus: 2,
        //   blood_flow_volume: 1,
        //   dialysate_flow: 1,
        //   kalium: 1,
        //   sodium: 1,
        //   calcium: 1,
        //   bicarbonate: 1
        // },
        // {
        //   id: 17,
        //   name: "HDF前置换",
        //   dialysis_duration: 1,
        //   replacement_way: 1,
        //   hemodialysis_machine: 2,
        //   blood_filter: 1,
        //   perfusion_apparatus: 2,
        //   blood_flow_volume: 1,
        //   dialysate_flow: 1,
        //   kalium: 1,
        //   sodium: 1,
        //   calcium: 1,
        //   bicarbonate: 1
        // },
        // {
        //   id: 18,
        //   name: "HDF后置换",
        //   dialysis_duration: 1,
        //   replacement_way: 1,
        //   hemodialysis_machine: 2,
        //   blood_filter: 1,
        //   perfusion_apparatus: 2,
        //   blood_flow_volume: 1,
        //   dialysate_flow: 1,
        //   kalium: 1,
        //   sodium: 1,
        //   calcium: 1,
        //   bicarbonate: 1
        // },
        {
          id: 19,
          name: "IUF+HD",
          dialysis_duration: 1,
          replacement_way: 2,
          hemodialysis_machine: 1,
          blood_filter: 2,
          perfusion_apparatus: 2,
          blood_flow_volume: 1,
          dialysate_flow: 1,
          kalium: 1,
          sodium: 1,
          calcium: 1,
          bicarbonate: 1
        }
      ],
      addPlan: {
        id: 0,
        mode: "",
        dialyzer: "",
        dialysis_duration: "",
        dialysis_duration_hour: "",
        dialysis_duration_minute: "",
        hemodialysis_machine: "",
        perfusion_apparatus: "",
        dialysate_formulation: "",
        blood_flow_volume: "",
        dewater: "",
        displace_liqui: 0,
        replacement_way: "",
        anticoagulant: "",
        anticoagulant_shouji: "",
        anticoagulant_weichi: "",
        anticoagulant_zongliang: "",
        anticoagulant_gaimingcheng: "",
        anticoagulant_gaijiliang: "",
        kalium: "",
        replacement_total: "",
        sodium: "",
        calcium: "",
        bicarbonate: "",
        glucose: "",
        dry_weight: "",
        dialysate_flow: "",
        dialysate_temperature: "",
        conductivity: "",
        doctor: "",
        remark: "",
        target_ktv: "",
        target_ultrafiltration: "",
        dialyzer_perfusion_apparatus: "",
        body_fluid: "",
        body_fluid_other: "",
        special_medicine: "",
        special_medicine_other: "",
        displace_liqui_part: "",
        displace_liqui_value: "",
        ultrafiltration: "",
        blood_access: ""
      },
      system_prescription: [],
      isEdit: false,
      anticoagulant: {
        id: 0,
        name: "",
        type: 1,
        shouji: 1,
        weichi: 1,
        zongliang: 1,
        gaimingcheng: -1,
        gaijiliang: -1,
        shouji_unit: "mg",
        weichi_unit: "mg/h",
        zongliang_unit: "mg",
        gaimingcheng_unit: "",
        gaijiliang_unit: ""
      },
      anticoagulantsSet: {
        type: 1
      },
      replacementWays: null,
      anticoagulantsConfit: null,
      dialysate_formulation: [],
      body_fluid_option: [],
      special_medicine_option: [],
      displace_liqui_part_option: [],
      blood_access_option: []
    };
  },
  computed: {},
  methods: {
    showInnerDialog: function(val) {
      this.InnerDialogProps.visibility = true
      switch (val) {
        case '5': // 透析器/灌流器
          this.InnerDialogProps.values = this.dialyzerPerfusionApparatus
          this.InnerDialogProps.titles = '透析器/灌流器'
          this.InnerDialogProps.type = 'dialyzer_perfusion_apparatus'
          this.InnerDialogProps.selected = this.addPlan.dialyzer_perfusion_apparatus
          this.InnerDialogProps.isShowTextArea = false
          break
      }
    },
    innerDialogComfirm: function(val) {
      this.InnerDialogProps.visibility = false
      switch (val.type) {
        case 'dialyzer_perfusion_apparatus':
          this.addPlan.dialyzer_perfusion_apparatus = val.value.join(',')
          break

      }
    },
    innerDialogCancle: function() {
      this.InnerDialogProps.visibility = false
    },
    openEdit() {},
    tableRows({ row, rowIndex }) {
      // 把每一行的索引放进row
      row.index = rowIndex;
    },
    onRowClicks(row, event, column) {
      this.current_select = row.id;
      this.mode_name = row.name;
      this.isEdit = false;
      this.addPlan.mode = row.id;
      this.getSystemPrescription(row.id);
    },
    getSystemPrescription(id) {
      var tempAddPlan = {
        id: 0,
        mode: "",
        dialyzer: "",
        dialysis_duration: "",
        dialysis_duration_hour: "",
        dialysis_duration_minute: "",
        hemodialysis_machine: "",
        dialysate_formulation: "",
        perfusion_apparatus: "",
        blood_flow_volume: "",
        dewater: "",
        displace_liqui: 0,
        replacement_way: "",
        anticoagulant: "",
        anticoagulant_shouji: "",
        anticoagulant_weichi: "",
        anticoagulant_zongliang: "",
        anticoagulant_gaimingcheng: "",
        anticoagulant_gaijiliang: "",
        kalium: "",
        replacement_total: "",
        sodium: "",
        calcium: "",
        bicarbonate: "",
        glucose: "",
        dry_weight: "",
        dialysate_flow: "",
        dialysate_temperature: "",
        conductivity: "",
        doctor: "",
        remark: "",
        target_ktv: "",
        target_ultrafiltration: "",
        dialyzer_perfusion_apparatus: "",
        body_fluid: "",
        body_fluid_other: "",
        special_medicine: "",
        special_medicine_other: "",
        displace_liqui_part: "",
        displace_liqui_value: "",
        ultrafiltration: "",
        blood_access: ""
      };

      for (const key in tempAddPlan) {
        this.addPlan[key] = tempAddPlan[key];
      }
      this.isEdit = false;
      let params = {
        id: id
      };

      this.dialysisTimeShow = new Date(
        2018,
        1,
        1,
        this.addPlan.dialysis_duration_hour,
        this.addPlan.dialysis_duration_minute
      );

      getSystemPrescription(params).then(response => {

        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
           var prescription =  response.data.data.prescription
           console.log("prescription",prescription)
            prescription.dialysis_duration = parseFloat(prescription.dialysis_duration_hour) + parseFloat((prescription.dialysis_duration_minute / 60).toFixed(2))
           if(prescription.dialysate_formulation == 0){
              prescription.dialysate_formulation = ""
           } 
           if(prescription.body_fluid ==0 ){
             prescription.body_fluid = ""
           }
           if(prescription.anticoagulant == 0){
               prescription.anticoagulant = ""
           }
           if(prescription.anticoagulant == 3){
              prescription.anticoagulant = "低分子肝素"
           }
          if (prescription.id > 0) {

            for (const key in prescription) {
              this.addPlan[key] = prescription[key];
            }

            this.dialysisTimeShow = new Date(
              2018,
              1,
              1,
              this.addPlan.dialysis_duration_hour,
              this.addPlan.dialysis_duration_minute
            );
            if (this.addPlan.id > 0) {
              this.isEdit = true;
            }
          }
          this.addPlan.mode = id;
        }
      });
    },
    getModeStatus: function(id) {
      for (let i = 0; i < this.system_prescription.length; i++) {
        if (this.system_prescription[i].mode_id == id) {
          return "已保存";
        }
      }
      return "未保存";
    },
    getAllSystemPrescription() {
      getAllSystemPrescription().then(response => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
          this.system_prescription = response.data.data.prescriptions;

          for (let i = 0; i < this.system_prescription.length; i++) {
            if (this.system_prescription[i].mode == 1) {
              this.isEdit = true;
            }
          }
        }
      });
    },
    selectnDialysisTime(val) {
      var valTime = val.split(":");
      this.addPlan.dialysis_duration =
        parseFloat(valTime[0]) + parseFloat((valTime[1] / 60).toFixed(2));
      this.addPlan.dialysis_duration_hour = valTime[0];
      this.addPlan.dialysis_duration_minute = valTime[1];
    },
    changeThisAnticoagulant() {
      var thismode = parseInt(this.addPlan.anticoagulant);

      if (isNaN(thismode) || thismode <= 0) {
        return false;
      }
      if (
        typeof this.anticoagulantsConfit[thismode] == "undefined" ||
        this.anticoagulantsConfit[thismode] == null
      ) {
        return false;
      }
      this.anticoagulant = this.anticoagulantsConfit[thismode];
    },
    savePrescription: function() {
      this.addPlan.anticoagulant_zongliang = this.addPlan.anticoagulant_zongliang.toString();
      if (!this.isEdit) {
        postSystemPrescription(this.addPlan).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg);
            return false;
          } else {
            this.$notify({
              title: "成功",
              message: "创建成功",
              type: "success",
              duration: 2000
            });
            this.isEdit = true;
            this.system_prescription = [];
            this.system_prescription = response.data.data.prescriptions;
            this.addPlan.id = response.data.data.prescription.id;
            return false;
          }
        });
      } else {
        updateSystemPrescription(this.addPlan.id, this.addPlan).then(
          response => {
            if (response.data.state == 0) {
              this.$message.error(response.data.msg);
              return false;
            } else {
              this.isEdit = true;
              this.addPlan.id = response.data.data.prescription.id;
              this.$notify({
                title: "成功",
                message: "编辑成功",
                type: "success",
                duration: 2000
              });
              return false;
            }
          }
        );
      }
    }
  },
  mounted() {
    this.$refs.singleTable.setCurrentRow(this.treatment_mode[0]);
    this.mode_name = this.treatment_mode[0].name;
    this.current_select = this.treatment_mode[0].id;
  },
  created() {
    this.anticoagulantsSet = this.$store.getters.anticoagulants_set;
    this.replacementWays = this.$store.getters.replacement_ways;
    this.anticoagulantsConfit = this.$store.getters.anticoagulants_confit;

    this.blood_filters = this.$store.getters.blood_filters;
    this.dialysate_formulation = this.$store.getters.dialysate_formulation;
   
    this.body_fluid_option = this.$store.getters.body_fluid;
    this.special_medicine_option = this.$store.getters.special_medicine;
    this.displace_liqui_part_option = this.$store.getters.displace_liqui;
    this.blood_access_option = this.$store.getters.blood_access;
    this.addPlan.mode = 1;
    this.dialyzerPerfusionApparatus = getDataConfig(
      'hemodialysis',
      'dialyzer_perfusion_apparatus'
    )
    this.getAllSystemPrescription();
    this.getSystemPrescription(this.addPlan.mode);

  },
  watch: {
    "addPlan.dialysis_duration": function() {
      // console.log(this.addPlan.anticoagulant_shouji);
      // console.log(this.addPlan.dialysis_duration);
      // console.log(this.addPlan.anticoagulant_weichi);

      this.addPlan.anticoagulant_zongliang = calculateAnticoagulantZL(
        1,
        this.addPlan.anticoagulant_shouji,
        this.addPlan.dialysis_duration,
        this.addPlan.anticoagulant_weichi
      );
    },
    "addPlan.anticoagulant_shouji": function() {
      // console.log("首剂", this.addPlan.anticoagulant_shouji);
      // console.log("维持", this.addPlan.anticoagulant_weichi);
      // console.log("总量", this.addPlan.dialysis_duration);
      this.addPlan.anticoagulant_zongliang = calculateAnticoagulantZL(
        1,
        this.addPlan.anticoagulant_shouji,
        this.addPlan.dialysis_duration,
        this.addPlan.anticoagulant_weichi
      );
      // console.log("总量----", this.addPlan.anticoagulant_zongliang);
    },
    "addPlan.anticoagulant_weichi": function() {
      // console.log(this.addPlan.anticoagulant_shouji);
      // console.log(this.addPlan.dialysis_duration);
      // console.log(this.addPlan.anticoagulant_weichi);
      this.addPlan.anticoagulant_zongliang = calculateAnticoagulantZL(
        1,
        this.addPlan.anticoagulant_shouji,
        this.addPlan.dialysis_duration,
        this.addPlan.anticoagulant_weichi
      );
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped></style>
