<script setup name='JianChaJianYanTable'>
import {yzHeaderSize} from "@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng";
import {getWindowSize} from "@/utils/window-size";
import {huoQuZhiXinKeShi} from "@/api/zhu-yuan-yi-sheng/yi-zhu-lu-ru";
import XcComboGrid from "@/components/xiao-chan/combo-grid/XcComboGrid.vue";
import {
  biaoBenApi,
  diagnosisInOurHospital,
  viewInspectionItemDetails
} from "@/api/zhu-yuan-yi-sheng/jian-yan-jian-cha-shen-qing";
import XcSelectV3 from "@/components/xiao-chan/select-v3/XcSelectV3.vue";
import {computed} from "vue";
import store from "@/store";

const {data, isCheck, publicData} = defineProps({
  data: {
    type: Array,
    default: []
  },
  isCheck: {
    type: Boolean,
    default: false
  },
  publicData: {
    type: Object,
    default: {}
  }
})

const emits = defineEmits(['delClick'])

const deleteInspectionCheck = (index, code) => {
  delete feeListMap.value[code]
  emits('delClick', index)
}

/* 获取执行科室 */
const zhiXingKeShiData = ref([])
const metZhiXingKeShi = (val) => {
  huoQuZhiXinKeShi(val).then((res) => {
    zhiXingKeShiData.value = res
  })
}

// 标本
const biaoBenShuJu = ref([])
const biaoBenSouSuo = (val) => {
  biaoBenApi(val).then((res) => {
    biaoBenShuJu.value = res
  })
}

// 临床诊断
const linChuangZhenDuanShuJu = ref([])
const linChuangZhenDuanSuoSou = (val) => {
  diagnosisInOurHospital(val).then((res) => {
    linChuangZhenDuanShuJu.value = res
  })
}

const divRef = ref(null)

const tableHeight = computed(() => {
  if (divRef.value != null) {
    return getWindowSize.value.h - (yzHeaderSize.value + 75) - divRef?.value.clientHeight;
  }
  return getWindowSize.value.h - (yzHeaderSize.value + 75)
})

const PATHOLOGY = '038'

const needPublicData = computed(() => {

  if (isCheck) {
    return true
  } else {
    for (let i = 0; i < data.length; i++) {
      let item = data[i];
      if (item.classes === PATHOLOGY) {
        return true
      }
    }
  }

})

const enterPadding = (val) => {
  data.forEach(item => {
    if (item.classes !== PATHOLOGY) {
      item.reqComment = val
    }
  })
}

const feeListMap = ref({})

const getFeeList = (row) => {
  if (feeListMap.value[row.orderCode]) {
    return
  }
  viewInspectionItemDetails(row.orderCode).then((res) => {
    feeListMap.value[row.orderCode] = res
  })
}

const userInfo = computed(() => {
  return store.getters['user/info']
})

const showStartTime = () => {
  return userInfo.value.deptCode === '1160000' || userInfo.value.deptCode === '3100000'
}

onMounted(() => {
  watch(() => data.length, () => {
    if (data.length === 0) {
      feeListMap.value = {}
    }
  })
})

</script>

<template>
  <div ref="divRef">
    <el-row :gutter="5" v-show="needPublicData">
      <el-col :span="12">
        病史摘要:
        <el-input v-model="publicData.reqComment"
                  :rows="2" maxlength="125"
                  show-word-limit
                  type="textarea"/>
      </el-col>

      <el-col :span="12">
        体征信息:
        <el-input v-model="publicData.reqTzComment"
                  :rows="2" maxlength="125"
                  show-word-limit
                  type="textarea"/>
      </el-col>

      <el-col :span="12">
        相关辅检结果:
        <el-input v-model="publicData.reqOtherResult"
                  :rows="2"
                  maxlength="125"
                  show-word-limit
                  type="textarea"/>
      </el-col>

      <el-col :span="12">
        临床诊断：
        <br>
        <xc-select-v3
            v-model="publicData"
            :data="linChuangZhenDuanShuJu"
            :remote-method="linChuangZhenDuanSuoSou"
            code="diagCode"
            name="diagText"/>
      </el-col>

    </el-row>
  </div>

  <el-table :data="data"
            :height="tableHeight">
    <el-table-column label="操作" width="120" fixed="left">
      <template #default="scope">
        <el-button-group>
          <el-button icon="Delete" type="danger"
                     @click="deleteInspectionCheck(scope.$index, scope.row.orderCode)"></el-button>

          <el-popover :width="500"
                      trigger="click"
                      placement="right"
                      title="费用详情">
            <template #reference>
              <el-button @click="getFeeList(scope.row)">
                明细
              </el-button>
            </template>
            <el-table :data="feeListMap[scope.row.orderCode]">
              <el-table-column property="orderCode" label="检查编码"/>
              <el-table-column property="occCode" label="收费编码"/>
              <el-table-column property="name" label="名字"/>
              <el-table-column property="amount" label="数量"/>
              <el-table-column property="chargeAmount" label="金额"/>
              <el-table-column property="unitPrice" label="单价"/>
            </el-table>
          </el-popover>

        </el-button-group>
      </template>
    </el-table-column>
    <el-table-column label="医嘱时间" prop="startTime" width="180" v-if="showStartTime()">
      <template #default="scope">
        <el-date-picker
            style="width: 150px;"
            type="datetime"
            v-model="scope.row.startTime"
            value-format="YYYY-MM-DD HH:mm:ss"
        />
      </template>
    </el-table-column>
    <el-table-column label="项目名称"
                     prop="orderName"
                     show-overflow-tooltip>
    </el-table-column>
    <el-table-column label="执行科室" prop="execDeptName" width="100">
      <template #default="scope">
        <xc-combo-grid
            v-model="scope.row"
            code="execDept"
            name="execDeptName"
            :query-data-func="metZhiXingKeShi"
            :data="zhiXingKeShiData"/>
      </template>
    </el-table-column>
    <el-table-column label="标本"
                     v-if="!isCheck"
                     prop="inspectStuffName" width="80">
      <template #default="scope">
        <xc-combo-grid
            v-model="scope.row"
            :data="biaoBenShuJu"
            :query-data-func="biaoBenSouSuo"
            code="inspectStuff"
            name="inspectStuffName"
        />
      </template>
    </el-table-column>
    <el-table-column label="申请说明"
                     width="220px"
                     v-if="!isCheck">
      <template #default="scope">
        <el-input v-model="scope.row.reqComment"
                  @keydown.enter.prevent="enterPadding(scope.row.reqComment)"
                  :disabled="scope.row.classes === PATHOLOGY"
                  maxlength="125"/>
      </template>
    </el-table-column>
    <el-table-column label="急诊" width="90">
      <template #default="scope">
        <el-switch
            v-model="scope.row.jzFlag"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            active-text="是"
            inactive-color="#ff4949"
            inactive-text="否">
        </el-switch>
      </template>
    </el-table-column>
    <el-table-column label="自费" width="90">
      <template #default="scope">
        <el-switch
            v-model="scope.row.ybSelfFlag"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            active-text="是"
            inactive-color="#ff4949"
            inactive-text="否">
        </el-switch>
      </template>
    </el-table-column>
  </el-table>

</template>

<style scoped lang="scss">

</style>
