<script setup lang="ts">
import { ref } from "vue";
import { usePermission } from "/@/hooks/usePermission";
import { PageSearch, usePageSearch } from "/@/components/PageSearch";
import { PageContent } from "/@/components/PageContent";
import searchConfig from "./config/search.config";
import contentConfig from "./config/content.config";
import EditModel from "./components/edit-dialog.vue";
import AddModel from "./components/add-dialog.vue";
import ListModalConfig from "./config/ListModal.config";
import ListModal from "/@/components/ListModal";
const pageName = "exchangeStock";
const { hasPermissionWithCode, permissions } = usePermission(pageName);
const modelEditRef = ref<InstanceType<typeof EditModel>>(null);
const modelAddRef = ref<InstanceType<typeof AddModel>>(null);

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

const ListModalRef = ref<InstanceType<typeof ListModal>>(null);

function showLog(id) {
  console.log(id);
  ListModalRef.value.show(id);
}
function handleAddChangeCheck(item: any, id: string, type: string) {
  if (type === "create") {
    modelAddRef.value.show(item, id, type);
  } else {
    modelEditRef.value.show(item, id, type);
  }
}
</script>

<template>
  <PageAuth :pageName="pageName">
    <PageSearch
      :form-config="searchConfig"
      @search-btn-click="handleSearchClick"
      @reset-btn-click="handleResetClick"
    >
      <template #action>
        <el-button
          type="primary"
          @click="handleAddChangeCheck({}, '', 'create')"
          v-if="hasPermissionWithCode('002')"
        >
          新增
        </el-button>
      </template>
    </PageSearch>
    <PageContent
      ref="pageContentRef"
      :powers="permissions"
      :content-config="contentConfig"
      @preview-btn-click="row => handleAddChangeCheck(row, row.id, 'view')"
      @update-btn-click="row => handleAddChangeCheck(row, row.id, 'edit')"
      @log-btn-click="({ id }) => showLog(id)"
    />

    <AddModel ref="modelAddRef" @reload="pageContentRef.onSearch()" />
    <EditModel ref="modelEditRef" @reload="pageContentRef.onSearch()" />
    <!-- <OrderDialog ref="modelOrderRef" @save-btn-click="handleAddOrder" /> -->
    <!-- <addModel ref="actionModalRef" @submitOk="Create" /> -->
    <ListModal ref="ListModalRef" :modalConfig="ListModalConfig" />
  </PageAuth>
</template>
