<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="kindergartenId">
        <SceneSelect v-model="form.kindergartenId" />
      </el-form-item>

      <el-form-item label="活动名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </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="cover">
        <upload v-model="form.cover" params="prefix=/activity-cover" />
        <el-row>建议尺寸：</el-row>
      </el-form-item>

      <el-form-item label="预览图Logo">
        <upload v-model="form.previewLogo" params="prefix=/activity-logo" />
        <el-row>建议尺寸：<a href="http://youxiangle.cn/yxl-back-end/static/yxl/fission/preview-logo-256*110.psd" target="_blank" style="color: rgb(245, 108, 108);">下载psd</a></el-row>
      </el-form-item>

      <el-form-item label="高清图Logo">
        <upload v-model="form.hdLogo" params="prefix=/activity-logo" />
        <el-row>建议尺寸：<a href="http://youxiangle.cn/yxl-back-end/static/yxl/fission/hd-logo-1252*521.psd" target="_blank" style="color: rgb(245, 108, 108);">下载psd</a></el-row>
      </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="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
        >
        </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
          controls-position="right"
          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: {
        kindergartenId: '',
        name: '',
        brief: '',
        cover: [],
        previewLogo: [],
        hdLogo: [],
        eventTimeArr: [
          this.$moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          this.$moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
        ],
        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' }
        ],
        kindergartenId: [
          { required: true, message: '请选择场景', trigger: 'change' }
        ],
        eventTimeArr: [{ validator: validateTimer, trigger: 'blur' }]
      },

      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 = data.cover ? [{ url: data.cover }] : [];
        result.previewLogo = data.previewLogo
          ? [{ url: data.previewLogo }]
          : [];
        result.hdLogo = data.hdLogo ? [{ url: data.hdLogo }] : [];
        result.eventTimeArr = [result.eventStartDate, result.eventEndDate];
        this.form = result;
      }
    },

    handleConfirm() {
      let _that = this;
      _that.$refs.form.validate(async valid => {
        if (!valid) {
          return false;
        }

        let params = Object.assign({}, _that.form);
        params.cover = (_that.form.cover && _that.form.cover.length > 0) ? _that.form.cover[0].url : '';
        params.previewLogo = (_that.form.previewLogo && _that.form.previewLogo.length > 0) ? _that.form.previewLogo[0].url : '';
        params.hdLogo = (_that.form.hdLogo && _that.form.hdLogo.length > 0) ? _that.form.hdLogo[0].url : '';
        params.eventStartDate = _that.form.eventTimeArr[0];
        params.eventEndDate = _that.form.eventTimeArr[1];

        if (_that.id) params.id = _that.id;

        const { success, msg } = await saveItem(params);

        if (success) {
          _that.$success('保存成功！');
          _that.modal = false;
          _that.$g_emit('activity_reload');
        }
      });
    }
  }
};
</script>

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