<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :ok-button-props="{ style: { display: 'none' } }"
    okText="保存"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading" style="background: #e6e9f1 !important;">
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="tableName"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :columns="columns"
        :dataSource="dataSource">
<!--        :locale="myLocale"-->

        <!-- 字符串超长截取省略号显示-->
      </a-table>
      <a-button type="primary" style="width:50%;" @click="exportTool(1)">导出csv</a-button>
      <a-button type="primary" style="width:50%;" @click="exportTool(2)">导出txt</a-button>
    </a-spin>
  </a-modal>
</template>

<script>
import moment from "moment"
import { getAction, deleteAction, putAction, postAction } from '@/api/manage'
import { metaDataTypeTree } from '@/api/metaData'

  export default {
    name: "tablelist",
    components: {
    },
    data () {
      return {
        title:"导出表数据",
        visible: false,
        confirmLoading: false,
        queryParam: {
          sourceType: 2,
          schemaMass: null,
          massKey: null
        },
        dataSource:[],
        selectedRowKeys: [],
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'id',
            width:60,
            align:"id",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title: '报文名',
            align:"center",
            dataIndex: 'massName',
          },
          {
            title: '表名',
            align:"center",
            dataIndex: 'tableName'
          },
        ],
      }
    },
    created () {
      
    },
    methods: {
      add (schemaMass) {
        this.visible =true;
        this.queryParam.schemaMass = schemaMass;
        this.getTableInfo();
      },
      getTableInfo(){
        metaDataTypeTree(this.queryParam).then(res => {
          if (res.code == 200) {
            var keys = Object.keys(res.result)
            keys.forEach((element, index) => {
              this.dataSource = res.result[element]
            });
          }
        })
      },
      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys;
      },
      exportTool(exportType){
        if(this.queryParam.schemaMass == ""){
            this.$message.warning("参数错误请重新打开导出");
            return;
        }
        if(this.selectedRowKeys.length <= 0){
            this.$message.warning("至少选择一个需要导出的表");
            return;
        }
        postAction("/dataManager/getDataManagerInfo?schemaMass="+this.queryParam.schemaMass+"&tableNames="+this.selectedRowKeys+"&exportType="+exportType,{}).then(res => {
           if (res.code == 200) {
              this.$message.success(res.result);
            }else{
              this.$message.warning(res.message)
            }
        })
      },
      onOk(value) {
        this.$emit('ok');
      },
      close () {
        this.$emit('ok');
        this.visible = false;
      },
      handleOk () {
        this.$emit('ok');
        this.close();
      },
      handleCancel () {
        this.$emit('ok');
        this.close()
      },
    }
  }
</script>

<style scoped>
.disabled{
  pointer-events: none;
}
</style>