<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="模式名称">
              <a-input placeholder="请输入搜索关键词" v-model="queryParam.schemaName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="表名">
              <a-input placeholder="请输入搜索关键词" v-model="queryParam.tableName"></a-input>
            </a-form-item>
          </a-col>
          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24" >
                <a-button type="primary"  style="left: 10px" @click="loadData" icon="search">查询</a-button>
                <a-button type="primary"  @click="searchReset" icon="reload" style="margin-left: 8px;left: 10px">重置</a-button>
                <a-button type="primary"  @click="handleAdd" icon="upload" style="margin-left: 8px;left: 10px">上传</a-button>
            </a-col>
          </span>

        </a-row>
      </a-form>
    </div>
    <!-- table区域-begin -->
    <div style="height:900px;overflow-y:auto;">
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys,onChange: onSelectChange}"
        @change="handleTableChange">
<!--        :locale="myLocale"-->

        <!-- 字符串超长截取省略号显示-->
        <template slot="parseStatus" slot-scope="text">
            <span style="color:black;" v-if="text === 0" >未解析</span>
            <span style="color:darkorange;" v-if="text === 1">解析中</span>
            <span style="color:#87d068;" v-if="text === 2">解析完成</span>
            <span style="color:red;" v-if="text === 3">解析失败</span>
            <span style="color:royalblue;" v-if="text === 4">等待中</span>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="processFile(record)" v-has="'cont:btn'">解析</a>
          <a-divider type="vertical" v-has="'cont:btn'" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <uploadModal ref="modalForm" @ok="loadData"></uploadModal>
  </a-card>
</template>

<script>
  import uploadModal from './modules/uploadModal'
  import { getFileLinkInfo,processFile,fileDataLinkDelete } from '@/api/fileDataLink'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import JEllipsis from "@/components/jeecg/JEllipsis";
  import {deleteAction, postAction} from '@/api/manage'
  export default {
    name: "waterDocuments",
    mixins:[JeecgListMixin],
    components: {
      uploadModal,
      JEllipsis,
      VNodes: {
        functional: true,
        render: (h, ctx) => ctx.props.vnodes,
      },
    },
    data () {
      return {
        description: '文件接引',
        dataSources: [],
        queryParam: {
          pageNum :1,
          pageSize:20,
          schemaName:'',
          tableName:'',
          dataLinkType:'3'
        },
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'id',
            width:60,
            align:"id",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title: '文件名称',
            align:"fileName",
            dataIndex: 'fileName',
          },
          {
            title: '模式名称',
            align:"schemaName",
            dataIndex: 'schemaName'
          },
          {
            title: '表名',
            align:"tableName",
            dataIndex: 'tableName',
          },
          {
            title: '创建时间',
            align:"createTime",
            dataIndex: 'createTime'
          },
          {
            title: '状态',
            align:"center",
            dataIndex: 'parseStatus',
            scopedSlots: { customRender: 'parseStatus' },
          },
          {
            title: '解析进度',
            align:"center",
            dataIndex: 'percent',
          },
          {
            title: '解析时间开始',
            align:"analyzeStartTime",
            dataIndex: 'analyzeStartTime',
          },
          {
            title: '解析时间完成',
            align:"analyzeEndTime",
            dataIndex: 'analyzeEndTime',
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            width:180,
            scopedSlots: { customRender: 'action' },
          }
        ]
      }
    },
    computed: {
    },
    created () {
      this.loadData();
    },
    methods: {
      //筛选需要重写handleTableChange
      handleTableChange(pagination, filters, sorter) {
        //分页、排序、筛选变化时触发
        //TODO 筛选
        if (Object.keys(sorter).length > 0) {
          this.isorter.column = sorter.field;
          this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
        }
        this.ipagination = pagination;
        this.loadData();
      },
      loadData() {
        getFileLinkInfo(this.queryParam).then((res) => {
            if (res.success) {
                this.dataSource = res.result.records;
                if(res.result.total)
                {
                  this.ipagination.total = res.result.total;
                }
            } else {
                this.$message.warning(res.message);
            }
        });
      },
      handleDelete: function (id) {
        var that = this;
        deleteAction("/fileDataLink/delete?id="+id,{}).then((res) => {
            if (res.success) {
                this.$message.success(res.message);
                that.loadData();
            } else {
                this.$message.warning(res.message);
            }
        });
      },
      processFile: function (record) {
        var that = this;
        isDataLinkFileParsed({fileId:record.fileId}).then((res) => {
            if (res) {
              postAction("/fileDataLink/processFile?dataLinkId="+record.id,{}).then((res) => {
                  if (res.success) {
                      this.$message.success(res.message);
                      that.loadData();
                  } else {
                      this.$message.warning(res.message);
                  }
              });
            } else {
                this.$message.warning("解析中请勿重复点击");
            }
        });
      },
      handleAdd() {
        this.$refs.modalForm.add();
        this.$refs.modalForm.title = "文件上传";
        this.$refs.modalForm.dataLinkType=this.queryParam.dataLinkType;
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>