<script setup lang="ts">
import contentConfig from "./config/content.config";
import searchConfig from "./config/search.config";
import modalConfig from "./config/modal.config";
import { usePageSearch, usePageModal, type PageHooks } from "/@/hooks/page";
import { useCompany } from "/@/hooks/core/useCompany";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import { ElMessage } from "element-plus";
import dayjs from "dayjs";
import { httpListAll } from "/@/api/reportForm/reportRecord";
import { ref, watchEffect } from "vue";
const PageName = "reportRecord";
const refModalConfig = ref(modalConfig);
const { currentCompany } = useCompany();
const list = ref([]);

async function get_list() {
  const arr = [];
  if (!currentCompany.value.companyNo) {
    list.value = [];
    refModalConfig.value.formItems[0].options = list.value;
    return;
  }
  const { code, data } = await httpListAll({
    companyNo: currentCompany.value.companyNo
  });
  if (code === 0) {
    data.forEach(si => {
      const model = {
        value: si.id,
        label: si.name,
        disabled: String(si.status) == "0"
      };
      arr.push(model);
    });
  }
  list.value = arr;
  refModalConfig.value.formItems[0].options = list.value;
}

const hooks: PageHooks = {
  pageSearchHook: () => usePageSearch(undefined, undefined, searchConfig),
  pageModalHook: pageContentRef =>
    usePageModal({
      pageContentRef,
      confirmCallback: ({ create_timer, type }) => {
        const start = dayjs(create_timer[0]).format("YYYY-MM-DD");
        const end = dayjs(create_timer[1]).format("YYYY-MM-DD");
        const delay = 24 * 60 * 60 * 1000;
        const start_updatetime = new Date(start).valueOf();
        const end_updatetime = new Date(end).valueOf() + delay;
        const day = (end_updatetime - start_updatetime) / delay;
        const companyNo = currentCompany.value.companyNo;
        if (String(type) === "1" && day > 31) {
          ElMessage.warning("立即执行时，时间区间不能超过31天！");
          return false;
        }
        if (String(type) === "2" && day > 62) {
          ElMessage.warning("延时执行时，时间区间不能超过62天！");
          return false;
        }
        if (!companyNo) {
          ElMessage.warning("请选择业务公司！");
          return false;
        }
        return {
          end,
          start,
          companyNo
        };
      }
    })
};
watchEffect(() => {
  if (currentCompany.value.companyNo) {
    get_list();
  }
});
</script>

<template>
  <PageAuth :pageName="PageName">
    <PageContainer
      :hooks="hooks"
      :content-config="contentConfig"
      :search-config="searchConfig"
      :modal-config="refModalConfig"
    >
      <template #content_action="{ status, down_url }">
        <el-link
          :href="down_url"
          v-if="String(status) === '2'"
          target="_blank"
          :underline="false"
        >
          <ElButton
            link
            size="small"
            type="primary"
            :icon="useRenderIcon('download-line')"
          />
        </el-link>
      </template>
    </PageContainer>
  </PageAuth>
</template>
