<template>
    <div class="mainBox">
      <div class="choice">
        <ul>
          <el-popover
            placement="bottom"
            trigger="click"
            v-model="show_patient_type_selector"
          >
            <li slot="reference">
              {{ patient_types[patient_selected].text }}
              <span class="iconfont">&#xe74a;</span>
            </li>
            <div class="popover-demo-content">
              <ul>
                <li
                  v-for="type in patient_types"
                  :key="type.value"
                  @click="handlePaitentType(type.value)"
                  :class="advice_type_selected == type.value ? 'tick' : ''"
                >
                  {{ type.text }}
                </li>
              </ul>
            </div>
          </el-popover>
  
          <el-popover
            placement="bottom"
            trigger="click"
            v-model="show_zone_selector"
          >
            <li slot="reference">
              {{ zones[zone_selected].text }}
              <span class="iconfont">&#xe74a;</span>
            </li>
            <div class="popover-demo-content">
              <ul>
                <li
                  v-for="(zone, index) in zones"
                  :key="zone.value"
                  @click="handleZoneChange(index)"
                  :class="zone_selected == index ? 'tick' : ''"
                >
                  {{ zone.text }}
                </li>
              </ul>
            </div>
          </el-popover>
          <el-popover
            placement="bottom"
            trigger="click"
            v-model="show_sch_type_selector"
          >
            <li slot="reference">
              {{ schedule_types[schedule_type_selected].text }}
              <span class="iconfont">&#xe74a;</span>
            </li>
            <div class="popover-demo-content">
              <ul>
                <li
                  v-for="scheduleType in schedule_types"
                  :key="scheduleType.value"
                  @click="handletimeType(scheduleType.value)"
                  :class="
                    schedule_type_selected == scheduleType.value ? 'tick' : ''
                  "
                >
                  {{ scheduleType.text }}
                </li>
              </ul>
            </div>
          </el-popover>
          <el-popover placement="bottom" trigger="click" v-model="show_sort_selector">
            <li slot="reference">{{sort_types[sort_type_selected].text}}<span class="iconfont">&#xe74a;</span></li>
            <div class="popover-demo-content">
            <ul>
            <li v-for="type in sort_types" :key="type.value" @click="handleSortType(type.value)" :class="sort_type_selected == type.value ? 'tick' : ''" >{{type.text}}</li>
            </ul>
          </div>
          </el-popover>
  
          <li @click="openPicker()">
            {{ selected_date_str }}
            <span class="iconfont">&#xe74a;</span>
          </li>
        </ul>
      </div>
  
      <div
        style="width: 100%; overflow: hildden"
        class="DialysisAdvice"
        ref="adviceScrollTop"
      >
        <div class="blueBorder"></div>
  
        <table class="table dialysisTable" v-show="showOne">
          <tr @click="cancelAdviceSelect">
            <th width="50px">姓名</th>
            <th width="">医嘱内容</th>
          </tr>
          <template v-for="(schedules, zone_name, index) in filtedScheduals" >
            <tr :key="index">
              <td>{{ zone_name }}</td>
              <td></td>
             
            </tr>
            <template v-for="schedule in schedules">
              <template v-for="(group, group_index) in schedule.new_advice">
                <tr
                  v-for="(advice, advice_index) in group.advices"
                  :key="'No' + advice.id + advice_index + group_index"
                  :class="{'row-class-active':current_patient_id == schedule.patient_id && group_flag &&current_advice_id < 0,}">
                  <td
                    v-if="advice_index == 0"
                    :rowspan="group.advices.length"
                    style="color: #409eff"
                    @click="selectGroupAdviceAction(schedule.patient_id,schedule.new_advice)">
                    {{ advice.parent_id == 0 ? schedule.patient.name : "" }}
                    <br />
                    （{{ advice.parent_id == 0 ? schedule.dialysis_no : "" }}）
                    <br />
                    {{getAge(schedule.patient.id_card_no)}}岁 
                    <span v-if="schedule.mode_id == 1">(HD)</span>
                    <span v-if="schedule.mode_id == 2">(HDF)</span>
                    <span v-if="schedule.mode_id == 3">(HD+HP)</span>
                    <span v-if="schedule.mode_id == 4">(HP)</span>
                    <span v-if="schedule.mode_id == 5">(HF)</span>
                    <span v-if="schedule.mode_id == 6">(SCUF)</span>
                    <span v-if="schedule.mode_id == 7">(IUF)</span>
                    <span v-if="schedule.mode_id == 8">(HFHD)</span>
                    <span v-if="schedule.mode_id == 9">(HFHD+HP)</span>
                    <span v-if="schedule.mode_id == 10">(PHF)</span>
                    <span v-if="schedule.mode_id == 11">(HFR)</span>
                    <span v-if="schedule.mode_id == 12">(HDF+HP)</span>
                    <span v-if="schedule.mode_id == 13">(CRRT)</span>
                    <span v-if="schedule.mode_id == 14">(腹水回输)</span>
                    <span v-if="schedule.mode_id == 21">HD+</span>
                    <span v-if="schedule.mode_id == 24">I-HDF</span>
                    <span v-if="schedule.mode_id == 25">HD高通</span>
                    <span v-if="schedule.mode_id == 26">CVVH</span>
                    <span v-if="schedule.mode_id == 27">CVVHD</span>
                    <span v-if="schedule.mode_id == 28">CVVHDF</span>
                    <span v-if="schedule.mode_id == 29">PE</span>
                  </td>
                 
                  <td
                    :class="{ 'advice_content': advice.parent_id == 0, 'subadvice_content': advice.parent_id > 0, 'td-active':(advice.id == current_advice_id && !group_flag) || (advice.parent_id == current_advice_id && !group_flag) || (advice.id == current_parent_id && !group_flag) }"
                     @click="selectAdviceAction(advice,schedule.new_advice)"
                  >
                   <span>{{ advice.advice_name }}</span>
                    <span v-if="advice.single_dose">
                      单次用量 {{ advice.single_dose
                      }}{{ advice.single_dose_unit }}
                    </span>
                    总量:<span v-if="advice.prescribing_number">
                      {{ advice.prescribing_number
                      }}{{ advice.prescribing_number_unit }}
                    </span>
                    <span>{{ advice.delivery_way }}</span>
                    <span>{{ advice.execution_frequency }}</span>
                  </td>
      
                 
                </tr>
                
              </template>
            </template>
          </template>
        </table>
  
        <!-- his渲染 -->
  
        <table class="table dialysisTable" v-show="show">
          <tr @click="cancelAdviceSelect">
            <th width="40px">姓名</th>
            <th width="40px">操作</th>
            <th width="40px">透析器</th>
            <!-- <th width="50px">类型</th> -->
            <th width="50px">开始时间</th>
            <th width="60px">医嘱内容</th>
            <th width="50px">执行时间</th>
            <th width="80px">执行护士</th>
            <th width="80px">校对护士</th>
            <th width="50px">校对时间</th>
            <th width="60px">开嘱医生</th>
            <th width="50px">开嘱时间</th>
            <th width="50px">收费状态</th>
          </tr>
          <template v-for="(schedules, zone_name, index) in filtedScheduals">
            <tr :key="index">
              <td>{{ zone_name }}</td>
              <!-- <td></td> -->
              <!-- <td></td> -->
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
            </tr>
            <template v-for="schedule in schedules">
              <template v-for="(group, group_index) in schedule.new_advice">
                <!-- @click="clickfunction(schedule)" -->
                <tr
                  v-for="(advice, advice_index) in group.advices"
                  :key="'No' + advice.id + advice_index + group_index"
                  :class="{'row-class-active':current_patient_id == schedule.patient_id && group_flag &&current_advice_id < 0,}">
                  <td
                    v-if="advice_index == 0"
                    :rowspan="group.advices.length"
                    style="color: #409eff"
                    @click="selectGroupAdviceAction(schedule.patient_id,schedule.new_advice)">
                    {{ advice.parent_id == 0 ? schedule.patient.name : "" }}
                    <br />
                    （{{ advice.parent_id == 0 ? schedule.dialysis_no : "" }}）
                    <br />
                    <span v-if="schedule.mode_id == 1">(HD)</span>
                    <span v-if="schedule.mode_id == 2">(HDF)</span>
                    <span v-if="schedule.mode_id == 3">(HD+HP)</span>
                    <span v-if="schedule.mode_id == 4">(HP)</span>
                    <span v-if="schedule.mode_id == 5">(HF)</span>
                    <span v-if="schedule.mode_id == 6">(SCUF)</span>
                    <span v-if="schedule.mode_id == 7">(IUF)</span>
                    <span v-if="schedule.mode_id == 8">(HFHD)</span>
                    <span v-if="schedule.mode_id == 9">(HFHD+HP)</span>
                    <span v-if="schedule.mode_id == 10">(PHF)</span>
                    <span v-if="schedule.mode_id == 11">(HFR)</span>
                    <span v-if="schedule.mode_id == 12">(HDF+HP)</span>
                    <span v-if="schedule.mode_id == 13">(CRRT)</span>
                    <span v-if="schedule.mode_id == 14">(腹水回输)</span>
                    <span v-if="schedule.mode_id == 21">HD+</span>
                    <span v-if="schedule.mode_id == 24">I-HDF</span>
                    <span v-if="schedule.mode_id == 25">HD高通</span>
                    <span v-if="schedule.mode_id == 26">CVVH</span>
                    <span v-if="schedule.mode_id == 27">CVVHD</span>
                    <span v-if="schedule.mode_id == 28">CVVHDF</span>
                    <span v-if="schedule.mode_id == 29">PE</span>
                    <div>
                      <span v-if="schedule.assesmentbefor != null">{{
                        getBloodAccess(
                          schedule.assesmentbefor.blood_access_part_opera_id
                        )
                      }}</span>
                    </div>
                  </td>
                  <td>
                    <el-button size="mini"  v-if="advice_index == 0"
                    :rowspan="group.advices.length" @click="clickfunction(schedule)"
                      >详情</el-button
                    >
                  </td>
                  <td
                    :class="{ 'advice_content': advice.parent_id == 0, 'subadvice_content': advice.parent_id > 0, 'td-active':current_patient_id == group.patient_id && group_flag &&current_advice_id < 0 }"
                    v-if="advice_index == 0"
                    :rowspan="group.advices.length">
                   
                    <span
                      v-if="$store.getters.user.template_info.org_id != 10340">
                      {{
                        advice.parent_id == 0 && schedule.prescription != null
                          ? schedule.prescription.dialyzer_perfusion_apparatus
                          : ""
                      }}
                      {{
                        advice.parent_id == 0 && schedule.prescription != null
                          ? schedule.prescription.dialysis_dialyszers
                          : ""
                      }}
                    </span>
                    <span
                      v-if="$store.getters.user.template_info.org_id == 10340"
                    >
                      {{
                        advice.parent_id == 0 && schedule.dialysis_order != null
                          ? schedule.dialysis_order.dialysis_dialyszers
                          : ""
                      }}
                    </span>
                    <span
                      v-if="
                        schedule.prescription != null &&
                        $store.getters.user.template_info.org_id != 10340
                      "
                    >
                      <span v-if="schedule.prescription.dialysis_irrigation != ''"
                        >/
                        {{
                          advice.parent_id == 0 && schedule.prescription != null
                            ? schedule.prescription.dialysis_irrigation
                            : ""
                        }}</span
                      >
                    </span>
                    <span
                      v-if="
                        schedule.dialysis_order != null &&
                        $store.getters.user.template_info.org_id == 10340
                      "
                    >
                      <span
                        v-if="schedule.dialysis_order.dialysis_irrigation != ''"
                        >/
                        {{
                          advice.parent_id == 0 && schedule.dialysis_order != null
                            ? schedule.dialysis_order.dialysis_irrigation
                            : ""
                        }}</span
                      >
                    </span>
                  </td>
                 
                  <td :class="{'td-active':(advice.id == current_advice_id && !group_flag) || (advice.parent_id == current_advice_id && !group_flag) || (advice.id == current_parent_id && !group_flag) }">
                    {{parseTime(advice.start_time, "{h}:{i}")}}
                  </td>
                  <td
                    :class="{ 'advice_content': advice.parent_id == 0, 'subadvice_content': advice.parent_id > 0, 'td-active':(advice.id == current_advice_id && !group_flag) || (advice.parent_id == current_advice_id && !group_flag) || (advice.id == current_parent_id && !group_flag) }"
                     @click="selectAdviceAction(advice,schedule.new_advice)"
                  >
                  <!-- <span>{{advice.is_selected}}</span> -->
                    <span>{{ advice.advice_name }}</span>
                    <span
                      >{{ advice.advice_desc }}{{ advice.drug_spec_unit }}</span
                    >
                    <span v-if="advice.prescribing_number">
                      {{ advice.prescribing_number
                      }}{{ advice.prescribing_number_unit }}
                    </span>
                    <span v-if="advice.single_dose">
                      单次用量 {{ advice.single_dose
                      }}{{ advice.single_dose_unit }}
                    </span>
                    <span>{{ advice.delivery_way }}</span>
                    <span>{{ advice.execution_frequency }}</span>
                    <span v-if="advice.parent_id == 0 && advice.remark.length > 0"
                      >({{ advice.remark }})</span
                    >
                  </td>
                  <td>{{ parseTime(advice.execution_time, "{h}:{i}") }}</td>
                  <td>{{ getName(advice.execution_staff) }}</td>
                  <td>{{ getName(advice.checker) }}</td>
                  <td>{{ parseTime(advice.check_time, "{h}:{i}") }}</td>
                  <td>{{ getName(advice.advice_doctor) }}</td>
                  <td>{{ parseTime(advice.created_time, "{h}:{i}") }}</td>
                  <td>
                    <span v-if="advice.is_settle == 1">已收费</span>
                    <span v-if="advice.is_settle == 2 || advice.is_settle == 0">未收费</span>
                  </td>
                </tr>
                <tr>
                  <td
                    style="background:#409eff !important;padding:1px 0 !important;"
                  ></td>
                  <td
                    style="background:#409eff !important;padding:1px 0 !important;"
                  ></td>
                  <td
                    style="background:#409eff !important;padding:1px 0 !important;"
                  ></td>
                  <td
                    style="background:#409eff !important;padding:1px 0 !important;"
                  ></td>
                  <td
                    style="background:#409eff !important;padding:1px 0 !important;"
                  ></td>
                  <td
                    style="background:#409eff !important;padding:1px 0 !important;"
                  ></td>
                  <td
                    style="background:#409eff !important;padding:1px 0 !important;"
                  ></td>
                  <td
                    style="background:#409eff !important;padding:1px 0 !important;"
                  ></td>
                  <td
                    style="background:#409eff !important;padding:1px 0 !important;"
                  ></td>
                  <td
                    style="background:#409eff !important;padding:1px 0 !important;"
                  ></td>
                   <td
                    style="background:#409eff !important;padding:1px 0 !important;"
                  ></td>
                </tr>
              </template>
            </template>
          </template>
        </table>

        <img class="statisticsImg" @click="toClick" src="@/assets/images/statistics.png" />
  
        <div class="NoData" v-show="zones.length <= 1">
          <img
            style="margin-top: 50px; margin-bottom: 50px"
            src="@/assets/login/data.jpg"
            alt
          />
        </div>
      </div>
  
      <mt-datetime-picker
        v-model="selected_date"
        type="date"
        ref="picker"
        year-format="{value} "
        month-format="{value} "
        date-format="{value} "
        @confirm="requestSchedualDoctors"
      ></mt-datetime-picker>
  
     <mt-datetime-picker
          class="datetimePicker"
          ref="exec_time"
          type="datetime"
          @confirm="handleExecTime"
          v-model="nowExecTime"
     ></mt-datetime-picker>
    </div>
  </template>
  
  <script>
  import SideBar from '@/pages/layout/SideBar'
  import { parseTime } from '@/utils'
  import {jsGetAge} from '@/utils/tools'
  import { getSchedualDoctors, excetionMobileAdvice, settleMobileAdvice, checkMobileAdvice } from '@/api/advice'
  import { getDataConfig } from '@/utils/data'
  import { Toast} from 'vant'
  export default {
    name: 'DialysisAdviceTable',
    components: {
      SideBar
    },
    data () {
      return {
        selected_date: new Date(),
        admin_user: [],
        current_group_index: -1,
        current_select_object: -1,
        current_advice_id: -1,
        current_parent_id: -1,
        group_flag: false,
        current_patient_id: -1,
        show_sch_type_selector: false,
        schedule_type_selected: 0,
        schedule_types: [
          { value: 0, text: '全部班次' },
          { value: 1, text: '上午' },
          { value: 2, text: '下午' },
          { value: 3, text: '晚上' }
        ],
  
        show_zone_selector: false,
        zone_selected: 0,
        show_patient_type_selector: false,
        zones: [{ value: 0, text: '全部分区', select: true }],
        currentAdvices: [],
        show_advice_type_selector: false,
        show_sort_selector: false,
        advice_type_selected: 0,
        sort_type_selected: 0,
        advice_types: [
          { value: 0, text: '全部医嘱' },
          { value: 1, text: '长期医嘱' },
          { value: 3, text: '临时医嘱' }
        ],
        sort_types: [
          {value: 0, text: '全部排序'},
          {value: 1, text: '按床位排序'},
          {value: 2, text: '按签到时间排序'},
          {value:3,text:'按透析号排序'}
        ],
  
        patient_types: [
          { value: 0, text: '全部病人' },
          { value: 1, text: '我的病人' },
          { value: 2, text: '未执行病人'},
          { value: 3, text: '已收费' },
          { value: 4, text: '未收费' }
  
        ],
        patient_selected: 0,
  
        scheduleMap: {},
        show: false,
        showOne: true,
        delivery_way: '',
        nowExecTime: new Date()
      }
    },
    computed: {
      selected_date_str: function () {
        return parseTime(this.selected_date, '{y}-{m}-{d}')
      },
      filtedScheduals: function () {
        var scheduleMap = new Object()
        if (this.zone_selected == 0) {
          for (const key in this.scheduleMap) {
            scheduleMap[key] = this.scheduleMap[key]
          }
        } else {
          var zone_name = this.zones[this.zone_selected].text
          scheduleMap[zone_name] = this.scheduleMap[zone_name]
        }
  
        if (this.schedule_type_selected != 0) {
          var _scheduleMap = {}
          for (const key in scheduleMap) {
            var origin_schedules = scheduleMap[key]
            var schedules = []
            for (let index = 0; index < origin_schedules.length; index++) {
              const schedule = origin_schedules[index]
  
              if (schedule.schedule_type == this.schedule_type_selected) {
                schedules.push(schedule)
              }
            }
            if (schedules.length > 0) {
              _scheduleMap[key] = schedules
            }
          }
  
          scheduleMap = _scheduleMap
        }
        for (var key in scheduleMap) {
          let mapArr = scheduleMap[key]
          for (let i = 0; i < mapArr.length; i++) {
            mapArr[i]['new_advice'] = []
          }
        }
  
        for (var key in scheduleMap) {
          let mapArr = scheduleMap[key]
          for (let i = 0; i < mapArr.length; i++) {
            var maps = mapArr[i]
            var resp_advices = maps.doctor_advice
            if (resp_advices.length > 0) {
              var newGroupObject = function () {
                return Object.assign(
                  {},
                  {
                    group_no: 0,
                    advices: []
                  }
                )
              }
              var initGroupBlock = function (group, advice) {
                group.group_no = advice.groupno
              }
  
              var advice_groups = []
              var group = newGroupObject()
              for (let index = 0; index < resp_advices.length; index++) {
                const advice = resp_advices[index]
                if (advice.groupno == 0) {
                  // 老版本的医嘱
                  if (advice.parent_id > 0) {
                    if (advice_groups.length > 0) {
                      var parent_group = advice_groups[advice_groups.length - 1]
                      if (parent_group.advices.length > 0) {
                        if (parent_group.advices[0].id == advice.parent_id) {
                          parent_group.advices.push(advice)
                        }
                      }
                    }
                    continue
                  } else {
                    if (group.group_no > 0) {
                      advice_groups.push(group)
                      group = newGroupObject()
                    }
  
                    initGroupBlock(group, advice)
                    group.advices.push(advice)
                    advice_groups.push(group)
                    group = newGroupObject()
                    continue
                  }
                } else {
                  // if (group.group_no > 0 && group.group_no != advice.groupno) {
                  //   advice_groups.push(group)
                  //   group = newGroupObject()
                  // }
                  if (group.group_no == 0) {
                    initGroupBlock(group, advice)
                  }
                  // if (group.group_no == advice.groupno) {
                  group.advices.push(advice)
                  // }
                }
              }
              if (group.group_no > 0) {
                // 上述的算法会导致最后一组没有加到advice_groups，这里要手动加
                advice_groups.push(group)
              }
              advice_groups = advice_groups
            } else {
              advice_groups = []
            }
            maps.new_advice = advice_groups
          }
        }
        // console.log('2222类别22222222', scheduleMap)
        return scheduleMap
      }
    },
    created () {
      if (sessionStorage.getItem('advicePaitentType') != null) {
        this.patient_selected = Number(
          sessionStorage.getItem('advicePaitentType')
        )
      }
  
      if (sessionStorage.getItem('adviceTimeType') != null) {
        this.schedule_type_selected = Number(
          sessionStorage.getItem('adviceTimeType')
        )
      }
      this.requestSchedualDoctors()
     
    },
    mounted: function () {
      this.addEventListener()
    },
    methods: {
      selectGroupAdviceAction: function (patient_id, advice) {
        if (this.group_flag && this.current_patient_id == patient_id) {
          this.group_flag = false
          this.currentAdvices = []
          this.currentSubAdvices = []
          this.current_advice_id = -1
        } else {
          this.group_flag = true
          this.currentAdvices = []
          this.currentSubAdvices = []
          this.current_advice_id = -1
          this.currentAdvices = []
  
          for (let i = 0; i < advice.length; i++) {
            for (let y = 0; y < advice[i].advices.length; y++) {
              var advices = advice[i].advices[y]
              if (advices.patient_id == patient_id) {
                advices.is_selected = 1
                this.currentAdvices.push(advices)
              }
            }
          }
          this.current_select_object = 2 // 单组医嘱
          // this.current_group_index = group_index
          // this.current_advice_index = advice_index
          this.current_advice = advice
          this.current_patient_id = patient_id
          // console.log('数据23323323223我的', this.currentAdvices)
        }
      },
      cancelAdviceSelect: function () {
        this.current_group_index = -1
        this.current_advice_index = -1
        this.current_advice = null
        this.currentAdvices = []
        this.currentSubAdvices = []
      },
      selectAdviceAction: function (advice, group) {
        this.group_flag = false
        if (this.current_advice_id > 0 && this.current_advice_id == advice.id) {
          this.current_advice_id = -1
          this.current_parent_id = -1
          this.currentAdvices = []
        } else {
          this.current_advice_id = advice.id
          this.current_parent_id = advice.parent_id
          this.currentAdvices = []
          this.currentAdvices.push(advice)
  
          for (let i = 0; i < group.length; i++) {
            // 设置选择状态
            for (let y = 0; y < group[i].advices.length; y++) {
              var advices = group[i].advices[y]
              if (advices.id == advice.parent_id) {
                this.currentAdvices.push(advices)
              }
              if (advices.parent_id == advice.id) {
                this.currentAdvices.push(advices)
              }
            }
          }
  
          this.current_advice = advice
          // console.log(this.currentAdvices)
        }
      },
      addEventListener () {
        window.addEventListener('scroll', this.handleScroll, true)
      },
      removeEventListener () {
        window.removeEventListener('scroll', this.handleScroll, true)
      },
      handleScroll () {
        this.$nextTick(() => {
          if (this.$refs.adviceScrollTop) {
            let adviceScrollTop = this.$refs.adviceScrollTop.scrollTop
            if (adviceScrollTop > 0) {
              this.$store.dispatch('SetAdviceScrollTop', adviceScrollTop)
            }
          }
        })
      },
    
      clickfunction: function (val) {
        this.$router.push({
          path: '/details',
          query: {
            patient_id: val.patient.id,
            date: this.selected_date.getTime() / 1000,
            patient_name: val.patient.name
          }
        })
      },
      handlePaitentType: function (index) {
        this.patient_selected = index
        sessionStorage.setItem('advicePaitentType', index)
        this.show_patient_type_selector = false
        this.requestSchedualDoctors()
      },
      openPicker () {
        this.$refs.picker.open()
      },
      getAdaviceType (type, parent_id) {
        if (parent_id == 0) {
          if (type == 1) {
            return '长嘱'
          } else if (type == 2) {
            return '临嘱'
          } else if (type == 3) {
            return '临嘱'
          }
        }
      },
      getName (val) {
        for (let i = 0; i < this.admin_user.length; i++) {
          if (this.admin_user[i].id == val) {
            return this.admin_user[i].name
          }
        }
      },
      handletimeType: function (index) {
        this.schedule_type_selected = index
        sessionStorage.setItem('adviceTimeType', index)
        this.show_sch_type_selector = false
      },
      handleAdviceType: function (index) {
        this.advice_type_selected = index
        this.show_advice_type_selector = false
        this.requestSchedualDoctors()
      },
      handleSortType: function (index) {
        this.sort_type_selected = index
        this.show_sort_selector = false
        this.requestSchedualDoctors()
      },
      handleZoneChange: function (index) {
        this.zone_selected = index
        sessionStorage.setItem('adviceZone', index)
        this.show_zone_selector = false
      },
      parseTime: function (time, layout) {
        if (time == 0) {
          return ''
        }
        return parseTime(time, layout)
      },
      requestSchedualDoctors () {
        getSchedualDoctors({
          date: this.selected_date_str,
          patient_type: this.patient_selected,
          advice_type: 2,
          delivery_way: this.delivery_way
        }).then((rs) => {
          var resp = rs.data
          if (resp.state == 1) {
            this.admin_user = resp.data.adminUser
            var schedules = resp.data.scheduals
            // console.log('数据列表2332323223wo', schedules)
            for (let i = 0; i < schedules.length; i++) {
              schedules[i].sort = schedules[i].device_number.sort
            }
  
            var config = resp.data.config
  
            // console.log('config', config)
            let project_config = resp.data.project_config
            // console.log('project_config', resp.data.project_config)
            if (project_config.is_open == 1) {
              this.project = resp.data.project
              // console.log('项目', this.project)
  
              // this.project =this.project.concat(resp.data.team_project)
  
              resp.data.hisAdvices.map((item, index) => {
                item.new_advice = item.new_advice ? item.new_advice : []
  
                if (
                  this.project[index].project != null &&
                  this.project[index].project.length > 0
                ) {
                  this.project[index].project.map((it) => {
                    let obj = {
                      advice_doctor: it.doctor,
                      start_time: it.start_time,
                      advice_name:
                        it.team_id > 0 && it.is_check_team == 1
                          ? it.team.project_team
                          : it.type == 2
                            ? it.project.project_name
                            : it.good_info.good_name,
                      execution_time: it.execution_time,
                      execution_state: it.execution_state,
                      execution_staff: it.execution_staff,
                      check_time: it.check_time,
                      check_state: it.check_state,
                      checker: it.checker,
                      created_time: it.ctime,
                      remark: '',
                      id: it.id,
                      parent_id: 0,
                      groupno: 0,
                      count: it.count
                    }
  
                    item.doctor_advice.push(obj)
                  })
                }
              })
            }
  
            if (config.is_open == 0 || config.is_open == 2) {
              this.showOne = true
              this.show = false
              for (let i = 0; i < schedules.length; i++) {
                schedules[i].dialysis_no = schedules[i].patient.dialysis_no
              }
              // console.log('schedules', schedules)
  
              var arr = []
              for (let j = 0; j < schedules.length; j++) {
                arr.push(schedules[j].dialysis_no)
              }
              arr.sort(function (a, b) {
                return a - b
              })
  
              var arrTwo = []
              for (let i = 0; i < arr.length; i++) {
                for (let j = 0; j < schedules.length; j++) {
                  if (arr[i] == schedules[j].dialysis_no) {
                    arrTwo.push(schedules[j])
                  }
                }
              }
              arrTwo.map((item, index) => {
                item.doctor_advice.sort(this.compare('start_time'))
              })
  
              const res = new Map()
              let a = arrTwo.filter(
                (a) => !res.has(a.dialysis_no) && res.set(a.dialysis_no, 1)
              )
              // 针对 监利大垸医院(按床位号排序)
              if (
                this.$store.getters.user.template_info.org_id == 9671 ||
                this.$store.getters.user.template_info.org_id == 10101 ||
                this.$store.getters.user.template_info.org_id == 3877 ||
                this.$store.getters.user.template_info.org_id == 9675 ||
                this.$store.getters.user.template_info.org_id == 4 ||
                this.$store.getters.user.template_info.org_id == 10340
              ) {
                a.sort(function (a, b) {
                  return a.sort - b.sort
                })
              }
              schedules = a
  
              if (schedules != null && schedules.length > 0) {
                for (let i = 0; i < schedules.length; i++) {
                  schedules[i].assess_time = schedules[i].dialysisassesmentbefor ? schedules[i].dialysisassesmentbefor.created_time : '3753619200'
                }
              }
              var zoneMap = {}
              var scheduleMap = {}
              if (this.sort_type_selected == 0) {
                for (let index = 0; index < schedules.length; index++) {
                  const schedule = schedules[index]
                  if (schedule.doctor_advice.length == 0) {
                    continue
                  }
                  if (scheduleMap[schedule.device_number.zone.name] == null) {
                    scheduleMap[schedule.device_number.zone.name] = []
                  }
                  scheduleMap[schedule.device_number.zone.name].push(schedule)
                  if (zoneMap[schedule.device_number.zone.name] == null) {
                    zoneMap[schedule.device_number.zone.name] =
                    schedule.device_number.zone
                  }
                }
              }
  
              if (this.sort_type_selected == 1) {
                
                var newOneList = schedules.sort(this.compareList('sort'))
                // console.log('newList', newOneList)
                for (let index = 0; index < newOneList.length; index++) {
                  const schedule = newOneList[index]
                  if (schedule.doctor_advice.length == 0) {
                    continue
                  }
                  if (scheduleMap[schedule.device_number.zone.name] == null) {
                    scheduleMap[schedule.device_number.zone.name] = []
                  }
                  scheduleMap[schedule.device_number.zone.name].push(schedule)
                  if (zoneMap[schedule.device_number.zone.name] == null) {
                    zoneMap[schedule.device_number.zone.name] =
                    schedule.device_number.zone
                  }
                }
              }
  
              if (this.sort_type_selected == 2) {
                var newList = schedules.sort(this.compareList('assess_time'))
                // console.log('newList', newList)
                for (let index = 0; index < newList.length; index++) {
                  const schedule = newList[index]
                  if (schedule.doctor_advice.length == 0) {
                    continue
                  }
                  if (scheduleMap[schedule.device_number.zone.name] == null) {
                    scheduleMap[schedule.device_number.zone.name] = []
                  }
                  scheduleMap[schedule.device_number.zone.name].push(schedule)
                  if (zoneMap[schedule.device_number.zone.name] == null) {
                    zoneMap[schedule.device_number.zone.name] =
                    schedule.device_number.zone
                  }
                }
              }
              //按透析号排序
              if(this.sort_type_selected == 3){
                if(this.$store.getters.user.template_info.org_id == 10447){
                  for(let i=0;i<schedules.length;i++){
                    schedules[i].is_dialysis_no = parseInt(schedules[i].dialysis_no)
                  }
                }
                var newList = schedules.sort(this.compareList('is_dialysis_no'))
                for (let index = 0; index < newList.length; index++) {
                  const schedule = newList[index]
                  if (schedule.doctor_advice.length == 0) {
                    continue
                  }
                  if (scheduleMap[schedule.device_number.zone.name] == null) {
                    scheduleMap[schedule.device_number.zone.name] = []
                  }
                  scheduleMap[schedule.device_number.zone.name].push(schedule)
                  if (zoneMap[schedule.device_number.zone.name] == null) {
                    zoneMap[schedule.device_number.zone.name] =
                    schedule.device_number.zone
                  }
                }
              }
  
              var zones = []
              zones.push({ value: 0, text: '全部分区' })
              for (var zoneName in zoneMap) {
                zones.push({ value: zoneMap[zoneName].id, text: zoneName })
              }
  
              zones = zones.sort(function (a, b) {
                return a.value > b.value
              })
              this.zones = zones

              this.scheduleMap = scheduleMap
            }
  
            if (config.is_open == 1) {
              this.showOne = false
              this.show = true
              var schedules = resp.data.hisAdvices
              for (let i = 0; i < schedules.length; i++) {
                // console.log(schedules[i])
                schedules[i]['dialysis_no'] = schedules[i].patient.dialysis_no
              }
  
              var arr = []
              // 按透析号排序
  
              for (let j = 0; j < schedules.length; j++) {
                arr.push(schedules[j].dialysis_no)
              }
              arr.sort(function (a, b) {
                return a - b
              })
              var arrTwo = []
              for (let i = 0; i < arr.length; i++) {
                for (let j = 0; j < schedules.length; j++) {
                  if (arr[i] == schedules[j].dialysis_no) {
                    arrTwo.push(schedules[j])
                  }
                }
              }
  
              arrTwo.map((item, index) => {
                item.doctor_advice.sort(this.compare('start_time'))
              })
  
              const res = new Map()
              let a = arrTwo.filter(
                (a) => !res.has(a.dialysis_no) && res.set(a.dialysis_no, 1)
              )
  
              schedules = a
  
              if (schedules != null && schedules.length > 0) {
                for (let i = 0; i < schedules.length; i++) {
                  schedules[i].assess_time = schedules[i].dialysisassesmentbefor ? schedules[i].dialysisassesmentbefor.created_time : '3753619200'
                }
              }
              var zoneMap = {}
              var scheduleMap = {}
              if (this.sort_type_selected == 0) {
                for (let index = 0; index < schedules.length; index++) {
                  const schedule = schedules[index]
                  if (schedule.doctor_advice.length == 0) {
                    continue
                  }
                  if (scheduleMap[schedule.device_number.zone.name] == null) {
                    scheduleMap[schedule.device_number.zone.name] = []
                  }
                  scheduleMap[schedule.device_number.zone.name].push(schedule)
                  if (zoneMap[schedule.device_number.zone.name] == null) {
                    zoneMap[schedule.device_number.zone.name] =
                    schedule.device_number.zone
                  }
                }
              }
  
              if (this.sort_type_selected == 1) {
                var newOneList = schedules.sort(this.compareList('sort'))
                for (let index = 0; index < newOneList.length; index++) {
                  const schedule = newOneList[index]
                  if (schedule.doctor_advice.length == 0) {
                    continue
                  }
                  if (scheduleMap[schedule.device_number.zone.name] == null) {
                    scheduleMap[schedule.device_number.zone.name] = []
                  }
                  scheduleMap[schedule.device_number.zone.name].push(schedule)
                  if (zoneMap[schedule.device_number.zone.name] == null) {
                    zoneMap[schedule.device_number.zone.name] =
                    schedule.device_number.zone
                  }
                }
              }
  
              if (this.sort_type_selected == 2) {
                var newList = schedules.sort(this.compareList('assess_time'))
                for (let index = 0; index < newList.length; index++) {
                  const schedule = newList[index]
                  if (schedule.doctor_advice.length == 0) {
                    continue
                  }
                  if (scheduleMap[schedule.device_number.zone.name] == null) {
                    scheduleMap[schedule.device_number.zone.name] = []
                  }
                  scheduleMap[schedule.device_number.zone.name].push(schedule)
                  if (zoneMap[schedule.device_number.zone.name] == null) {
                    zoneMap[schedule.device_number.zone.name] =
                    schedule.device_number.zone
                  }
                }
              }

              var zones = []
              zones.push({ value: 0, text: '全部分区' })
              for (var zoneName in zoneMap) {
                zones.push({ value: zoneMap[zoneName].id, text: zoneName })
              }
  
              zones = zones.sort(function (a, b) {
                return a.value > b.value
              })
              this.zones = zones
  
              this.scheduleMap = scheduleMap
            }
            if (sessionStorage.getItem('adviceZone') != null) {
              this.zone_selected = Number(sessionStorage.getItem('adviceZone'))
            }
            this.$nextTick(() => {
              if (this.$refs.adviceScrollTop) {
                this.$refs.adviceScrollTop.scrollTop =
                  this.$store.getters.adviceScrollTop
              }
            })
          } else {
            this.$toast({
              message: resp.msg
            })
          }
        })
      },
      adviceDesc (advice) {},
      compare (property) {
        return function (a, b) {
          var value1 = a[property]
          var value2 = b[property]
          return value1 - value2
        }
      },
  
      getBloodAccess (id) {
        var blood_access = getDataConfig('hemodialysis', 'vascular_access_desc')
        // console.log('血管通路', blood_access)
        var name = ''
        for (let i = 0; i < blood_access.length; i++) {
          if (id == blood_access[i].id) {
            name = blood_access[i].name
          }
        }
        return name
      },
      handleTime (time) {
        var year = time.getFullYear()
        var month = time.getMonth() + 1
        var day = time.getDate()
        var hour = time.getHours()
        var minute = time.getMinutes()
        if (month < 10) {
          month = '0' + month
        }
        if (day < 10) {
          day = '0' + day
        }
        if (hour < 10) {
          hour = '0' + hour
        }
        if (minute < 10) {
          minute = '0' + minute
        }
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':00'
      },
      toExcetionDoctor () {
        if (this.currentAdvices.length == 0) {
          this.$toast('请选择需要执行的医嘱!')
        }
        this.$refs.exec_time.open()
      },
      handleExecTime () {
        var ids = []
        if (this.currentAdvices != null && this.currentAdvices.length > 0) {
          for (let i = 0; i < this.currentAdvices.length; i++) {
            if (this.currentAdvices[i].execution_state == 1) {
              Toast.fail('所选医嘱已执行')
              return false
            }
            if (this.currentAdvices[i].checker == this.$store.getters.user.admin.id) {
              Toast.fail('执行与核对不能是同一人')
              return false
            }
            ids.push(this.currentAdvices[i].id)
          }
        }
  
        var params = {
          ids: ids.join(','),
          exce_time: this.handleTime(this.nowExecTime)
        }
        excetionMobileAdvice(params).then(response => {
          if (response.data.state == 1) {
            if (response.data.data.msg == 1) {
              var ids = response.data.data.ids
              if (ids != null && ids.length > 0) {
                var arr = ids.split(',')
                for (let i = 0; i < this.currentAdvices.length; i++) {
                  for (let j = 0; j < arr.length; j++) {
                    if (this.currentAdvices[i].id == arr[j]) {
                      this.currentAdvices[i].execution_staff = this.$store.getters.user.admin.id
                    }
                  }
                }
              }
  
              Toast.success('执行成功!')
              this.requestSchedualDoctors()
            }
            if (response.data.data.msg == 2) {
              var drug = response.data.data.drug
              var str = ''
  
              str = drug.dose + drug.dose_unit + '*' + drug.min_number + drug.min_unit + '/' + drug.max_unit
  
              Toast.err(drug.drug_name + str + '库存不足!')
            }
          }
        })
      },
      toCheck () {
        var ids = []
        if (this.currentAdvices != null && this.currentAdvices.length > 0) {
          for (let i = 0; i < this.currentAdvices.length; i++) {
            if (this.currentAdvices[i].check_state == 1) {
              Toast.fail('所选医嘱已核对')
              return false
            }
            if (this.currentAdvices[i].execution_staff == this.$store.getters.user.admin.id) {
              Toast.fail('核对与执行不能是同一人')
              return false
            }
            ids.push(this.currentAdvices[i].id)
          }
        }
        var params = {
          ids: ids.join(',')
        }
        checkMobileAdvice(params).then(response => {
          if (response.data.state == 1) {
            Toast.success('核对成功!')
            var ids = response.data.data.ids
            if (ids != null && ids.length > 0) {
              var arr = ids.split(',')
              for (let i = 0; i < this.currentAdvices.length; i++) {
                for (let j = 0; j < arr.length; j++) {
                  if (this.currentAdvices[i].id == arr[j]) {
                    this.currentAdvices[i].checker = this.$store.getters.user.admin.id
                  }
                }
              }
            }
            this.requestSchedualDoctors()
          }
        })
      },
      toSettle () {
        if (this.currentAdvices.length == 0) {
          this.$toast('请选择需要收费的医嘱!')
        }
  
        var ids = []
        if (this.currentAdvices != null && this.currentAdvices.length > 0) {
          for (let i = 0; i < this.currentAdvices.length; i++) {
            ids.push(this.currentAdvices[i].id)
          }
        }
  
        var params = {
          ids: ids.join(',')
        }
  
        settleMobileAdvice(params).then(response => {
          if (response.data.state == 1) {
            Toast.success('收费成功!')
            this.requestSchedualDoctors()
          }
        })
      },
  
      compareList: function (k) {
        return function (a, b) {
          var M = a[k]
          var N = b[k]
          return M - N // 从低向高排
          // return N - M;   // 从高向低排
        }
      },
      getAge(id_card_no) {
        var thisLen = id_card_no.length;
        var birth = "";
        if (thisLen == 15) {
        birth = "19" + id_card_no.substr(6, 6);
        } else {
        birth = id_card_no.substr(6, 8);
        }
        birth =
        birth.substr(0, 4) +
        "-" +
        birth.substr(4, 2) +
        "-" +
        birth.substr(6, 2);
        return jsGetAge(birth, "-");
     },
     getDialysisGoods(){

     },
     toClick(){
      //  this.$router.push('/new/dialyisAdviceDialog')
      this.$router.push('/new/dialyisAdviceDialog/one')
     }
    }
  }
  </script>
  
  <style style="stylesheet/scss" lang="scss" scoped>
   .statisticsImg{
    position: fixed;
    bottom: 0.625rem;
    right: 0.2rem;
    width: 1.875rem;
    height: 1.875rem;
  }
  .mainBox {
    height: 100%;
    display: flex;
    flex-direction: column;
  }
  .top {
    .hospital {
      width: 2rem;
    }
    .TopTitle {
      font-size: 0.36rem;
      color: $title-color;
      font-weight: normal;
    }
    padding: 0.3rem 0.3rem;
    @include display-flex;
    @include align-items-center;
    @include text-align;
    @include justify-content-between;
    border-bottom: 1px #e5e5e5 solid;
    .title {
      font-size: 0.3rem;
      font-weight: bold;
      color: $pgh-color;
    }
    .iconfont {
      font-size: 0.4rem;
      color: #a8b3ba;
    }
  }
  .search {
    background: #ebf1f7;
    border-radius: 30px;
    padding: 0 0.3rem;
    height: 0.6rem;
    line-height: 0.6rem;
    color: #a8b3ba;
  
    .iconfont {
      color: #a8b3ba;
      font-size: 0.28rem;
    }
    .searchInput {
      font-size: 0.28rem;
      border: none;
      outline: none;
      background: #ebf1f7;
    }
  }
  .floatLeft {
    padding-left: 0.3rem;
    height: 1rem;
    display: flex;
    align-items: center;
    color: #fff;
    font-size: 0.6rem;
    background: #258ffc;
    .titleName {
      font-size: 0.6rem;
    }
    .icons {
      font-size: 0.8rem;
    }
    .jiantou {
      font-size: 0.8rem;
      color: #fff;
      padding-right: 4rem;
    }
  }
  .choice {
    border-bottom: 1px #e5e5e5 solid;
    width: 100%;
    // position: fixed;
    // top: 86px;
    // right: 0;
    // z-index: 66;
    // left: 0;
    // background: #258ffc;
    background: #fff;
  
    // @media only screen and (max-width: 415px) {
    //   top: 38px !important;
    // }
    // @media only screen and (min-width: 416px) and (max-width: 767px) {
    //   top: 114px !important;
    // }
    // @media only screen and (min-width: 768px) {
    //   top: 143px !important;
    // }
    ul {
      @include display-flex;
      @include align-items-center;
      @include text-align;
      @include justify-content-between;
      width: 94%;
      margin: 0 auto;
      font-size: 0.32rem;
      // color: $pgh-color;
      color: #fff;
      li {
        @include display-flex;
        @include align-items-center;
        @include text-align;
        @include justify-content-between;
        padding: 0.3rem 0;
        font-size: 0.45rem;
        color: #000;
        .iconfont {
          margin: 0 0.1rem;
          color: #a8b3ba;
        }
        .line {
          background: #a8b3ba;
          width: 0.2rem;
          height: 1px;
          margin: 0 0.2rem;
          display: inline-block;
        }
      }
    }
  }
  .bottom_btn{
      width: 100%;
      background: #fff;
      position: fixed;
      bottom: 0;
      display: flex;
      justify-content: center;
      height: 50px;
      align-items: center;
  }
  .DialysisAdvice {
    // padding-top: 40px;
    // background: #fff;
    // min-height: calc(100vh - 2px);
    padding-bottom: 40px;
    height: 100%;
    overflow-y: auto;
    flex: 1;
    // @media only screen and (max-width: 812px) {
    //   padding-top: 70px !important;
    // }
    @media only screen and (min-width: 768px) {
      // padding-top: 60px !important;
      padding-bottom: 70px;
    }
    @media only screen and (min-width: 415px) and (max-width: 767px) {
      // padding-top: 50px;
      padding-bottom: 56px;
    }
    // @media only screen and (min-width: 813px) and (max-width: 1024px) {
    //   padding-top: 118px !important;
    // }
  }
  .table tr .subadvice_content {
      padding-left: 20px !important;
  }
  .td-active {
    background: #badcff !important;
  }
  .row-class-active {
    td {
      background: #badcff;
    }
  }
  
  .advice_content {
    text-align: left;
    padding-left: 5px;
    padding-right: 5px;
    padding: 15px 5px;
    // background: #eff6fc;
  }
  .subadvice_content {
    text-align: left;
    padding-left: 25px;
    padding-right: 5px;
    // background: #fafcfe;
  }
  .dialysisTable {
    @media only screen and (max-width: 450px) {
      width: 800px;
      //
    }
  }
  </style>
  <style lang="scss">
  // .el-table {
  //   margin-top: 64px;
  // }
  .dialysisTable tr th {
    background: #f7f7fa;
    color: #000;
    font-weight: bold;
  }
  .dialysisTable tr td {
    background: #f7f7fa;
  }
  .el-table td {
    padding: 0;
  }
  .el-table th {
    padding: 6px 0;
  }
  </style>
  