<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="200px"
      style="padding-right: 30px"
    >
      <el-form-item label="活动名称" prop="name">
        <el-input
          placeholder="活动名称"
          v-model="dataForm.name"
          maxlength="15"
        ></el-input>
      </el-form-item>
      <el-form-item label="起止时间" prop="startTime" class="date-lable">
        <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="cycleMode">
        <el-radio-group v-model="dataForm.cycleMode">
          <el-radio :label="0">每天</el-radio>
          <el-radio :label="1">每周</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="" v-show="dataForm.cycleMode == 1">
        <el-checkbox-group v-model="dataForm.weekList">
          <el-checkbox-button
            v-for="(item, i) in weeklist"
            :label="item.label"
            :key="i"
            >{{ item.name }}</el-checkbox-button
          >
        </el-checkbox-group>
      </el-form-item>

      <div class="mt10 timeQuantum-box">
        <el-form-item label="时间段" prop="startHour">
          <el-radio-group v-model="timeQuantum" @change="selectTimeQuantum">
            <el-radio
              :label="item.id"
              v-for="item in timeQuantumList"
              :key="item.id"
            >
              {{ item.startHour }}-{{ item.endHour }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </div>

      <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="explainInfo">
        <el-input
          type="textarea"
          placeholder=""
          v-model="dataForm.explainInfo"
          :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>
        可输入3-360分钟
      </el-form-item>
      <el-form-item>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script>
import {
  seckillActivityAdd_api,
  seckillActivityEdit_api,
  seckillActivityInfo_api,
} from "@/api/marketing/seckill";
import { seckillTimeConfigList } from "@/api/operating";

export default {
  data() {
    return {
      visible: false,
      value: 1,
      week: "", // 选择星期
      timeQuantum: "", // 选择时间段
      dialogVisible: false,
      dataForm: {},
      weeklist: [
        {
          label: 1,
          name: "周一",
        },
        {
          label: 2,
          name: "周二",
        },
        {
          label: 3,
          name: "周三",
        },
        {
          label: 4,
          name: "周四",
        },
        {
          label: 5,
          name: "周五",
        },
        {
          label: 6,
          name: "周六",
        },
        {
          label: 0,
          name: "周日",
        },
      ],
      dataRule: {
        name: [
          {
            required: true,
            message: "菜单名称不能为空",
            trigger: "blur",
          },
        ],
        startTime: [
          {
            required: true,
            message: "起止时间不能为空",
            trigger: "blur",
          },
        ],
        endTime: [
          {
            required: true,
            message: "起止时间不能为空",
            trigger: "blur",
          },
        ],
        imgUrl: [
          {
            required: true,
            message: "活动图片不能为空",
            trigger: "blur",
          },
        ],
        closeTime: [
          {
            required: true,
            message: "订单自动关闭时间(小时)不能为空",
            trigger: "blur",
          },
        ],
        startHour: [
          {
            required: true,
            message: "时间段不能为空",
            trigger: "blur",
          },
        ],
        cycleMode: [
          {
            required: true,
            message: "周期不能为空",
            trigger: "change",
          },
        ],
      },
      timeQuantumList: [],
    };
  },

  methods: {
    init(id) {
      this.resetData();
      this.getAllKillConfig();
      this.dataForm.id = id || null;
      this.visible = true;

      if (id != undefined) {
        seckillActivityInfo_api(id).then((res) => {
          if (res && res.code == 200) {
            res.data.weekList = res.data.weekList || [];
            this.dataForm = res.data;

            for (let item of this.timeQuantumList) {
              if (item.startHour == res.data.startHour) {
                this.timeQuantum = item.id;
                return false;
              }
            }
            // console.log(this.dataForm);
          }
        });
      }
    },
    /** 重置表单数据 */
    resetData() {
      this.dataForm = {
        name: "",
        startHour: "",
        endHour: "",
        endHour: "",
        endTime: "",
        imgUrl: "",
        explainInfo: "",
        closeTime: "",
        cycleMode: "",
        weekList: [],
      };
      this.resetForm("dataForm");
      this.timeQuantum = "";
      this.timeQuantumList = [];
    },
    // 选择周几
    selectWeek() {
      // console.log(this.dataForm.week_list)
    },
    // 选择时间段
    selectTimeQuantum(e) {
      for (let item of this.timeQuantumList) {
        if (item.id == e) {
          this.dataForm.startHour = item.startHour;
          this.dataForm.endHour = item.endHour;
          return false;
        }
      }
      this.$refs["dataForm"].clearValidate(["startHour"]);
    },
    getAllKillConfig() {
      seckillTimeConfigList().then((res) => {
        this.timeQuantumList = res.data || [];
      });
    },
    // 提交
    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;
          }
          let url = this.dataForm.id
            ? seckillActivityEdit_api
            : seckillActivityAdd_api;
          url(this.dataForm).then((res) => {
            if (res && res.code == 200) {
              this.$message.success(this.dataForm.id ? "修改成功" : "添加成功");
              this.visible = false;
              this.resetData();
              this.$emit("updataList", 1);
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-radio__label {
  border: 1px solid #dcdfe6;
  padding: 8px 12px;
  margin-left: 10px;
  border-radius: 4px;
}

.mt10 {
  margin-top: 10px;
}

.mt30 {
  margin-top: 30px;
}
.date-lable {
  ::v-deep .el-form-item__content {
    display: flex;
    justify-content: flex-start;
  }
}

.timeQuantum-box {
  .el-radio-group {
    .el-radio {
      margin-left: 0;
      margin-bottom: 20px;
    }
  }
}
</style>
