<template>
  <el-dialog
    :visible="visible"
    width="70%"
    style="height: 98%"
    :before-close="handleClose"
    top="5vh"
    lock-scroll
  >
    <div class="title" slot="title">
      <div class="divider"></div>
      <span class="label">{{ getTitle }}</span>
    </div>
    <div class="upload-progress">
      <div class="top-bar">
        <SearchBar :config="searchConfig" @search="search" @reset="reset" />
      </div>
      <TablePage
        :data="list.records"
        :tableTitle="tableTitle"
        :operates="tableOperates"
      >
        <template v-slot:fileSize="scope">
          {{ getFileSize(scope.scope.fileSize) }}
        </template>
        <template v-slot:createTime="scope">
          {{ scope.scope.createTime }}
          <el-tag
            size="mini"
            type="success"
            v-if="isToday(scope.scope.createTime)"
            >今天</el-tag
          >
          <el-tag
            size="mini"
            type="primary"
            v-if="getYestoday == scope.scope.createTime.split(' ')[0]"
            >昨天
          </el-tag>
        </template>
      </TablePage>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="Number(list.current)"
        :page-sizes="[10, 20, 40, 50]"
        :page-size="Number(list.size)"
        layout="total, sizes, prev, pager, next, jumper"
        :total="Number(list.total)"
        class="pagination"
      >
      </el-pagination>
    </div>
    <div class="dialog-footer">
      <el-button type="primary" size="mini" @click.native="handleClose"
        >关闭</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { importRecordsTitle } from "./config";
import { parseTime, getYestoday, renderSize } from "@/utils/index";
import { getImportListPage } from "@/api/file";

export default {
  name: "ImportRecordDialog",
  props: {
    bizType: {
      type: String,
      default: "biz_cultural_relic",
    },
  },
  watch: {
    visible: {
      handler: function (value) {
        if (value) {
          this.loadData();
        }
      },
      deep: true,
      immediate: true,
    },
  },
  computed: {
    getTitle() {
      if (!this.bizType) {
        return;
      }
      return this.bizType == "biz_cultural_relic"
        ? "文物导入记录"
        : "展览导入记录";
    },
    tableTitle() {
      return importRecordsTitle;
    },
    getFileSize(fileSize) {
      return (fileSize) => {
        return renderSize(Number(fileSize));
      };
    },

    isToday(time) {
      return (time) => {
        if (time) {
          let timeDate = time.split(" ")[0];
          let today = parseTime(new Date(), "{yy}-{mm}-{dd}");
          return timeDate == today;
        } else {
          return false;
        }
      };
    },
    getYestoday() {
      return getYestoday();
    },
  },
  data() {
    return {
      visible: false,
      list: {
        records: [],
        size: 10,
        current: 1,
        total: 0,
      },
      searchForm: {
        name: "",
        status: "",
      },
      searchConfig: [
        {
          prop: "batchNum",
          type: "input",
          label: "批次",
        },
      ],
      searchParams: {},
    };
  },
  methods: {
    async search(form) {
      // 记录搜索的参数
      this.searchParams = form;
      this.resetPage();
      this.loadData(form);
    },

    reset() {
      this.searchParams = {};
      this.loadData();
    },

    //   加载表格数据
    async loadData(form) {
      var params = {
        page: this.list.current,
        limit: this.list.size,
        type: this.bizType,
      };
      const queryParams = form || this.searchParams;
      if (Object.keys(queryParams)) {
        params = { ...params, ...queryParams };
      }
      let res = await getImportListPage(params);
      if (res.code == 0) {
        this.list = res.data;
      }
    },
    // 改变页容量
    handleSizeChange(value) {
      this.list.size = value;
      this.loadData();
    },

    // 改变当前显示页
    handleCurrentChange(value) {
      this.list.current = value;
      this.loadData();
    },

    handleClose() {
      this.visible = false;
      this.$emit("handleClose");
    },
  },
};
</script>

<style lang="scss" scoped>
.title {
  display: flex;
  .divider {
    width: 8px;
    height: 16px;
    border-left: 4px solid #409eff;
    margin-right: 8px;
  }
  .label {
    font-weight: bold;
  }
}
.dialog-content {
  padding: 0 32px;
  display: flex;
  flex-direction: column;
}

.dialog-footer {
  margin-top: 30px;
  display: flex;
  justify-content: flex-end;
}
.el-dialog__body {
  padding: 0 20px 30px 20px;
}

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.pagination {
  margin: 16px;
}
</style>
