<template>
  <el-dialog v-model="dialog" :title="title">
    模板名称：
    <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 :disabled="keZhuRenFlag" :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="999" :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 {needRule} from "@/utils/public";
import {baoCunMuBan, muBanCaoZuo} from "@/api/zhu-yuan-yi-sheng/yi-zhu-lu-ru";
import {muBanMing} from '@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng'
import {ElMessageBox} from "element-plus";
import {stringNotBlank} from "@/utils/blank-utils";

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

const dianJiBaoCun = () => {
  if (flag.value === 1) {
    let data = {
      patternName: patternName.value,
      inputType: inputType.value,
      sortNo: sortNo.value,
      list: list.value,
      deptCode: user.value.deptCode,
      qingZhiTiHuan: stringNotBlank(muBanMing.value.patternName)
    }
    baoCunMuBan(data).then((res) => {
      qingKong()
    }).catch(e => {
      if (e.indexOf('是否替换：【900】') > -1) {
        ElMessageBox.confirm('已存在相同的模板名称，是否覆盖原来的数据。', '提示', {
          type: 'warning'
        }).then(() => {
          let data = {
            patternName: patternName.value,
            inputType: inputType.value,
            sortNo: sortNo.value,
            list: list.value,
            deptCode: user.value.deptCode,
            qingZhiTiHuan: true
          }
          baoCunMuBan(data).then((res) => {
            qingKong()
          })
        }).catch((action) => {
        })
      }
    })
  } else if (flag.value === 2) {
    muBanCaoZuo(patternCode.value, patternName.value, user.value.deptCode, sortNo.value, 1).then((res) => {
      qingKong()
    })
  }
}

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

</script>

<style scoped>

</style>
