<template>
  <el-dialog v-model="dialog" title="编辑检验"
             @close="guanBi">
    <el-pagination :current-page="index"
                   :page-size="1"
                   :total="count"
                   background layout="total,  prev, pager, next"
                   @current-change="bianJiFenYe"/>
    <el-form label-width="100px" size="small" :model="bianJiJianChaShuJu">
      <el-row>
        <el-col :span="12">
          <el-form-item label="检查项目编码:">
            {{ bianJiJianChaShuJu.orderCode }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检查项目名称:">
            {{ bianJiJianChaShuJu.orderName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="bi_tian" label="检查时间:">
            <el-date-picker
                v-model="bianJiJianChaShuJu.startTime"
                ref="checkTheTimeRef"
                format="YYYY-MM-DD HH:mm:ss"
                style="width: 180px"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="bi_tian" label="执行科室:">
            <xc-select-v3
                style="width: 120px"
                v-model="bianJiJianChaShuJu"
                :data="zhiXingKeShiData"
                code="execDept"
                name="execDeptName"
                :remote-method="metZhiXingKeShi"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="急诊:">
            <el-switch
                v-model="bianJiJianChaShuJu.jzFlag"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                active-text="急诊"
                inactive-color="#ff4949"
                inactive-text="普通"
            >
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="自费:">
            <el-switch
                v-model="bianJiJianChaShuJu.ybSelfFlag"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                active-text="急诊"
                inactive-color="#ff4949"
                inactive-text="普通">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col v-if="jianCha" :span="12">
          <el-form-item class="bi_tian" label="临床诊断:" prop="diagCode">
            <xc-select-v3
                v-model="bianJiJianChaShuJu"
                :data="linChuangZhenDuanShuJu"
                :remote-method="linChuangZhenDuanSuoSou"
                code="diagCode"
                name="diagText"
            />
          </el-form-item>
        </el-col>
        <el-col v-if="jianCha" :span="12">
          <el-switch v-model="zhenDuanLaiYuan" :active-value="true" :inactive-value="false" active-color="#13ce66"
                     active-text="医保" inactive-color="#ff4949" inactive-text="本院">
          </el-switch>
        </el-col>
        <el-col :span="24">
          <el-form-item v-if="!jianCha" class="bi_tian" label="标本：">
            <el-select v-model="bianJiJianChaShuJu.inspectStuff"
                       :remote-method="biaoBenSouSuo"
                       clearable filterable
                       remote @change="bianBenMing">
              <el-option v-for="item in biaoBenShuJu" :key="item.code" :label="item.name" :value="item.code">
                <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
                <el-divider direction="vertical"></el-divider>
                <span>{{ item.name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="jianCha ? '病室摘要:' : '申请说明'" class="bi_tian">
            <el-input v-model="bianJiJianChaShuJu.reqComment" :rows="6" maxlength="125" show-word-limit
                      type="textarea"></el-input>
          </el-form-item>
        </el-col>
        <el-col v-if="jianCha" :span="12">
          <el-button @click="daKaiShuoMingMuBan">说明模板</el-button>
        </el-col>
        <el-col v-if="jianCha" :span="24">
          <el-form-item class="bi_tian" label="体征信息:">
            <el-input v-model="bianJiJianChaShuJu.reqTzComment" :rows="6" maxlength="125" show-word-limit
                      type="textarea"></el-input>
          </el-form-item>
        </el-col>
        <el-col v-if="jianCha" :span="24">
          <el-form-item class="bi_tian" label="相关辅检结果:">
            <el-input v-model="bianJiJianChaShuJu.reqOtherResult" :rows="6" maxlength="125" show-word-limit
                      type="textarea"></el-input>
          </el-form-item>
        </el-col>
        <el-col :offset="20" :span="24">
          <el-button icon="Check" type="primary" @click="fillData">确定</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
  <shuo-ming-mu-ban ref="shouMingMuBan" @huoQuXuanZhongShuJu="xuanZhongMuBan"></shuo-ming-mu-ban>
</template>
<!--编辑检验项目信息-->
<script setup name="BianJiJianChaShenQing">
import {computed, ref} from 'vue'
import store from '@/store'
import ShuoMingMuBan from '@/components/zhu-yuan-yi-sheng/jian-cha-shen-qing/ShuoMingMuBan.vue'
import {ElMessage} from 'element-plus'
import {stringIsBlank, stringNotBlank} from '@/utils/blank-utils'
import {biaoBenApi, diagnosisInOurHospital} from '@/api/zhu-yuan-yi-sheng/jian-yan-jian-cha-shen-qing'
import {logoutShortcut, xcHotKey} from '@/utils/xckeydown'
import XcSelectV3 from "@/components/xiao-chan/select-v3/XcSelectV3.vue";
import XcCode from "@/components/xiao-chan/code/XcCode";
import {huoQuZhiXinKeShi} from "@/api/zhu-yuan-yi-sheng/yi-zhu-lu-ru";
import {xuanZhongJianCha} from "@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng";
import {clone} from "@/utils/clone";

const props = defineProps({
  jianCha: {
    type: Boolean,
    default: true,
  },
})

const emit = defineEmits(['fan-ye', 'dialogCloses'])

const fillData = () => {
  let tData = bianJiJianChaShuJu.value
  xuanZhongJianCha.value.data.forEach((item, i) => {
    if (i !== index.value - 1) {
      item.execDept = tData.execDept
      item.execDeptName = tData.execDeptName

      item.jzFlag = tData.jzFlag
      item.ybSelfFlag = tData.ybSelfFlag

      item.diagCode = tData.diagCode
      item.diagText = tData.diagText

      item.inspectStuff = tData.inspectStuff
      item.inspectStuffName = tData.inspectStuffName

      item.reqComment = tData.reqComment
    }
  })
  guanBi()
}
const guanBi = () => {
  biaoBenShuJu.value = []
  linChuangZhenDuanShuJu.value = []
  bianJiJianChaShuJu.value = {}
  dialog.value = false
  logoutShortcut()
  emit('dialogCloses', {bianJiJianChaShuJu})
}

const windowSize = computed(() => {
  return store.state.app.windowSize
})

const dialog = ref(false)
const bianJiJianChaShuJu = ref({})
const shouMingMuBan = ref(null)
const linChuangZhenDuanShuJu = ref([])
const biaoBenShuJu = ref([])
let checkTheTimeRef = $ref(null)

const count = ref(0)
const index = ref(0)


// 诊断来源
const zhenDuanLaiYuan = ref(false)

const daKaiBenDuiHua = async (val, xiaBiao, zhongShu) => {
  dialog.value = true
  index.value = xiaBiao + 1
  count.value = zhongShu
  bianJiJianChaShuJu.value = val

  if (!props.jianCha) {
    if (stringNotBlank(val.inspectStuff)) {
      let index = biaoBenShuJu.value.findIndex(item => {
        return item.code === val.inspectStuff
      })
      if (index === -1) {
        biaoBenShuJu.value.push({code: val.inspectStuff, name: val.inspectStuffName})
      }
    }
  }
}
const daKaiShuoMingMuBan = () => {
  shouMingMuBan.value.dialog = true
  shouMingMuBan.value.huoQuShuoMingMuBan(0)
}

const xuanZhongMuBan = (val) => {
  if (stringIsBlank(bianJiJianChaShuJu.value.reqComment)) {
    bianJiJianChaShuJu.value.reqComment = ''
  }
  if (val.length + bianJiJianChaShuJu.value.reqComment.length > 125) {
    return ElMessage.error('字符超长无法添加，病室摘要最多125个字。')
  }
  bianJiJianChaShuJu.value.reqComment += val + '，'
  ElMessage.success('复制成功。')
}

/**
 * 临床诊断搜索
 * @param val
 */
const linChuangZhenDuanSuoSou = (val) => {
  diagnosisInOurHospital(val).then((res) => {
    linChuangZhenDuanShuJu.value = res
  })
}

const bianJiFenYe = async (val) => {
  biaoBenShuJu.value = []
  linChuangZhenDuanShuJu.value = []
  index.value = val
  emit('fan-ye', {val})
}

const bianBenMing = (val) => {
  for (let item of biaoBenShuJu.value) {
    if (item.code === val) {
      return (bianJiJianChaShuJu.value.inspectStuffName = item.name)
    }
  }
}
const biaoBenSouSuo = (val) => {
  if (val.length > 1) {
    biaoBenApi(val).then((res) => {
      biaoBenShuJu.value = res
    })
  }
}

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

defineExpose({daKaiBenDuiHua})
</script>

<style scoped></style>
