<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @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="__('活动开始时间')" prop="activity_starttime">
        <el-date-picker
          v-model="form.activity_starttime"
          :placeholder="__('选择日期')"
          :readonly="isReadOnly"
          :style="{ width: '100%' }"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>
      <el-form-item :label="__('活动结束时间')" prop="activity_endtime">
        <el-date-picker
          v-model="form.activity_endtime"
          :placeholder="__('选择日期')"
          :readonly="isReadOnly"
          :style="{ width: '100%' }"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>

      <el-form-item :label="__('会员等级')" prop="activity_use_level">
        <el-select
          v-model="form.activity_use_level"
          multiple
          clearable
          :placeholder="__('会员等级')"
        >
          <el-option
            v-for="item in options"
            :key="item.user_level_id"
            :label="item.user_level_name"
            :value="item.user_level_id"
          />
        </el-select>
      </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 { doEdit, Update } from '@/api/store/activity/base'
  import { renderTime } from '@/utils/date'
  import {getList as getLists} from "@/api/base/user/level";

  export default {
    name: 'StoreActivityBaseEdit',
    data() {
      return {
        form: {
          activity_type_id: 1103,
          activity_use_level: [],
          activity_starttime: '',
          activity_endtime: '',
        },
        rules: {
          activity_title: [
            { required: true, trigger: 'blur', message: this.__('活动标题') },
          ],

          activity_starttime: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('活动开始时间'),
            },
          ],
          activity_endtime: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('活动结束时间'),
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
        options:[],
      }
    },
    computed: {
      isReadOnly() {
        return this.form.activity_id !== undefined
      },
    },
    created() {
      this.fetchdataFindStoreLevel()
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = {
            activity_id: row.activity_id,
            activity_type_id: row.activity_type_id,
            activity_starttime: renderTime(row.activity_starttime),
            activity_endtime: renderTime(row.activity_endtime),
            activity_title: row.activity_title,
          }
          //会员等级
          if (row.activity_use_level.trim()) {
            this.form.activity_use_level = row.activity_use_level.split(',').map(Number)
          }

        }

        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          activity_type_id: 1103,
          activity_starttime: '',
          activity_endtime: '',
          activity_use_level: [],
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      async fetchdataFindStoreLevel() {
        await getLists()
          .then((res) => {
            this.options = res.data.items
          })
          .catch((err) => {
            console.log(err)
          })
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.form.activity_use_level = this.form.activity_use_level.toString()

            if (this.form.activity_id && this.form.activity_id != undefined) {
              const { msg, status } = await Update(this.form)
              if (status === 200) {
                if (200 == status) {
                  this.$baseMessage(msg, 'success')
                } else {
                  this.$baseMessage(msg, 'error')
                }
              } else {
                this.$baseMessage(msg, 'error')
              }
            } else {
              const { msg, status } = await doEdit(this.form)
              if (status === 200) {
                if (200 == status) {
                  this.$baseMessage(msg, 'success')
                } else {
                  this.$baseMessage(msg, 'error')
                }
              } else {
                this.$baseMessage(msg, 'error')
              }
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
