<template>
  <div style="display: flex">
    <button @click="queryYz">重置</button>
    <label>
      显示范围：
      <select v-model="queryParam.displayRange">
        <option :value="0">全部</option>
        <option :value="1">停止</option>
        <option :value="2">当前</option>
        <option :value="3">今天</option>
      </select>
    </label>
    <label>
      类型：
      <select v-model="queryParam.frequCode">
        <option :value="0">全部</option>
        <option :value="1">临时</option>
        <option :value="2">长期</option>
      </select>
    </label>
    <label>
      状态：
      <select v-model.number="queryParam.zhuangTai">
        <option :value="0">全部</option>
        <option :value="1">录入</option>
        <option :value="2">确认</option>
        <option :value="3">执行</option>
        <option :value="5">停止</option>
      </select>
    </label>
    <label>
      药房：
      <select v-model="queryParam.groupNo">
        <option value="73">住院</option>
        <option value="71">门诊</option>
      </select>
    </label>

    <button style="margin-left: 5px" @click="props.addYiZhuClick" title="ALT+A">添加</button>
    <button style="margin-left: 5px" @click="props.toDeleteAnOrder">删除</button>
    <button style="margin-left: 5px" @click="batchDeleteOrdersClick">批量删除</button>
    <button style="margin-left: 5px" @click="currentConfirmOrdersClick" v-if="!isCydy()">确认</button>
    <button style="margin-left: 5px" @click="confirmTheDoctorSOrderWithMedicineClick" v-else>确认带药</button>
    <button style="margin-left: 5px" @click="props.clickOnTheOrderTemplate">模板</button>

    <div style="display: flex;">
      <status-color color="#05ff00" label="R:录入"/>
      <status-color color="#0000fb" label="Q:确认"/>
      <status-color color="#ff07f3" label="Z:执行"/>
      <status-color color="red" label="T:停止"/>
      <status-color color="#00ffe0" label="D:作废"/>
    </div>
  </div>

</template>

<script setup name='YzQueryCondition'>
import {
  confirmTheDoctorSOrderWithMedicine,
  deleteMultipleOrders,
  huoQuYiZhuShuJu
} from "@/api/zhu-yuan-yi-sheng/yi-zhu-lu-ru";
import StatusColor from "@/components/zhu-yuan-yi-sheng/yi-zhu-lu-ru/yz-header/StatusColor";
import {
  errorMsg, getYzIndex, isCydy,
  queryParam,
  selectedData,
  yzData
} from "@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng";
import {BizException, ExceptionEnum} from "@/utils/BizException";
import {ElMessageBox} from "element-plus";

const props = defineProps({
  patientInfo: {
    type: Object
  },
  returnData: {
    type: Function
  },
  addYiZhuClick: Function,
  toDeleteAnOrder: Function,
  frequCodeChange: Function,
  confirmOrdersClick: Function,
  clickOnTheOrderTemplate: Function
})

const queryYz = async () => {
  if (!props.patientInfo.inpatientNo) {
    BizException(ExceptionEnum.MESSAGE_ERROR, '请先选择患者')
  }
  queryParam.value.patNo = props.patientInfo.inpatientNo
  queryParam.value.times = props.patientInfo.admissTimes

  try {
    yzData.value = await huoQuYiZhuShuJu(queryParam.value)
  } catch (e) {
    yzData.value = []
  }

}

const currentConfirmOrdersClick = () => {
  if (!props.patientInfo.inpatientNo) {
    BizException(ExceptionEnum.MESSAGE_ERROR, '请先选择患者')
  }
  props.confirmOrdersClick()
}

const confirmTheDoctorSOrderWithMedicineClick = () => {
  if (!props.patientInfo.inpatientNo) {
    BizException(ExceptionEnum.MESSAGE_ERROR, '请先选择患者')
  }
  confirmTheDoctorSOrderWithMedicine(props.patientInfo.inpatientNo, props.patientInfo.admissTimes).then((res) => {
    if (res?.error) {
      errorMsg.value.type = 3
      errorMsg.value.dialog = true
      errorMsg.value.data = res.data
    } else {
      queryYz()
    }
  })
}

/**
 * 点击批量删除数据
 */
const batchDeleteOrdersClick = () => {
  if (selectedData.value.length === 0) {
    BizException(ExceptionEnum.LOGICAL_ERROR, "请先选择要删除的数据");
  }
  ElMessageBox.confirm('是否要批量删除这些医嘱？', '提示', {
    type: 'warning'
  }).then((res) => {
    let param = {
      inpatientNo: props.patientInfo.inpatientNo,
      admissTimes: props.patientInfo.admissTimes,
      list: selectedData.value
    }
    deleteMultipleOrders(param).then((res) => {
      if (res !== null && res.error) {
        let errData = []
        for (const key in res.data) {
          let index = getYzIndex(key)
          let tempYzData = yzData.value[index]
          yzData.value[index].error = true
          errData.push({
            actOrderNo: key,
            orderName: tempYzData.orderName,
            errorMessage: res.data[key]
          })
        }
        errorMsg.value.dialog = true
        errorMsg.value.type = 2
        errorMsg.value.data = errData
      } else {
        queryYz();
      }
    })
  }).catch(() => {

  })
}

watch(() => props.patientInfo, () => {
  queryYz()
})

defineExpose({
  queryYz
})

</script>

<style scoped lang="scss">
.status_color {
  width: 10px;
  height: 10px;
}

</style>
