<template>
  <div class="p-4">
    <div class="rounded shadow-lg p-4">
      <el-form label-position="top">
        <el-form-item label="下次评估时间：" required>
          <el-date-picker
            v-model="date"
            type="date"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            placeholder="请选择评估时间"
          />
        </el-form-item>
        <el-form-item label="下次评估时期望达成效果：" required>
          <Editor v-model:value="goals" />
        </el-form-item>
      </el-form>
      <el-form-item>
        <Auth value="healthManage">
          <el-button type="primary" @click="save">保存并提交</el-button>
        </Auth>
      </el-form-item>
    </div>
  </div>
</template>

<script setup>
import Editor from "@/components/Editor.vue";
import { request } from "@/utils";
import { ElMessage } from "element-plus";
import {
  computed,
  watch,
  markRaw,
  ref,
  inject,
  onMounted,
  provide,
  defineAsyncComponent,
  nextTick
} from "vue";
import { useRoute, useRouter } from "vue-router";
const [route, router] = [useRoute(), useRouter()];

const parentState = inject("parentState");
const updateStateValueForKey = inject("updateStateValueForKey");
const date = ref(parentState.value.nextTime || "");
const goals = ref(parentState.value.target || "");
watch(
  () => date.value,
  () => {
    updateStateValueForKey("nextTime", date.value);
  }
);
watch(
  () => goals.value,
  () => {
    updateStateValueForKey("target", goals.value);
  }
);

const save = async () => {
  const req = {
    ...parentState.value
  };
  req.etiologies = req.etiologies.map(v => ({
    id: v.id,
    label: v.label,
    type: v.type
  }));
  console.log(req);
  // if (!req.effectId) {
  //   return ElMessage.error('请选择管理效果')
  // }
  if (!req.nextTime) {
    return ElMessage.error("请选择下次评估时间");
  }
  if (!req.target) {
    return ElMessage.error("请填写下次评估时期望达成效果");
  }
  await request.post(`/dataService/healthManage/create`, req);
  ElMessage.success("操作成功");
  router.back();
};
</script>

<style lang="scss" scoped></style>
