import * as API from "api/virtual/refunds";

export default {
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      my_show: false,
      loading: false,
      params: {
        sim: ""
      }
    };
  },
  watch: {
    show(bool) {
      this.my_show = bool;
      if (bool) {
        if (this.data) {
          for (let k in this.data) {
            if (k in this.params) {
              this.params[k] = this.data[k];
            }
          }
        }
      }
    }
  },
  methods: {
    ok() {
      if (!this.params.sim) {
        this.$Message.info("请输入SIM卡号");
        return;
      }

      // 添加
      API.check(this.params)
        .then(res => {
          this.loading = false;
          if (res.code == 0) {
            if (res.data.length) {
              this.$Modal.confirm({
                title: "提示: 存在问题， 请确认是否继续操作",
                render: h => {
                  return h("Table", {
                    props: {
                      columns: [
                        {
                          title: "SIM",
                          key: "sim"
                        },
                        {
                          title: "错误",
                          key: "errors"
                        }
                      ],
                      data: res.data
                    }
                  });
                },
                onOk: () => {
                  this.createRefund();
                }
              });
            } else {
              console.log(2);
              this.createRefund();
            }
          }
        })
        .catch(err => {
          this.loading = false;
        });
    },
    createRefund() {
      API.create(this.params)
        .then(res => {
          this.loading = false;
          if (res.code == 0) {
            this.$emit("add-success");
            this.$Message.success("添加成功");
            this.clear();
          }
        })
        .catch(err => {
          this.loading = false;
        });
    },
    visibleChange(bool) {
      if (!bool) {
        this.$emit("update:show", false);
      }
    },
    clear() {
      for (let k in this.params) {
        this.params[k] = "";
      }

      this.my_show = false;
    }
  }
};
