<template>
  <div>
    <CardCommon
      :type="type"
      :search-data-by-name="findSceneModuleFun"
      :get-data="getSceneLabelListFun"
      :delete-table-data="deleteSceneModuleFun"
      :submit-drawer="updateSceneModuleFun"
      :add-data="saveSceneModuleFun"
    >
    </CardCommon>
  </div>
</template>

<script setup lang="ts">
  import {
    updateSceneModule,
    getSceneModuleList,
    deleteSceneModule,
    findSceneModuleByName,
    saveSceneModule,
    SceneModule,
  } 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('模板');

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

  // 根据名称查找
  function findSceneModuleFun(data: DataCommon) {
    console.log('findSceneModuleByName', data.name, data.page, data.size);
    return findSceneModuleByName(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 getSceneLabelListFun(data) {
    return getSceneModuleList(data.page, data.size)
      .then((res) => {
        return res;
      })
      .catch((error) => {
        ElMessageBox.alert(error.message, '异常', {
          confirmButtonText: 'OK',
          callback: () => {
            ElMessage({
              type: 'info',
              message: `查询数据失败`,
            });
          },
        });
      });
  }

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

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

<style scoped></style>
