<template>
  <el-drawer
    :title="!dataForm.id ? '新增' : '修改'"
    size="900px"
    :visible.sync="visible"
    :wrapperClosable="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="150px"
      style="padding-right: 20px"
    >
      <el-form-item label="活动名称" prop="name">
        <el-input
          maxlength="15"
          placeholder="活动名称"
          v-model="dataForm.name"
        ></el-input>
      </el-form-item>
      <el-form-item label="起止时间" class="date-lable" prop="startTime">
        <el-form-item prop="startTime" label-width="0px">
          <el-date-picker
            v-model="dataForm.startTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <span style="padding: 0 20px">——</span>
        <el-form-item prop="endTime" label-width="0px">
          <el-date-picker
            v-model="dataForm.endTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
      </el-form-item>
      <el-form-item label="活动图片" prop="imgUrl">
        <ImageUpload
          v-model="dataForm.imgUrl"
          :fileSize="0"
          :limit="100"
          :isShowTip="false"
          :fileType="['jpg', 'png']"
        />
        <div>上传jpg/png文件;尺寸建议750x268</div>
      </el-form-item>
      <el-form-item label="活动说明" prop="explain">
        <el-input
          type="textarea"
          v-model="dataForm.explain"
          placeholder=""
          :maxlength="200"
        ></el-input>
      </el-form-item>
      <el-form-item label="订单自动关闭时间" prop="closeTime">
        <el-input-number
          v-model="dataForm.closeTime"
          :min="3"
          :precision="0"
          :controls="false"
          :max="360"
        ></el-input-number>
        <span>分钟（可输入3-360分钟）</span>
      </el-form-item>
      <el-form-item label="成团小时数" prop="limitHour">
        <el-input-number
          v-model="dataForm.limitHour"
          :min="1"
          :precision="0"
          :controls="false"
          :max="360"
        ></el-input-number>
        <span>小时</span>
      </el-form-item>
      <el-form-item>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script>
import {
  groupBookingActivityAdd_api,
  groupBookingActivityEdit_api,
  groupBookingActivityInfo_api
} from "@/api/marketing/groupBooking.js";
export default {
  data() {
    return {
      visible: false,
      dialogVisible: false,
      dataForm: {},
      dataRule: {
        name: [
          { required: true, message: "活动名称不能为空", trigger: "blur" },
        ],
        startTime: [
          { required: true, message: "起止时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "起止时间不能为空", trigger: "blur" },
        ],
        imgUrl: [
          {
            required: true,
            message: "活动图片不能为空",
            trigger: "change",
          },
        ],
        closeTime: [
          {
            required: true,
            message: "订单自动关闭时间不能为空",
            trigger: "blur",
          },
        ],
        limitHour: [
          {
            required: true,
            message: "成团小时数不能为空",
            trigger: "blur",
          },
        ],
      },
      loading: false,
    };
  },

  methods: {
    init(id) {
      this.resetData();
      this.dataForm.id = id || null;
      this.visible = true;
      if (id) {
        groupBookingActivityInfo_api(id).then((res) => {
          if (res && res.code == 200) {
            this.dataForm = res.data;
          }
        });
      }
    },
    resetData() {
      (this.dataForm = {
        name: "", // 拼团活动名称
        startTime: "", // 活动开始时间
        endTime: "", // 活动结束时间
        imgUrl: "", // 活动图片
        explain: "", // 活动说明
        limitHour: "", // 成团小时数(开团之后的小时数)
        closeTime: "", // 订单自动关闭时间（商品按下单减库存，请设置未付款订单自动取消时间及时释放库存，可输入3-360分钟）
        discard: false, // 是否置弃
      }),
        this.resetForm("dataForm");
      this.timeQuantum = "";
    },
    // 提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (
            new Date(this.dataForm.endTime).getTime() -
              new Date(this.dataForm.startTime).getTime() <
            0
          ) {
            this.$message.error("结束时间不能小于开始时间");
            return false;
          }
          if(this.loading) return;
          this.loading = true;
          let url = this.dataForm.id == null ? groupBookingActivityAdd_api : groupBookingActivityEdit_api;
          url(this.dataForm).then((res) => {
            this.loading = false;
            if (res && res.code == 200) {
              this.$message.success(this.dataForm.id ? "修改成功" : "添加成功");
              this.visible = false;
              this.$emit("updataList", 1);
            }
          }).catch((err) => {
            this.loading = false;
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.date-lable {
  ::v-deep .el-form-item__content {
    display: flex;
    justify-content: flex-start;
  }
}
</style>
