<template>
  <el-form ref="form" :model="form" label-width="120px">
    <el-form-item label="说明">
      <el-input v-model="form.rewark" placeholder="请输入说明" size="mini"></el-input>
    </el-form-item>
    <el-form-item label="数量">
      <el-input v-model="form.num" :readonly="numReadonly" placeholder="请输入生成兑换码书里那个" size="mini"></el-input>
    </el-form-item>
    <el-form-item label="有效期">
      <el-date-picker
        size="mini"
        v-model="form.endtime"
        value-format="yyyy-MM-dd"
        type="date"
        placeholder="选择兑换码有效期"
      ></el-date-picker>
    </el-form-item>
    <el-form-item label="标签状态">
      <el-switch v-model="form.status" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="save" size="mini">保存</el-button>
      <el-button @click="goBack" size="mini">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { timeFormater } from "../../../util";
export default {
  data() {
    return {
      form: {
        rewark: "",
        num: "",
        endtime: "",
        status: true
      },
      gid: 0,
      eid: 0,
      numReadonly: false
    };
  },
  methods: {
    save() {
      if (this.eid === 0) {
        this.add();
      } else {
        this.update();
      }
    },
    add() {
      let that = this;
      this.$http
        .exchangeCodeCreate(
          {
            gid: this.gid,
            num: this.form.num,
            remark: this.form.rewark,
            endtime: this.form.endtime
          },
          this
        )
        .then(res => {
          if (res.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1000,
              onClose: function() {
                that.goBack();
              }
            });
          }
        });
    },
    update() {
      let that = this;
      this.$http
        .exchangeCodeUpdate(
          {
            id: this.eid,
            remark: this.form.rewark,
            endtime: this.form.endtime,
            status: this.form.status ? 1 : 0
          },
          this
        )
        .then(res => {
          if (res.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1000,
              onClose: function() {
                that.goBack();
              }
            });
          }
        });
    },
    goBack() {
      this.$router.back();
    }
  },
  mounted: function() {
    let queryObj = this.$route.query;
    this.gid = queryObj.gid;
    this.eid = queryObj.id;

    if (this.eid !== 0) {
      this.$http.echangeCodeGet({ id: this.eid }, this).then(res => {
        if (res.code === 0) {
          this.form.rewark = res.obj.remark;
          this.form.num = res.obj.num;
          this.form.endtime = timeFormater(res.obj.endtime);
          this.form.status = res.obj.status === 1;

          this.numReadonly = true;
        }
      });
    }
  }
};
</script>

<style scoped>
.el-form-item {
  margin-bottom: 0px;
}
</style>
