/*
 * @Author: zwq
 * @Date: 2022-08-24 11:19:43
 * @LastEditors: zwq
 * @LastEditTime: 2023-05-12 12:41:57
 * @Description: 
 */
export default {
  data() {
    /* eslint-disable */
    return {
      urlOptions: {
        getDataListURL: '',
        deleteURL: '',
        statusUrl: '',
        exportURL: ''
      },
      addOrEditTitle: '',
      tableData: [],
      listQuery: {
        limit: 10,
        page: 1,
        total: 1,
      },
      dataListLoading: false,
      addOrUpdateVisible: false,
    }
  },
  created() {
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http
        .get(this.urlOptions.getDataListURL, {
          params: this.listQuery,
        })
        .then(({ data: res }) => {
          this.dataListLoading = false;
          if (res.code !== 0) {
            this.tableData = [];
            this.listQuery.total = 0;
            return this.$message.error(res.msg);
          }
          this.tableData = res.data.list;
          this.listQuery.total = res.data.total;
        })
        .catch(() => {
          this.dataListLoading = false;
        });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.listQuery.limit = val;
      this.listQuery.page = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.listQuery.page = 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(() => { });
    },
    //tableBtn点击
    handleClick(val) {
      if (val.type === "edit") {
        this.addOrUpdateVisible = true;
        this.addOrEditTitle = "编辑";
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(val.data.id);
        });
      } else if (val.type === "delete") { 
        this.deleteHandle(val.data.id, val.data.name,val.data._pageIndex)
      } else if (val.type === "change") {
        this.changeStatus(val.data.id)
      } else {
        this.otherMethods(val)
      }
    },
    // 删除 
    deleteHandle(id, name,index) {
      this.$confirm(`确定对${name?'[名称='+name+']':'[序号='+index+']'}进行删除操作?`, "提示", {
        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(() => { });
    },
    //search-bar点击
    buttonClick(val) {
      switch (val.btnName) {
        case "search":
          this.listQuery.xm1 = val.xm1;
          this.listQuery.xm2 = val.xm2;
          this.listQuery.page = 1;
          this.getDataList();
          break;
        case "add":
          this.addOrEditTitle = '新增'
          this.addOrUpdateVisible = true;
          this.addOrUpdateHandle()
          break;
        default:
          console.log(val)
       }
    },
    handleCancel() {
      this.$refs.addOrUpdate.formClear()
      this.addOrUpdateVisible = false
      this.addOrEditTitle = ''
    },
    handleConfirm() {
      this.$refs.addOrUpdate.dataFormSubmit()
    },
    successSubmit() {
      this.handleCancel()
      this.getDataList()
    },
    // 导出
    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(() => { });
    }
  }
}