<template>
  <div>
    <CardCommon
      :type="type"
      :tab="props.tab"
      :search-data-by-name="findWorkshopModuleFun"
      :get-data="getWorkshopModuleListFun"
      :delete-table-data="deleteWorkshopModuleFun"
      :submit-drawer="updateWorkshopModuleFun"
      :add-data="saveWorkshopModuleFun"
    >
    </CardCommon>
  </div>
</template>

<script setup lang="ts">
  import {
    updateWorkshopModule,
    getWorkshopModuleList,
    deleteWorkshopModule,
    findWorkshopModuleByName,
    saveWorkshopModule,
    WorkshopModule,
  } from '@/api/template/template';
  import { ElMessage, ElMessageBox } from 'element-plus';
  import CardCommon, { TableModel } from './CardCommon.vue';
  import { DataCommon } from './TitleCommon.vue';
  import { ref } from 'vue';
  const type = ref('模板');

  const props = defineProps<{
    tab: LabelType;
  }>();

  // 添加数据
  function saveWorkshopModuleFun(data: WorkshopModule) {
    return saveWorkshopModule(data)
      .then((res) => {
        console.log(res);
        return res;
      })
      .catch((error) => {
        ElMessageBox.alert(error, '异常', {
          confirmButtonText: 'OK',
          callback: () => {
            ElMessage({
              type: 'info',
              message: `添加数据失败`,
            });
          },
        });
      });
  }

  // 根据名称查找
  function findWorkshopModuleFun(data: DataCommon) {
    console.log('findWorkshopModuleByName', data.name, data.page, data.size);
    return findWorkshopModuleByName(data.name, data.page, data.size)
      .then((res) => {
        console.log(res);
        return res;
      })
      .catch((error) => {
        ElMessageBox.alert(error, '异常', {
          confirmButtonText: 'OK',
          callback: () => {
            ElMessage({
              type: 'info',
              message: `查询数据失败`,
            });
          },
        });
      });
  }

  // 查找数据列表-finish
  function getWorkshopModuleListFun(data) {
    return getWorkshopModuleList(data.page, data.size)
      .then((res) => {
        return res;
      })
      .catch((error) => {
        ElMessageBox.alert(error.message, '异常', {
          confirmButtonText: 'OK',
          callback: () => {
            ElMessage({
              type: 'info',
              message: `查询数据失败`,
            });
          },
        });
      });
  }

  // 删除数据
  function deleteWorkshopModuleFun(data: TableModel) {
    console.log('detele', data);
    return deleteWorkshopModule(data.id)
      .then((res) => {
        console.log(res);
        return res;
      })
      .catch((error) => {
        ElMessageBox.alert(error, '异常', {
          confirmButtonText: 'OK',
          callback: () => {
            ElMessage({
              type: 'info',
              message: `删除数据失败`,
            });
          },
        });
      });
  }

  // 编辑数据
  function updateWorkshopModuleFun(data: WorkshopModule) {
    console.log('edit', data);
    return updateWorkshopModule(data)
      .then((res) => {
        console.log(res);
        return res;
      })
      .catch((error) => {
        ElMessageBox.alert(error, '异常', {
          confirmButtonText: 'OK',
          callback: () => {
            ElMessage({
              type: 'info',
              message: `编辑数据失败`,
            });
          },
        });
      });
  }
</script>

<style scoped></style>
