<template>
  <el-card>
    <el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="state.queryForm.name" placeholder="表名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="importHandle()">导入</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="downloadBatchHandle()">生成代码</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="deleteBatchHandle()">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="state.dataListLoading"
      :data="state.dataList"
      border
      style="width: 100%"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="表名"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="description"
        label="表说明"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column prop="template" label="模板名" header-align="center" align="center">
        <template #default="{ row }">
          <el-select v-model="row.template">
            <el-option
              v-for="item in templateList"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="300">
        <template #default="scope">
          <el-button type="primary" link @click="editHandle(scope.row.id)">编辑</el-button>
          <el-button type="primary" link @click="previewHandle(scope.row.id)">预览</el-button>
          <el-button type="primary" link @click="generatorHandle(scope.row.id)">生成代码</el-button>
          <el-button type="primary" link @click="deleteBatchHandle(scope.row.id)">删除</el-button>
          <el-button type="primary" link @click="syncHandle(scope.row)">同步</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="state.page"
      :page-sizes="state.pageSizes"
      :page-size="state.limit"
      :total="state.total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
    >
    </el-pagination>

    <import ref="importRef" @refresh-data-list="getDataList"></import>
    <preview ref="previewRef" @refresh-data-list="getDataList"></preview>
    <edit ref="editRef" @refresh-data-list="getDataList"></edit>
    <generator ref="generatorRef" @refresh-data-list="getDataList"></generator>
  </el-card>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import { useCrud } from '@/hooks'
import Import from './import.vue'
import Edit from './edit.vue'
import Preview from './preview.vue'
import Generator from './generator.vue'
import { useTableSyncApi } from '@/api/table'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useDownloadApi } from '@/api/generator'

const state: IHooksOptions = reactive({
  dataListUrl: '/gen/table/page',
  deleteUrl: '/gen/table',
  queryForm: {
    name: ''
  }
})

const importRef = ref()
const editRef = ref()
const previewRef = ref()
const generatorRef = ref()

const importHandle = (id?: number) => {
  importRef.value.init(id)
}

const editHandle = (id?: number) => {
  editRef.value.init(id)
}
const previewHandle = (tableId?: number) => {
  previewRef.value.init(tableId)
}
const generatorHandle = (id?: number) => {
  generatorRef.value.init(id)
}

const downloadBatchHandle = () => {
  const tableIds = state.dataListSelections ? state.dataListSelections : []

  if (tableIds.length === 0) {
    ElMessage.warning('请选择生成代码的表')
    return
  }

  useDownloadApi(tableIds)
}

const syncHandle = (row: any) => {
  ElMessageBox.confirm(`确定同步数据表${row.name}吗?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      useTableSyncApi(row.id).then(() => {
        ElMessage.success('同步成功')
      })
    })
    .catch(() => {})
}

const templateList = ref([
  {
    label: 'relation-demo',
    value: 'relation-demo'
  },
  {
    label: 'vue-fastapi-admin',
    value: 'vue-fastapi-admin'
  }
])

const {
  getDataList,
  selectionChangeHandle,
  sizeChangeHandle,
  currentChangeHandle,
  deleteBatchHandle
} = useCrud(state)
</script>
