<template>
  <div class="p-4" style="height: 80vh">
    <el-card shadow="never">
      <div class="text-center" v-if="!state.list.length">
        <el-empty />
        <el-button type="primary" plain class="mb-10" @click="handleAdd"
          >添加管理计划</el-button
        >
      </div>
      <div v-else>
        <HealthManagementPlanDetail
          :list="state?.list"
          @onChangeStatus="onChangeStatus"
          @onEdit="onEdit"
          @onEvaluate="toDetail"
          @onDelete="onDelete"
        />
      </div>
    </el-card>
    <ComponentDialogSelectIllness
      v-model:show="state.visible"
      nodeTag="功能医学管理"
      :confirmFn="
        (diseaseId, name) =>
          router.push({
            name: 'healthPlanDetail',
            query: {
              archivesId: route.query.archivesId,
              name,
              diseaseId
            }
          })
      "
    />
    <el-dialog v-model="updateNextTimeVisible" title="修改下次反馈设定">
      <el-form label-position="top">
        <el-form-item label="下次评估时间：" required>
          <el-date-picker
            v-model="updateNextTime.nextTime"
            type="date"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            placeholder="请选择评估时间"
          />
        </el-form-item>
        <el-form-item label="下次评估时期望达成效果：" required>
          <Editor v-model:value="updateNextTime.target" />
        </el-form-item>
      </el-form>
      <el-form-item>
        <el-button type="primary" @click="saveUpdateNextTime"
          >保存并提交</el-button
        >
      </el-form-item>
    </el-dialog>
  </div>
</template>
<script setup>
import Editor from "@/components/Editor.vue";
import { request } from "@/utils";
import { reactive, computed, ref, provide } from "vue";

import { useRouter, useRoute } from "vue-router";
const [route, router] = [useRoute(), useRouter()];

import { useManageProjectStoreHook } from "@/store/modules/manageProject";
const useManageProject = useManageProjectStoreHook();

import ComponentDialogSelectIllness from "./DialogSelectIllness.vue";
import HealthManagementPlanDetail from "./HealthManagementPlanDetail.vue";
import { ElMessage, ElMessageBox } from "element-plus";
const state = reactive({
  visible: false,
  list: []
});

const currentArchiveId = computed(() => {
  return useManageProjectStoreHook()?.currentArchiveId;
});
const getDetail = async () => {
  const { data } = await request.get(
    "/platformApi/dataService/healthManage/detail",
    {
      params: {
        archiveId: currentArchiveId.value
      }
    }
  );
  state.list = data;
};
const handleAdd = () => {
  state.visible = true;
};
const updateNextTimeVisible = ref(false);
const updateNextTime = ref({
  id: "",
  nextTime: "",
  target: ""
});
const saveUpdateNextTime = async () => {
  await request.post(`/dataService/healthManage/update`, updateNextTime.value);
  ElMessage.success("操作成功");
  getDetail();
  updateNextTimeVisible.value = false;
};
const onEdit = index => {
  const current = state.list[index];
  updateNextTime.value = {
    id: current.id,
    nextTime: current.nextTime,
    target: current.target
  };
  updateNextTimeVisible.value = true;
  // router.push({
  //   name: "archivesPlanHealth",
  //   query: {
  //     archivesId: route.query.archivesId,
  //     name: state.list[index]?.disease,
  //     source: state.list[index]?.id,
  //     diseaseId: state.list[index]?.diseaseId
  //   }
  // });
};
const toDetail = async index => {
  // await ElMessageBox.confirm('未到设定的评估时间，是否要忽略设定日期，立即开始评估', '提示')
  router.push({
    name: "healthPlanDetail",
    query: {
      archivesId: route.query.archivesId,
      name: state.list[index]?.disease,
      source: state.list[index]?.source,
      diseaseId: state.list[index]?.diseaseId
    }
  });
};
const onDelete = async index => {
  const { message } = await request.post(
    "/platformApi/dataService/healthManage/delete",
    {
      id: state.list[index]?.id
    }
  );
  ElMessage.success(message);
  getDetail();
};
const onChangeStatus = async row => {
  const { message } = await request.post(
    "/platformApi/dataService/healthManage/update/status",
    {
      id: row.id,
      status: row.status ? 0 : 1
    }
  );
  ElMessage.success(message);
  getDetail();
};
getDetail();

const pptNode = ref();
const getPPTNode = async () => {
  const { data } = await request.get(`/graphService/open/node/paginate`, {
    params: {
      page: 1,
      pageSize: 1,
      pagesize: 1,
      tag: "用户ID",
      name: route.query.archivesId
    }
  });
  if (data.list[0]) {
    pptNode.value = data.list[0];
  }
};
getPPTNode();
provide("pptNode", pptNode);
</script>
