<template>
  <div>
    <el-card>
      <el-form :model="query" label-width="100px" size="mini" v-if="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item label="记录ID:">
              <el-input v-model="filterQuery.genKey" style="width: 90%" placeholder="请输入记录ID" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="top-right-btn">
        <el-button-group style="display:flex;">
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
          <el-button type="primary" icon="el-icon-top-right" @click="exportExcel">导出产品信息</el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>
      <el-table
        v-loading="loading"
        :data="list"
        style="width: 100%"
        border highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="记录ID" prop="genKey"></el-table-column>
        <el-table-column label="更新日期" prop="updateTime" show-overflow-tooltip></el-table-column>
        <el-table-column label="状态" prop="status" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag :type="statusFilterType(scope.row.status)">{{ status[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="下载次数" prop="dlCount" show-overflow-tooltip></el-table-column>
        <el-table-column label="下载信息" prop="remark" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作"  width="120">
          <template slot-scope="scope">
            <el-button type="text" @click.native.stop="downloadExcel(scope.row)">下载
            </el-button>
            <el-button type="text" @click.native.stop="deleteDialog(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="filterQuery.page"
        :limit.sync="filterQuery.limit"
        @pagination="getList"
      ></pagination>

      <el-dialog
        title="导出产品信息"
        :visible.sync="thrProductsSelectVisible"
        width="80%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        v-if="thrProductsSelectVisible"
      >
        <thrProductsSelect :selectType="selectType" v-on:cancelDialog="cancelDialog"
        ></thrProductsSelect>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
import {filterLog, deleteLog} from "@/api/thrsys/thrProductsExport.js";
import thrProductsSelect from "./thrProductsSelect";
import axios from "axios";
import {formatDate} from "../../../utils/date";

export default {
  data() {
    return {
      BASE_URL: process.env.VUE_APP_BASE_API,
      showSearch: true,
      filterQuery: {
        genKey: null,
        status: null,
        fromType: null,
        page: 1,
        limit: 20,
        thirdSysFk: null,
      },
      selectType: 0,
      thrProductsSelectVisible: false,
      list: [],
      detailList: [],
      thirdSys: [],
      thirdSysDetail: null,
      total: 0,
      editQuery: null,
      fromStatus: {
        0: "产品信息",
        1: "库存信息",
        2: "异常第三方上传"
      },
      status: {
        0: "等待处理",
        1: "正在处理",
        2: "处理异常",
        3: "处理完成"
      },
      uploadFileUrl: null,
      uploadData: {
        thirdSys: "thirdId",
      },
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        fromType: null,
        genKey: null,
        status: null,
        page: 1,
        limit: 20,
        thirdSysFk: null,
      };
      this.getList();
    },
    getList() {
      this.loading = true;
      filterLog(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    exportExcel() {
      this.selectType = 0;
      this.thrProductsSelectVisible = true;
    },
    uploadSmp() {
      this.selectType = 1;
      this.thrProductsSelectVisible = true;
    },
    downloadExcel(row) {
        let baseUrl = this.BASE_URL
        let href = baseUrl + "/udiwms/thrProducts/exportLog/download" + "?genKey=" + row.genKey;
        let a = document.createElement("a");
        var timestamp = formatDate(new Date(), "yyyyMMddhhmmss")
        fetch(href)
          .then((res) => res.blob())
          .then((blob) => {
            a.href = URL.createObjectURL(blob);
            console.log(a.href);
            a.download = "ThirdProduct_" + timestamp + ".UpperIn";
            document.body.appendChild(a);
            a.click();
            this.getList();
          });
    },

    cancelDialog(val) {
      if (val) {
        this.getList();
      }
      this.thrProductsSelectVisible = false;
    },
    deleteDialog(rowId) {
      this.$confirm("此操作将删除该导出记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let dQuery = {
            id: rowId,
          };
          deleteLog(dQuery)
            .then((response) => {
              this.loading = false;
              if (response.code == 20000) {
                this.$message.success("删除成功");
              } else {
                this.$message.error(response.message);
              }
              this.getList();
            })
            .catch(() => {
              this.loading = false;
            });
        })
        .catch(() => {
        });
    },
    handleChange(response, files, fileList) {
      console.log(response);
      if (response.code != 20000) {
        this.$message.error(response.message);
      } else {
        console.log(files[0] + "\n" + this.fileList[0] + "\n" + fileList[0]);
        this.$message.success("文件上传成功,请稍后刷新查看！");
      }
    },
    statusFilterType(status) {
      const statusMap = {
        0: "warning",
        1: "warning",
        2: "danger",
        3: "success",
      };
      return statusMap[status];
    },
  },
  mounted() {
  },
  components: {thrProductsSelect},
  created() {
    // this.getBasicThirdSys();
    this.getList();
  },
};
</script>
<style scoped>
.itemTag {
  float: left;
  text-align: left;
  margin-top: 10px;
  width: 100px;
}

.text {
  font-size: 13px;
  font-family: "Microsoft YaHei";
}

.el-row {
  display: flex;
  flex-wrap: wrap;

}

.el-col {
  border-radius: 4px;
  flex-wrap: wrap;
}
</style>
