<template>
  <el-dialog v-model="dialog" center title="保存检查模板" width="30%">
    <el-form ref="baoCunMuBanRef" :model="baoCunMuBan" :rules="baoCunMuBanJiaoYan" label-width="80px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="模板类型" prop="orderType">
            <el-radio-group v-model="baoCunMuBan.orderType">
              <el-radio-button :label="1">个人</el-radio-button>
              <el-radio-button :label="2">科室</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="名称" prop="tcName">
            <el-input v-model="baoCunMuBan.tcName" ref="tcNameRef" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="排序码" prop="sortNo">
            <el-input-number v-model="baoCunMuBan.sortNo" :max="9999" :min="0"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="录入方式" prop="tcFlag">
            <el-radio-group v-model="baoCunMuBan.tcFlag">
              <el-radio-button :label="0">自动选择</el-radio-button>
              <el-radio-button :label="1">手工录入</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-button @click="dianJiBaoCunMuBan(baoCunMuBanRef)">提交</el-button>
          <el-button>关闭</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script setup name='SaveTemplateInspection'>
import {ref} from "vue";
import {baoCunJianChaJianYanMuBan, jianChaJianYanMuBanMing} from "@/api/zhu-yuan-yi-sheng/jian-yan-jian-cha-shen-qing";
import {ElMessageBox} from "element-plus";
import store from "@/store";
import {addCheckList} from "@/views/hospitalization/zhu-yuan-yi-sheng/public-js/zhu-yuan-yi-sheng";

const dialog = ref(false)

const baoCunMuBan = ref({
  tcName: '',
  orderType: 1,
  sortNo: 0,
  tcFlag: 1,
  fuGaiYuanShuJu: false,
  chongFuMing: false,
})

const baoCunMuBanRef = ref(null)

const baoCunMuBanJiaoYan = ref({
  tcName: [
    {required: true, message: '名称不能为空', trigger: 'blur'},
    {min: 1, max: 30, message: '长度在 1 到 30 个字', trigger: 'blur'},
  ],
  orderType: [{required: true, message: '名称不能为空', trigger: 'blur'}],
  sortNo: [{required: true, message: '名称不能为空', trigger: 'blur'}],
  tcFlag: [{required: true, message: '名称不能为空', trigger: 'blur'}],
})

const dianJiBaoCunMuBan = async (form) => {
  if (!form) return
  try {
    await form.validate()
    jianChaJianYanMuBanMing(baoCunMuBan.value.tcName, '3').then((res) => {
      if (res) {
        ElMessageBox.confirm('已经存在相同的模板名称，是否覆盖数据', '温馨提示', {
          type: 'warning',
          distinguishCancelAndClose: true,
          confirmButtonText: '覆盖',
          cancelButtonText: '取消',
        })
            .then(() => {
              faSongBaoCunMuBan()
            })
            .catch((e) => {
            })
      } else {
        faSongBaoCunMuBan()
      }
    })
  } catch (e) {
    console.log(e)
  }
}

const faSongBaoCunMuBan = () => {
  baoCunMuBan.value.reqType = 3
  baoCunMuBan.value.list = addCheckList.value
  baoCunMuBan.value.tcExec = store.state.user.info.deptCode
  baoCunJianChaJianYanMuBan(baoCunMuBan.value).then(() => {
    dialog.value = false
    baoCunMuBan.value = {
      tcName: '',
      orderType: 1,
      sortNo: 0,
      tcFlag: 1,
      fuGaiYuanShuJu: false,
      chongFuMing: false,
    }
  })
}

const openOrClose = (val = true) => {
  dialog.value = val
}
defineExpose({
  openOrClose
})

</script>

<style scoped lang="scss">

</style>
