<template>
  <div style="height: 10%;display: flex">
    <el-form inline>
      <el-form-item>
        <xc-date-picker style="width: 180px"
                        type="daterange"
                        clearable
                        @change="caoYaoShuJu.currentPage = 1"
                        v-model="dateRange"/>
      </el-form-item>
      <el-form-item>
        <el-switch v-model="shiFouFaYao"
                   :active-value="true"
                   :inactive-value="false"
                   active-color="#13ce66"
                   active-text="已发药"
                   inactive-color="#ff4949"
                   inactive-text="未发药"/>
      </el-form-item>
      <el-form-item>
        <el-button @click="dianJiChaXunYiZhu(0)">查询</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div style="height: 90%">
    <el-dialog v-model="xiangQing.dialog" title="草药医嘱详情">
      <el-table :close-on-click-modal="false"
                :data="xiangQing.data"
                ref="tableRef"
                :height="getWindowSize.h / 1.5"
                highlight-current-row
                stripe>
        <el-table-column label="选择" type="selection"/>
        <el-table-column label="编码" prop="chargeCode"></el-table-column>
        <el-table-column label="名称" prop="chargeCodeName"></el-table-column>
        <el-table-column label="药房" prop="groupNoName"></el-table-column>
        <el-table-column label="数量" prop="quantity"></el-table-column>
        <el-table-column label="单价" prop="unitPrice"></el-table-column>
        <el-table-column label="说明" prop="instruction"></el-table-column>
        <el-table-column label="包装规格" prop="serial">
          <template #default="scope">
            <span v-if="scope.row.serial === '01'">小包装</span>
            <span v-else-if="scope.row.serial === '99'">小包装</span>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <el-button type="danger" @click="xiangQing.dialog = false">关闭</el-button>
        <el-button @click="copyClick" type="success">复制选中</el-button>
        <el-button @click="copyAllClick" type="primary">复制全部</el-button>
      </template>
    </el-dialog>
    <el-table :data="caoYaoShuJu.data"
              @rowClick="caoYaoYiZhuXiangQing"
              :height="getWindowSize.h / 1.1">
      <el-table-column label="名称" prop="orderName"></el-table-column>
      <el-table-column label="开单时间" prop="inputDate" show-overflow-tooltip></el-table-column>
      <el-table-column label="医生" prop="doctorCodeName"></el-table-column>
      <el-table-column label="状态" prop="statusFlag">
        <template #default="scope">
          {{ getHerbsStatusFlag(scope.row.statusFlag) }}
        </template>
      </el-table-column>
      <el-table-column label="处方总额" prop="zongJia"></el-table-column>
      <el-table-column label="剂数" prop="quantity"></el-table-column>
      <el-table-column label="单剂金额" prop="orderJe"></el-table-column>
      <el-table-column label="医嘱号" prop="orderNo"></el-table-column>
      <el-table-column label="执行科室" prop="execDeptName"></el-table-column>
      <el-table-column label="操作" width="90">
        <template #default="scope">
          <el-button-group>
            <el-button type="primary" @click.stop="caoYaoYiZhuXiangQing(scope.row)" icon="View" title="查看详情"/>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        :current-page="caoYaoShuJu.currentPage"
        :page-size="caoYaoShuJu.pageSize"
        :pager-count="7"
        :total="caoYaoShuJu.total"
        layout="total, prev, pager, next"
        @current-change="handleCurrentChange"/>
  </div>
</template>

<script setup name='EmrHerbs'>
import XcDatePicker from "@/components/xiao-chan/date-picker/XcDatePicker.vue";
import {ref} from "vue";
import {getDateRangeFormatDate} from "@/utils/date";
import {huoQuCaoYaoShuJu, huoQuChaoYaoMingXi} from "@/api/zhu-yuan-yi-sheng/cao-yao-yi-zhu";
import {getWindowSize} from "@/utils/window-size";
import {getHerbsStatusFlag} from "@/views/hospitalization/zhu-yuan-yi-sheng/cao-yao-yi-zhu/cao-yao";
import {listIsBlank} from "@/utils/blank-utils";
import {xcMessage} from "@/utils/xiaochan-element-plus";
import {
  emrCopyFunc
} from "@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/emr-init";

const props = defineProps({
  patNo: String,
  times: Number
})

const emits = defineEmits(['close'])

const dateRange = ref([])
const caoYaoShuJu = ref({
  data: [],
  currentPage: 1,
  pageSize: 20,
  total: 0,
})
const shiFouFaYao = ref(true)

const xiangQing = ref({
  dialog: false,
  data: [],
  yizhu: {},
})


const dianJiChaXunYiZhu = (val) => {
  let dateS = getDateRangeFormatDate(dateRange.value)
  let data = {
    startTime: dateS.startTime,
    endTime: dateS.endTime,
    shiFouFaYao: shiFouFaYao.value,
    patNo: props.patNo,
    times: props.times,
    currentPage: caoYaoShuJu.value.currentPage,
    pageSize: caoYaoShuJu.value.pageSize,
    total: val,
  }
  huoQuCaoYaoShuJu(data).then((res) => {
    caoYaoShuJu.value.data = res.records
    if (val === 0) {
      caoYaoShuJu.value.total = res.total
    }
  })
}

const caoYaoYiZhuXiangQing = (row) => {

  huoQuChaoYaoMingXi(row.orderNo).then((res) => {
    xiangQing.value.dialog = true
    xiangQing.value.data = res
    xiangQing.value.yizhu = row
  })
}

const handleCurrentChange = (val) => {
  caoYaoShuJu.value.currentPage = val
  dianJiChaXunYiZhu(0)
}

const tableRef = ref(null)

const copyFunc = (data) => {
  let str = ''
  let v = xiangQing.value.yizhu
  let length = data.length
  data.forEach((item, index) => {
    str += item.chargeCodeName + item.quantity + 'g'
    if (length !== index + 1) {
      str += '  '
    }
  })
  str += '。'
  str += v.cyJssm + '，' + '日' + v.cyMtjs + '剂' + '，分' + v.cyFfcs + '次' + ',' + v.cyFysmName + '。'
  emrCopyFunc(str)
  emits('close')
}

const copyClick = () => {
  let temp = tableRef.value.getSelectionRows()
  if (listIsBlank(temp)) {
    xcMessage.error('请选择数据。')
  } else {
    copyFunc(temp)
  }
}


const copyAllClick = () => {
  copyFunc(xiangQing.value.data)
}

onMounted(() => {
  dianJiChaXunYiZhu(0)
})

</script>

<style scoped lang="scss">

</style>
