<template>
  <u-sticky class="shadow-default" bgColor="#fff" style="top: 0">
    <u-navbar :titleStyle="{ color: '#000' }" :autoBack="true" :title="pageTitle" :placeholder="true" :safeAreaInsetTop="true" bgColor="#fff">
      <template #left>
        <view class="u-navbar__content__left__item">
          <u-icon name="arrow-left" size="20" color="#000"></u-icon>
        </view>
      </template>
      <template #right>
        <view class="u-navbar__content__right__item">
          <u-icon name="more-dot-fill" size="20" color="#000" @click="moreClick()"></u-icon>
        </view>
      </template>
    </u-navbar>
  </u-sticky>

  <oa-scroll
    customClass="record-container scroll-height"
    :isSticky="false"
    :customStyle="{
      //#ifdef APP-PLUS || MP-WEIXIN
      height: 'calc(100vh - 44px)',
      //#endif
    }"
    :refresherLoad="false"
    :refresherEnabled="false"
    :refresherDefaultStyle="'none'"
    :refresherThreshold="44"
    :lowerThreshold="44"
    :refresherBackground="'#f5f6f7'"
    :data-theme="'theme-' + proxy.$settingStore.themeColor.name"
  >
    <template #default>
      <template1 v-if="parseInt(props.templateId) == 1" :projectList="newProjectList" />
    </template>
  </oa-scroll>

  <u-modal :show="modalShow" title="" :cancelText="'取消'" :zoom="false" :showConfirmButton="false" :showCancelButton="true" @cancel="modalShow = false">
    <view class="slot-content">
      <view @click="handleSubmit('importPrevious')">导入上一篇</view>
    </view>
  </u-modal>
</template>

<script setup>
/*----------------------------------依赖引入-----------------------------------*/
import { onLoad, onShow, onReady, onHide, onLaunch, onUnload, onNavigationBarButtonTap, onPageScroll } from "@dcloudio/uni-app";
import { ref, reactive, computed, getCurrentInstance, toRefs, inject } from "vue";
/*----------------------------------接口引入-----------------------------------*/
import { projectApi } from "@/api/business/project.js";
/*----------------------------------组件引入-----------------------------------*/
import template1 from "./components/template1.vue";
/*----------------------------------store引入-----------------------------------*/
/*----------------------------------公共方法引入-----------------------------------*/
/*----------------------------------公共变量-----------------------------------*/
const { proxy } = getCurrentInstance();
const props = defineProps({
  templateId: {
    type: String,
    default: undefined,
  },
});
var pages = getCurrentPages(); //获取当前页面栈的实例；
/*----------------------------------变量声明-----------------------------------*/
const state = reactive({
  modalShow: false,
  newProjectList: {},
  pageTitle: "",
});
const { modalShow, newProjectList,pageTitle } = toRefs(state);

function moreClick() {
  state.modalShow = true;
}


/**
 * @列表查询
 * @api接口查询
 */
 function selectListApi() {
  projectApi()
    .ProjectsWeekList({
      id: parseInt(props.id),
    })
    .then((requset) => {
      if (requset.data.length > 0) {
        const data = requset.data[1].weekData[0];
        state.newProjectList =data
        console.log(state.newProjectList)
      }
    })
    .catch((err) => {});
}

function handleSubmit(type, id) {
  if (type === "importPrevious") {
    projectApi()
      .ProjectsWeekList({
        startDate: proxy.$time.getCurrentWeekDate().startDate,
        endDate: proxy.$time.getCurrentWeekDate().endDate,
      })
      .then((requset) => {
        var newData = requset.data[0].newData[0];

        state.newProjectList = {
          tomorrowPlan: newData.tomorrowPlan,
          coordinateWork: newData.coordinateWork,
          workContents: newData.workContents,
          sendDingTalk: newData.sendDingTalk,
          ccTo: newData.ccTo,
          imgList: newData.imgList,
        };

        state.modalShow = false;
      });
  }
  if (type === "update") {
    projectApi()
      .ReportRecord({
        reportId: id,
        pageNum: 1,
        pageSize: 10000,
      })
      .then((requset) => {
        var newData = requset.data.records[0];
        state.newProjectList = {
          id: newData.id,
          tomorrowPlan: newData.tomorrowPlan,
          coordinateWork: newData.coordinateWork,
          workContents: newData.workContents,
          sendDingTalk: newData.sendDingTalk,
          ccTo: newData.ccTo,
        };
      });
  }
}

onReady(() => {});

onShow(() => {
  console.log("onShow", pages[pages.length - 1].options);
  if (pages[pages.length - 1]?.options?.templateId && pages[pages.length - 1]?.options?.id) {
    handleSubmit("update", pages[pages.length - 1]?.options?.id);
  }

  //调用系统主题颜色
  proxy.$settingStore.systemThemeColor([1]);
});

onLoad((options) => {
  if ("id" in options) {
    state.pageTitle = "报告编辑";
    selectListApi();
  } else {
    state.pageTitle = "报告填写";
    state.newProjectList={}
  }

});

onUnload(() => {});
</script>
<style lang="scss" scoped>
:deep(.u-modal__content) {
  font-size: 14px;
  justify-content: left;
}
</style>
