<script lang="ts" setup>
import TreeSidePageableTable from "@/components/base/tree-side-pageable-table/TreeSidePageableTable.vue";
import DefaultToolButton from "@/components/base/default-tool-button/DefaultToolButton.vue";
import DefaultOperateButtonColumn from "@/components/base/default-column/DefaultOperateButtonColumn.vue";
import { usePage } from "@/composables/use-page";
import BaseForm from "@/components/base/base-form/BaseForm.vue";
import { $t } from "@/common/languages";
import type { FormInstance, FormRules } from "element-plus";

/**
 * 必须要的变量
 */

const getPageUrl = "/sale/repairrecord/getRepairRecordPage";
const addUrl = "/sale/repairrecord/addRepairRecord";
const editUrl = "/sale/repairrecord/updateRepairRecord";
const removeUrl = "/sale/repairrecord/deleteRepairRecord";
const treeSideUrl = "/sale/repairrecord/getKeyAccount";
const searchers = [
  { name: "searchCode", type: "text" as const, placeholder: $t("_prop.sale.repairrecord.searchCode") },
];
const rules = reactive<FormRules>({});
/**
 * 基本不变通用变量
 */
const tableRef = ref<InstanceType<typeof TreeSidePageableTable> | null>(null);
const { useAdd, useEdit, useRemove, useGeneralPageRef } = usePage(tableRef);
const { title, visible, formType, form } = useGeneralPageRef();
/**
 * 可以自定义的变量
 */

const add = () => {
  form.value = {};
  title.value = "_title.sale.repairrecord.add";
  visible.value = true;
  formType.value = false;
};
const edit = (row: any) => {
  title.value = "_title.sale.repairrecord.edit";
  form.value = { ...row };
  visible.value = true;
  formType.value = true;
};
const remove = (row: any) => {
  useRemove(removeUrl, row.id, "_message.sale.repairrecord.delete_message");
};
const submit = (form: any, formRef: FormInstance | undefined) => {
  if (formRef !== undefined) {
    formRef.validate(valid => {
      if (valid) {
        if (formType.value) useEdit(editUrl, form, visible);
        else useAdd(addUrl, form, visible);
      }
    });
  }
};
const topButtonClick = (eventName: string) => {
  switch (eventName) {
    case "add":
      add();
      break;
  }
};
const operateButtonClick = (eventName: string, row: any) => {
  switch (eventName) {
    case "edit":
      edit(row);
      break;
    case "remove":
      remove(row);
      break;
  }
};
</script>
<template>
  <TreeSidePageableTable
    :url="getPageUrl"
    :searchers="searchers"
    ref="tableRef"
    :tree-side-url="treeSideUrl"
    tree-side-node-name="keyAccountId"
    tree-side-param-name="keyAccountId"
    tree-side-title="所有客户"
  >
    <template #tool-button>
      <DefaultToolButton @top-button-click="topButtonClick" />
    </template>
    <template #columns>
      <el-table-column prop="id" type="hidden" width="40" />
      <el-table-column :label="$t('_prop.sale.repairrecord.productType')" prop="productType" />
      <el-table-column :label="$t('_prop.sale.repairrecord.productSn')" prop="productSn" />
      <el-table-column :label="$t('_prop.sale.repairrecord.mac')" prop="mac" />
      <el-table-column :label="$t('_prop.sale.repairrecord.outProductDate')" prop="outProductDate" />
      <el-table-column :label="$t('_prop.sale.repairrecord.repairDate')" prop="repairDate" />
      <el-table-column :label="$t('_prop.sale.repairrecord.repairMark')" prop="repairMark" />
      <DefaultOperateButtonColumn @operate-button-click="operateButtonClick" />
    </template>
  </TreeSidePageableTable>
  <BaseForm v-model:visible="visible" @submit="submit" v-model:form="form" :title="$t(title)" :rules="rules">
    <template #form-items>
      <el-form-item prop="id" v-if="false"><el-input v-model="form.id" /></el-form-item>
    </template>
  </BaseForm>
</template>
