<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="40%"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item :label="__('活动名称')" prop="pa_name">
        <el-input v-model="form.pa_name" :placeholder="__('活动名称')" />
      </el-form-item>
      <el-form-item :label="__('活动标题')" prop="pa_title">
        <el-input v-model="form.pa_title" :placeholder="__('活动标题')" />
      </el-form-item>
      <el-form-item :label="__('活动标注')" prop="pa_remark">
        <el-input v-model="form.pa_remark" :placeholder="__('活动标注')" />
      </el-form-item>
      <el-form-item :label="__('开始时间')" prop="pa_starttime">
        <el-date-picker
          v-model="form.pa_starttime"
          :placeholder="__('选择日期')"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>
      <el-form-item :label="__('结束时间')" prop="pa_endtime">
        <el-date-picker
          v-model="form.pa_endtime"
          :placeholder="__('选择日期')"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>
      <el-form-item :label="__('活动状态')" prop="pa_state">
        <el-radio-group v-model="form.pa_state" size="medium">
          <el-radio
            v-for="(item, index) in pa_stateOptions"
            :key="index"
            :disabled="item.disabled"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="__('排序')" prop="pa_order">
        <el-input v-model="form.pa_order" :placeholder="__('排序')" />
      </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 { renderTime } from '@/utils/date'
  import { doEdit } from '@/api/plantform/activity'

  export default {
    name: 'PlantformActivityEdit',
    data() {
      return {
        form: {
          pa_type_id: 5003,
          pa_starttime: null,
          pa_endtime: null,
        },
        rules: {
          pa_id: [
            { required: true, trigger: 'blur', message: this.__('活动编号') },
          ],
          pa_name: [
            { required: true, trigger: 'blur', message: this.__('活动名称') },
          ],
          pa_title: [
            { required: true, trigger: 'blur', message: this.__('活动标题') },
          ],
          pa_remark: [
            { required: true, trigger: 'blur', message: this.__('活动标注') },
          ],
          pa_type_id: [
            { required: true, trigger: 'blur', message: this.__('活动类型') },
          ],
          pa_starttime: [
            { required: true, trigger: 'blur', message: this.__('开始时间') },
          ],
          pa_endtime: [
            { required: true, trigger: 'blur', message: this.__('结束时间') },
          ],
          pa_state: [
            {
              required: true,
              message: '不能为空',
              trigger: 'change',
            },
          ],
          pa_order: [
            { required: true, trigger: 'blur', message: this.__('排序') },
          ],
          pa_type: [
            { required: true, trigger: 'blur', message: this.__('活动类型') },
          ],
        },
        title: '',
        dialogFormVisible: false,
        pa_stateOptions: [
          {
            label: this.__('未开启'),
            value: 0,
          },
          {
            label: this.__('正常'),
            value: 1,
          },
          {
            label: this.__('已结束'),
            value: 2,
          },
          {
            label: this.__('管理员关闭'),
            value: 3,
          },
        ],
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('新增')
        } else {
          this.title = this.__('编辑')

          this.form = {
            pa_type_id: 5003,
            pa_id: row.pa_id,
            pa_order: row.pa_order,
            pa_remark: row.pa_remark,
            pa_title: row.pa_title,
            pa_state: row.pa_state,
            pa_name: row.pa_name,
            pa_endtime: renderTime(row.pa_endtime),
            pa_starttime: renderTime(row.pa_starttime),
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = {}
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            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>
