<template>
  <div>
    <el-dialog
      title="待办事项"
      v-model="dialogVisible"
      fullscreen
      :show-close="false"
    >
      <template #header>
        <el-card border>
          <template #header>
            <div class="flex items-center justify-between">
              <div>
                <ComponentProfileCard
                  :id="currentArchiveId"
                ></ComponentProfileCard>
              </div>
              <div class="text-right">
                <el-button type="danger" plain @click="dialogVisible = false"
                  >关闭</el-button
                >
              </div>
            </div>
          </template>
          <div>
            <div>
              <el-date-picker
                v-model="state.date"
                type="month"
                placeholder="选择月份"
                value-format="YYYY-MM"
                @change="handleMonth"
                class="ml-2"
              />

              <el-text class="ml-2" style="color: #e6a23c" v-if="isEdit"
                >温馨提示：您可以通过点击日历日期进行待办事项查看和管理</el-text
              >
            </div>
            <el-calendar v-model="state.calendarValue">
              <template #header>
                <div></div>
              </template>
              <template #date-cell="{ data }">
                <div @click="handleCheckDate(data)" class="item-card">
                  <p :class="data.isSelected ? 'is-selected' : ''">
                    {{ data.day.split("-").slice(1).join("-") }}
                    {{ data.isSelected ? "✔️" : "" }}
                  </p>
                  <div style="height: calc(100% - 20px)">
                    <el-scrollbar v-if="state.itemDateList.includes(data.day)">
                      <div
                        v-for="(item, index) in state.itemList[data.day]
                          ?.checkItems"
                        :key="index"
                      >
                        <el-text
                          :tag="item.done ? 'del' : 'span'"
                          line-clamp="1"
                          size="small"
                          >{{ index + 1 }}.{{
                            item.type == 2
                              ? `${item.name}回访`
                              : item.type == 0
                              ? `${item.name}`
                              : item.content
                          }}
                        </el-text>
                      </div>
                    </el-scrollbar>
                  </div>
                </div>
              </template>
            </el-calendar>
          </div>
        </el-card>
      </template>
      <div>
        <!-- <el-card class="mb-4" v-if="!isEdit">
          <div>
            <ComponentProfileCard :id="currentArchiveId"></ComponentProfileCard>
          </div>
        </el-card> -->
      </div>
    </el-dialog>
    <ComponentDialogBacklogCheckbox
      v-model:show="state.visibleBacklogHandle"
      :date="state.time"
      :type="props.type"
      @success="getList"
    ></ComponentDialogBacklogCheckbox>
  </div>
</template>
<script lang="ts" setup>
import { request } from "@/utils";
import moment from "moment";
import { ref, computed, defineProps, defineEmits, watch, reactive } from "vue";
import ComponentDialogBacklogCheckbox from "./DialogBacklogCheckbox.vue";
import ComponentProfileCard from "./ProfileCard.vue";
import { useManageProjectStoreHook } from "@/store/modules/manageProject";
const useManageProject = useManageProjectStoreHook();

const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  type: {
    type: String,
    default: "edit"
  }
});
const emits = defineEmits(["update:show"]);
const dialogVisible = computed({
  get() {
    return props.show;
  },
  set(n) {
    emits("update:show", n);
  }
});
const isEdit = computed(() => {
  return props.type == "edit";
});
const state = reactive({
  date: moment().format("YYYY-MM"),
  calendarValue: "",
  visibleBacklogHandle: false,
  time: "",
  itemList: {},
  itemDateList: []
});
const currentArchiveId = computed(() => {
  return useManageProjectStoreHook()?.currentArchiveId;
});
watch(
  () => dialogVisible.value,
  n => {
    if (n) {
      getList();
    } else {
    }
  }
);
const getList = () => {
  request
    .get("/platformApi/dataService/diseaseManage/todoList", {
      params: {
        archiveId: currentArchiveId.value,
        date: state.date
      }
    })
    .then(resp => {
      let itemObj = {};
      resp.data.map(v => {
        itemObj[v.date] = v;
      });

      state.itemList = itemObj;

      state.itemDateList = resp.data
        .filter(v => {
          return v.checkItems;
        })
        .map(v => v.date);
    });
};
const handleMonth = date => {
  state.calendarValue = date;
  getList();
};
const handleCheckDate = date => {
  state.time = date.day;
  state.visibleBacklogHandle = true;
};
</script>
<style scoped lang="scss">
::v-deep .el-calendar__button-group {
  display: none;
}
::v-deep .el-calendar-table .el-calendar-day {
  height: 170px;
}
.item-card {
  height: 100%;
}
</style>
