<template>
  <div>
    <el-dialog title="管理级别设置" v-model="dialogVisible">
      <div>
        <el-form ref="formRef" :model="form" label-width="120px">
          <el-form-item label="慢病管理级别：" prop="title">
            <el-text tag="b">{{ form.title }}</el-text>
          </el-form-item>
          <el-form-item
            label="适用人群："
            prop="scope"
            :rules="[
              {
                required: true,
                message: '请输入适用人群',
                trigger: 'blur'
              }
            ]"
          >
            <el-input
              v-model="form.scope"
              placeholder="请输入适用人群"
              clearable
              type="textarea"
              :rows="8"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="介绍信息："
            prop="content"
            :rules="[
              {
                required: true,
                message: '请输入该级别介绍信息',
                trigger: 'blur'
              }
            ]"
          >
            <el-input
              v-model="form.content"
              placeholder="请输入该级别介绍信息"
              clearable
              type="textarea"
              :rows="8"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div>
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit">保 存</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { request } from "@/utils";
import { ElMessage } from "element-plus";
import { ref, computed, defineProps, defineEmits, watch, reactive } from "vue";
import Editor from "@/components/Editor.vue";

const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => {}
  }
});
const emits = defineEmits(["update:show", "success"]);
const dialogVisible = computed({
  get() {
    return props.show;
  },
  set(n) {
    emits("update:show", n);
  }
});

watch(
  () => dialogVisible.value,
  n => {
    if (n) {
      if (props.rowData?.id) {
        form.title = props.rowData.title;
        form.scope = props.rowData.scope;
        form.id = props.rowData.id;
        form.content = props.rowData.content;
      }
    } else {
      formRef.value.resetFields();
      form.id = undefined;
    }
  }
);
const form = reactive({
  title: "",
  scope: "",
  content: "",
  id: undefined
});
const formRef = ref();

const onSubmit = () => {
  formRef.value.validate(valid => {
    if (!valid) return;
    let api = "dataService/schemeManage/diseaseManageLevel/update";
    request.post(api, form).then(resp => {
      ElMessage.success(resp.message);
      dialogVisible.value = false;
      emits("success");
    });
  });
};
</script>
