<script setup lang="ts">
import { ref, shallowRef, watch } from "vue";
import searchConfig from "./config/search.config";
import contentConfig from "./config/content.config";

import { PageContentInstance } from "/@/components/PageContent";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import { usePageSearch, type PageHooks } from "/@/hooks/page";
import { isBeyondTime } from "/@/views/time/_utils";
import { httpRequsetExport } from "/@/utils/export";
import { usePermission } from "/@/hooks/core";
import { useUserStore } from "/@/store/modules/user";
import dayjs from "dayjs"

const pageName = "receivableDetail";
const { hasPermissionWithCode } = usePermission(pageName);
const pageContentRef = ref<PageContentInstance | null>(null);
const userStore = useUserStore();
const loading = shallowRef(false);

const hooks: PageHooks = {
  pageSearchHook: () => usePageSearch((params) => { 
    const { date, ...rest } = params;
    const start = dayjs(date).format('YYYY-MM-DD');
    const daysInMonth = dayjs(date).daysInMonth();
    const end = dayjs(start).subtract(-(daysInMonth - 1), 'days').format('YYYY-MM-DD');

    return {
      result: {
        start: start + " 00:00:00",
        end: end + " 23:59:59",
        plat_type:"1",
        ...rest
      },
      deleteProps:['date']
    }
  },
   (params) => { 
    const { date , depart_id} = params;
    const start = dayjs(date).format('YYYY-MM-DD');
    const daysInMonth = dayjs(date).daysInMonth();
    const end = dayjs(start).subtract(-(daysInMonth - 1), 'days').format('YYYY-MM-DD');

    return {
      result: {
        start: start + " 00:00:00",
        end: end + " 23:59:59",
        plat_type: "1",
        depart_id
      },
      deleteProps:['date']
    }
  }, searchConfig)
};

const searchConfigRef = ref(searchConfig)
watch(
  () => userStore.level,
  level => {
    const { formItems } = searchConfigRef.value;
    const index = formItems.findIndex(({ field }) => field === "depart_id");
    const { itemid = "" } = userStore.info || {};

    if (Number(level) === 2) {
      searchConfigRef.value.formItems[index].disabled = Number(level) === 2;
      searchConfigRef.value.formItems[index].defaultValue = itemid;
    }
  },
  {
    immediate: true
  }
);

const callback = {
  onSuccess: () => (loading.value = false),
  onStart: () => (loading.value = true),
  onFail: () => (loading.value = false)
};

async function handleExportAllReport() {
  const params = pageContentRef.value.getBasicParams() || {};
  const { start, end } = params;

  const beyondTime = isBeyondTime({ start, end, len: 30 });

  console.log(params)

  if (beyondTime) return;
  await httpRequsetExport({
    fileType: "aplication/x-msexecl",
    name: "应收账款明细.xlsx",
    url: "/admin/report/dzListExport",
    ...callback,
    params
  });
}
</script>

<template>
  <page-auth :page-name="pageName" v-loading="loading">
    <page-container
      :hooks="hooks"
      :page-name="pageName"
      :get-content-ref="ref => (pageContentRef = ref)"
      :content-config="contentConfig"
      :search-config="searchConfig"
    >
      <template #content_header>
        <el-button
          v-if="hasPermissionWithCode('9')"
          @click="handleExportAllReport"
          size="small"
          :icon="useRenderIcon('arrow-up-line')"
          type="primary"
          >导出</el-button
        >
      </template>
    </page-container>
  </page-auth>
</template>
