<script setup lang="ts">
import { PageSearch, usePageSearch } from "/@/components/PageSearch";
import searchConfig from "./config/search.config";
import contentConfig from "./config/content.config";
import PageAuth from "/@/components/PageAuth";
import { PageModal, usePageModal } from "/@/components/PageModalShell";
import modalConfig from "./config/modal.config";
import { PageContent } from "/@/components/PageContent";
import type { PageContentInstance } from "/@/components/PageContent";
import { Company, Card } from "/@/components/RemoteSelect";
import { projectFormConfig } from "./config/_details";
import { projectFormRules } from "./config/_rules";
import {
  BasicForm,
  transform,
  createDefaultData
} from "/@/components/BasicForm";
import { ElImage } from "element-plus";
import { httpDetail } from "/@/api/operate/setService";
import { ElForm } from "element-plus";
import { ref, unref } from "vue";
import dayjs from "dayjs";
import { useResponseHandle } from "/@/hooks/useAsync";
const responseHandle = useResponseHandle();
// import { httpDetail } from "/@/api/parameter/video";
const pageContentRef = ref<PageContentInstance | null>(null);
const {
  pageModalRef,
  handleUpdateData,
  handleCreateData,
  handlePreviewData,
  handleConfrim,
  defaultInfo
} = usePageModal({
  pageContentRef
});
const { handleResetClick, handleSearchClick } = usePageSearch();
const { formItems } = projectFormConfig;

const basicFormRef = ref<InstanceType<typeof ElForm>>(null);
const formData = ref<Record<string, any>>(createDefaultData(formItems));
function handleCreate() {
  basicFormRef.value.validate(isValid => {
    if (!isValid) return;
    const params = unref(formData);
    const { starttime, endtime, expiretime } = params;
    params.starttime = starttime ? dayjs(starttime).format("YYYY-MM-DD") : "";
    params.endtime = endtime ? dayjs(endtime).format("YYYY-MM-DD") : "";
    params.expiretime = expiretime
      ? dayjs(expiretime).format("YYYY-MM-DD")
      : "";
    if (
      new Date(params.starttime).valueOf() >= new Date(params.endtime).valueOf()
    ) {
      ElImage.warning("活动开始日期不能大于活动结束日期！");
      return;
    }
    handleConfrim("create", params);
  });
}
async function handleDetailData(row, type) {
  const { id } = row;
  const { code, data, message } = await httpDetail({ id: id });
  responseHandle({
    code,
    message,
    handler: () => {
      if (type === "preview") {
        handlePreviewData(data);
      }
      if (type === "update") {
        formData.value = transform(formItems, data, {});
        handleUpdateData(data);
      }
    }
  });
}
</script>

<template>
  <!-- <PageAuth :pageName="pageName"> -->
  <PageSearch
    :form-config="searchConfig"
    @search-btn-click="handleSearchClick"
    @reset-btn-click="handleResetClick"
  />
  <PageContent
    ref="pageContentRef"
    :content-config="contentConfig"
    @create-btn-click="handleCreateData"
    @preview-btn-click="row => handleDetailData(row, 'preview')"
    @update-btn-click="row => handleDetailData(row, 'update')"
  />
  <!--需要优化 上架/下架提示优化-->
  <PageModal
    ref="pageModalRef"
    :modal-config="modalConfig"
    :default-info="defaultInfo"
    @confirm-btn-click="handleConfrim"
  >
    <BasicForm
      ref="basicFormRef"
      v-bind="projectFormConfig"
      :form-data="formData"
      :rules="projectFormRules"
      :disabled="false"
      label-width="120px"
    >
      <template #company_id>
        <Company v-model="formData.company_id" placeholder="公司" />
      </template>
      <template #card_id>
        <Card v-model="formData.card_id" placeholder="卡类型" />
      </template>
      <template #footer>
        <div class="w-full flex justify-end">
          <ElButton type="primary" @click="handleCreate">保存</ElButton>
        </div>
      </template>
    </BasicForm>
  </PageModal>
  <!-- </PageAuth> -->
</template>
