<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('充值卡名称')" prop="card_type_name">
        <el-input
          v-model="form.card_type_name"
          clearable
          :placeholder="__('请输入充值卡名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('充值卡额度')" prop="card_type_card">
        <el-input
          v-model="form.card_type_card"
          clearable
          :placeholder="__('请输入充值卡额度')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('类型描述')" prop="card_type_desc">
        <el-input
          v-model="form.card_type_desc"
          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"
          @change="change"
        />
      </el-form-item>
      <el-form-item :label="__('卡片logo')" prop="card_type_image">
        <upload
          height="100px"
          :image="form.card_type_image"
          width="100px"
          @upImage="
            (image) => {
              form.card_type_image = image
            }
          "
        />
      </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 } from '@/api/card/type'
  import { renderTime } from '@/utils/date'
  import { URL } from '@/config'
  import Upload from '@/components/upload'

  export default {
    name: 'CardTypeEdit',
    components: { Upload },
    data() {
      return {
        action: URL.action,
        form: {
          timeArr: [],
          card_type_image: '',
        },
        card_type_card: undefined,
        rules: {
          card_type_name: [
            {
              required: true,
              message: this.__('请输入充值卡名称'),
              trigger: 'blur',
            },
          ],
          card_type_card: [
            {
              required: true,
              message: this.__('请输入充值卡额度'),
              trigger: 'blur',
            },
          ],
          card_type_desc: [
            {
              required: true,
              message: this.__('请输入类型描述'),
              trigger: 'blur',
            },
          ],
          timeArr: [
            {
              required: true,
              message: this.__('请选择有效时间'),
              trigger: 'change',
            },
          ],
          card_type_image: [
            {
              required: true,
              message: this.__('请上传卡片logo'),
              trigger: 'change',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      change(timeArr) {},
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = {
            card_type_id: row.card_type_id,
            card_type_name: row.card_type_name,
            card_type_desc: row.card_type_desc,
            card_type_image: row.card_type_image,
            pay_type: 1,
            timeArr: [],
          }
          const cardTypePrize = row.card_type_prize
          if (cardTypePrize) {
            const prize = JSON.parse(cardTypePrize)
            this.form.card_type_card = prize.c
          }
          this.form.timeArr.push(row.card_type_starttime)
          this.form.timeArr.push(row.card_type_endtime)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          timeArr: [],
          card_type_image: '',
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.form.card_type_starttime = renderTime(this.form.timeArr[0])
            this.form.card_type_endtime = renderTime(this.form.timeArr[1])
            this.form.card_type_prize = JSON.stringify({
              c: this.form.card_type_card,
              p: 0,
              m: 0,
            })
            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
          }
        })
      },
      handleAvatarSuccess(name, res) {
        this.form[name] = res.data
      },
      BeforeUpload(file) {
        let isRightSize = file.size / 1024 / 1024 < 2
        if (!isRightSize) {
          this.$message.error('文件大小超过 2MB')
        }
        return isRightSize
      },
    },
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
