/*
 * @Author: zwq
 * @Date: 2022-08-24 11:19:43
 * @LastEditors: zwq
 * @LastEditTime: 2022-09-16 14:19:44
 * @Description: 
 */
export default {
    data () {
      /* eslint-disable */
      return {
        urlOptions: {
          getDataListURL: '',
          deleteURL: '',
          statusUrl: '',
          exportUrl: ''
        },
        dataList: [],
        dataForm: {},
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
      }
    },
    created () {
    },
    activated() {
      this.getDataList();
    },
    methods: {
    // 获取数据列表
    getDataList() {
        this.dataListLoading = true;
        this.$http
          .get(this.urlOptions.getDataListURL, {
            params: {
              page: this.pageIndex,
              limit: this.pageSize,
              ...this.dataForm
            },
          })
          .then(({ data: res }) => {
            this.dataListLoading = false;
            if (res.code !== 0) {
              this.dataList = [];
              this.totalPage = 0;
              return this.$message.error(res.msg);
            }
            this.dataList = res.data.list;
            this.totalPage = res.data.total;
          })
          .catch(() => {
            this.dataListLoading = false;
          });
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id);
        });
      },
      cancel(id) {
        this.$refs["popover-" + id].showPopper = false;
      },
      changeStatus(id) {
        this.$http
          .post(this.urlOptions.statusUrl, { id })
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.$refs["popover-" + id].showPopper = false;
            this.$message({
              message: this.$t("prompt.success"),
              type: "success",
              duration: 500,
              onClose: () => {
                this.getDataList();
              },
            });
          })
          .catch(() => {});
      },
      // 删除
      deleteHandle(id, name) {
        this.$confirm(`确定对[名称=${name}]进行删除操作?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$http.delete(this.urlOptions.deleteURL, { data: [id] }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.getDataList();
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          })
          .catch(() => {});
      },
      // 导出
      exportHandle(name) {
          this.$http
            .get(this.urlOptions.exportUrl, { responseType: "blob" })
            .then(({ data: res }) => {
              var date = new Date();
              var year = date.getFullYear();
              var month = date.getMonth() + 1;
              var strDate = date.getDate();
              if (month >= 1 && month <= 9) {
                month = "0" + month;
              }
              if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
              }
              var currentdate = year + "-" + month + "-" + strDate;
              const blob = new Blob([res]);
              const downloadElement = document.createElement("a");
              const href = window.URL.createObjectURL(blob); // 创建下载的链接
              downloadElement.href = href;
              downloadElement.download = `${name+currentdate}.xls`; // 下载后文件名
              document.body.appendChild(downloadElement);
              downloadElement.click(); // 点击下载
              document.body.removeChild(downloadElement); // 下载完成移除元素
              window.URL.revokeObjectURL(href);
            })
            .catch(() => {});
        }
    }
}