<template>
  <div class="pre-export">
    <ex-table
      style="margin-top: -10px"
      v-loading="loading"
      :table="table"
      :data="tableData"
      :columns="columns"
      :page="pageInfo"
      :size="size"
      @page-curr-change="handlePageChange"
      @page-size-change="handleSizeChange"
      @screen-reset="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
      @screen-submit="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
    >
      <template #table-header="{}">
        <div style="width: 100%">
          <el-row style="padding: 0 0 0 80px">
            <el-col :span="24">
              <el-col :span="12" style="width: 355px">
                <el-alert
                  :closable="false"
                  title="申请导出后，文件会在几分钟后生成！"
                  type="warning"
                >
                </el-alert>
              </el-col>

              <el-col
                :span="3"
                style="width: 66px; float: right"
                v-if="powers.some((item) => item == '002')"
              >
                <el-button
                  :size="searchSize"
                  type="primary"
                  style="float: right; margin-left: 5px"
                  @click="searchList"
                >
                  刷新
                </el-button>
              </el-col>
            </el-col>
          </el-row>
        </div>
      </template>
      <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="
            (statusOptions.find((item) => item.id == scope.row.status) || {})
              .type || ''
          "
          v-text="
            (statusOptions.find((item) => item.id == scope.row.status) || {})
              .label || '--'
          "
        ></el-tag>
      </template>
      <template #operation="{ scope }">
        <el-tooltip
          effect="dark"
          content="申请导出"
          placement="top"
          v-if="powers.some((item) => item == '049')"
        >
          <i class="el-icon-thumb tb-icon" @click="setStatus(scope.row.id)"></i>
        </el-tooltip>
        <el-tooltip
          v-if="powers.some((item) => item == '049') && scope.row.status == '2'"
          effect="dark"
          content="导出"
          placement="top"
        >
          <i
            class="el-icon-download tb-icon"
            @click="batchExport(scope.row.down_url)"
          ></i>
        </el-tooltip>
      </template>
    </ex-table>
  </div>
</template>
<script>
import mixinPage from "@/mixins/elPaginationHandle";
import asyncRequest from "@/apis/service/search/standBook";
import ExTable from "@/components/ExTableNew.vue";
import { mapGetters } from "vuex";
import urlConfig from "@/apis/url-config";
import resToken from "@/mixins/resToken";
export default {
  name: "Account",
  components: {
    ExTable,
  },
  mixins: [mixinPage, resToken],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "standBook"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      fileUrl: urlConfig.testURL,
      // 状态
      statusOptions: [
        { id: "0", label: "待申请", type: "info" },
        { id: "1", label: "系统处理中", type: "warning" },
        { id: "2", label: "已完成", type: "success" },
        { id: "3", label: "导出失败", type: "danger" },
      ],
      loading: true,
      showModel: false,
      sitem: null,
      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        {
          prop: "name",
          label: "业务表名称",
        },
        {
          prop: "status",
          label: "状态",
          _slot_: "status",
        },
        {
          prop: "remark",
          label: "下载反馈备注",
        },

        {
          prop: "expiretime",
          label: "文件过期时间",
        },
        {
          prop: "updatetime",
          label: "更新时间",
        },
        {
          prop: "addtime",
          label: "创建时间",
        },
        {
          prop: "",
          label: "操作",
          fixed: "right",
          width: "80px",
          _slot_: "operation",
        },
      ],
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    restSearch() {
      this.parmValue = {
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      this.searchList();
    },
    openModal(sitem) {
      this.showModel = true;
      this.sitem = sitem;
    },

    batchExport(content) {
      if (!this.loading) {
        this.loading = true;
        let aEle = document.createElement("a"); // 创建a标签
        // aEle.download = fileName; // 设置下载文件的文件名
        aEle.href = this.fileUrl + content; // content为后台返回的下载地址
        aEle.click(); // 设置点击事件
        // document.body.removeChild(aEle); //下载完成移除元素
        this.$message.success(`导出成功！`);
        setTimeout(() => {
          this.loading = false;
        }, 500);
      }
    },
    async setStatus(id) {
      if (!this.loading) {
        this.loading = true;
        const model = {
          id: id,
        };
        const res = await asyncRequest.download(model);
        if (res && res.code === 0) {
          this.loading = false;
          this.$notify.success({
            title: "申请成功，请等待系统执行完成！",
            message: "",
          });
          await this.searchList();
        } else if (res && res.code >= 100 && res.code <= 104) {
          await this.logout();
        } else {
          this.$message.warning(res.message);
        }
      }
    },
    async searchList() {
      this.loading = true;
      const res = await asyncRequest.hlist(this.parmValue);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.forEach((v) => {
          if (v.status !== "3") {
            v.remark = "";
          }
        });
        this.pageInfo.total = Number(res.data.count);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
  },
};
</script>
   <style lang="scss" scoped>
.account {
}
</style>
