<template>
  <div class="gl-page-content">
    <div class="table">
      <BaseTable
        ref="baseTableRef"
        tableName="DYNAMIC_TABLE"
        :api="listFormConfigureApi"
        :actionsWidth="130"
      >
        <template #leftOptions>
          <el-button type="primary" :icon="Plus" @click="editFun({})">新增表头</el-button>
        </template>
        <template #tableActions="{ row }">
          <div class="gl-table-actions">
            <el-link type="primary" @click="checkFun(row)">查看</el-link>
            <el-link type="primary" @click="editFun(row)">编辑</el-link>
            <el-popconfirm title="删除后不可恢复，确定删除吗？" :width="250" placement="left" @confirm="delFun(row.id)">
              <template #reference>
                <el-link type="danger">删除</el-link>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </BaseTable>
    </div>
    <Detail v-model="detailVisible" :id="detailId" :tableName="tableName" @submit="submitFun" />
    <TableFormDia v-model="formDiaVisible" :name="tableName" />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { Plus } from '@element-plus/icons-vue';
import BaseTable from '@/components/common/table/baseTable.vue';
import { listFormConfigureApi, deleteFormConfigureApi } from '@/api/system/systemData';
import Detail from './components/detail.vue';
import TableFormDia from '@/components/common/table/tableFormDia.vue';

const baseTableRef = ref();
const detailVisible = ref(false);
const formDiaVisible = ref(false);
const tableName = ref('');
const detailId = ref(0);

const submitFun = () => {
  baseTableRef.value.resetFun();
};

const checkFun = (data: any) => {
  tableName.value = data.formName;
  formDiaVisible.value = true;
};

const editFun = (data: any) => {
  detailId.value = data.id;
  tableName.value = data.formName;
  detailVisible.value = true;
};

const delFun = (id: number) => {
  const params = {
    id,
  };
  deleteFormConfigureApi(params).then((res: any) => {
    if (res.code === 200) {
      baseTableRef.value.resetFun();
      ElMessage.success('操作成功');
    }
  });
};
</script>
