import * as API from 'api/virtual/packages';

export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    type: {
      type: Number,
      default: 0
    },
    data: {
      type: Object,
      default() {
        return null;
      }
    }
  },
  data() {
    return {
      my_show: false,
      isUpdate: false,
      loading: false,
      params: {
        type: 0,
        sn: '',
        name: '',
        carrier_operator: 255,
        cost_price: 0,
        guide_price: 0,
        flows: 0,
        voices: 0,
        messages: 0,
        has_messages: 0,
        has_lbs: 0,
        reset_months: 0,
        service_months: 0,
        effect_months: 0,
        description: '',
        status: 0,
        flowed: 0
      }
    };
  },
  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.name) {
        this.$Message.info('请填写套餐名称');
        return;
      }

      if (this.data && this.params.sn && !/^[A-Z0-9._]{2,32}$/.test(this.data && this.params.sn)) {
        this.$Message.info('套餐编码为大写字母、数字、下划线的2-32位字符');
        return;
      }

      if (this.params.carrier_operator === 255) {
        this.$Message.info('请选择运营商');
        return;
      }

      if (!this.params.reset_months) {
        this.$Message.info('请输入重置周期');
        return;
      }

      if (!this.params.service_months) {
        this.$Message.info('请输入套餐周期');
        return;
      }

      if (!this.params.flows) {
        this.$Message.info('请输入套餐流量');
        return;
      }

      if (this.params.reset_months > this.params.service_months) {
        this.$Message.info('重置周期必须小于套餐周期');
        return;
      }

      this.params.type = this.type;

      if (this.data) {
        // 编辑
        API.update(this.params, this.data.id).then(res => {
          this.loading = false;
          if (res.code == 0) {
            this.$emit('update-success');
            this.$Message.success('更新成功');
            this.clear();
            this.completePackageInitialized = false;
          }
        }).catch(err => {
          this.loading = false;
        });
      } else {
        // 添加
        API.create(this.params).then(res => {
          this.loading = false;
          if (res.code == 0) {
            this.$emit('add-success');
            this.$Message.success('添加成功');
            this.clear();
            this.completePackageInitialized = false;
          }
        }).catch(err => {
          this.loading = false;
        });
      }
    },

    visibleChange(bool) {
      if (!bool) {
        this.$emit('update:show', false);
      }
    },

    clear() {
      let strKeys = ['sn', 'name', 'carrier_operator', 'description'];
      for (let k in this.params) {
        if (strKeys.indexOf(k) === -1) {
          this.params[k] = 0;
        } else {
          this.params[k] = '';
        }
      }

      this.my_show = false;
    }
  }
};
