<!--
 * @Author: 曹捷
 * @Date: 2021-03-15 14:54:07
 * @LastEditors: 曹捷
 * @LastEditTime: 2021-08-31 17:13:44
 * @Description: 后端代码生成
-->
<template>

  <obitUniversalTable name="后端代码生成" :pagination="pagination">
    <template #search>
      <obit-search-form :params="params" :paramList="paramList" @search="pageQuery(true)">
      </obit-search-form>
    </template>
    <template #btn>
      <a-button @click="handleGenTable" class="m-r-15">
        <i class="iconfont icon-guide  p-r-5"></i> 生 成
      </a-button>
      <a-button @click="openImportTable" class="m-r-15">
        <i class="iconfont icon-import   p-r-5"></i> 导 入
      </a-button>
      <a-button @click="addDialog" class="m-r-15">
        <i class="iconfont icon-edit  p-r-5"></i> 修 改
      </a-button>
      <a-button @click="removeAll" class="m-r-15">
        <i class="iconfont icon-ashbin  p-r-5"></i> 删 除
      </a-button>
    </template>
    <a-table :loading="pageLoading" rowKey="tableId" :columns="tableColumns" :dataSource="pageDataList"
      :pagination="false" :row-selection="rowSelection" class="obit-atable" highlight-current-row style="width: 100%">
      <template #operation="{ record  }" :key="record.configId">
        <!-- <a-button class="operation-btn" type="link" @click="details(record)">预览</a-button> -->
        <a-button class="operation-btn" type="link" @click="editGenDialog(record)">编辑</a-button>
        <a-button class="operation-btn" type="link" @click="remove(record)"> 删除</a-button>
        <a-button class="operation-btn" type="link" @click="handleSynchDb(record)"> 同步 </a-button>
        <a-button class="operation-btn" type="link" @click="handleGenTable(record)"> 生成代码</a-button>
      </template>
    </a-table>
  </obitUniversalTable>
  <previewDeatil v-model:open="detailsVisable" :detailsInfo="detailsInfo" @reload="pageQuery(true)">
  </previewDeatil>
  <importTable v-model:open="importVisable" :detailsInfo="detailsInfo" @reload="pageQuery(true)">
  </importTable>
</template>

<script>
import { getCurrentInstance, onMounted, reactive, toRefs, inject } from 'vue'
import page from '../../../hook/page.js'
import previewDeatil from './preview.vue'
import importTable from './importTable.vue'
import { createVNode, defineComponent } from 'vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { useRoute, useRouter } from 'vue-router'
export default {
  components: { previewDeatil, importTable },
  setup (props, contenxt) {
    let http = inject('http')
    let { proxy } = getCurrentInstance()
    let router = useRouter()
    let pageIns = page({
      url: 'code/gen/pageList',
      key: 'tableId',
      removeUrl: 'code/gen/remove',
      removeAll: 'code/gen/removeAll',
    })
    let _data = reactive({
      detailsVisable: false,
      importVisable: false,
      detailsInfo: {},
      tableNames: [],
      selectedRows: [],

      tableColumns: [
        { dataIndex: 'tableId', title: '序号', width: 80 },
        { dataIndex: 'tableName', title: '表名称', width: 180 },
        { dataIndex: 'tableComment', title: '表描述', width: 180 },
        { dataIndex: 'className', title: '实体', width: 200 },
        { dataIndex: 'createTime', title: '创建时间', width: 200 },
        { dataIndex: 'updateTime', title: '更新时间', width: 200 },
        { title: '操作', slots: { customRender: 'operation' }, width: 300 },
      ],
      paramList: [
        { name: '表名称', key: 'tableName' },
        { name: '表描述', key: 'tableComment' },
        { name: '创建时间', endKey: 'endTime', startKey: 'beginTime', type: 'dateBetween' },
      ],
      rowSelection: {
        onChange: (selectedRowKeys, selectedRows) => {
          _data.selectedRows = selectedRows
          console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
        }
      },
    })
    let _methods = {
      details (item) {
        _data.detailsVisable = true;
        _data.detailsInfo = item
      },
      openImportTable () {
        _data.importVisable = true
        _data.detailsInfo = {}
      },
      /** 生成代码操作 */
      handleGenTable (row) {
        _data.selectedRows.forEach(element => {
          _data.tableNames.push(element.tableName)
        })
        const tableNames = row.tableName || _data.tableNames;
        if (tableNames == "") {
          proxy.$message.error('请选择要生成的数据')
          return;
        }
        console.log("11111", row.genType)
        let params = ''
        if (_data.selectedRows.length > 1) {
          params = { tables: tableNames.join(",") }
        } else {
          params = { tables: tableNames }
        }

        let config = {
          // fileName一般由后台传递 
          // 目前系统和后端约定 fileName 为response.headers['filename']
          fileName: tableNames + ".zip"
        }
        http.downloadFile(`code/gen/batchGenCode`, params, config, 'get')
        params = {}
      },
      /** 同步数据库操作 */
      handleSynchDb (row) {
        const tableName = row.tableName;
        proxy.$confirm({
          title: '警告',
          icon: createVNode(ExclamationCircleOutlined),
          content: '确认要强制同步"' + tableName + '"表结构吗？',
          onOk () {
            http.removeResultInfo(row).then((res) => {
              proxy.$message.success('同步成功')
            })
          },
          onCancel () { },
        })
      },
      editGenDialog (item) {
        router.push({ path: 'gen/editTable', query: { tableId: item.tableId } })
      },
    }
    onMounted(() => {
      pageIns._method.pageQuery()
    })
    return {
      ...toRefs(pageIns._data),
      ...pageIns._method,
      ...toRefs(_data),
      ..._methods,
    }
  },
}
</script>

<style>
</style>