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

export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    source: {
      type: Number,
      default: 0
    },
    data: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      my_show: false,
      loading: false,
      type: 1,
      params: {},
      file: null,
      segments: [{ start_no: "", end_no: "" }]
    };
  },
  watch: {
    show(bool) {
      this.my_show = bool;
    }
  },
  methods: {
    ok() {
      let params = {};
      params.type = this.type;
      params.order_id = this.data.id;

      if (this.type === 1) {
        if (this.file === null) {
          return this.$Message.error("请上传文件");
        }

        params.file = this.file;
      }

      if (this.type === 2) {
        for (let index = 0; index < this.segments.length; index++) {
          const element = this.segments[index];
          if (element.start_no === "" || element.end_no === "") {
            return this.$Message.error("请输入起止卡号");
          }
        }

        params.segments = this.segments;
      }

      this.$Modal.confirm({
        title: "提示",
        content: "请确认是否提交排单?",
        onOk: () => {
          this.loading = true;
          API.ship(params).then(res => {
            if (res.code == 0) {
              this.$Message.success("修改成功");
              this.$emit("update-success");
              this.clear();
            }
            this.loading = false;
          });
        }
      });
    },
    selectFile(file) {
      this.file = file;
      return false;
    },

    visibleChange(bool) {
      if (!bool) {
        this.$emit("update:show", false);
      }
    },
    clear() {
      this.file = null;
      this.segments = [{ start_no: "", end_no: "" }];
      this.my_show = false;
    },
    handleAdd() {
      this.segments.push({ start_no: "", end_no: "" });
    },
    handleRemove(index) {
      this.segments.splice(index, 1);
    }
  }
};
