<template>
  <el-dialog v-model="dialog"
             title="保存模板"
             @closed="emit('update:modelValue',false)">
    模板名称：
    <el-input v-model="patternName" maxlength="50" show-word-limit style="width: 220px"
              @keyup.enter="dianJiBaoCun"></el-input>
    <div v-if="flag === 1">
      <br><br>
      模板类型：
      <el-radio-group v-model="inputType" size="small">
        <el-radio-button :disabled="quanYuanFlag" :label="1">全院</el-radio-button>
        <el-radio-button :label="2">本科室</el-radio-button>
        <el-radio-button :label="3">个人</el-radio-button>
      </el-radio-group>
    </div>
    <br><br> 排序码：
    <el-input-number v-model="sortNo" :max="3000" :min="0"></el-input-number>
    排序码越大，就越靠前。
    <br><br>
    <el-button @click="dianJiBaoCun">保存</el-button>
    <el-button @click="qingKong">关闭</el-button>
  </el-dialog>
</template>

<script name="BaoCunMuBan" setup>
import {computed, ref} from "vue";
import store from '../../../store'
import {noNeedRule} from "@/utils/public";
import {muBanMing} from '@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng'
import {stringNotBlank} from "@/utils/blank-utils";
import {
  doesTheTemplateExist,
  getDoctorSOrderTemplateMaxSortNo,
  saveTemplate
} from "@/api/zhu-yuan-yi-sheng/yi-zhu-lu-ru";
import {ElMessageBox} from "element-plus";

const props = defineProps({
  list: {
    type: Array
  },
  modelValue: {
    type: Boolean
  }
})
const emit = defineEmits(['update:modelValue'])

const user = computed(() => {
  return store.state.user.info
})
const dialog = ref(true)
const title = ref('')
const flag = ref(1)
const quanYuanFlag = noNeedRule(38)
const keZhuRenFlag = noNeedRule(38, 11)
// 模板名称
const patternCode = ref('')
const patternName = ref('')
const inputType = ref(3)
const sortNo = ref(0)
const list = ref([])

const daKaiBaoCunMuBan = (val) => {
  if (stringNotBlank(muBanMing.value.patternName)) {
    patternName.value = muBanMing.value.patternName
    sortNo.value = muBanMing.value.sortNo
    inputType.value = muBanMing.value.inputType
    patternCode.value = muBanMing.value.patternCode
  }
  list.value = val
}

const dianJiBaoCun = () => {
  let data = {
    patternCode: patternCode.value,
    patternName: patternName.value,
    sortNo: sortNo.value,
    list: list.value,
    inputType: inputType.value
  }

  doesTheTemplateExist(patternName.value).then((res) => {
    saveTemplate(data).then(() => {
      qingKong()
      dialog.value = false
    })
  }).catch(() => {
    ElMessageBox.confirm('模板名称重复是否替换?', '提示', {
      type: 'warning'
    }).then(() => {
      saveTemplate(data).then(() => {
        qingKong()
        dialog.value = false
      })
    })
  })
}

const qingKong = () => {
  patternName.value = null
  sortNo.value = 0
  inputType.value = 3
  dialog.value = false
  muBanMing.value = {}
}

onMounted(() => {
  getDoctorSOrderTemplateMaxSortNo().then((res) => {
    sortNo.value = res
  })
  daKaiBaoCunMuBan(props.list)
})

defineExpose({daKaiBaoCunMuBan})
</script>

<style scoped>

</style>
