<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 :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="标题">
              <a-input placeholder="标题" v-model="queryParam.title"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="">
              <j-dict-select-tag type="list" v-model="queryParam.groupId"  dict-code='gt_export_group,name,id' placeholder="请选择分组" />
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>


    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <!-- <a-button @click="handleAdd" type="primary" icon="plus">搜索</a-button> -->

      <!-- 高级查询区域 -->
      <!-- <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query> -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleExport(record)">导出</a>
          <a-divider type="vertical" />
          <a @click="showAddress(record)">链接</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <gtExport-modal ref="modalForm" @ok="modalFormOk"></gtExport-modal>
  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import GtExportModal from './modules/GtExportModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import '@/assets/less/TableExpand.less'
  import { downFile,httpAction } from '@/api/manage'

  export default {
    name: "GtExportList",
    mixins:[JeecgListMixin],
    components: {
      GtExportModal
    },
    data () {
      return {
        description: '导出基础类管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'标题',
            align:"center",
            dataIndex: 'title'
          },
          {
            title:'编码',
            align:"center",
            dataIndex: 'code'
          },
          {
            title:'分组名称',
            align:"center",
            dataIndex: 'groupId_dictText'
          },
          {
            title:'从第几行写入数据',
            align:"center",
            dataIndex: 'fromNum'
          },
          {
            title:'复制第几页',
            align:"center",
            dataIndex: 'copyNum'
          },
          {
            title:'是否文本化',
            align:"center",
            dataIndex: 'toText_dictText'
          },
          {
            title:'模板地址',
            align:"center",
            dataIndex: 'templateUrl',
            scopedSlots: {customRender: 'fileSlot'}
          },
          {
            title:'导出模式',
            align:"center",
            dataIndex: 'exportType_dictText'
          },
          {
            title:'数据源id',
            align:"center",
            dataIndex: 'datasourceId_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/export/gtExport/list",
          delete: "/export/gtExport/delete",
          deleteBatch: "/export/gtExport/deleteBatch",
          exportXlsUrl: "/export/gtExport/exportXls",
          importExcelUrl: "export/gtExport/importExcel",
          downloadUrl:"/sys/common/static/",
          runExportUrl:"/export/gtExport/runExport",

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'title',text:'标题',dictCode:''})
        fieldList.push({type:'int',value:'fromNum',text:'从第几行写入数据',dictCode:''})
        fieldList.push({type:'int',value:'groupId',text:'分组名称',dictCode:''})
        fieldList.push({type:'string',value:'copyNum',text:'复制第几页',dictCode:''})
        fieldList.push({type:'int',value:'toText',text:'是否文本化',dictCode:'yn'})
        fieldList.push({type:'string',value:'templateUrl',text:'模板地址',dictCode:''})
        fieldList.push({type:'int',value:'exportType',text:'导出模式',dictCode:'export_type'})
        fieldList.push({type:'Text',value:'exportSql',text:'需要导出的SQL',dictCode:''})
        fieldList.push({type:'int',value:'datasourceId',text:'数据源id',dictCode:'sys_data_source,name,id'})
        this.superFieldList = fieldList
      },
      handleExport(record){
        //打开打开显示页面
        // httpAction(this.url.runExportUrl,{exportId:record.id},"POST").then((data)=>{
        //   if ('200' == data.code) {
        //     console.info("开始下载");
        //     this.downloadFile(data.result.serverPath, data.result.fileName)
        //   }
        // });
        console.info("打开导出页面");

         // this.$router.push('/online/cgreport/1260179852088135682')
        //this.$router.push('/list/query-list')
        //this.$emit('changeTitle', '测试')
        // this.$router.push("/export/GtExportPage?abc=123");
        this.$router.push({"name":"GtExportPage","query":{"exportId":record.id},"path":"/export/GtExportPage"});
        //let url = "/export/GtExportPage/"+record.id;
        //this.$router.push({path:url});
        //this.data.description = "测试";
        // this.changeTitle('piaoyu')
      },
      showAddress(record){
        this.$message.info('/export/GtExportPage?exportId=' + record.id);
      },
      downloadFile(filePath,fileName){
        downFile(this.url.downloadUrl+filePath,{}).then((data)=>{
          if (!data) {
            this.$message.warning("文件下载失败")
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName)
          }else{
            let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName+'.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
          }
        })
      }

    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>