<template>
  <div>
    <el-form
      ref="formRef"
      :model="form"
      label-width="120px"
      label-position="left"
    >
      <el-form-item
        label="事项主题:"
        :rules="[
          {
            required: true,
            message: '请输入事项主题',
            trigger: 'blur'
          }
        ]"
      >
        <el-input
          v-model="form.name"
          class="w-96"
          placeholder="请输入事项主题"
        />
      </el-form-item>
      <el-form-item label="检查事项:" required>
        <el-select
          v-model="form.node"
          placeholder="检查项目"
          style="width: 115px"
          filterable
          @change="handleSearchrRelatedNode"
        >
          <el-option
            v-for="item in state.nodeList"
            :label="item.properties.name"
            :value="item.id"
            :key="item.id"
          />
        </el-select>
        <el-select
          v-model="form.related"
          placeholder="请选择包含的检查项目"
          style="width: 315px"
          multiple
          filterable
        >
          <el-option
            v-for="item in state.relatedList"
            :label="item.properties.name"
            :value="item.properties.name"
            :key="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="待办日期:" required>
        <el-space wrap>
          <el-select
            v-model="form.frequencyType"
            placeholder="请选择检查周期/时间"
          >
            <el-option label="指定日期" :value="3" />
            <el-option label="每天" :value="0" />
            <el-option label="每周" :value="1" />
            <el-option label="每月" :value="2" />
          </el-select>
          <template v-if="form.frequencyType < 3">
            <!-- <el-date-picker
              value-format="YYYY-MM-DD"
              v-model="time"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            /> -->
          </template>
          <template v-else>
            <TagsDatePicker v-model:list="dayList" placeholder="请选择日期" />
          </template>
          <el-button type="primary" plain @click="handleAdd" class="ml-2"
            >确认添加</el-button
          >
        </el-space>
      </el-form-item>
    </el-form>
    <div>
      <ComponentTableSetItem
        :tableData="state.tableData"
        :config="config"
        @remove="handleRemove"
        date="2020-09-01"
      />
    </div>
  </div>
</template>
<script setup>
import { ref, reactive } from "vue";
import { request } from "@/utils";

import TagsDatePicker from "@/components/TagsDatePicker.vue";
import { ElMessage } from "element-plus";
import ComponentTableSetItem from "./TableSetItem.vue";

const form = reactive({
  name: "",
  node: "", // 检查项目
  related: "",
  date: "",
  frequencyType: 3
});

const config = {
  name: { label: "检查项目", prop: "name" },
  content: { label: "包含的检查内容", prop: "content" },
  duration: { label: "检查周期", prop: "duration", width: "420" },
  timePreview: { label: "检查时间预览", prop: "timePreview" },
  frequency: { label: "完成次数", prop: "frequency" },
  remark: { label: "备注信息", prop: "remark" },
  handle: { label: "操作", prop: "handle" }
};
const time = ref([]);
const dayList = ref([]);
const state = reactive({
  nodeList: [],
  relatedList: [],
  frequencyType: 3,
  tableData: []
  // tableData: []
});

const handleSearchrRelatedNode = id => {
  request
    .get("/platformApi/graphService/open/node/related", {
      params: {
        id: id,
        relationship: "可检测"
      }
    })
    .then(resp => {
      state.relatedList = resp.data;
      form.related = [];
    });
};
const handleAdd = () => {
  if (!form.node || !form.related.length) {
    return ElMessage.error("请选择检查项目");
  }
  const config = {
    name: state.nodeList.find(v => v.id == form.node)?.properties.name,
    content: form.related?.join("、"),
    frequencyType: state.frequencyType,
    duration: state.frequencyType == 3 ? dayList.value.join(",") : "",
    timePreview: "",
    frequency: 1, // 完后次数
    remark: "",
    handle: "",
    type: 0
  };
  state.tableData.push(config);
  form.node = "";
  form.related = [];
  dayList.value = [];
};

const handleRemove = index => {
  state.tableData.splice(index, 1);
};
const getNodeList = () => {
  request
    .get("/platformApi/graphService/open/node/paginate", {
      params: {
        pageSize: 999,
        tag: "医学检查项目"
      }
    })
    .then(resp => {
      state.nodeList = resp.data.list;
    });
};
getNodeList();
</script>
<style lang="scss " scoped></style>
