/*
 * @Author: zhp
 * @Date: 2024-01-17 16:59:30
 * @LastEditTime: 2024-01-17 16:59:30
 * @LastEditors: zhp
 * @Description:
 */
/*
 * @Author: zwq
 * @Date: 2022-08-24 11:19:43
 * @LastEditors: zhp
 * @LastEditTime: 2024-01-10 10:29:29
 * @Description:
 */
export default {
  data() {
    /* eslint-disable */
    return {
      urlOptions: {
        getDataListURL: '',
        deleteURL: '',
        statusUrl: '',
        exportURL: ''
      },
      tableData: [],
      listQuery: {
        pageSize: 10,
        pageNo: 1,
        total: 1,
      },
      exportLoading: false,
      dataListLoading: false,
      addOrEditTitle: '',
      addOrUpdateVisible: false,
    }
  },
  created() {
  },
  mounted() {
    // this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.urlOptions.getDataListURL(this.listQuery).then(response => {
        this.tableData = response.data.list;
        this.listQuery.total = response.data.total;
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.listQuery.pageSize = val;
      this.listQuery.pageNo = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.listQuery.pageNo = 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,val.data)
      } 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.urlOptions.deleteURL(id).then(({ data }) => {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          });
        })
        .catch(() => { });
    },
    //search-bar点击
    buttonClick(val) {
      switch (val.btnName) {
        case "search":
          this.listQuery.xm1 = val.xm1;
          this.listQuery.xm2 = val.xm2;
          this.listQuery.pageNo = 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()
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有数据项?').then(() => {
        this.exportLoading = true;
        return this.urlOptions.exportURL(params);
      }).then(response => {
        this.$download.excel(response, '工厂.xls');
        this.exportLoading = false;
      }).catch(() => { });
    }
  }
}