<template>
  <div style="height: calc(100vh - 126px)">
    <div
      style="
        height: 51px;
        border-bottom: 2px solid #e4e7ed;
        display: flex;
        align-items: center;
        justify-content: space-between;
      "
    >
      <div>
        <span class="descriptions-item-label">截止时间：</span>
        <el-date-picker
          v-model="deadline"
          type="datetime"
          format="YYYY/MM/DD HH:mm:ss"
          value-format="YYYY/MM/DD HH:mm:ss"
          :clearable="false"
          @change="handleGetPrescription"
        />
        <el-radio-group v-model="therapyEnum" class="ml20" @change="handleRadioChange">
          <el-radio :value="undefined">全部</el-radio>
          <el-radio :value="1">长期</el-radio>
          <el-radio :value="2">临时</el-radio>
        </el-radio-group>
        <el-button class="ml20" type="primary" plain @click="handleGetPrescription">
          查询
        </el-button>
      </div>
      <div>
        <span class="descriptions-item-label">全选：</span>
        <el-switch v-model="chooseAll" @change="handelSwicthChange" />
        <el-button
          class="ml20"
          type="primary"
          @click="handleExecute"
          :disabled="props.exeStatus == 6"
        >
          执行选中
        </el-button>
        <el-button
          class="ml20"
          type="primary"
          @click="handleNoExecute"
          :disabled="props.exeStatus == 6"
        >
          不执行
        </el-button>
        <el-button
          class="ml20 mr20"
          type="danger"
          :disabled="props.exeStatus != 6"
          plain
          @click="handleCancel"
        >
          取消执行
        </el-button>
      </div>
    </div>
    <div
      style="padding: 10px; background-color: #eef9fd; height: 100%; overflow-y: auto"
      v-loading="loading"
    >
      <el-collapse
        v-model="activeNames"
        expand-icon-position="left"
        v-if="prescriptionList.length > 0"
        style="
          border-bottom: 0px;
          border-top: 0px;
          border-radius: 0px;
          overflow-y: auto;
          max-height: calc(100vh - 200px);
        "
      >
        <el-collapse-item
          v-for="(item, index) in prescriptionList"
          :key="index"
          :name="item[0].encounterId"
          style="
            border: 2px solid #e4e7ed;
            border-radius: 8px;
            margin-bottom: 10px;
            overflow: hidden;
          "
        >
          <template #title>
            <div style="display: flex; justify-content: space-between; align-items: center">
              <div>
                <span>{{ item[0].bedName + '【' + item[0].busNo + '】' }}</span>
                <span>
                  {{
                    item[0].patientName + ' / ' + item[0].genderEnum_enumText + ' / ' + item[0].age
                  }}
                </span>
                <el-tag style="margin-left: 10px">{{ item[0].contractName }}</el-tag>
                <span style="margin-left: 30px; font-weight: 600">
                  {{ item[0].conditionNames }}
                </span>
              </div>
              <div
                style="
                  display: flex;
                  justify-content: space-between;
                  gap: 20px;
                  align-items: center;
                  margin-right: 20px;
                "
              >
                <div style="display: inline-block; margin-right: 10px">
                  <span class="descriptions-item-label">住院医生：</span>
                  <span>{{ item[0].requesterId_dictText }}</span>
                </div>
                <div style="display: inline-block; margin-right: 10px">
                  <div
                    class="descriptions-item-label"
                    style="height: 20px; line-height: 20px; text-align: center; margin: 0"
                  >
                    预交金额
                  </div>
                  <div
                    style="
                      height: 20px;
                      line-height: 20px;
                      text-align: center;
                      font-size: 15px;
                      color: #ec8c43;
                    "
                  >
                    {{ item[0].balanceAmount?.toFixed(2) }}
                  </div>
                </div>
              </div>
            </div>
          </template>
          <el-table
            :data="item"
            border
            :ref="'tableRef' + index"
            :header-cell-style="{ background: '#eef9fd !important' }"
          >
            <el-table-column type="selection" align="center" width="50" />
            <el-table-column label="类型" align="center" prop="therapyEnum_enumText" width="80">
              <template #default="scope">
                <span :style="scope.row.therapyEnum == '1' ? 'color: #a6745c' : 'color: #3787a5'">
                  {{ scope.row.therapyEnum_enumText }}
                </span>
              </template>
            </el-table-column>
            <el-table-column label="医嘱内容" prop="adviceName">
              <template #default="scope">
                <span>
                  {{ scope.row.adviceName }}
                </span>
                <template v-if="scope.row.adviceTable == 'med_medication_request'">
                  <span>
                    {{ ' 【' + scope.row.volume + '】' }}
                  </span>
                  <span style="color: #447c95 !important">
                    {{
                      '(' +
                      scope.row.methodCode_dictText +
                      ' ' +
                      scope.row.dose +
                      scope.row.doseUnitCode_dictText +
                      ' ' +
                      scope.row.rateCode_dictText +
                      ')'
                    }}
                  </span>
                </template>
              </template>
            </el-table-column>
            <el-table-column label="开始/终止" prop="requestTime" width="200" />
            <el-table-column label="预计执行" prop="times">
              <template #default="scope">
                <div
                  v-for="(item, timeIndex) in scope.row.times"
                  :key="timeIndex"
                  style="padding-bottom: 5px"
                >
                  <span>{{ item }}</span>
                  <el-checkbox-group
                    v-model="scope.row.checkedRates[timeIndex]"
                    style="display: inline-block; margin-left: 15px"
                  >
                    <el-checkbox
                      v-for="(rateItem, rateIndex) in scope.row.rate[item]"
                      :key="rateIndex"
                      :value="rateItem.rate"
                      @change="
                        (value) => handleRateChange(value, item, rateItem.rate, scope.row, rateItem)
                      "
                      border
                      size="small"
                      style="margin-right: 15px"
                    >
                      {{ rateItem.rate }}
                      <template v-if="rateItem.practitionerName">
                        {{ rateItem.practitionerName }}
                      </template>
                    </el-checkbox>
                  </el-checkbox-group>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-collapse-item>
      </el-collapse>
      <el-empty v-else description="点击查询获取患者医嘱信息"></el-empty>
    </div>
  </div>
</template>

<script setup>
import { getPrescriptionList, adviceExecute, adviceCancel, adviceNoExecute } from './api';
import { patientInfoList } from '../store/patient.js';
import { formatDate, formatDateStr } from '@/utils/index';
import { ref, getCurrentInstance } from 'vue';

const activeNames = ref([]);
const prescriptionList = ref([]);
const deadline = ref(formatDateStr(new Date(), 'YYYY-MM-DD') + ' 23:59:59');
const therapyEnum = ref(undefined);
const { proxy } = getCurrentInstance();
const loading = ref(false);
const chooseAll = ref(false);
const props = defineProps({
  exeStatus: {
    type: Number,
    default: 1,
  },
  requestStatus: {
    type: Number,
    default: 1,
  },
});

function handleRadioChange() {
  handleGetPrescription();
}

function handleGetPrescription() {
  if (patientInfoList.value.length > 0) {
    loading.value = true;
    let encounterIds = patientInfoList.value.map((i) => i.encounterId).join(',');
    getPrescriptionList({
      encounterIds: encounterIds,
      pageSize: 10000,
      pageNo: 1,
      therapyEnum: therapyEnum.value,
      exeStatus: props.exeStatus,
      requestStatus: props.requestStatus,
    }).then((res) => {
      // try {
      // 根据encounterId分组
      const groupedPrescriptions = res.data.records.reduce((groups, prescription) => {
        // 获取当前医嘱全部执行频次
        let rate;
        let times;
        if (prescription.therapyEnum == 1) {
          // 长期医嘱 后台返回执行时间点字符串，直接拆分取时间点
          rate = prescription.dayTimes?.split(',');
          // 用截止时间和医嘱签发时间算出全部执行日期
          times = getDateRange(prescription.requestTime, deadline.value);
        } else {
          // 临时医嘱 直接取医嘱签发时间截取执行时间和执行日期
          rate = [formatDateStr(prescription.requestTime, 'HH:mm')];
          times = [formatDateStr(prescription.requestTime, 'MM-DD')];
        }
        prescription.year = formatDateStr(prescription.requestTime, 'YYYY');
        // 初始化执行时间列表
        prescription.executeTimes = [];
        // 初始化取消执行Id列表
        prescription.procedureIds = [];
        // 添加复选框状态管理
        prescription.checkedRates = {};
        // 已执行时间点列表
        let exeTimeList = prescription.exePerformRecordList.map((item) => {
          return formatDateStr(item.occurrenceTime, 'YYYY-MM-DD HH:mm:ss');
        });
        // 不执行时间点列表
        let stopTimeList = prescription.stopPerformRecordList.map((item) => {
          return formatDateStr(item.occurrenceTime, 'YYYY-MM-DD HH:mm:ss');
        });
        // 取消执行时间点列表
        let cancelTimeList = prescription.cancelPerformRecordList.map((item) => {
          return formatDateStr(item.occurrenceTime, 'YYYY-MM-DD HH:mm:ss');
        });
        if (rate) {
          // 拼成日期加全部时间点的形式，显示示例：03-01 [09:00, 10:00, 11:00]
          // 重构后的逻辑
          let validTimes = [];
          let validRates = {};
          let newCheckedRates = {};

          times.forEach((time, timeIndex) => {
            let checkedRatesForTime = [];
            let rateItems = [];
            rate.forEach((rateItem) => {
              let dateStr = prescription.year + '-' + time + ' ' + rateItem + ':00';
              switch (props.exeStatus) {
                case 1: // 待执行tab
                  // 如果该时间点未执行并且不在不执行列表中，则加入待选中列表
                  if (!exeTimeList.includes(dateStr) && !stopTimeList.includes(dateStr)) {
                    rateItems.push({
                      rate: rateItem,
                    });
                    checkedRatesForTime.push(rateItem);
                    prescription.executeTimes.push(dateStr);
                  }
                  break;
                case 6: // 已执行tab
                  let index = exeTimeList.findIndex((item) => {
                    return item == dateStr;
                  });
                  if (index != -1) {
                    // 显示执行人practitionerName
                    rateItems.push({
                      procedureId: prescription.exePerformRecordList[index].procedureId,
                      practitionerName: prescription.exePerformRecordList[index].practitionerName,
                      rate: rateItem,
                    });
                    checkedRatesForTime.push(rateItem);
                  }
                  // 这里把时间点都加到执行时间列表中，后续遍历全部执行者id用
                  prescription.executeTimes.push(dateStr);
                  break;
                case 5: // 不执行tab
                  let index1 = stopTimeList.findIndex((item) => {
                    return item == dateStr;
                  });
                  if (stopTimeList.includes(dateStr)) {
                    // 显示执行人practitionerName
                    rateItems.push({
                      practitionerName: prescription.stopPerformRecordList[index1].practitionerName,
                      rate: rateItem,
                    });
                    checkedRatesForTime.push(rateItem);
                  }
                  break;
                case 9: // 取消执行tab
                  let index2 = cancelTimeList.findIndex((item) => {
                    return item == dateStr;
                  });
                  if (cancelTimeList.includes(dateStr)) {
                    rateItems.push({
                      practitionerName:
                        prescription.cancelPerformRecordList[index2].practitionerName,
                      rate: rateItem,
                    });
                    checkedRatesForTime.push(rateItem);
                  }
                  break;
              }
            });
            validRates[time] = rateItems;

            // 只有当该时间点有待执行的项目时，才保留该时间点
            if (checkedRatesForTime.length > 0) {
              let newIndex = validTimes.length;
              validTimes.push(time);
              newCheckedRates[newIndex] = checkedRatesForTime;
            }
          });

          console.log(validRates, '111');
          // 更新处方对象
          prescription.times = validTimes;
          prescription.rate = validRates;
          prescription.checkedRates = newCheckedRates;
          // 处理已执行记录，拿到全部的执行人id，取消执行时候要传id
          prescription.exePerformRecordList.forEach((item) => {
            if (prescription.executeTimes.includes(item.occurrenceTime)) {
              prescription.procedureIds.push(item.procedureId);
            }
          });
        }
        // 把相同encounterId的医嘱放在同一个数组中
        const encounterId = prescription.encounterId;
        if (!groups[encounterId]) {
          groups[encounterId] = [];
        }
        if (!activeNames.value.includes(encounterId)) {
          activeNames.value.push(encounterId);
        }
        groups[encounterId].push(prescription);
        return groups;
      }, {});

      // 将分组结果转换为数组形式
      prescriptionList.value = Object.values(groupedPrescriptions);
      loading.value = false;
      // } catch {
      //   loading.value = false;
      // }
    });
    chooseAll.value = false;
  } else {
    prescriptionList.value = [];
    // proxy.$message.warning('请选择患者');
  }
}

// 执行
function handleExecute() {
  let list = getSelectRows();
  list = list.map((item) => {
    return {
      requestId: item.requestId,
      accountId: item.accountId,
      adviceTable: item.adviceTable,
      executeTimes: item.executeTimes,
    };
  });
  console.log(list, 'list');
  adviceExecute({ adviceExecuteDetailList: list }).then((res) => {
    if (res.code == 200) {
      handleGetPrescription();
    }
  });
}

// 不执行
function handleNoExecute() {
  let list = getSelectRows();
  list = list.map((item) => {
    return {
      requestId: item.requestId,
      encounterId: item.encounterId,
      patientId: item.patientId,
      adviceTable: item.adviceTable,
      executeTimes: item.executeTimes,
    };
  });
  console.log(list, 'list');
  adviceNoExecute({ adviceExecuteDetailList: list }).then((res) => {
    if (res.code == 200) {
      handleGetPrescription();
    }
  });
}

// 取消执行
function handleCancel() {
  let list = getSelectRows();
  let producerIds = [];
  list.forEach((item) => {
    producerIds.push(
      ...item.procedureIds.map((value) => {
        return {
          procedureId: value,
          therapyEnum: item.therapyEnum,
        };
      })
    );
  });
  adviceCancel({ adviceExecuteDetailList: producerIds }).then((res) => {
    if (res.code == 200) {
      handleGetPrescription();
    }
  });
}

function getSelectRows() {
  // 获取选中的医嘱信息
  let list = [];
  prescriptionList.value.forEach((item, index) => {
    list = [...list, ...proxy.$refs['tableRef' + index][0].getSelectionRows()];
  });
  return list;
}

/**
 * 计算两个日期之间的所有日期（包含起始和结束日期）
 * @param {string|Date} startDate - 开始日期
 * @param {string|Date} endDate - 结束日期
 * @returns {Array<string>} 格式为MM-DD的日期数组
 */
function getDateRange(startDate, endDate) {
  const start = new Date(startDate);
  const end = new Date(endDate);

  // 重置时间部分，只保留日期
  start.setHours(0, 0, 0, 0);
  end.setHours(0, 0, 0, 0);

  const dates = [];
  const current = new Date(start);

  // 循环添加日期直到结束日期
  while (current <= end) {
    // 格式化为MM-DD
    const month = String(current.getMonth() + 1).padStart(2, '0');
    const day = String(current.getDate()).padStart(2, '0');
    dates.push(`${month}-${day}`);

    // 移动到下一天
    current.setDate(current.getDate() + 1);
  }
  return dates;
}

function handleRateChange(value, date, time, row, rateItem) {
  // 拼接当前选中时间
  let tiemStr = row.year + '-' + date + ' ' + time + ':00';
  let index = row.executeTimes.indexOf(tiemStr);
  debugger;
  if (value) {
    row.executeTimes.push(tiemStr);
    row.procedureIds.push(rateItem.producerId);
  } else {
    row.executeTimes.splice(row.executeTimes.indexOf(tiemStr), 1);
    row.procedureIds.splice(row.executeTimes.indexOf(rateItem.producerId), 1);
  }
}

function handelSwicthChange(value) {
  prescriptionList.value.forEach((item, index) => {
    proxy.$refs['tableRef' + index][0].toggleAllSelection();
  });
}

// 处理后端返回的时间集合
function handleTime() {}

defineExpose({
  handleGetPrescription,
});
</script>

<style scoped>
.el-collapse-icon-position-left :deep(.el-collapse-item__header) {
  padding: 10px;
}

:deep(.el-collapse-item__wrap) {
  padding: 10px;
}

/* 表头背景色 */
:deep(.prescription-table .el-table__header th) {
  background-color: #eef9fd !important;
}

:deep(.el-table__row:hover > td) {
  background-color: #eef9fd !important;
}

.item-value {
  color: #606266;
  font-size: 15px;
  font-weight: 500;
}
</style>