<template>
  <el-card v-loading="boxCardLoading" class="box-card" shadow="always">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="项目" prop="projectId">
        <el-select v-model="queryParams.projectId" clearable size="small" placeholder="项目" class="filter-item" @change="handleQuery">
          <el-option v-for="item in projectsOptions" :key="item.id" :label="item.projectName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="离线文件名称" prop="sourceName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入离线文件名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row type="flex" justify="space-between">
      <el-col :span="12">
        <el-button-group>
          <el-upload
            class="upload-demo"
            action="#"
            :show-file-list="false"
            :http-request="uploadOfflineFile"
          >
            <el-button size="mini" type="primary" icon="el-icon-upload2">上传</el-button>
          </el-upload>
        </el-button-group>
      </el-col>
      <el-col :span="12">
        <div class="right-toolbar">
          <el-tooltip content="密度" effect="dark" placement="top">
            <el-dropdown trigger="click" @command="handleCommand">
              <el-button circle size="mini">
                <i class="el-icon-s-grid"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="medium">正常</el-dropdown-item>
                <el-dropdown-item command="small">中等</el-dropdown-item>
                <el-dropdown-item command="mini">紧凑</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-tooltip>
          <el-tooltip content="刷新" effect="dark" placement="top">
            <el-button circle size="mini" @click="handleRefresh">
              <i class="el-icon-refresh"></i>
            </el-button>
          </el-tooltip>
          <el-tooltip content="列设置" effect="dark" placement="top">
            <el-popover placement="bottom" width="100" trigger="click">
              <el-checkbox-group v-model="checkedTableColumns" @change="handleCheckedColsChange">
                <el-checkbox
                  v-for="(item, index) in tableColumns"
                  :key="index"
                  :label="item.prop"
                >{{ item.label }}</el-checkbox>
              </el-checkbox-group>
              <span slot="reference">
                <el-button circle size="mini">
                  <i class="el-icon-setting"></i>
                </el-button>
              </span>
            </el-popover>
          </el-tooltip>
        </div>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableDataList"
      border
      tooltip-effect="dark"
      :size="tableSize"
      :height="tableHeight"
      style="width: 100%;margin: 15px 0;"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="55" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index +1 }}</span>
        </template>
      </el-table-column>
      <template v-for="(item, index) in tableColumns">
        <el-table-column
          v-if="item.show"
          :key="index"
          :prop="item.prop"
          :label="item.label"
          :formatter="item.formatter"
          align="center"
          show-overflow-tooltip
        />
      </template>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.isSwitch != 'jrcg'"
            size="mini"
            type="text"
            icon="el-icon-sort"
            @click="handleSwitch(scope.row)"
          >执行接入</el-button>
          <el-button
            v-if="scope.row.isSwitch == 'jrcg'"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
          >查看接入数据</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownload(scope.row)"
          >下载</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDetail(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :current-page.sync="queryParams.pageNum"
      :page-size.sync="queryParams.pageSize"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>

<script>
  import { pageOfflineData, delDataSource, uploadFile } from '@/api/metadata/datasource'

  export default {
    name: 'OfflineDataList',
    data() {
      return {
        tableHeight: document.body.offsetHeight - 330 + 'px',
        // 展示切换
        showOptions: {
          data: {},
          showList: true,
          showAdd: false,
          showEdit: false,
          showDetail: false
        },
        // 遮罩层
        loading: true,
        boxCardLoading: false,
        // 表格头
        tableColumns: [
          { prop: 'fileName', label: '离线文件名称', show: true },
          {
            prop: 'offlineType',
            label: '离线文件类型',
            show: true,
            formatter: this.offlineTypeeFormatter
          },
          {
            prop: 'isSwitch',
            label: '接入状态',
            show: true,
            formatter: this.isSwitchFormatter
          },
          { prop: 'createTime', label: '上传时间', show: true },
          { prop: 'switchTime', label: '接入时间', show: true }
        ],
        projectsOptions: [
          { id: '1111', projectName: '演示项目' }
        ],
        // 默认选择中表格头
        checkedTableColumns: [],
        tableSize: 'medium',
        // 状态数据字典
        statusOptions: [],
        dbTypeOptions: [],
        // 数据源表格数据
        tableDataList: [],
        // 总数据条数
        total: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
          projectId: '1111',
          fileName: ''
        }
      }
    },
    created() {
      this.getDicts('sys_common_status').then(response => {
        if (response.success) {
          this.statusOptions = response.data
        }
      })
      this.getDicts('data_db_type').then(response => {
        if (response.success) {
          this.dbTypeOptions = response.data
        }
      })
      this.getList()
    },
    mounted() {
      this.initCols()
    },
    methods: {
      /** 查询数据源列表 */
      getList() {
        this.loading = true
        pageOfflineData(this.queryParams).then(response => {
          this.loading = false
          if (response.success) {
            const { data } = response
            this.tableDataList = data.data
            this.total = data.total
          }
        })
      },
      initCols() {
        this.checkedTableColumns = this.tableColumns.map(col => col.prop)
      },
      handleSwitch(row) {
        this.boxCardLoading = true
        setTimeout(() => {
          row.isSwitch = 'jrcg'
          this.boxCardLoading = false
          this.$message.success('接入成功')
        }, 2000)
      },
      /** 详情按钮操作 */
      handleDetail(row) {
        this.showOptions.data.id = row.id
        this.showOptions.showList = false
        this.showOptions.showDetail = true
        this.$emit('showCard', this.showOptions)
      },
      handleCheckedColsChange(val) {
        this.tableColumns.forEach(col => {
          if (!this.checkedTableColumns.includes(col.prop)) {
            col.show = false
          } else {
            col.show = true
          }
        })
      },
      handleCommand(command) {
        this.tableSize = command
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams = {
          pageNum: 1,
          pageSize: 20,
          fileName: ''
        }
        this.handleQuery()
      },
      /** 刷新列表 */
      handleRefresh() {
        this.getList()
      },
      handleUpload() {

      },
      handleDownload(row) {

      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('选中数据将被永久删除, 是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delDataSource(row.id).then(response => {
            if (response.success) {
              this.$message.success('删除成功')
              this.getList()
            }
          })
        }).catch(() => {
        })
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.queryParams.pageNum = 1
        this.queryParams.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`)
        this.queryParams.pageNum = val
        this.getList()
      },
      offlineTypeeFormatter(row, column, cellValue, index) {
        if (cellValue === 'sjy') {
          return '数据源文件'
        } else if (cellValue === 'sj') {
          return '数据文件'
        }
      },
      isSwitchFormatter(row, column, cellValue, index) {
        if (cellValue === 'djr') {
          return <el-tag type='info'>待接入</el-tag>
        } else if (cellValue === 'jrcg') {
          return <el-tag type='success'>接入成功</el-tag>
        } else if (cellValue === 'jrsb') {
          return <el-tag type='error'>接入失败</el-tag>
        }
      },
      uploadOfflineFile(file) {
        this.formData = new FormData()
        this.formData.append('file', file.file)
        // 请求接口
        uploadFile(this.formData, 'sjy').then((response) => {
          if (response.success) {
            this.$message.success('上传成功')
          } else {
            this.$message.error('上传失败')
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .right-toolbar {
    float: right;
  }
  .el-card ::v-deep .el-card__body {
    height: calc(100vh - 170px);
  }
</style>

