<template>
  <div class="index">
    <el-row>
      <el-col :span="24" class="main" v-loading="loading">
        <el-col :span="24" class="two">
          <el-table :data="list" style="width: 100%" size="large" :header-cell-style="{ backgroundColor: '#edf3ff' }">
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
            <el-table-column prop="table_name" align="center" label="数据类型" />
            <el-table-column prop="created_time" align="center" label="创建时间" />
            <el-table-column prop="progress" align="center" label="进度" width="180" />
            <el-table-column align="center" label="操作" width="180">
              <template #default="{ row }">
                <el-link :underline="false" type="primary" size="mini" @click="reExecute(row)" style="margin-right: 10px">重执行</el-link>
                <el-link :underline="false" type="primary" size="mini" v-if="row.uri" @click="download(row)"> 下载 </el-link>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
        <el-col :span="24" class="thr">
          <el-pagination background layout="prev, pager, next" :total="total" :page-size="limit" v-model:current-page="currentPage" @current-change="changePage" @size-change="sizeChange" />
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { cloneDeep, get } from 'lodash-es'
const $checkRes = inject('$checkRes')
import { UserStore } from '@/store/user'
const userStore = UserStore()
const user = computed(() => userStore.user)
// 接口
import { ExportConfigStore } from '@/store/api/exportConfig'
const store = ExportConfigStore()
// 加载中
const loading = ref(false)

// 列表
const list = ref([])
let skip = 0
let limit = inject('limit')
const total = ref(0)
const currentPage = ref(1)
// 请求
onMounted(async () => {
  loading.value = true
  await search()
  loading.value = false
})
const search = async (query = { skip, limit }) => {
  skip = query.skip
  limit = query.limit
  const info = {
    skip: query.skip,
    limit: query.limit,
    user: user.value.id
  }
  const res = await store.query(info)
  if (res.errcode == '0') {
    list.value = res.data
    total.value = res.total
  }
}
const reExecute = async (data) => {
  const id = get(data, 'id')
  const result = await store.reExecute(id)
  if ($checkRes(result, true, result.errmsg)) search({ skip, limit })
}
const download = (data) => {
  const uri = get(data, 'uri')
  window.open(uri)
}
// 分页
const changePage = (page = currentPage.value) => {
  search({ skip: (page - 1) * limit, limit: limit })
}
const sizeChange = (limits) => {
  limit = limits
  currentPage.value = 1
  search({ skip: 0, limit: limit })
}
</script>
<style scoped lang="scss">
.main {
  .one {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0 0 10px 0;
    .one_left {
      background: #1875df;
      padding: 0 10px;
      height: 30px;
      color: #fff;
      line-height: 30px;
      text-align: center;
      font-size: 16px;
      cursor: default;
    }
  }
  .thr {
    display: flex;
    justify-content: center;
    margin: 20px 0 0 0;
  }
}
</style>
