<template>
  <div class="app-container">
    <!-- 搜索 -->
    <page-search
      ref="searchRef"
      :search-config="searchConfig"
      @query-click="handleQueryClick"
      @reset-click="handleResetClick"
    />
    <!-- 列表 -->
    <page-content
      ref="contentRef"
      :content-config="contentConfig"
      @add-click="handleAddClick"
      @edit-click="handleEditClick"
      @export-click="handleExportClick"
      @search-click="handleSearchClick"
      @toolbar-click="handleToolbarClick"
      @operat-click="handleOperatClick"
      @filter-change="handleFilterChange"
    >
      <template #conUnit="scope">
        <span v-if="scope.row.inOutItem == 'manual-in'">手动入库</span>
        <span v-if="scope.row.inOutItem == 'manual-out'">手动出库</span>
        <span v-if="scope.row.inOutItem == 'win-in'">盘盈入库</span>
        <span v-if="scope.row.inOutItem == 'loss-out'">盘亏出库</span>
        <span v-if="scope.row.inOutItem == 'order-in'">订单退款入库</span>
        <span v-if="scope.row.inOutItem == 'order-out'">订单消费出库</span>
      </template>
    </page-content>

    <!-- 新增 -->
    <page-modal
      ref="addModalRef"
      :modal-config="addModalConfig"
      @submit-click="handleSubmitClick"
    />

    <!-- 编辑 -->
    <page-modal
      ref="editModalRef"
      :modal-config="editModalConfig"
      @submit-click="handleSubmitClick"
    />
  </div>
</template>

<script setup lang="ts">
import VersionApi from "@/api/system/version";
import type { IObject, IOperatData } from "@/components/CURD/types";
import usePage from "@/components/CURD/usePage";
import addModalConfig from "./config/add";
import contentConfig from "./config/content";
import editModalConfig from "./config/edit";
import searchConfig from "./config/search";

const {
  searchRef,
  contentRef,
  addModalRef,
  editModalRef,
  handleQueryClick,
  handleResetClick,
  // handleAddClick,
  // handleEditClick,
  handleSubmitClick,
  handleExportClick,
  handleSearchClick,
  handleFilterChange,
} = usePage();

// 默认初始数据
const route = useRoute();
const { inOutItem, conId } = route.query;
if (inOutItem || conId) {
  contentConfig.indexActionData = { inOutItem, conId };
  if (inOutItem) {
    searchConfig.formItems[1].initialValue = inOutItem;
  }
}

// 新增
async function handleAddClick() {
  addModalRef.value?.setModalVisible();
  // addModalConfig.formItems[2]!.attrs!.data =
}
// 编辑
async function handleEditClick(row: IObject) {
  editModalRef.value?.handleDisabled(false);
  editModalRef.value?.setModalVisible();
  // 根据id获取数据进行填充
  console.log(row);
  editModalRef.value?.setFormData({ ...row });
}
// 其他工具栏
function handleToolbarClick(name: string) {
  console.log(name);
  if (name === "custom1") {
    ElMessage.success("点击了自定义1按钮");
  }
}
// 其他操作列
async function handleOperatClick(data: IOperatData) {
  console.log(data);
}
</script>
