<template>
  <el-dialog
    :title="title"
    :visible.sync="modal"
    width="800px"
    :close-on-click-modal="false"
    @close="
      res => {
        $emit('cancel');
      }
    "
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="活动名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>

      <el-form-item label="封面图" prop="cover">
        <upload v-model="form.cover" params="prefix=/activity" />
      </el-form-item>

      <el-form-item label="活动介绍" prop="brief">
        <el-input v-model="form.brief" type="textarea" :rows="2"></el-input>
      </el-form-item>

      <el-form-item label="场景" prop="type">
        <el-select
          v-model="form.type"
          :clearable="true"
          placeholder="请选择所属位置"
        >
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="活动时间" prop="eventTimeArr">
        <el-date-picker
          v-model="form.eventTimeArr"
          :picker-options="pickerOptions"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="form.isShow" prop="isShow">
          <el-radio :label="true">显示</el-radio>
          <el-radio :label="false">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number
          v-model="form.sort"
          :precision="0"
          :min="1"
          :max="9999"
          label="序号"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="modal = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getItem, saveItem } from '@/api/activity';

export default {
  name: 'ActivityItemModal',

  props: {
    id: {
      type: String,
      default: ''
    }
  },

  data() {
    const validateTimer = (rule, value, callback) => {
      if (value === null || value[0] === null || value[1] === null) {
        callback(new Error('请选择时间段'));
      } else {
        callback();
      }
    };
    const disabledDate = e => {
      if (e > new Date()) {
        return false;
      }
      return true;
    };
    return {
      modal: true,

      form: {
        name: '',
        cover: [
          {
            url: 'abc'
          }
        ],
        brief: '',
        kindergartenId: '123',
        eventTimeArr: [new Date(), new Date()],
        isShow: false,
        sort: 1
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'change' }
        ],
        cover: [
          {
            type: 'array',
            required: true,
            message: '请上传图片',
            trigger: 'change'
          }
        ],
        brief: [
          { required: true, message: '请输入活动介绍', trigger: 'change' }
        ],
        eventTimeArr: [{ validator: validateTimer, trigger: 'blur' }]
      },

      typeOptions: [],

      pickerOptions: {
        disabledDate
      }
    };
  },

  computed: {
    title() {
      if (this.id) {
        return '编辑活动';
      } else {
        return '新增活动';
      }
    }
  },

  watch: {
    id: {
      handler(id) {
        id && this.loadData();
      },
      immediate: true
    }
  },

  mounted() {},

  methods: {
    async loadData() {
      const { success, data, msg } = await getItem({
        id: this.id
      });
      if (success) {
        let result = data;
        result.cover = [
          {
            url: data.cover
          }
        ];
        result.eventTimeArr = [result.eventStartDate, result.eventEndDate];
        this.form = result;
      }
    },

    handleConfirm() {
      this.$refs.form.validate(async valid => {
        console.warn(valid);
        if (valid) {
          let params = Object.assign({}, this.form);
          params.cover = this.form.cover[0].url;
          params.eventStartDate = this.form.eventTimeArr[0];
          params.eventEndDate = this.form.eventTimeArr[1];
          if (this.id) params.id = this.id;
          const { success, msg } = await saveItem(params);
          if (success) {
            this.$success('保存成功！');
            this.modal = false;
            this.$g_emit('activity_reload');
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
