<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="50%"
    @close="close"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item :label="__('活动标题')" prop="activity_title">
        <el-input
          v-model="form.activity_title"
          :placeholder="__('活动标题')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        :label="__('活动图片')"
        label-width="120px"
        prop="lottery_image"
        required
      >
        <upload
          height="64px"
          :image="form.lottery_image"
          width="64px"
          @upImage="(image) => (form.lottery_image = image)"
        />
        <div>{{ __('尺寸 64 * 64') }}</div>
      </el-form-item>
      <el-form-item :label="__('活动类型')" prop="category_keywords">
        <el-radio v-model="form.lottery_type" :label="1">
          {{ __('幸运大转盘') }}
        </el-radio>
        <el-radio v-model="form.lottery_type" :label="2">
          {{ __('砸金蛋') }}
        </el-radio>
      </el-form-item>

      <el-row :gutter="20">
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item :label="__('活动开始时间')" prop="activity_starttime">
              <el-date-picker
                v-model="form.activity_starttime"
                :disabled="row != undefined"
                :placeholder="__('选择日期')"
                :style="{ width: '100%' }"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple"></div>
          <el-form-item :label="__('活动结束时间')" prop="activity_endtime">
            <el-date-picker
              v-model="form.activity_endtime"
              :disabled="row != undefined"
              :placeholder="__('选择日期')"
              :style="{ width: '100%' }"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item :label="__('抽奖次数')" prop="lottery_day_times">
        <el-input
          v-model="form.lottery_day_times"
          :placeholder="__('抽奖次数')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('活动规则')" prop="activity_intro">
        <el-input
          v-model="form.activity_intro"
          :placeholder="__('活动规则')"
          :rows="6"
          :style="{ width: '100%' }"
          type="textarea"
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import Upload from '@/components/upload'
  import { doEdit, Update } from '@/api/store/activity/base'
  import { renderTime } from '@/utils/date'
  import { URL } from '@/config'

  export default {
    name: 'StoreActivityBaseEdit',
    components: { Upload },
    data() {
      return {
        action: URL.action,
        row: {},
        form: {
          lottery_type: 1,
          activity_type_id: 1121,
          activity_starttime: '',
          activity_endtime: '',
          lottery_image: '',
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.rules = {
        activity_title: [
          { required: true, trigger: 'blur', message: this.__('活动标题') },
        ],

        activity_starttime: [
          { required: true, trigger: 'blur', message: this.__('活动开始时间') },
        ],
        lottery_image: [
          { required: true, trigger: 'blur', message: this.__('请选择图片') },
        ],
        activity_endtime: [
          { required: true, trigger: 'blur', message: this.__('活动结束时间') },
        ],
      }

      this.$forceUpdate()
    },

    methods: {
      __,
      showEdit(row) {
        this.row = row
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = {
            activity_type_id: 1121,
            activity_id: row.activity_id,
            activity_starttime: renderTime(row.activity_starttime),
            activity_endtime: renderTime(row.activity_endtime),
            activity_title: row.activity_title,
            lottery_type: row.activity_rule.lottery_type,
            lottery_day_times: row.activity_rule.lottery_day_times,
            activity_intro: row.activity_rule.activity_intro,
            lottery_image: row.activity_rule.lottery_image,
          }
        }

        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.activity_id && this.form.activity_id != undefined) {
              const { msg, status } = await Update(this.form)
              if (200 == status) {
                this.$baseMessage(msg, 'success')
              } else {
                this.$baseMessage(msg, 'error')
              }
              this.$emit('fetch-data')
              this.close()
            } else {
              const { msg, status } = await doEdit(this.form)
              if (200 == status) {
                this.$baseMessage(msg, 'success')
              } else {
                this.$baseMessage(msg, 'error')
              }
              this.$emit('fetch-data')
              this.close()
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
