<template>
  <div>
    <el-card style="margin: 8px">
      <el-form :inline="true" :model="selectForm" class="demo-form-inline" label-width="120px">
        <el-form-item label="页面名称">
          <el-input v-model="selectForm.pageConfigName" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryList">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin: 8px">
      <el-button plain type="primary" icon="Plus" @click="addBoard">新增</el-button>
    </el-card>
    <el-card style="margin: 8px">
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="pageConfigName" label="页面名称" align="center" />
        <el-table-column label="操作" width="300" align="center">
          <template #default="scope">
            <el-button text type="primary" size="small" @click="compileBoard(scope.row)">编辑
            </el-button>
            <el-button text type="primary" size="small" @click="viewboard(scope.row)">查看
            </el-button>
            <el-button text type="primary" size="small" @click="editDataSource(scope.row)">修改
            </el-button>
            <el-button
              size="small"
              type="danger"
              text
              @click="delDataSource(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        v-model:current-page="selectForm.pageNum"
        v-model:page-size="selectForm.pageSize"
        :page-sizes="[10,20,50,100]"
        layout="total, sizes, prev, pager, next, jumper"
        v-if="total>0"
        :total="total"
        @size-change="pageChange"
        @current-change="pageChange"
      />
    </el-card>

    <el-dialog v-model="constructionDataFormVisible" draggable width="800">
      <template #header>
        <div style="width: 100%">
          <span>{{ constructionDataForm.pageConfigId ? '修改' : '添加' }}看板</span>
        </div>
      </template>
      <el-form :model="constructionDataForm">
        <el-form-item label="页面名称" prop="pageConfigName">
          <el-input v-model="constructionDataForm.pageConfigName" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="constructionDataFormVisible = false">关闭</el-button>
          <el-button type="primary" @click="createData ">
            {{ constructionDataForm.pageConfigId ? '确定' : '创建' }}
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { useRouter } from 'vue-router';
import axios from 'axios';
import request from '@/utils/request';
import { options } from './tool.js';
import { ElMessage, ElMessageBox } from 'element-plus';
import { addBoardApi, delBoardApi, editBoardApi, getBoardListApi } from '@/views/boardGenerate/api/boardList';

const router = useRouter();


const loading = ref(false);
const selectForm = ref({
  pageNum: 1,
  pageSize: 10
});
const total = ref(0);
const tableData = ref([{}]);
const constructionDataForm = ref({ designDataFieldList: [] });
const constructionDataFormVisible = ref(false);
const getList = async () => {
  loading.value = true;
  getBoardListApi(selectForm.value)
    .then((res) => {
      console.log(res);
      tableData.value = res.rows;
      total.value = res.total;
    })
    .finally(() => {
      loading.value = false;
    });
};
const queryList = async () => {
  selectForm.value.pageNum = 1;
  await getList();
};
const pageChange = () => {
  getList();
};
const addBoard = () => {
  constructionDataForm.value = { designDataFieldList: [] };
  constructionDataFormVisible.value = true;
};
const editDataSource = (e) => {
  constructionDataForm.value = e;
  constructionDataFormVisible.value = true;
};
const delDataSource = (e) => {
  ElMessageBox.confirm(
    '要删除这条数据源吗?',
    'Warning',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
    .then(() => {
      delBoardApi(e.pageConfigId).then(() => {
        ElMessage({
          type: 'success',
          message: '删除成功'
        });
        getList();
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消'
      });
    });
};
const createData = () => {
  if (!constructionDataForm.value.designDataFieldList) {
    constructionDataForm.value.designDataFieldList = [];
  }
  if (constructionDataForm.value.pageConfigId) {
    editBoardApi({
      ...constructionDataForm.value,
      designPageEdgesList: constructionDataForm.value.designPageEdgesList || [],
      designPagePointList: constructionDataForm.value.designPagePointList || []
    }).then(() => {
      constructionDataFormVisible.value = false;
      ElMessage({
        type: 'success',
        message: '修改成功'
      });
      getList();
    });
  } else {
    addBoardApi({ ...constructionDataForm.value, designPageEdgesList: [], designPagePointList: [] }).then(() => {
      constructionDataFormVisible.value = false;
      ElMessage({
        type: 'success',
        message: '创建成功'
      });
      getList();
    });
  }
};
const compileBoard = (row) => {
  router.push({ path: '/boardGenerate1', query: { id: row.pageConfigId } });
};
const viewboard = (row) => {
  router.push({ path: '/boardView', query: { id: row.pageConfigId } });
};
onMounted(async () => {
  await queryList();
});
</script>
<style lang="less" scoped>
.pagination {
  float: right;
  margin: 12px 0;
}
</style>
