<script setup lang="ts">
import { usePermission } from "/@/hooks";
import { PageContent } from "/@/components/PageContent";
import { PageSearch, usePageSearch } from "/@/components/PageSearch";
import { useRouter } from "vue-router";
import contentConfig from "./config/content.config";
import searchFormConfig from "./config/search.config";

defineOptions({
  name: "orderRecord"
});

const searchCallback = ({ timer }) => {
  const [startTime, endTime] = timer;

  return {
    result: {
      startTime,
      endTime
    },
    deleteProps: ["timer"]
  };
};

const { pageContentRef, handleSearchClick, handleResetClick } =
  usePageSearch(searchCallback);

const { push } = useRouter();

const handleCreate = () => push("/purchase/orderRecordDetail");

function toDetail(id) {
  push({
    path: "/purchase/orderRecordDetail",
    query: {
      id
    }
  });
}

const { permission, contentConfigRef } = usePermission({
  pageName: "orderRecord",
  contentConfig,
  callback: powers => (contentConfig.powers = powers)
});
</script>

<template>
  <div class="main orderRecord">
    <div v-show="permission.list">
      <PageSearch
        ref="pageContentRef"
        :form-config="searchFormConfig"
        @search-btn-click="handleSearchClick"
        @reset-btn-click="handleResetClick"
      />

      <PageContent
        ref="pageContentRef"
        :content-config="contentConfigRef"
        @preview-btn-click="({ payNo }) => toDetail(payNo)"
      >
        <template #create>
          <el-button
            type="primary"
            v-if="permission.create"
            @click="handleCreate"
            >新建</el-button
          >
        </template>
      </PageContent>
    </div>
    <NoAuth v-show="!permission.list" />
  </div>
</template>
