<template>
  <el-dialog
    :title="title"
    :visible.sync="addVisible"
    width="600px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('次卡编号')" prop="card_type_id">
        <el-input
          v-model="form.card_type_id"
          clearable
          :placeholder="__('请输入次卡编号')"
          readonly
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('使用次数')" prop="card_use_num">
        <el-input
          v-model="form.card_use_num"
          clearable
          :placeholder="__('请输入使用次数')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('有效时间')" prop="timeArr" required>
        <el-date-picker
          v-model="form.timeArr"
          :end-placeholder="__('活动结束日期')"
          :start-placeholder="__('活动开始日期')"
          :style="{ width: '100%' }"
          type="datetimerange"
        />
      </el-form-item>
      <el-form-item :label="__('卡片logo')" prop="card_type_image" required>
        <el-upload
          ref="card_type_image"
          :action="action"
          class="avatar-uploader"
          :show-file-list="false"
        >
          <img
            v-if="form.card_type_image"
            class="avatar"
            :src="form.card_type_image"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </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 { doInfoEdit } from '@/api/card/type'
  import { URL } from '@/config/net.config'

  export default {
    name: 'CardInfoAdd',
    data() {
      return {
        title: this.__('生成卡片'),
        addVisible: false,
        action: URL.action,
        form: {
          pay_type: 2,
        },
        rules: {
          card_type_id: [
            {
              required: true,
              message: '请输入卡片id',
              trigger: 'blur',
            },
          ],
          card_num: [
            {
              required: true,
              message: '请输入生成数量',
              trigger: 'blur',
            },
          ],
          card_prefix: [
            {
              required: true,
              message: '请输入前缀',
              trigger: 'blur',
            },
          ],
        },
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        this.form.card_type_id = row.card_type_id
        this.addVisible = true
      },
      close() {
        this.form = {}
        this.$refs['form'].resetFields()
        this.addVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doInfoEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
