<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :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_num">
        <el-input
          v-model="form.card_num"
          clearable
          :placeholder="__('请输入生成数量')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('前缀')" prop="card_prefix">
        <el-input
          v-model="form.card_prefix"
          clearable
          :placeholder="__('请输入前缀')"
          :style="{ width: '100%' }"
        />
      </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'

  export default {
    name: 'CardInfoAdd',
    data() {
      return {
        title: this.__('生成卡片'),
        addVisible: false,
        form: {
          pay_type: 1,
        },
        rules: {
          card_type_id: [
            {
              required: true,
              message: this.__('请输入卡片id'),
              trigger: 'blur',
            },
          ],
          card_num: [
            {
              required: true,
              message: this.__('请输入生成数量'),
              trigger: 'blur',
            },
          ],
          card_prefix: [
            {
              required: true,
              message: this.__('请输入前缀'),
              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>
